001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.revolt.dialect;
025:
026: import java.util.Collection;
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.Map;
030:
031: import org.riotfamily.revolt.Script;
032: import org.riotfamily.revolt.definition.Column;
033: import org.riotfamily.revolt.definition.ForeignKey;
034: import org.riotfamily.revolt.definition.Identifier;
035: import org.riotfamily.revolt.definition.Index;
036: import org.riotfamily.revolt.definition.RecordEntry;
037: import org.riotfamily.revolt.definition.Table;
038: import org.riotfamily.revolt.definition.UniqueConstraint;
039:
040: /**
041: * @author Felix Gnass [fgnass at neteye dot de]
042: *
043: */
044: public abstract class Sql92Dialect extends AbstractDialect {
045: private static final Map actions = new HashMap();
046:
047: static {
048: actions.put(ForeignKey.NO_ACTION_HANDLER, "NO ACTION");
049: actions.put(ForeignKey.CASCADE_HANDLER, "CASCADE");
050: actions.put(ForeignKey.SET_NULL_HANDLER, "SET NULL");
051: actions.put(ForeignKey.SET_DEFAULT_HANDLER, "SET DEFAULT");
052: }
053:
054: private String getDeleteAction(ForeignKey foreignKey) {
055: return (String) actions.get(foreignKey.getDeleteAction());
056: }
057:
058: private String getUpdateAction(ForeignKey foreignKey) {
059: return (String) actions.get(foreignKey.getUpdateAction());
060: }
061:
062: public Sql92Dialect() {
063: }
064:
065: public Script createTable(Table table) {
066:
067: Script sql = new Script("CREATE TABLE").append(quote(table))
068: .append('(');
069:
070: Iterator it = table.getColumns().iterator();
071: while (it.hasNext()) {
072: Column column = (Column) it.next();
073: addColumnDefinition(sql, column);
074: if (it.hasNext()) {
075: sql.append(',');
076: }
077: }
078: if (!(table.getPrimaryKeys().isEmpty())) {
079: sql.append(',').append("PRIMARY KEY");
080: addColumnNames(sql, table.getPrimaryKeys());
081: }
082:
083: sql.append(')');
084:
085: return sql;
086: }
087:
088: public Script renameTable(String name, String renameTo) {
089: throw new OperationNotSupportedException(
090: "Tables can't be renamed in SQL 92");
091: }
092:
093: public Script dropTable(String name) {
094: return new Script("DROP TABLE").append(quote(name));
095: }
096:
097: public Script addColumn(String table, Column column) {
098: Script sql = alterTable(table).append("ADD COLUMN");
099: addColumnDefinition(sql, column);
100: return sql;
101: }
102:
103: public Script renameColumn(String table, String name,
104: String renameTo) {
105: throw new OperationNotSupportedException(
106: "Columns can't be renamed in SQL 92");
107: }
108:
109: public Script modifyColumn(String table, Column column) {
110: throw new OperationNotSupportedException(
111: "Columns can't be modified in SQL 92");
112: }
113:
114: public Script dropColumn(String table, String name) {
115:
116: return alterTable(table).append("DROP COLUMN").append(
117: quote(name));
118: }
119:
120: public Script addIndex(String table, Index index) {
121: throw new OperationNotSupportedException(
122: "SQL 92 does not support indices");
123: }
124:
125: public Script dropIndex(String table, String name) {
126: throw new OperationNotSupportedException(
127: "SQL 92 does not support indices");
128: }
129:
130: public Script addUniqueConstraint(String table,
131: UniqueConstraint constraint) {
132: Script sql = alterTable(table).append("ADD CONSTRAINT").append(
133: constraint.getName()).append("UNIQUE");
134:
135: addColumnNames(sql, constraint.getColumns());
136: return sql;
137: }
138:
139: public Script dropUniqueConstraint(String table, String name) {
140: return dropConstraint(table, name);
141: }
142:
143: public Script addForeignKey(String table, ForeignKey fk) {
144: Script sql = alterTable(table).append("ADD CONSTRAINT").append(
145: fk.getName()).append("FOREIGN KEY");
146:
147: addColumnNames(sql, fk.getLocalColumns());
148: sql.append("REFERENCES").append(fk.getForeignTable());
149: addColumnNames(sql, fk.getForeignColumns());
150: if (fk.hasUpdateAction()) {
151: sql.append("ON UPDATE").append(getUpdateAction(fk));
152: }
153: if (fk.hasDeleteAction()) {
154: sql.append("ON DELETE").append(getDeleteAction(fk));
155: }
156: return sql;
157: }
158:
159: public Script dropForeignKey(String table, String name) {
160: return dropConstraint(table, name);
161: }
162:
163: public Script insert(String table, Collection data) {
164: Script sql = new Script("INSERT INTO").append(quote(table));
165:
166: addColumnNames(sql, data);
167: sql.append("VALUES").append('(');
168: Iterator it = data.iterator();
169: while (it.hasNext()) {
170: RecordEntry entry = (RecordEntry) it.next();
171: sql.append(convertQuotes(entry.getValue()));
172: if (it.hasNext()) {
173: sql.append(',');
174: }
175: }
176: sql.append(')');
177: return sql;
178: }
179:
180: protected Script alterTable(String name) {
181: return new Script("ALTER TABLE").append(quote(name));
182: }
183:
184: protected Script dropConstraint(String table, String name) {
185: return alterTable(table).append("DROP CONSTRAINT").append(name);
186: }
187:
188: protected void addColumnDefinition(Script sql, Column column) {
189: sql.append(quote(column)).append(getColumnType(column));
190:
191: if (column.isDefaultValueSet()) {
192: sql.append("DEFAULT").append(
193: convertQuotes(column.getDefaultValue()));
194: }
195: if (column.isNotNullSet()) {
196: if (column.isNotNull()) {
197: sql.append("NOT");
198: }
199: sql.append("NULL");
200: }
201: }
202:
203: protected void addColumnNames(Script sql, Collection columns) {
204: sql.append('(');
205: Iterator it = columns.iterator();
206: while (it.hasNext()) {
207: sql.append(quote((Identifier) it.next()));
208: if (it.hasNext()) {
209: sql.append(',');
210: }
211: }
212: sql.append(')');
213: }
214:
215: protected String getIdentifierQuote() {
216: return "\"";
217: }
218:
219: protected String quote(String id) {
220: return quote(new Identifier(id));
221: }
222:
223: protected String quote(Identifier id) {
224: if (id.isQuoted()) {
225: return getIdentifierQuote() + id.getName()
226: + getIdentifierQuote();
227: }
228: return id.getName();
229: }
230:
231: protected String convertQuotes(String value) {
232: return value;
233: }
234:
235: }
|