001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestCreateTableH2.java 3696 2007-03-16 13:20:26Z gbevin $
007: */
008: package com.uwyn.rife.database.queries;
009:
010: import java.math.BigDecimal;
011: import java.sql.Blob;
012:
013: import com.uwyn.rife.database.BeanImpl;
014: import com.uwyn.rife.database.BeanImplConstrained;
015: import com.uwyn.rife.database.exceptions.ColumnsRequiredException;
016: import com.uwyn.rife.database.exceptions.TableNameRequiredException;
017:
018: public class TestCreateTableH2 extends TestCreateTable {
019: public TestCreateTableH2(String name) {
020: super (name);
021: }
022:
023: public void testInstantiationH2() {
024: CreateTable query = new CreateTable(mH2);
025: assertNotNull(query);
026: try {
027: query.getSql();
028: fail();
029: } catch (TableNameRequiredException e) {
030: assertEquals(e.getQueryName(), "CreateTable");
031: }
032: }
033:
034: public void testIncompleteQueryH2() {
035: CreateTable query = new CreateTable(mH2);
036: try {
037: query.getSql();
038: fail();
039: } catch (TableNameRequiredException e) {
040: assertEquals(e.getQueryName(), "CreateTable");
041: }
042: query.table("tablename");
043: try {
044: query.getSql();
045: fail();
046: } catch (ColumnsRequiredException e) {
047: assertEquals(e.getQueryName(), "CreateTable");
048: }
049: query.table("tablename").column("string", String.class);
050: assertNotNull(query.getSql());
051: }
052:
053: public void testClearH2() {
054: CreateTable query = new CreateTable(mH2);
055: query.table("tablename").column("string", String.class);
056: assertNotNull(query.getSql());
057: query.clear();
058: try {
059: query.getSql();
060: fail();
061: } catch (TableNameRequiredException e) {
062: assertEquals(e.getQueryName(), "CreateTable");
063: }
064: }
065:
066: public void testColumnH2() {
067: CreateTable query = new CreateTable(mH2);
068: query.table("tablename1").column("string", String.class)
069: .column("stringbuffer", StringBuffer.class).column(
070: "characterobject", Character.class).column(
071: "booleanobject", Boolean.class).column(
072: "byteobject", Byte.class).column(
073: "doubleobject", Double.class).column(
074: "floatobject", Float.class).column(
075: "integerobject", Integer.class).column(
076: "longobject", Long.class).column("shortobject",
077: Short.class).column("bigdecimal",
078: BigDecimal.class).column("charcolumn",
079: char.class).column("booleancolumn",
080: boolean.class).column("bytecolumn", byte.class)
081: .column("doublecolumn", double.class).column(
082: "floatcolumn", float.class).column("intcolumn",
083: int.class).column("longcolumn", long.class)
084: .column("shortcolumn", short.class).column(
085: "blobcolumn", Blob.class);
086: assertEquals(
087: query.getSql(),
088: "CREATE TABLE tablename1 (string LONGVARCHAR, stringbuffer LONGVARCHAR, characterobject CHAR, booleanobject BOOLEAN, byteobject SMALLINT, doubleobject FLOAT, floatobject FLOAT, integerobject INTEGER, longobject BIGINT, shortobject SMALLINT, bigdecimal NUMERIC, charcolumn CHAR, booleancolumn BOOLEAN, bytecolumn SMALLINT, doublecolumn FLOAT, floatcolumn FLOAT, intcolumn INTEGER, longcolumn BIGINT, shortcolumn SMALLINT, blobcolumn LONGVARBINARY)");
089: execute(query);
090: }
091:
092: public void testColumnPrecisionH2() {
093: CreateTable query = new CreateTable(mH2);
094: query.table("tablename1").column("string", String.class, 255)
095: .column("stringbuffer", StringBuffer.class, 100)
096: .column("characterobject", Character.class, 20).column(
097: "booleanobject", Boolean.class, 7).column(
098: "byteobject", Byte.class, 9).column(
099: "doubleobject", Double.class, 30, 2).column(
100: "floatobject", Float.class, 20, 2).column(
101: "integerobject", Integer.class, 10).column(
102: "longobject", Long.class, 8).column(
103: "shortobject", Short.class, 8).column(
104: "bigdecimal", BigDecimal.class, 19, 9).column(
105: "charcolumn", char.class, 10).column(
106: "booleancolumn", boolean.class, 4).column(
107: "bytecolumn", byte.class, 8).column(
108: "doublecolumn", double.class, 12, 3).column(
109: "floatcolumn", float.class, 13, 2).column(
110: "intcolumn", int.class, 10).column(
111: "longcolumn", long.class, 12).column(
112: "shortcolumn", short.class, 9).column(
113: "blobcolumn", Blob.class, 20);
114: assertEquals(
115: query.getSql(),
116: "CREATE TABLE tablename1 (string VARCHAR(255), stringbuffer VARCHAR(100), characterobject CHAR(20), booleanobject BOOLEAN, byteobject SMALLINT, doubleobject FLOAT, floatobject FLOAT, integerobject INTEGER, longobject BIGINT, shortobject SMALLINT, bigdecimal NUMERIC(19,9), charcolumn CHAR(10), booleancolumn BOOLEAN, bytecolumn SMALLINT, doublecolumn FLOAT, floatcolumn FLOAT, intcolumn INTEGER, longcolumn BIGINT, shortcolumn SMALLINT, blobcolumn LONGVARBINARY)");
117: execute(query);
118: }
119:
120: public void testColumnsBeanH2() {
121: CreateTable query = new CreateTable(mH2);
122: query.table("tablename").columns(BeanImpl.class);
123: assertEquals(
124: query.getSql(),
125: "CREATE TABLE tablename (propertyBigDecimal NUMERIC, propertyBoolean BOOLEAN, propertyBooleanObject BOOLEAN, propertyByte SMALLINT, propertyByteObject SMALLINT, propertyCalendar TIMESTAMP, propertyChar CHAR, propertyCharacterObject CHAR, propertyDate TIMESTAMP, propertyDouble FLOAT, propertyDoubleObject FLOAT, propertyEnum VARCHAR(255), propertyFloat FLOAT, propertyFloatObject FLOAT, propertyInt INTEGER, propertyIntegerObject INTEGER, propertyLong BIGINT, propertyLongObject BIGINT, propertyShort SMALLINT, propertyShortObject SMALLINT, propertySqlDate DATE, propertyString LONGVARCHAR, propertyStringbuffer LONGVARCHAR, propertyTime TIME, propertyTimestamp TIMESTAMP, CHECK (propertyEnum IS NULL OR propertyEnum IN ('VALUE_ONE','VALUE_TWO','VALUE_THREE')))");
126: execute(query);
127: }
128:
129: public void testColumnsBeanIncludedH2() {
130: CreateTable query = new CreateTable(mH2);
131: query.table("tablename").columnsIncluded(
132: BeanImpl.class,
133: new String[] { "propertyBigDecimal", "propertyByte",
134: "propertyFloat", "propertyStringbuffer",
135: "propertyTime" });
136: assertEquals(
137: query.getSql(),
138: "CREATE TABLE tablename (propertyBigDecimal NUMERIC, propertyByte SMALLINT, propertyFloat FLOAT, propertyStringbuffer LONGVARCHAR, propertyTime TIME)");
139: execute(query);
140: }
141:
142: public void testColumnsBeanExcludedH2() {
143: CreateTable query = new CreateTable(mH2);
144: query.table("tablename").columnsExcluded(
145: BeanImpl.class,
146: new String[] { "propertyBigDecimal", "propertyByte",
147: "propertyFloat", "propertyStringbuffer",
148: "propertyTime" });
149: assertEquals(
150: query.getSql(),
151: "CREATE TABLE tablename (propertyBoolean BOOLEAN, propertyBooleanObject BOOLEAN, propertyByteObject SMALLINT, propertyCalendar TIMESTAMP, propertyChar CHAR, propertyCharacterObject CHAR, propertyDate TIMESTAMP, propertyDouble FLOAT, propertyDoubleObject FLOAT, propertyEnum VARCHAR(255), propertyFloatObject FLOAT, propertyInt INTEGER, propertyIntegerObject INTEGER, propertyLong BIGINT, propertyLongObject BIGINT, propertyShort SMALLINT, propertyShortObject SMALLINT, propertySqlDate DATE, propertyString LONGVARCHAR, propertyTimestamp TIMESTAMP, CHECK (propertyEnum IS NULL OR propertyEnum IN ('VALUE_ONE','VALUE_TWO','VALUE_THREE')))");
152: execute(query);
153: }
154:
155: public void testColumnsBeanFilteredH2() {
156: CreateTable query = new CreateTable(mH2);
157: query
158: .table("tablename")
159: .columnsFiltered(
160: BeanImpl.class,
161: new String[] { "propertyBigDecimal",
162: "propertyByte", "propertyFloat",
163: "propertyStringbuffer", "propertyTime" },
164: new String[] { "propertyByte",
165: "propertyStringbuffer" });
166: assertEquals(
167: query.getSql(),
168: "CREATE TABLE tablename (propertyBigDecimal NUMERIC, propertyFloat FLOAT, propertyTime TIME)");
169: execute(query);
170: }
171:
172: public void testColumnsBeanPrecisionH2() {
173: CreateTable query = new CreateTable(mH2);
174: query.table("tablename").columns(BeanImpl.class).precision(
175: "propertyBigDecimal", 19, 9).precision(
176: "propertyBoolean", 4).precision(
177: "propertyBooleanObject", 7)
178: .precision("propertyByte", 8).precision(
179: "propertyByteObject", 9).precision(
180: "propertyCalendar", 20).precision(
181: "propertyChar", 10).precision(
182: "propertyCharacterObject", 12).precision(
183: "propertyDate", 7).precision("propertyDouble",
184: 12, 3).precision("propertyDoubleObject", 14, 4)
185: .precision("propertyFloat", 13, 2).precision(
186: "propertyFloatObject", 12, 1).precision(
187: "propertyInt", 10).precision(
188: "propertyIntegerObject", 8).precision(
189: "propertyLong", 12).precision(
190: "propertyLongObject", 11).precision(
191: "propertyShort", 9).precision(
192: "propertyShortObject", 6).precision(
193: "propertySqlDate", 8).precision(
194: "propertyString", 255).precision(
195: "propertyStringbuffer", 100).precision(
196: "propertyTime", 9).precision(
197: "propertyTimestamp", 30, 2).precision(
198: "propertyEnum", 12);
199: assertEquals(
200: query.getSql(),
201: "CREATE TABLE tablename (propertyBigDecimal NUMERIC(19,9), propertyBoolean BOOLEAN, propertyBooleanObject BOOLEAN, propertyByte SMALLINT, propertyByteObject SMALLINT, propertyCalendar TIMESTAMP, propertyChar CHAR(10), propertyCharacterObject CHAR(12), propertyDate TIMESTAMP, propertyDouble FLOAT, propertyDoubleObject FLOAT, propertyEnum VARCHAR(255), propertyFloat FLOAT, propertyFloatObject FLOAT, propertyInt INTEGER, propertyIntegerObject INTEGER, propertyLong BIGINT, propertyLongObject BIGINT, propertyShort SMALLINT, propertyShortObject SMALLINT, propertySqlDate DATE, propertyString VARCHAR(255), propertyStringbuffer VARCHAR(100), propertyTime TIME, propertyTimestamp TIMESTAMP, CHECK (propertyEnum IS NULL OR propertyEnum IN ('VALUE_ONE','VALUE_TWO','VALUE_THREE')))");
202: execute(query);
203: }
204:
205: public void testColumnsBeanConstrainedH2() {
206: CreateTable query = new CreateTable(mH2);
207: query.table("tablename").columns(BeanImplConstrained.class);
208: assertEquals(
209: query.getSql(),
210: "CREATE TABLE tablename (propertyBigDecimal NUMERIC(17,6), propertyBoolean BOOLEAN, propertyBooleanObject BOOLEAN, propertyByte SMALLINT, propertyByteObject SMALLINT NOT NULL, propertyCalendar TIMESTAMP, propertyChar CHAR, propertyCharacterObject CHAR, propertyDate TIMESTAMP, propertyDouble FLOAT, propertyDoubleObject FLOAT, propertyFloat FLOAT, propertyFloatObject FLOAT, propertyInt INTEGER DEFAULT 23, propertyIntegerObject INTEGER, propertyLongObject BIGINT, propertyShort SMALLINT, propertySqlDate DATE, propertyString VARCHAR(30) DEFAULT 'one' NOT NULL, propertyStringbuffer VARCHAR(20) NOT NULL, propertyTime TIME, propertyTimestamp TIMESTAMP, PRIMARY KEY (propertyString), UNIQUE (propertyStringbuffer, propertyByteObject), UNIQUE (propertyStringbuffer), CHECK (propertyByteObject != -1), CHECK (propertyInt != 0), CHECK (propertyLongObject IS NULL OR propertyLongObject IN (89,1221,66875,878)), CHECK (propertyString IS NULL OR propertyString IN ('one','tw''''o','someotherstring')), CHECK (propertyStringbuffer != ''), CHECK (propertyStringbuffer != 'some''blurp'))");
211: execute(query);
212: }
213:
214: public void testNullableH2() {
215: CreateTable query = new CreateTable(mH2);
216: query.table("tablename").column("intColumn1", int.class,
217: CreateTable.NULL).column("stringColumn", String.class,
218: 12, CreateTable.NOTNULL)
219: .column("intColumn2", int.class).column("intColumn3",
220: int.class).column("floatColumn", float.class,
221: 13, 6, CreateTable.NOTNULL).nullable(
222: "intColumn2", CreateTable.NULL).nullable(
223: "intColumn3", CreateTable.NOTNULL).nullable(
224: "floatColumn", null);
225: assertEquals(
226: query.getSql(),
227: "CREATE TABLE tablename (intColumn1 INTEGER NULL, stringColumn VARCHAR(12) NOT NULL, intColumn2 INTEGER NULL, intColumn3 INTEGER NOT NULL, floatColumn FLOAT)");
228: execute(query);
229: }
230:
231: public void testDefaultH2() {
232: CreateTable query = new CreateTable(mH2);
233: query.table("tablename1").column("string", String.class, 255)
234: .column("stringbuffer", StringBuffer.class, 100)
235: .column("characterobject", Character.class, 22).column(
236: "booleanobject", Boolean.class, 7).column(
237: "byteobject", Byte.class, 9).column(
238: "doubleobject", Double.class, 30, 2).column(
239: "floatobject", Float.class, 20, 2).column(
240: "integerobject", Integer.class, 10).column(
241: "longobject", Long.class, 8).column(
242: "shortobject", Short.class, 8).column(
243: "bigdecimal", BigDecimal.class, 19, 9).column(
244: "charcolumn", char.class, 10).column(
245: "booleancolumn", boolean.class, 4).column(
246: "bytecolumn", byte.class, 8).column(
247: "doublecolumn", double.class, 12, 3).column(
248: "floatcolumn", float.class, 13, 2).column(
249: "intcolumn", int.class, 10).column(
250: "longcolumn", long.class, 12).column(
251: "shortcolumn", short.class, 9).defaultValue(
252: "string", "stringDefault").defaultValue(
253: "stringbuffer", "stringbufferDefault")
254: .defaultValue("characterobject",
255: "characterobjectDefault").defaultValue(
256: "booleanobject", new Boolean(true))
257: .defaultValue("byteobject", new Byte((byte) 34))
258: .defaultValue("doubleobject", new Double(234.87d))
259: .defaultValue("floatobject", new Float(834.43f))
260: .defaultValue("integerobject", new Integer(463))
261: .defaultValue("longobject", new Long(34876L))
262: .defaultValue("shortobject", new Short((short) 98))
263: .defaultValue("bigdecimal", new BigDecimal("347.14"))
264: .defaultValue("charcolumn", "OSJFDZ").defaultValue(
265: "booleancolumn", false).defaultValue(
266: "bytecolumn", (byte) 27).defaultValue(
267: "doublecolumn", 934.5d).defaultValue(
268: "floatcolumn", 35.87f).defaultValue(
269: "intcolumn", 983734).defaultValue("longcolumn",
270: 2343345L).defaultValue("shortcolumn", 12);
271: assertEquals(
272: query.getSql(),
273: "CREATE TABLE tablename1 (string VARCHAR(255) DEFAULT 'stringDefault', stringbuffer VARCHAR(100) DEFAULT 'stringbufferDefault', characterobject CHAR(22) DEFAULT 'characterobjectDefault', booleanobject BOOLEAN DEFAULT true, byteobject SMALLINT DEFAULT 34, doubleobject FLOAT DEFAULT 234.87, floatobject FLOAT DEFAULT 834.43, integerobject INTEGER DEFAULT 463, longobject BIGINT DEFAULT 34876, shortobject SMALLINT DEFAULT 98, bigdecimal NUMERIC(19,9) DEFAULT 347.14, charcolumn CHAR(10) DEFAULT 'OSJFDZ', booleancolumn BOOLEAN DEFAULT false, bytecolumn SMALLINT DEFAULT 27, doublecolumn FLOAT DEFAULT 934.5, floatcolumn FLOAT DEFAULT 35.87, intcolumn INTEGER DEFAULT 983734, longcolumn BIGINT DEFAULT 2343345, shortcolumn SMALLINT DEFAULT 12)");
274: execute(query);
275: }
276:
277: public void testDefaultFunctionH2() {
278: CreateTable query = new CreateTable(mH2);
279: query.table("tablename1").column("dateobject",
280: java.sql.Date.class).defaultFunction("dateobject",
281: "now()");
282: assertEquals(query.getSql(),
283: "CREATE TABLE tablename1 (dateobject DATE DEFAULT now())");
284: execute(query);
285: }
286:
287: public void testCustomAttributeH2() {
288: CreateTable query = new CreateTable(mH2);
289: query.table("tablename1").column("intColumn", Integer.class)
290: .customAttribute("intColumn", "CHECK (intColumn > 0)");
291: assertEquals(query.getSql(),
292: "CREATE TABLE tablename1 (intColumn INTEGER CHECK (intColumn > 0))");
293: execute(query);
294: }
295:
296: public void testTemporaryH2() {
297: CreateTable query = new CreateTable(mH2);
298: query.table("tablename").temporary(true).column("boolColumn",
299: boolean.class);
300: assertEquals(query.getSql(),
301: "CREATE TEMPORARY TABLE tablename (boolColumn BOOLEAN)");
302: execute(query);
303: }
304:
305: public void testPrimaryKeySimpleH2() {
306: CreateTable query = new CreateTable(mH2);
307: query.table("tablename").column("intColumn", int.class)
308: .primaryKey("intColumn");
309: assertEquals(query.getSql(),
310: "CREATE TABLE tablename (intColumn INTEGER NOT NULL, PRIMARY KEY (intColumn))");
311: execute(query);
312: }
313:
314: public void testPrimaryKeyMultipleH2() {
315: CreateTable query = new CreateTable(mH2);
316: query.table("tablename").column("intColumn", int.class).column(
317: "stringColumn", String.class, 50).primaryKey(
318: new String[] { "intColumn", "stringColumn" });
319: assertEquals(
320: query.getSql(),
321: "CREATE TABLE tablename (intColumn INTEGER NOT NULL, stringColumn VARCHAR(50) NOT NULL, PRIMARY KEY (intColumn, stringColumn))");
322: execute(query);
323: }
324:
325: public void testPrimaryKeyNamedH2() {
326: CreateTable query = new CreateTable(mH2);
327: query.table("tablename").column("intColumn", int.class)
328: .primaryKey("constraint_name", "intColumn");
329: assertEquals(
330: query.getSql(),
331: "CREATE TABLE tablename (intColumn INTEGER NOT NULL, CONSTRAINT constraint_name PRIMARY KEY (intColumn))");
332: execute(query);
333: }
334:
335: public void testPrimaryKeyMultipleNamedH2() {
336: CreateTable query = new CreateTable(mH2);
337: query.table("tablename").column("intColumn", int.class).column(
338: "stringColumn", String.class, 50).primaryKey(
339: "constraint_name",
340: new String[] { "intColumn", "stringColumn" });
341: assertEquals(
342: query.getSql(),
343: "CREATE TABLE tablename (intColumn INTEGER NOT NULL, stringColumn VARCHAR(50) NOT NULL, CONSTRAINT constraint_name PRIMARY KEY (intColumn, stringColumn))");
344: execute(query);
345: }
346:
347: public void testUniqueSimpleH2() {
348: CreateTable query = new CreateTable(mH2);
349: query.table("tablename").column("intColumn", int.class).unique(
350: "intColumn");
351: assertEquals(query.getSql(),
352: "CREATE TABLE tablename (intColumn INTEGER, UNIQUE (intColumn))");
353: execute(query);
354: }
355:
356: public void testUniqueMultipleH2() {
357: CreateTable query = new CreateTable(mH2);
358: query.table("tablename").column("intColumn", int.class).column(
359: "stringColumn", String.class, 50).unique(
360: new String[] { "intColumn", "stringColumn" });
361: assertEquals(
362: query.getSql(),
363: "CREATE TABLE tablename (intColumn INTEGER, stringColumn VARCHAR(50), UNIQUE (intColumn, stringColumn))");
364: execute(query);
365: }
366:
367: public void testUniqueNamedH2() {
368: CreateTable query = new CreateTable(mH2);
369: query.table("tablename").column("intColumn", int.class).unique(
370: "constraint_name", "intColumn");
371: assertEquals(
372: query.getSql(),
373: "CREATE TABLE tablename (intColumn INTEGER, CONSTRAINT constraint_name UNIQUE (intColumn))");
374: execute(query);
375: }
376:
377: public void testUniqueMultipleNamedH2() {
378: CreateTable query = new CreateTable(mH2);
379: query.table("tablename").column("intColumn", int.class).column(
380: "stringColumn", String.class, 50).unique(
381: "constraint_name",
382: new String[] { "intColumn", "stringColumn" });
383: assertEquals(
384: query.getSql(),
385: "CREATE TABLE tablename (intColumn INTEGER, stringColumn VARCHAR(50), CONSTRAINT constraint_name UNIQUE (intColumn, stringColumn))");
386: execute(query);
387: }
388:
389: public void testForeignKeySimpleH2() {
390: CreateTable query = new CreateTable(mH2);
391: query.table("tablename").column("intColumn", int.class)
392: .foreignKey("foreigntable", "intColumn",
393: "foreignIntColumn");
394: assertEquals(
395: query.getSql(),
396: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn))");
397: execute(query);
398: }
399:
400: public void testForeignKeyMultipleH2() {
401: CreateTable query = new CreateTable(mH2);
402: query.table("tablename").column("intColumn", int.class).column(
403: "stringColumn", String.class, 50).foreignKey(
404: "foreigntable",
405: new String[] { "intColumn", "foreignIntColumn",
406: "stringColumn", "foreignStringColumn" });
407: assertEquals(
408: query.getSql(),
409: "CREATE TABLE tablename (intColumn INTEGER, stringColumn VARCHAR(50), FOREIGN KEY (intColumn, stringColumn) REFERENCES foreigntable (foreignIntColumn, foreignStringColumn))");
410: execute(query);
411: }
412:
413: public void testForeignKeySimpleNamedH2() {
414: CreateTable query = new CreateTable(mH2);
415: query.table("tablename").column("intColumn", int.class)
416: .foreignKey("constraint_name", "foreigntable",
417: "intColumn", "foreignIntColumn");
418: assertEquals(
419: query.getSql(),
420: "CREATE TABLE tablename (intColumn INTEGER, CONSTRAINT constraint_name FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn))");
421: execute(query);
422: }
423:
424: public void testForeignKeyMultipleNamedH2() {
425: CreateTable query = new CreateTable(mH2);
426: query.table("tablename").column("intColumn", int.class).column(
427: "stringColumn", String.class, 50).foreignKey(
428: "constraint_name",
429: "foreigntable",
430: new String[] { "intColumn", "foreignIntColumn",
431: "stringColumn", "foreignStringColumn" });
432: assertEquals(
433: query.getSql(),
434: "CREATE TABLE tablename (intColumn INTEGER, stringColumn VARCHAR(50), CONSTRAINT constraint_name FOREIGN KEY (intColumn, stringColumn) REFERENCES foreigntable (foreignIntColumn, foreignStringColumn))");
435: execute(query);
436: }
437:
438: public void testForeignKeyViolationsSingleH2() {
439: CreateTable query = new CreateTable(mH2);
440: query.table("tablename").column("intColumn", int.class)
441: .foreignKey("foreigntable", "intColumn",
442: "foreignIntColumn", CreateTable.CASCADE, null);
443: assertEquals(
444: query.getSql(),
445: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON UPDATE CASCADE)");
446: execute(query);
447: query.clear();
448:
449: query.table("tablename").column("intColumn", int.class)
450: .foreignKey("foreigntable", "intColumn",
451: "foreignIntColumn", CreateTable.NOACTION, null);
452: assertEquals(
453: query.getSql(),
454: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON UPDATE NO ACTION)");
455: execute(query);
456: query.clear();
457:
458: query.table("tablename").column("intColumn", int.class)
459: .foreignKey("foreigntable", "intColumn",
460: "foreignIntColumn", CreateTable.RESTRICT, null);
461: assertEquals(
462: query.getSql(),
463: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON UPDATE RESTRICT)");
464: execute(query);
465: query.clear();
466:
467: query.table("tablename").column("intColumn", int.class)
468: .foreignKey("foreigntable", "intColumn",
469: "foreignIntColumn", CreateTable.SETDEFAULT,
470: null);
471: assertEquals(
472: query.getSql(),
473: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON UPDATE SET DEFAULT)");
474: execute(query);
475: query.clear();
476:
477: query.table("tablename").column("intColumn", int.class)
478: .foreignKey("foreigntable", "intColumn",
479: "foreignIntColumn", CreateTable.SETNULL, null);
480: assertEquals(
481: query.getSql(),
482: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON UPDATE SET NULL)");
483: execute(query);
484: query.clear();
485:
486: query.table("tablename").column("intColumn", int.class)
487: .foreignKey("foreigntable", "intColumn",
488: "foreignIntColumn", null, CreateTable.CASCADE);
489: assertEquals(
490: query.getSql(),
491: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON DELETE CASCADE)");
492: execute(query);
493: query.clear();
494:
495: query.table("tablename").column("intColumn", int.class)
496: .foreignKey("foreigntable", "intColumn",
497: "foreignIntColumn", null, CreateTable.NOACTION);
498: assertEquals(
499: query.getSql(),
500: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON DELETE NO ACTION)");
501: execute(query);
502: query.clear();
503:
504: query.table("tablename").column("intColumn", int.class)
505: .foreignKey("foreigntable", "intColumn",
506: "foreignIntColumn", null, CreateTable.RESTRICT);
507: assertEquals(
508: query.getSql(),
509: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON DELETE RESTRICT)");
510: execute(query);
511: query.clear();
512:
513: query.table("tablename").column("intColumn", int.class)
514: .foreignKey("foreigntable", "intColumn",
515: "foreignIntColumn", null,
516: CreateTable.SETDEFAULT);
517: assertEquals(
518: query.getSql(),
519: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON DELETE SET DEFAULT)");
520: execute(query);
521: query.clear();
522:
523: query.table("tablename").column("intColumn", int.class)
524: .foreignKey("foreigntable", "intColumn",
525: "foreignIntColumn", null, CreateTable.SETNULL);
526: assertEquals(
527: query.getSql(),
528: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON DELETE SET NULL)");
529: execute(query);
530: query.clear();
531: }
532:
533: public void testForeignKeyViolationsH2() {
534: CreateTable query = new CreateTable(mH2);
535: query.table("tablename").column("intColumn", int.class)
536: .foreignKey("foreigntable", "intColumn",
537: "foreignIntColumn", CreateTable.CASCADE,
538: CreateTable.NOACTION);
539: assertEquals(
540: query.getSql(),
541: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON UPDATE CASCADE ON DELETE NO ACTION)");
542: execute(query);
543: }
544:
545: public void testForeignKeyMultipleViolationsH2() {
546: CreateTable query = new CreateTable(mH2);
547: query.table("tablename").column("intColumn", int.class).column(
548: "stringColumn", String.class, 50).foreignKey(
549: "foreigntable",
550: new String[] { "intColumn", "foreignIntColumn",
551: "stringColumn", "foreignStringColumn" },
552: CreateTable.RESTRICT, CreateTable.SETDEFAULT);
553: assertEquals(
554: query.getSql(),
555: "CREATE TABLE tablename (intColumn INTEGER, stringColumn VARCHAR(50), FOREIGN KEY (intColumn, stringColumn) REFERENCES foreigntable (foreignIntColumn, foreignStringColumn) ON UPDATE RESTRICT ON DELETE SET DEFAULT)");
556: execute(query);
557: }
558:
559: public void testCheckSimpleH2() {
560: CreateTable query = new CreateTable(mH2);
561: query.table("tablename").column("intColumn", int.class).check(
562: "intColumn > 0");
563: assertEquals(query.getSql(),
564: "CREATE TABLE tablename (intColumn INTEGER, CHECK (intColumn > 0))");
565: execute(query);
566: }
567:
568: public void testCheckNamedH2() {
569: CreateTable query = new CreateTable(mH2);
570: query.table("tablename").column("intColumn", int.class).check(
571: "NAME_CK", "intColumn > 0");
572: assertEquals(
573: query.getSql(),
574: "CREATE TABLE tablename (intColumn INTEGER, CONSTRAINT NAME_CK CHECK (intColumn > 0))");
575: execute(query);
576: }
577:
578: public void testCloneH2() {
579: CreateTable query = new CreateTable(mH2);
580: query
581: .table("tablename")
582: .columns(BeanImpl.class)
583: .precision("propertyBigDecimal", 19, 9)
584: .precision("propertyBoolean", 4)
585: .precision("propertyByte", 8)
586: .precision("propertyCalendar", 20)
587: .precision("propertyChar", 10)
588: .precision("propertyDate", 7)
589: .precision("propertyDouble", 12, 3)
590: .precision("propertyFloat", 13, 2)
591: .precision("propertyInt", 10)
592: .precision("propertyLong", 12)
593: .precision("propertyShort", 9)
594: .precision("propertySqlDate", 8)
595: .precision("propertyString", 255)
596: .precision("propertyStringbuffer", 100)
597: .precision("propertyTime", 9)
598: .precision("propertyTimestamp", 30, 2)
599: .nullable("propertyString", CreateTable.NULL)
600: .nullable("propertyInt", CreateTable.NOTNULL)
601: .defaultValue("propertyString", "stringDefault")
602: .defaultFunction("propertyDate", "now()")
603: .customAttribute("propertyInt",
604: "CHECK (propertyInt > 0)")
605: .temporary(true)
606: .primaryKey(
607: "constraint_name1",
608: new String[] { "propertyInt", "propertyString" })
609: .unique(
610: "constraint_name2",
611: new String[] { "propertyLong", "propertyString" })
612: .foreignKey(
613: "foreigntable",
614: new String[] { "propertyInt",
615: "foreignIntColumn", "propertyString",
616: "foreignStringColumn" },
617: CreateTable.RESTRICT, CreateTable.SETDEFAULT)
618: .check("NAME_CK", "propertyInt > 0");
619: CreateTable query_clone = query.clone();
620: assertEquals(query.getSql(), query_clone.getSql());
621: assertTrue(query != query_clone);
622: execute(query_clone);
623: }
624: }
|