KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > revolt > dialect > Sql92Dialect


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.revolt.dialect;
25
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import org.riotfamily.revolt.Script;
30 import org.riotfamily.revolt.definition.Column;
31 import org.riotfamily.revolt.definition.ForeignKey;
32 import org.riotfamily.revolt.definition.Identifier;
33 import org.riotfamily.revolt.definition.Index;
34 import org.riotfamily.revolt.definition.RecordEntry;
35 import org.riotfamily.revolt.definition.Table;
36 import org.riotfamily.revolt.definition.UniqueConstraint;
37
38 /**
39  * @author Felix Gnass [fgnass at neteye dot de]
40  *
41  */

42 public abstract class Sql92Dialect extends AbstractDialect {
43
44     public Sql92Dialect() {
45     }
46
47     public Script createTable(Table table) {
48
49         Script sql = new Script("CREATE TABLE")
50                 .append(quote(table)).append('(');
51
52         Iterator JavaDoc it = table.getColumns().iterator();
53         while (it.hasNext()) {
54             Column column = (Column) it.next();
55             addColumnDefinition(sql, column);
56             if (it.hasNext()) {
57                 sql.append(',');
58             }
59         }
60         if (!(table.getPrimaryKeys().isEmpty())) {
61             sql.append(',').append("PRIMARY KEY");
62             addColumnNames(sql, table.getPrimaryKeys());
63         }
64
65         sql.append(')');
66
67         return sql;
68     }
69
70     public Script renameTable(String JavaDoc name, String JavaDoc renameTo) {
71         throw new OperationNotSupportedException(
72                 "Tables can't be renamed in SQL 92");
73     }
74
75     public Script dropTable(String JavaDoc name) {
76         return new Script("DROP TABLE").append(quote(name));
77     }
78
79     public Script addColumn(String JavaDoc table, Column column) {
80         Script sql = alterTable(table).append("ADD COLUMN");
81         addColumnDefinition(sql, column);
82         return sql;
83     }
84
85     public Script renameColumn(String JavaDoc table, String JavaDoc name, String JavaDoc renameTo) {
86         throw new OperationNotSupportedException(
87                 "Columns can't be renamed in SQL 92");
88     }
89
90     public Script modifyColumn(String JavaDoc table, Column column) {
91         throw new OperationNotSupportedException(
92                 "Columns can't be modified in SQL 92");
93     }
94     
95     public Script dropColumn(String JavaDoc table, String JavaDoc name) {
96
97         return alterTable(table).append("DROP COLUMN")
98                 .append(quote(name));
99     }
100
101     public Script addIndex(String JavaDoc table, Index index) {
102         throw new OperationNotSupportedException(
103                 "SQL 92 does not support indices");
104     }
105
106     public Script dropIndex(String JavaDoc table, String JavaDoc name) {
107         throw new OperationNotSupportedException(
108                 "SQL 92 does not support indices");
109     }
110
111     public Script addUniqueConstraint(String JavaDoc table, UniqueConstraint constraint) {
112         Script sql = alterTable(table).append("ADD CONSTRAINT")
113                 .append(constraint.getName()).append("UNIQUE");
114         
115         addColumnNames(sql, constraint.getColumns());
116         return sql;
117     }
118
119     public Script dropUniqueConstraint(String JavaDoc table, String JavaDoc name) {
120         return dropConstraint(table, name);
121     }
122
123     public Script addForeignKey(String JavaDoc table, ForeignKey fk) {
124         Script sql = alterTable(table).append("ADD CONSTRAINT")
125                 .append(fk.getName()).append("FOREIGN KEY");
126         
127         addColumnNames(sql, fk.getLocalColumns());
128         sql.append("REFERENCES").append(fk.getForeignTable());
129         addColumnNames(sql, fk.getForeignColumns());
130         // TODO ON DELETE / ON UPDATE
131
return sql;
132     }
133
134     public Script dropForeignKey(String JavaDoc table, String JavaDoc name) {
135         return dropConstraint(table, name);
136     }
137
138     public Script insert(String JavaDoc table, Collection JavaDoc data) {
139         Script sql = new Script("INSERT INTO")
140                 .append(quote(table));
141         
142         addColumnNames(sql, data);
143         sql.append("VALUES").append('(');
144         Iterator JavaDoc it = data.iterator();
145         while (it.hasNext()) {
146             RecordEntry entry = (RecordEntry) it.next();
147             sql.append(convertQuotes(entry.getValue()));
148             if (it.hasNext()) {
149                 sql.append(',');
150             }
151         }
152         sql.append(')');
153         return sql;
154     }
155     
156     protected Script alterTable(String JavaDoc name) {
157         return new Script("ALTER TABLE").append(quote(name));
158     }
159
160     protected Script dropConstraint(String JavaDoc table, String JavaDoc name) {
161         return alterTable(table).append("DROP CONSTRAINT").append(name);
162     }
163
164     protected void addColumnDefinition(Script sql, Column column) {
165         sql.append(quote(column)).append(getColumnType(column));
166         
167         if (column.isDefaultValueSet()) {
168             sql.append("DEFAULT").append(convertQuotes(column.getDefaultValue()));
169         }
170         if (column.isNotNullSet()) {
171             if (column.isNotNull()) {
172                 sql.append("NOT");
173             }
174             sql.append("NULL");
175         }
176     }
177
178     protected void addColumnNames(Script sql, Collection JavaDoc columns) {
179         sql.append('(');
180         Iterator JavaDoc it = columns.iterator();
181         while (it.hasNext()) {
182             sql.append(quote((Identifier) it.next()));
183             if (it.hasNext()) {
184                 sql.append(',');
185             }
186         }
187         sql.append(')');
188     }
189
190     protected String JavaDoc getIdentifierQuote() {
191         return "\"";
192     }
193     
194     protected String JavaDoc quote(String JavaDoc id) {
195         return quote(new Identifier(id));
196     }
197     
198     protected String JavaDoc quote(Identifier id) {
199         if (id.isQuoted()) {
200             return getIdentifierQuote() + id.getName() + getIdentifierQuote();
201         }
202         return id.getName();
203     }
204     
205     protected String JavaDoc convertQuotes(String JavaDoc value) {
206         return value;
207     }
208
209 }
210
Popular Tags