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: TestCreateTableDerby.java 3696 2007-03-16 13:20:26Z gbevin $
007: */
008: package com.uwyn.rife.database.queries;
009:
010: import com.uwyn.rife.database.BeanImpl;
011: import com.uwyn.rife.database.BeanImplConstrained;
012: import com.uwyn.rife.database.exceptions.ColumnsRequiredException;
013: import com.uwyn.rife.database.exceptions.TableNameRequiredException;
014: import com.uwyn.rife.database.exceptions.UnsupportedSqlFeatureException;
015: import java.math.BigDecimal;
016: import java.sql.Blob;
017:
018: public class TestCreateTableDerby extends TestCreateTable {
019: public TestCreateTableDerby(String name) {
020: super (name);
021: }
022:
023: public void testInstantiationDerby() {
024: CreateTable query = new CreateTable(mDerby);
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 testIncompleteQueryDerby() {
035: CreateTable query = new CreateTable(mDerby);
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 testClearDerby() {
054: CreateTable query = new CreateTable(mDerby);
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 testColumnDerby() {
067: CreateTable query = new CreateTable(mDerby);
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 VARCHAR(32672), stringbuffer VARCHAR(32672), characterobject CHAR, booleanobject NUMERIC(1), byteobject SMALLINT, doubleobject FLOAT, floatobject FLOAT, integerobject INTEGER, longobject BIGINT, shortobject SMALLINT, bigdecimal NUMERIC, charcolumn CHAR, booleancolumn NUMERIC(1), bytecolumn SMALLINT, doublecolumn FLOAT, floatcolumn FLOAT, intcolumn INTEGER, longcolumn BIGINT, shortcolumn SMALLINT, blobcolumn BLOB)");
089: execute(query);
090: }
091:
092: public void testColumnPrecisionDerby() {
093: CreateTable query = new CreateTable(mDerby);
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 NUMERIC(1), byteobject SMALLINT, doubleobject FLOAT, floatobject FLOAT, integerobject INTEGER, longobject BIGINT, shortobject SMALLINT, bigdecimal NUMERIC(19,9), charcolumn CHAR(10), booleancolumn NUMERIC(1), bytecolumn SMALLINT, doublecolumn FLOAT, floatcolumn FLOAT, intcolumn INTEGER, longcolumn BIGINT, shortcolumn SMALLINT, blobcolumn BLOB)");
117: execute(query);
118: }
119:
120: public void testColumnsBeanDerby() {
121: CreateTable query = new CreateTable(mDerby);
122: query.table("tablename").columns(BeanImpl.class);
123: assertEquals(
124: query.getSql(),
125: "CREATE TABLE tablename (propertyBigDecimal NUMERIC, propertyBoolean NUMERIC(1), propertyBooleanObject NUMERIC(1), 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 VARCHAR(32672), propertyStringbuffer VARCHAR(32672), 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 testColumnsBeanIncludedDerby() {
130: CreateTable query = new CreateTable(mDerby);
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 VARCHAR(32672), propertyTime TIME)");
139: execute(query);
140: }
141:
142: public void testColumnsBeanExcludedDerby() {
143: CreateTable query = new CreateTable(mDerby);
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 NUMERIC(1), propertyBooleanObject NUMERIC(1), 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 VARCHAR(32672), propertyTimestamp TIMESTAMP, CHECK (propertyEnum IS NULL OR propertyEnum IN ('VALUE_ONE','VALUE_TWO','VALUE_THREE')))");
152: execute(query);
153: }
154:
155: public void testColumnsBeanFilteredDerby() {
156: CreateTable query = new CreateTable(mDerby);
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 testColumnsBeanPrecisionDerby() {
173: CreateTable query = new CreateTable(mDerby);
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 NUMERIC(1), propertyBooleanObject NUMERIC(1), 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 testColumnsBeanConstrainedDerby() {
206: CreateTable query = new CreateTable(mDerby);
207: query.table("tablename").columns(BeanImplConstrained.class);
208: assertEquals(
209: query.getSql(),
210: "CREATE TABLE tablename (propertyBigDecimal NUMERIC(17,6), propertyBoolean NUMERIC(1), propertyBooleanObject NUMERIC(1), 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 testNullableDerby() {
215: CreateTable query = new CreateTable(mDerby);
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 , stringColumn VARCHAR(12) NOT NULL, intColumn2 INTEGER , intColumn3 INTEGER NOT NULL, floatColumn FLOAT)");
228: execute(query);
229: }
230:
231: public void testDefaultDerby() {
232: CreateTable query = new CreateTable(mDerby);
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 NUMERIC(1) DEFAULT 1, 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 NUMERIC(1) DEFAULT 0, 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 testDefaultFunctionDerby() {
278: CreateTable query = new CreateTable(mDerby);
279: query.table("tablename1").column("dateobject",
280: java.sql.Date.class).defaultFunction("dateobject",
281: "CURRENT_DATE");
282: assertEquals(query.getSql(),
283: "CREATE TABLE tablename1 (dateobject DATE DEFAULT CURRENT_DATE)");
284: execute(query);
285: }
286:
287: public void testCustomAttributeDerby() {
288: CreateTable query = new CreateTable(mDerby);
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 testTemporaryDerby() {
297: CreateTable query = new CreateTable(mDerby);
298: query.table("tablename").temporary(true).column("boolColumn",
299: boolean.class);
300: try {
301: query.getSql();
302: fail();
303: } catch (UnsupportedSqlFeatureException e) {
304: assertTrue(true);
305: }
306: }
307:
308: public void testPrimaryKeySimpleDerby() {
309: CreateTable query = new CreateTable(mDerby);
310: query.table("tablename").column("intColumn", int.class)
311: .primaryKey("intColumn");
312: assertEquals(query.getSql(),
313: "CREATE TABLE tablename (intColumn INTEGER NOT NULL, PRIMARY KEY (intColumn))");
314: execute(query);
315: }
316:
317: public void testPrimaryKeyMultipleDerby() {
318: CreateTable query = new CreateTable(mDerby);
319: query.table("tablename").column("intColumn", int.class).column(
320: "stringColumn", String.class, 50).primaryKey(
321: new String[] { "intColumn", "stringColumn" });
322: assertEquals(
323: query.getSql(),
324: "CREATE TABLE tablename (intColumn INTEGER NOT NULL, stringColumn VARCHAR(50) NOT NULL, PRIMARY KEY (intColumn, stringColumn))");
325: execute(query);
326: }
327:
328: public void testPrimaryKeyNamedDerby() {
329: CreateTable query = new CreateTable(mDerby);
330: query.table("tablename").column("intColumn", int.class)
331: .primaryKey("constraint_name", "intColumn");
332: assertEquals(
333: query.getSql(),
334: "CREATE TABLE tablename (intColumn INTEGER NOT NULL, CONSTRAINT constraint_name PRIMARY KEY (intColumn))");
335: execute(query);
336: }
337:
338: public void testPrimaryKeyMultipleNamedDerby() {
339: CreateTable query = new CreateTable(mDerby);
340: query.table("tablename").column("intColumn", int.class).column(
341: "stringColumn", String.class, 50).primaryKey(
342: "constraint_name",
343: new String[] { "intColumn", "stringColumn" });
344: assertEquals(
345: query.getSql(),
346: "CREATE TABLE tablename (intColumn INTEGER NOT NULL, stringColumn VARCHAR(50) NOT NULL, CONSTRAINT constraint_name PRIMARY KEY (intColumn, stringColumn))");
347: execute(query);
348: }
349:
350: public void testUniqueSimpleDerby() {
351: CreateTable query = new CreateTable(mDerby);
352: query.table("tablename").column("intColumn", int.class,
353: CreateTable.NOTNULL).unique("intColumn");
354: assertEquals(query.getSql(),
355: "CREATE TABLE tablename (intColumn INTEGER NOT NULL, UNIQUE (intColumn))");
356: execute(query);
357: }
358:
359: public void testUniqueMultipleDerby() {
360: CreateTable query = new CreateTable(mDerby);
361: query.table("tablename").column("intColumn", int.class,
362: CreateTable.NOTNULL).column("stringColumn",
363: String.class, 50, CreateTable.NOTNULL).unique(
364: new String[] { "intColumn", "stringColumn" });
365: assertEquals(
366: query.getSql(),
367: "CREATE TABLE tablename (intColumn INTEGER NOT NULL, stringColumn VARCHAR(50) NOT NULL, UNIQUE (intColumn, stringColumn))");
368: execute(query);
369: }
370:
371: public void testUniqueNamedDerby() {
372: CreateTable query = new CreateTable(mDerby);
373: query.table("tablename").column("intColumn", int.class,
374: CreateTable.NOTNULL).unique("constraint_name",
375: "intColumn");
376: assertEquals(
377: query.getSql(),
378: "CREATE TABLE tablename (intColumn INTEGER NOT NULL, CONSTRAINT constraint_name UNIQUE (intColumn))");
379: execute(query);
380: }
381:
382: public void testUniqueMultipleNamedDerby() {
383: CreateTable query = new CreateTable(mDerby);
384: query.table("tablename").column("intColumn", int.class,
385: CreateTable.NOTNULL).column("stringColumn",
386: String.class, 50, CreateTable.NOTNULL).unique(
387: "constraint_name",
388: new String[] { "intColumn", "stringColumn" });
389: assertEquals(
390: query.getSql(),
391: "CREATE TABLE tablename (intColumn INTEGER NOT NULL, stringColumn VARCHAR(50) NOT NULL, CONSTRAINT constraint_name UNIQUE (intColumn, stringColumn))");
392: execute(query);
393: }
394:
395: public void testForeignKeySimpleDerby() {
396: CreateTable query = new CreateTable(mDerby);
397: query.table("tablename").column("intColumn", int.class)
398: .foreignKey("foreigntable", "intColumn",
399: "foreignIntColumn");
400: assertEquals(
401: query.getSql(),
402: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn))");
403: execute(query);
404: }
405:
406: public void testForeignKeyMultipleDerby() {
407: CreateTable query = new CreateTable(mDerby);
408: query.table("tablename").column("intColumn", int.class).column(
409: "stringColumn", String.class, 50).foreignKey(
410: "foreigntable",
411: new String[] { "intColumn", "foreignIntColumn",
412: "stringColumn", "foreignStringColumn" });
413: assertEquals(
414: query.getSql(),
415: "CREATE TABLE tablename (intColumn INTEGER, stringColumn VARCHAR(50), FOREIGN KEY (intColumn, stringColumn) REFERENCES foreigntable (foreignIntColumn, foreignStringColumn))");
416: execute(query);
417: }
418:
419: public void testForeignKeySimpleNamedDerby() {
420: CreateTable query = new CreateTable(mDerby);
421: query.table("tablename").column("intColumn", int.class)
422: .foreignKey("constraint_name", "foreigntable",
423: "intColumn", "foreignIntColumn");
424: assertEquals(
425: query.getSql(),
426: "CREATE TABLE tablename (intColumn INTEGER, CONSTRAINT constraint_name FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn))");
427: execute(query);
428: }
429:
430: public void testForeignKeyMultipleNamedDerby() {
431: CreateTable query = new CreateTable(mDerby);
432: query.table("tablename").column("intColumn", int.class).column(
433: "stringColumn", String.class, 50).foreignKey(
434: "constraint_name",
435: "foreigntable",
436: new String[] { "intColumn", "foreignIntColumn",
437: "stringColumn", "foreignStringColumn" });
438: assertEquals(
439: query.getSql(),
440: "CREATE TABLE tablename (intColumn INTEGER, stringColumn VARCHAR(50), CONSTRAINT constraint_name FOREIGN KEY (intColumn, stringColumn) REFERENCES foreigntable (foreignIntColumn, foreignStringColumn))");
441: execute(query);
442: }
443:
444: public void testForeignKeyViolationsSingleDerby() {
445: CreateTable query = new CreateTable(mDerby);
446: query.table("tablename").column("intColumn", int.class)
447: .foreignKey("foreigntable", "intColumn",
448: "foreignIntColumn", CreateTable.CASCADE, null);
449: try {
450: query.getSql();
451: fail();
452: } catch (UnsupportedSqlFeatureException e) {
453: assertTrue(true);
454: }
455: query.clear();
456:
457: query.table("tablename").column("intColumn", int.class)
458: .foreignKey("foreigntable", "intColumn",
459: "foreignIntColumn", CreateTable.NOACTION, null);
460: assertEquals(
461: query.getSql(),
462: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON UPDATE NO ACTION)");
463: execute(query);
464: query.clear();
465:
466: query.table("tablename").column("intColumn", int.class)
467: .foreignKey("foreigntable", "intColumn",
468: "foreignIntColumn", CreateTable.RESTRICT, null);
469: assertEquals(
470: query.getSql(),
471: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON UPDATE RESTRICT)");
472: execute(query);
473: query.clear();
474:
475: query.table("tablename").column("intColumn", int.class)
476: .foreignKey("foreigntable", "intColumn",
477: "foreignIntColumn", CreateTable.SETDEFAULT,
478: null);
479: try {
480: query.getSql();
481: fail();
482: } catch (UnsupportedSqlFeatureException e) {
483: assertTrue(true);
484: }
485: query.clear();
486:
487: query.table("tablename").column("intColumn", int.class)
488: .foreignKey("foreigntable", "intColumn",
489: "foreignIntColumn", CreateTable.SETNULL, null);
490: try {
491: query.getSql();
492: fail();
493: } catch (UnsupportedSqlFeatureException e) {
494: assertTrue(true);
495: }
496: query.clear();
497:
498: query.table("tablename").column("intColumn", int.class)
499: .foreignKey("foreigntable", "intColumn",
500: "foreignIntColumn", null, CreateTable.CASCADE);
501: assertEquals(
502: query.getSql(),
503: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON DELETE CASCADE)");
504: execute(query);
505: query.clear();
506:
507: query.table("tablename").column("intColumn", int.class)
508: .foreignKey("foreigntable", "intColumn",
509: "foreignIntColumn", null, CreateTable.NOACTION);
510: assertEquals(
511: query.getSql(),
512: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON DELETE NO ACTION)");
513: execute(query);
514: query.clear();
515:
516: query.table("tablename").column("intColumn", int.class)
517: .foreignKey("foreigntable", "intColumn",
518: "foreignIntColumn", null, CreateTable.RESTRICT);
519: assertEquals(
520: query.getSql(),
521: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON DELETE RESTRICT)");
522: execute(query);
523: query.clear();
524:
525: query.table("tablename").column("intColumn", int.class)
526: .foreignKey("foreigntable", "intColumn",
527: "foreignIntColumn", null,
528: CreateTable.SETDEFAULT);
529: try {
530: query.getSql();
531: fail();
532: } catch (UnsupportedSqlFeatureException e) {
533: assertTrue(true);
534: }
535: query.clear();
536:
537: query.table("tablename").column("intColumn", int.class)
538: .foreignKey("foreigntable", "intColumn",
539: "foreignIntColumn", null, CreateTable.SETNULL);
540: assertEquals(
541: query.getSql(),
542: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON DELETE SET NULL)");
543: execute(query);
544: query.clear();
545: }
546:
547: public void testForeignKeyViolationsDerby() {
548: CreateTable query = new CreateTable(mDerby);
549: query.table("tablename").column("intColumn", int.class)
550: .foreignKey("foreigntable", "intColumn",
551: "foreignIntColumn", CreateTable.RESTRICT,
552: CreateTable.NOACTION);
553: assertEquals(
554: query.getSql(),
555: "CREATE TABLE tablename (intColumn INTEGER, FOREIGN KEY (intColumn) REFERENCES foreigntable (foreignIntColumn) ON UPDATE RESTRICT ON DELETE NO ACTION)");
556: execute(query);
557: }
558:
559: public void testForeignKeyMultipleViolationsDerby() {
560: CreateTable query = new CreateTable(mDerby);
561: query.table("tablename").column("intColumn", int.class).column(
562: "stringColumn", String.class, 50).foreignKey(
563: "foreigntable",
564: new String[] { "intColumn", "foreignIntColumn",
565: "stringColumn", "foreignStringColumn" },
566: CreateTable.RESTRICT, CreateTable.CASCADE);
567: assertEquals(
568: query.getSql(),
569: "CREATE TABLE tablename (intColumn INTEGER, stringColumn VARCHAR(50), FOREIGN KEY (intColumn, stringColumn) REFERENCES foreigntable (foreignIntColumn, foreignStringColumn) ON UPDATE RESTRICT ON DELETE CASCADE)");
570: execute(query);
571: }
572:
573: public void testCheckSimpleDerby() {
574: CreateTable query = new CreateTable(mDerby);
575: query.table("tablename").column("intColumn", int.class).check(
576: "intColumn > 0");
577: assertEquals(query.getSql(),
578: "CREATE TABLE tablename (intColumn INTEGER, CHECK (intColumn > 0))");
579: execute(query);
580: }
581:
582: public void testCheckNamedDerby() {
583: CreateTable query = new CreateTable(mDerby);
584: query.table("tablename").column("intColumn", int.class).check(
585: "NAME_CK", "intColumn > 0");
586: assertEquals(
587: query.getSql(),
588: "CREATE TABLE tablename (intColumn INTEGER, CONSTRAINT NAME_CK CHECK (intColumn > 0))");
589: execute(query);
590: }
591:
592: public void testCloneDerby() {
593: CreateTable query = new CreateTable(mDerby);
594: query
595: .table("tablename")
596: .columns(BeanImpl.class)
597: .precision("propertyBigDecimal", 19, 9)
598: .precision("propertyBoolean", 4)
599: .precision("propertyByte", 8)
600: .precision("propertyCalendar", 20)
601: .precision("propertyChar", 10)
602: .precision("propertyDate", 7)
603: .precision("propertyDouble", 12, 3)
604: .precision("propertyFloat", 13, 2)
605: .precision("propertyInt", 10)
606: .precision("propertyLong", 12)
607: .precision("propertyShort", 9)
608: .precision("propertySqlDate", 8)
609: .precision("propertyString", 255)
610: .precision("propertyStringbuffer", 100)
611: .precision("propertyTime", 9)
612: .precision("propertyTimestamp", 30, 2)
613: .nullable("propertyString", CreateTable.NULL)
614: .nullable("propertyLong", CreateTable.NOTNULL)
615: .defaultValue("propertyString", "stringDefault")
616: .defaultFunction("propertyDate", "CURRENT_TIMESTAMP")
617: .customAttribute("propertyInt",
618: "CHECK (propertyInt > 0)")
619: // .temporary(true)
620: .primaryKey(
621: "constraint_name1",
622: new String[] { "propertyInt", "propertyString" })
623: .unique(
624: "constraint_name2",
625: new String[] { "propertyLong", "propertyString" })
626: .foreignKey(
627: "foreigntable",
628: new String[] { "propertyInt",
629: "foreignIntColumn" },
630: CreateTable.RESTRICT, CreateTable.CASCADE)
631: .check("NAME_CK", "propertyInt > 0");
632: CreateTable query_clone = query.clone();
633: assertEquals(query.getSql(), query_clone.getSql());
634: assertTrue(query != query_clone);
635: execute(query_clone);
636: }
637: }
|