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: TestInsertMysql.java 3695 2007-03-16 09:26:50Z 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.DbPreparedStatement;
013: import com.uwyn.rife.database.DbPreparedStatementHandler;
014: import com.uwyn.rife.database.exceptions.FieldsRequiredException;
015: import com.uwyn.rife.database.exceptions.TableNameRequiredException;
016: import com.uwyn.rife.database.types.SqlNull;
017: import java.math.BigDecimal;
018: import java.sql.Time;
019: import java.sql.Timestamp;
020: import java.util.Arrays;
021: import java.util.Calendar;
022:
023: public class TestInsertMysql extends TestInsert {
024: public TestInsertMysql(String name) {
025: super (name);
026: }
027:
028: public void testInstantiationMysql() {
029: Insert query = new Insert(mMysql);
030: assertNotNull(query);
031: try {
032: query.getSql();
033: fail();
034: } catch (TableNameRequiredException e) {
035: assertEquals(e.getQueryName(), "Insert");
036: }
037: }
038:
039: public void testIncompleteQueryMysql() {
040: Insert query = new Insert(mMysql);
041: try {
042: query.getSql();
043: fail();
044: } catch (TableNameRequiredException e) {
045: assertEquals(e.getQueryName(), "Insert");
046: }
047: query.into("tablename");
048: try {
049: query.getSql();
050: fail();
051: } catch (FieldsRequiredException e) {
052: assertEquals(e.getQueryName(), "Insert");
053: }
054: query.field("col1", "val1");
055: assertNotNull(query.getSql());
056: }
057:
058: public void testClearMysql() {
059: Insert query = new Insert(mMysql);
060: query.into("tablename").field("col1", "val1");
061: assertNotNull(query.getSql());
062: query.clear();
063: try {
064: query.getSql();
065: fail();
066: } catch (TableNameRequiredException e) {
067: assertEquals(e.getQueryName(), "Insert");
068: }
069: }
070:
071: public void testParameterMysql() {
072: Insert query = new Insert(mMysql);
073: query.into("tablename").fieldParameter("col1");
074: assertEquals(query.getSql(),
075: "INSERT INTO tablename (col1) VALUES (?)");
076: }
077:
078: public void testHintMysql() {
079: Insert query = new Insert(mMysql);
080: query.hint("DELAYED").into("tablename")
081: .field("propertyInt", 34);
082: assertEquals(query.getSql(),
083: "INSERT DELAYED INTO tablename (propertyInt) VALUES (34)");
084: assertTrue(execute(query));
085: }
086:
087: public void testFieldMysql() {
088: Calendar cal = Calendar.getInstance();
089: cal.set(2002, 7, 19, 12, 17, 52);
090: Insert query = new Insert(mMysql);
091: query.into("tablename").field("nullColumn", SqlNull.NULL)
092: .field("propertyBigDecimal",
093: new BigDecimal("98347.876438637")).field(
094: "propertyBoolean", true).field("propertyByte",
095: (byte) 16).field("propertyCalendar",
096: cal.getTime()).field("propertyChar", 'M')
097: .field("propertyDate", cal).field("propertyDouble",
098: 12.3d).field("propertyFloat", 13.4f).field(
099: "propertyInt", 34).field("propertyLong", 45L)
100: .field("propertyShort", (short) 12).field(
101: "propertySqlDate",
102: new java.sql.Date(cal.getTime().getTime()))
103: .field("propertyString", "string'value").field(
104: "propertyStringbuffer",
105: new StringBuffer("stringbuffer'value")).field(
106: "propertyTime",
107: new Time(cal.getTime().getTime())).field(
108: "propertyTimestamp",
109: new Timestamp(cal.getTime().getTime()));
110: assertEquals(
111: query.getSql(),
112: "INSERT INTO tablename (nullColumn, propertyBigDecimal, propertyBoolean, propertyByte, propertyCalendar, propertyChar, propertyDate, propertyDouble, propertyFloat, propertyInt, propertyLong, propertyShort, propertySqlDate, propertyString, propertyStringbuffer, propertyTime, propertyTimestamp) VALUES (NULL, 98347.876438637, 1, 16, '2002-08-19 12:17:52.0', 'M', '2002-08-19 12:17:52.0', 12.3, 13.4, 34, 45, 12, '2002-08-19', 'string''value', 'stringbuffer''value', '12:17:52', '2002-08-19 12:17:52.0')");
113: assertTrue(execute(query));
114: }
115:
116: public void testFieldCustomMysql() {
117: Insert query = new Insert(mMysql);
118: query.into("tablename").fieldCustom("propertySqlDate", "now()");
119: assertEquals(query.getSql(),
120: "INSERT INTO tablename (propertySqlDate) VALUES (now())");
121: assertTrue(execute(query));
122: }
123:
124: public void testFieldsMysql() {
125: Calendar cal = Calendar.getInstance();
126: cal.set(2002, 7, 19, 12, 17, 52);
127: Insert query = new Insert(mMysql);
128: query.into("tablename").fields(
129: new Object[] { "nullColumn", SqlNull.NULL,
130: "propertyBigDecimal",
131: new BigDecimal("98347.876438637"),
132: "propertyBoolean", new Boolean(true),
133: "propertyByte", new Byte((byte) 16),
134: "propertyCalendar", cal.getTime(),
135: "propertyChar", new Character('M'),
136: "propertyDate", cal, "propertyDouble",
137: new Double(12.3d), "propertyFloat",
138: new Float(13.4f), "propertyInt",
139: new Integer(34), "propertyLong", new Long(45L),
140: "propertyShort", new Short((short) 12),
141: "propertySqlDate",
142: new java.sql.Date(cal.getTime().getTime()),
143: "propertyString", new String("string'value"),
144: "propertyStringbuffer",
145: new StringBuffer("stringbuffer'value"),
146: "propertyTime",
147: new Time(cal.getTime().getTime()),
148: "propertyTimestamp",
149: new Timestamp(cal.getTime().getTime()) });
150: assertEquals(
151: query.getSql(),
152: "INSERT INTO tablename (nullColumn, propertyBigDecimal, propertyBoolean, propertyByte, propertyCalendar, propertyChar, propertyDate, propertyDouble, propertyFloat, propertyInt, propertyLong, propertyShort, propertySqlDate, propertyString, propertyStringbuffer, propertyTime, propertyTimestamp) VALUES (NULL, 98347.876438637, 1, 16, '2002-08-19 12:17:52.0', 'M', '2002-08-19 12:17:52.0', 12.3, 13.4, 34, 45, 12, '2002-08-19', 'string''value', 'stringbuffer''value', '12:17:52', '2002-08-19 12:17:52.0')");
153: assertTrue(execute(query));
154: }
155:
156: public void testFieldParametersMysql() {
157: Insert query = new Insert(mMysql);
158: query.into("tablename");
159:
160: assertNull(query.getParameters());
161:
162: query.fieldParameter("nullColumn").fieldParameter(
163: "propertyBigDecimal").fieldParameter("propertyBoolean")
164: .fieldParameter("propertyByte").fieldParameter(
165: "propertyCalendar").fieldParameter(
166: "propertyChar").fieldParameter("propertyDate")
167: .fieldParameter("propertyDouble").fieldParameter(
168: "propertyFloat").fieldParameter("propertyInt")
169: .fieldParameter("propertyLong").fieldParameter(
170: "propertyShort").fieldParameter(
171: "propertySqlDate").fieldParameter(
172: "propertyString").fieldParameter(
173: "propertyStringbuffer").fieldParameter(
174: "propertyTime").fieldParameter(
175: "propertyTimestamp");
176:
177: assertEquals(query.getParameters().getOrderedNames().size(), 17);
178: assertEquals(query.getParameters().getOrderedNames().get(0),
179: "nullColumn");
180: assertEquals(query.getParameters().getOrderedNames().get(1),
181: "propertyBigDecimal");
182: assertEquals(query.getParameters().getOrderedNames().get(2),
183: "propertyBoolean");
184: assertEquals(query.getParameters().getOrderedNames().get(3),
185: "propertyByte");
186: assertEquals(query.getParameters().getOrderedNames().get(4),
187: "propertyCalendar");
188: assertEquals(query.getParameters().getOrderedNames().get(5),
189: "propertyChar");
190: assertEquals(query.getParameters().getOrderedNames().get(6),
191: "propertyDate");
192: assertEquals(query.getParameters().getOrderedNames().get(7),
193: "propertyDouble");
194: assertEquals(query.getParameters().getOrderedNames().get(8),
195: "propertyFloat");
196: assertEquals(query.getParameters().getOrderedNames().get(9),
197: "propertyInt");
198: assertEquals(query.getParameters().getOrderedNames().get(10),
199: "propertyLong");
200: assertEquals(query.getParameters().getOrderedNames().get(11),
201: "propertyShort");
202: assertEquals(query.getParameters().getOrderedNames().get(12),
203: "propertySqlDate");
204: assertEquals(query.getParameters().getOrderedNames().get(13),
205: "propertyString");
206: assertEquals(query.getParameters().getOrderedNames().get(14),
207: "propertyStringbuffer");
208: assertEquals(query.getParameters().getOrderedNames().get(15),
209: "propertyTime");
210: assertEquals(query.getParameters().getOrderedNames().get(16),
211: "propertyTimestamp");
212: assertTrue(Arrays.equals(query.getParameters()
213: .getOrderedNamesArray(), new String[] { "nullColumn",
214: "propertyBigDecimal", "propertyBoolean",
215: "propertyByte", "propertyCalendar", "propertyChar",
216: "propertyDate", "propertyDouble", "propertyFloat",
217: "propertyInt", "propertyLong", "propertyShort",
218: "propertySqlDate", "propertyString",
219: "propertyStringbuffer", "propertyTime",
220: "propertyTimestamp" }));
221:
222: assertEquals(
223: query.getSql(),
224: "INSERT INTO tablename (nullColumn, propertyBigDecimal, propertyBoolean, propertyByte, propertyCalendar, propertyChar, propertyDate, propertyDouble, propertyFloat, propertyInt, propertyLong, propertyShort, propertySqlDate, propertyString, propertyStringbuffer, propertyTime, propertyTimestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
225:
226: assertTrue(execute(query, new DbPreparedStatementHandler() {
227: public void setParameters(DbPreparedStatement statement) {
228: Calendar cal = Calendar.getInstance();
229: cal.set(2002, 7, 19, 12, 17, 52);
230: cal.set(Calendar.MILLISECOND, 462);
231: statement.setString(1, null).setBigDecimal(2,
232: new BigDecimal("98347.876438637")).setBoolean(
233: 3, true).setByte(4, (byte) 16).setDate(5,
234: new java.sql.Date(cal.getTime().getTime()))
235: .setString(6, "M").setDate(
236: 7,
237: new java.sql.Date(cal.getTime()
238: .getTime()))
239: .setDouble(8, 12.3d).setFloat(9, 13.4f).setInt(
240: 10, 34).setLong(11, 45L).setShort(12,
241: (short) 12).setDate(
242: 13,
243: new java.sql.Date(cal.getTime()
244: .getTime())).setString(14,
245: "string'value").setString(15,
246: "string'value2").setTime(16,
247: new Time(cal.getTime().getTime()))
248: .setTimestamp(17,
249: new Timestamp(cal.getTime().getTime()));
250: }
251: }));
252: }
253:
254: public void testFieldParametersMixedMysql() {
255: Insert query = new Insert(mMysql);
256: query.into("tablename");
257:
258: assertNull(query.getParameters());
259:
260: final Calendar cal = Calendar.getInstance();
261: cal.set(2002, 7, 19, 12, 17, 52);
262: cal.set(Calendar.MILLISECOND, 462);
263: query.fieldParameter("nullColumn").field("propertyBigDecimal",
264: new BigDecimal("98347.876438637")).fieldParameter(
265: "propertyBoolean").fieldParameter("propertyByte")
266: .field("propertyCalendar", cal.getTime())
267: .fieldParameter("propertyChar").field("propertyDate",
268: cal).field("propertyDouble", 12.3d)
269: .fieldParameter("propertyFloat").fieldParameter(
270: "propertyInt").field("propertyLong", 45L)
271: .field("propertyShort", (short) 12).fieldParameter(
272: "propertySqlDate").fieldParameter(
273: "propertyString").field("propertyStringbuffer",
274: new StringBuffer("stringbuffer'value")).field(
275: "propertyTime",
276: new Time(cal.getTime().getTime()))
277: .fieldParameter("propertyTimestamp");
278:
279: assertEquals(query.getParameters().getOrderedNames().size(), 9);
280: assertEquals(query.getParameters().getOrderedNames().get(0),
281: "nullColumn");
282: assertEquals(query.getParameters().getOrderedNames().get(1),
283: "propertyBoolean");
284: assertEquals(query.getParameters().getOrderedNames().get(2),
285: "propertyByte");
286: assertEquals(query.getParameters().getOrderedNames().get(3),
287: "propertyChar");
288: assertEquals(query.getParameters().getOrderedNames().get(4),
289: "propertyFloat");
290: assertEquals(query.getParameters().getOrderedNames().get(5),
291: "propertyInt");
292: assertEquals(query.getParameters().getOrderedNames().get(6),
293: "propertySqlDate");
294: assertEquals(query.getParameters().getOrderedNames().get(7),
295: "propertyString");
296: assertEquals(query.getParameters().getOrderedNames().get(8),
297: "propertyTimestamp");
298: assertTrue(Arrays.equals(query.getParameters()
299: .getOrderedNamesArray(), new String[] { "nullColumn",
300: "propertyBoolean", "propertyByte", "propertyChar",
301: "propertyFloat", "propertyInt", "propertySqlDate",
302: "propertyString", "propertyTimestamp" }));
303:
304: assertEquals(
305: query.getSql(),
306: "INSERT INTO tablename (nullColumn, propertyBigDecimal, propertyBoolean, propertyByte, propertyCalendar, propertyChar, propertyDate, propertyDouble, propertyFloat, propertyInt, propertyLong, propertyShort, propertySqlDate, propertyString, propertyStringbuffer, propertyTime, propertyTimestamp) VALUES (?, 98347.876438637, ?, ?, '2002-08-19 12:17:52.0', ?, '2002-08-19 12:17:52.0', 12.3, ?, ?, 45, 12, ?, ?, 'stringbuffer''value', '12:17:52', ?)");
307: assertTrue(execute(query, new DbPreparedStatementHandler() {
308: public void setParameters(DbPreparedStatement statement) {
309: statement.setString(1, null).setBoolean(2, true)
310: .setByte(3, (byte) 16).setString(4, "M")
311: .setFloat(5, 13.4f).setInt(6, 34).setDate(
312: 7,
313: new java.sql.Date(cal.getTime()
314: .getTime())).setString(8,
315: "string'value").setTimestamp(9,
316: new Timestamp(cal.getTime().getTime()));
317: }
318: }));
319: }
320:
321: public void testFieldsBeanMysql() {
322: Insert query = new Insert(mMysql);
323: query.into("tablename").fields(BeanImpl.getPopulatedBean());
324: assertEquals(
325: query.getSql(),
326: "INSERT INTO tablename (propertyBigDecimal, propertyBoolean, propertyBooleanObject, propertyByte, propertyByteObject, propertyCalendar, propertyChar, propertyCharacterObject, propertyDate, propertyDouble, propertyDoubleObject, propertyEnum, propertyFloat, propertyFloatObject, propertyInt, propertyIntegerObject, propertyLong, propertyLongObject, propertyShort, propertyShortObject, propertySqlDate, propertyString, propertyStringbuffer, propertyTime, propertyTimestamp) VALUES (219038743.392874, 1, 0, 89, 34, '2002-06-18 15:26:14.0', 'v', 'r', '2002-06-18 15:26:14.0', 53348.34, 143298.692, 'VALUE_THREE', 98634.2, 8734.7, 545, 968, 34563, 66875, 43, 68, '2002-06-18', 'someotherstring', 'someotherstringbuff', '15:26:14', '2002-06-18 15:26:14.0')");
327: assertTrue(execute(query));
328: }
329:
330: public void testFieldsBeanConstrainedMysql() {
331: Insert query = new Insert(mMysql);
332: query.into("tablename").fields(
333: BeanImplConstrained.getPopulatedBean());
334: assertEquals(
335: query.getSql(),
336: "INSERT INTO tablename (propertyBigDecimal, propertyBoolean, propertyBooleanObject, propertyByteObject, propertyCalendar, propertyChar, propertyCharacterObject, propertyDate, propertyDouble, propertyDoubleObject, propertyFloat, propertyFloatObject, propertyInt, propertyIntegerObject, propertyLongObject, propertyShort, propertySqlDate, propertyString, propertyStringbuffer, propertyTime, propertyTimestamp) VALUES (219038743.392874, 1, 0, 34, '2002-06-18 15:26:14.0', 'v', 'r', '2002-06-18 15:26:14.0', 53348.34, 143298.692, 98634.2, 8734.7, 545, 968, 66875, 43, '2002-06-18', 'someotherstring', 'someotherstringbuff', '15:26:14', '2002-06-18 15:26:14.0')");
337: assertTrue(execute(query));
338: }
339:
340: public void testFieldsBeanNullValuesMysql() {
341: Insert query = new Insert(mMysql);
342: query.into("tablename").fields(BeanImpl.getNullBean());
343: assertEquals(
344: query.getSql(),
345: "INSERT INTO tablename (propertyBoolean, propertyBooleanObject, propertyByte, propertyByteObject, propertyDouble, propertyDoubleObject, propertyFloat, propertyFloatObject, propertyInt, propertyIntegerObject, propertyLong, propertyLongObject, propertyShort, propertyShortObject) VALUES (0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0)");
346: assertTrue(execute(query));
347: }
348:
349: public void testFieldsBeanIncludedMysql() {
350: Insert query = new Insert(mMysql);
351: query.into("tablename").fieldsIncluded(
352: BeanImpl.getPopulatedBean(),
353: new String[] { "propertyByte", "propertyDouble",
354: "propertyShort", "propertyStringbuffer",
355: "propertyTime" });
356: assertEquals(
357: query.getSql(),
358: "INSERT INTO tablename (propertyByte, propertyDouble, propertyShort, propertyStringbuffer, propertyTime) VALUES (89, 53348.34, 43, 'someotherstringbuff', '15:26:14')");
359: assertTrue(execute(query));
360: }
361:
362: public void testFieldsBeanExcludedMysql() {
363: Insert query = new Insert(mMysql);
364: query.into("tablename").fieldsExcluded(
365: BeanImpl.getPopulatedBean(),
366: new String[] { "propertyByte", "propertyDouble",
367: "propertyShort", "propertyStringbuffer",
368: "propertyTime" });
369: assertEquals(
370: query.getSql(),
371: "INSERT INTO tablename (propertyBigDecimal, propertyBoolean, propertyBooleanObject, propertyByteObject, propertyCalendar, propertyChar, propertyCharacterObject, propertyDate, propertyDoubleObject, propertyEnum, propertyFloat, propertyFloatObject, propertyInt, propertyIntegerObject, propertyLong, propertyLongObject, propertyShortObject, propertySqlDate, propertyString, propertyTimestamp) VALUES (219038743.392874, 1, 0, 34, '2002-06-18 15:26:14.0', 'v', 'r', '2002-06-18 15:26:14.0', 143298.692, 'VALUE_THREE', 98634.2, 8734.7, 545, 968, 34563, 66875, 68, '2002-06-18', 'someotherstring', '2002-06-18 15:26:14.0')");
372: assertTrue(execute(query));
373: }
374:
375: public void testFieldsBeanFilteredMysql() {
376: Insert query = new Insert(mMysql);
377: query.into("tablename").fieldsFiltered(
378: BeanImpl.getPopulatedBean(),
379: new String[] { "propertyByte", "propertyDouble",
380: "propertyShort", "propertyStringbuffer",
381: "propertyTime" },
382: new String[] { "propertyByte", "propertyShort",
383: "propertyTime" });
384: assertEquals(
385: query.getSql(),
386: "INSERT INTO tablename (propertyDouble, propertyStringbuffer) VALUES (53348.34, 'someotherstringbuff')");
387: assertTrue(execute(query));
388: }
389:
390: public void testMultipleRowsMysql() {
391: Insert query = new Insert(mMysql);
392: query.into("tablename").field("propertyChar", 'M').field(
393: "propertyDouble", 12.3d).field("propertyFloat", 13.4f)
394: .field("propertyInt", 34);
395: query.field("propertyChar", 'S').field("propertyDouble", 45.1d)
396: .field("propertyFloat", 27.9f);
397: query.field("propertyChar", 'T');
398: assertEquals(
399: query.getSql(),
400: "INSERT INTO tablename (propertyChar, propertyDouble, propertyFloat, propertyInt) VALUES ('M', 12.3, 13.4, 34), ('S', 45.1, 27.9, NULL), ('T', NULL, NULL, NULL)");
401: assertTrue(execute(query));
402: }
403:
404: public void testFieldsParametersBeanMysql() {
405: Insert query = new Insert(mPgsql);
406: query.into("tablename").fieldsParameters(BeanImpl.class);
407: assertEquals(
408: query.getSql(),
409: "INSERT INTO tablename (propertyBigDecimal, propertyBoolean, propertyBooleanObject, propertyByte, propertyByteObject, propertyCalendar, propertyChar, propertyCharacterObject, propertyDate, propertyDouble, propertyDoubleObject, propertyEnum, propertyFloat, propertyFloatObject, propertyInt, propertyIntegerObject, propertyLong, propertyLongObject, propertyShort, propertyShortObject, propertySqlDate, propertyString, propertyStringbuffer, propertyTime, propertyTimestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
410:
411: assertEquals(query.getParameters().getOrderedNames().size(), 25);
412: assertEquals(query.getParameters().getOrderedNames().get(0),
413: "propertyBigDecimal");
414: assertEquals(query.getParameters().getOrderedNames().get(1),
415: "propertyBoolean");
416: assertEquals(query.getParameters().getOrderedNames().get(2),
417: "propertyBooleanObject");
418: assertEquals(query.getParameters().getOrderedNames().get(3),
419: "propertyByte");
420: assertEquals(query.getParameters().getOrderedNames().get(4),
421: "propertyByteObject");
422: assertEquals(query.getParameters().getOrderedNames().get(5),
423: "propertyCalendar");
424: assertEquals(query.getParameters().getOrderedNames().get(6),
425: "propertyChar");
426: assertEquals(query.getParameters().getOrderedNames().get(7),
427: "propertyCharacterObject");
428: assertEquals(query.getParameters().getOrderedNames().get(8),
429: "propertyDate");
430: assertEquals(query.getParameters().getOrderedNames().get(9),
431: "propertyDouble");
432: assertEquals(query.getParameters().getOrderedNames().get(10),
433: "propertyDoubleObject");
434: assertEquals(query.getParameters().getOrderedNames().get(11),
435: "propertyEnum");
436: assertEquals(query.getParameters().getOrderedNames().get(12),
437: "propertyFloat");
438: assertEquals(query.getParameters().getOrderedNames().get(13),
439: "propertyFloatObject");
440: assertEquals(query.getParameters().getOrderedNames().get(14),
441: "propertyInt");
442: assertEquals(query.getParameters().getOrderedNames().get(15),
443: "propertyIntegerObject");
444: assertEquals(query.getParameters().getOrderedNames().get(16),
445: "propertyLong");
446: assertEquals(query.getParameters().getOrderedNames().get(17),
447: "propertyLongObject");
448: assertEquals(query.getParameters().getOrderedNames().get(18),
449: "propertyShort");
450: assertEquals(query.getParameters().getOrderedNames().get(19),
451: "propertyShortObject");
452: assertEquals(query.getParameters().getOrderedNames().get(20),
453: "propertySqlDate");
454: assertEquals(query.getParameters().getOrderedNames().get(21),
455: "propertyString");
456: assertEquals(query.getParameters().getOrderedNames().get(22),
457: "propertyStringbuffer");
458: assertEquals(query.getParameters().getOrderedNames().get(23),
459: "propertyTime");
460: assertEquals(query.getParameters().getOrderedNames().get(24),
461: "propertyTimestamp");
462: assertTrue(Arrays.equals(query.getParameters()
463: .getOrderedNamesArray(), new String[] {
464: "propertyBigDecimal", "propertyBoolean",
465: "propertyBooleanObject", "propertyByte",
466: "propertyByteObject", "propertyCalendar",
467: "propertyChar", "propertyCharacterObject",
468: "propertyDate", "propertyDouble",
469: "propertyDoubleObject", "propertyEnum",
470: "propertyFloat", "propertyFloatObject", "propertyInt",
471: "propertyIntegerObject", "propertyLong",
472: "propertyLongObject", "propertyShort",
473: "propertyShortObject", "propertySqlDate",
474: "propertyString", "propertyStringbuffer",
475: "propertyTime", "propertyTimestamp" }));
476:
477: assertTrue(execute(query, new DbPreparedStatementHandler() {
478: public void setParameters(DbPreparedStatement statement) {
479: Calendar cal = Calendar.getInstance();
480: cal.set(2002, 7, 19, 12, 17, 52);
481: cal.set(Calendar.MILLISECOND, 462);
482: statement.setBigDecimal(1,
483: new BigDecimal("98347.876438637")).setBoolean(
484: 2, false).setBoolean(3, true).setByte(4,
485: (byte) 16).setByte(5, (byte) 72)
486: .setTimestamp(
487: 6,
488: new java.sql.Timestamp(cal.getTime()
489: .getTime())).setString(7, "M")
490: .setString(8, "p").setTimestamp(
491: 9,
492: new java.sql.Timestamp(cal.getTime()
493: .getTime())).setDouble(10,
494: 12.3d).setDouble(11, 68.7d).setString(
495: 12, "VALUE_THREE").setFloat(13, 13.4f)
496: .setFloat(14, 42.1f).setInt(15, 92).setInt(16,
497: 34).setLong(17, 687L).setLong(18, 92)
498: .setShort(19, (short) 7).setShort(20,
499: (short) 12).setDate(
500: 21,
501: new java.sql.Date(cal.getTime()
502: .getTime())).setString(22,
503: "string'value").setString(23,
504: "string'value2").setTime(24,
505: new Time(cal.getTime().getTime()))
506: .setTimestamp(25,
507: new Timestamp(cal.getTime().getTime()));
508: }
509: }));
510: }
511:
512: public void testFieldsParametersBeanConstrainedMysql() {
513: Insert query = new Insert(mMysql);
514: query.into("tablename").fieldsParameters(
515: BeanImplConstrained.class);
516: assertEquals(
517: query.getSql(),
518: "INSERT INTO tablename (propertyBigDecimal, propertyBoolean, propertyBooleanObject, propertyByteObject, propertyCalendar, propertyChar, propertyCharacterObject, propertyDate, propertyDouble, propertyDoubleObject, propertyFloat, propertyFloatObject, propertyInt, propertyIntegerObject, propertyLongObject, propertyShort, propertySqlDate, propertyString, propertyStringbuffer, propertyTime, propertyTimestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
519:
520: assertEquals(query.getParameters().getOrderedNames().size(), 21);
521: assertEquals(query.getParameters().getOrderedNames().get(0),
522: "propertyBigDecimal");
523: assertEquals(query.getParameters().getOrderedNames().get(1),
524: "propertyBoolean");
525: assertEquals(query.getParameters().getOrderedNames().get(2),
526: "propertyBooleanObject");
527: assertEquals(query.getParameters().getOrderedNames().get(3),
528: "propertyByteObject");
529: assertEquals(query.getParameters().getOrderedNames().get(4),
530: "propertyCalendar");
531: assertEquals(query.getParameters().getOrderedNames().get(5),
532: "propertyChar");
533: assertEquals(query.getParameters().getOrderedNames().get(6),
534: "propertyCharacterObject");
535: assertEquals(query.getParameters().getOrderedNames().get(7),
536: "propertyDate");
537: assertEquals(query.getParameters().getOrderedNames().get(8),
538: "propertyDouble");
539: assertEquals(query.getParameters().getOrderedNames().get(9),
540: "propertyDoubleObject");
541: assertEquals(query.getParameters().getOrderedNames().get(10),
542: "propertyFloat");
543: assertEquals(query.getParameters().getOrderedNames().get(11),
544: "propertyFloatObject");
545: assertEquals(query.getParameters().getOrderedNames().get(12),
546: "propertyInt");
547: assertEquals(query.getParameters().getOrderedNames().get(13),
548: "propertyIntegerObject");
549: assertEquals(query.getParameters().getOrderedNames().get(14),
550: "propertyLongObject");
551: assertEquals(query.getParameters().getOrderedNames().get(15),
552: "propertyShort");
553: assertEquals(query.getParameters().getOrderedNames().get(16),
554: "propertySqlDate");
555: assertEquals(query.getParameters().getOrderedNames().get(17),
556: "propertyString");
557: assertEquals(query.getParameters().getOrderedNames().get(18),
558: "propertyStringbuffer");
559: assertEquals(query.getParameters().getOrderedNames().get(19),
560: "propertyTime");
561: assertEquals(query.getParameters().getOrderedNames().get(20),
562: "propertyTimestamp");
563: assertTrue(Arrays.equals(query.getParameters()
564: .getOrderedNamesArray(), new String[] {
565: "propertyBigDecimal", "propertyBoolean",
566: "propertyBooleanObject", "propertyByteObject",
567: "propertyCalendar", "propertyChar",
568: "propertyCharacterObject", "propertyDate",
569: "propertyDouble", "propertyDoubleObject",
570: "propertyFloat", "propertyFloatObject", "propertyInt",
571: "propertyIntegerObject", "propertyLongObject",
572: "propertyShort", "propertySqlDate", "propertyString",
573: "propertyStringbuffer", "propertyTime",
574: "propertyTimestamp" }));
575:
576: assertTrue(execute(query, new DbPreparedStatementHandler() {
577: public void setParameters(DbPreparedStatement statement) {
578: Calendar cal = Calendar.getInstance();
579: cal.set(2002, 5, 18, 15, 26, 14);
580: cal.set(Calendar.MILLISECOND, 764);
581: statement.setBigDecimal(1,
582: new BigDecimal("219038743.392874")).setBoolean(
583: 2, true).setBoolean(3, false).setByte(4,
584: (byte) 34)
585: .setTimestamp(
586: 5,
587: new java.sql.Timestamp(cal.getTime()
588: .getTime())).setString(6, "v")
589: .setString(7, "r").setTimestamp(
590: 8,
591: new java.sql.Timestamp(cal.getTime()
592: .getTime())).setDouble(9,
593: 53348.34d).setDouble(10, 143298.692d)
594: .setFloat(11, 98634.2f).setFloat(12, 8734.7f)
595: .setInt(13, 545).setInt(14, 968).setLong(15,
596: 66875L).setShort(16, (short) 43)
597: .setDate(
598: 17,
599: new java.sql.Date(cal.getTime()
600: .getTime())).setString(18,
601: "someotherstring").setString(19,
602: "someotherstringbuff").setTime(20,
603: new Time(cal.getTime().getTime()))
604: .setTimestamp(21,
605: new Timestamp(cal.getTime().getTime()));
606: }
607: }));
608: }
609:
610: public void testFieldsParametersBeanExcludedMysql() {
611: Insert query = new Insert(mPgsql);
612: query.into("tablename").fieldsParametersExcluded(
613: BeanImpl.class,
614: new String[] { "propertyBoolean", "propertyByte",
615: "propertyChar", "propertyDouble",
616: "propertyInt", "propertyLong",
617: "propertySqlDate", "propertyStringbuffer",
618: "propertyTimestamp" });
619: assertEquals(
620: query.getSql(),
621: "INSERT INTO tablename (propertyBigDecimal, propertyBooleanObject, propertyByteObject, propertyCalendar, propertyCharacterObject, propertyDate, propertyDoubleObject, propertyEnum, propertyFloat, propertyFloatObject, propertyIntegerObject, propertyLongObject, propertyShort, propertyShortObject, propertyString, propertyTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
622:
623: assertEquals(query.getParameters().getOrderedNames().size(), 16);
624: assertEquals(query.getParameters().getOrderedNames().get(0),
625: "propertyBigDecimal");
626: assertEquals(query.getParameters().getOrderedNames().get(1),
627: "propertyBooleanObject");
628: assertEquals(query.getParameters().getOrderedNames().get(2),
629: "propertyByteObject");
630: assertEquals(query.getParameters().getOrderedNames().get(3),
631: "propertyCalendar");
632: assertEquals(query.getParameters().getOrderedNames().get(4),
633: "propertyCharacterObject");
634: assertEquals(query.getParameters().getOrderedNames().get(5),
635: "propertyDate");
636: assertEquals(query.getParameters().getOrderedNames().get(6),
637: "propertyDoubleObject");
638: assertEquals(query.getParameters().getOrderedNames().get(7),
639: "propertyEnum");
640: assertEquals(query.getParameters().getOrderedNames().get(8),
641: "propertyFloat");
642: assertEquals(query.getParameters().getOrderedNames().get(9),
643: "propertyFloatObject");
644: assertEquals(query.getParameters().getOrderedNames().get(10),
645: "propertyIntegerObject");
646: assertEquals(query.getParameters().getOrderedNames().get(11),
647: "propertyLongObject");
648: assertEquals(query.getParameters().getOrderedNames().get(12),
649: "propertyShort");
650: assertEquals(query.getParameters().getOrderedNames().get(13),
651: "propertyShortObject");
652: assertEquals(query.getParameters().getOrderedNames().get(14),
653: "propertyString");
654: assertEquals(query.getParameters().getOrderedNames().get(15),
655: "propertyTime");
656: assertTrue(Arrays.equals(query.getParameters()
657: .getOrderedNamesArray(), new String[] {
658: "propertyBigDecimal", "propertyBooleanObject",
659: "propertyByteObject", "propertyCalendar",
660: "propertyCharacterObject", "propertyDate",
661: "propertyDoubleObject", "propertyEnum",
662: "propertyFloat", "propertyFloatObject",
663: "propertyIntegerObject", "propertyLongObject",
664: "propertyShort", "propertyShortObject",
665: "propertyString", "propertyTime" }));
666:
667: assertTrue(execute(query, new DbPreparedStatementHandler() {
668: public void setParameters(DbPreparedStatement statement) {
669: Calendar cal = Calendar.getInstance();
670: cal.set(2002, 7, 19, 12, 17, 52);
671: cal.set(Calendar.MILLISECOND, 462);
672: statement.setBigDecimal(1,
673: new BigDecimal("98347.876438637")).setBoolean(
674: 2, true).setByte(3, (byte) 72)
675: .setTimestamp(
676: 4,
677: new java.sql.Timestamp(cal.getTime()
678: .getTime())).setString(5, "o")
679: .setTimestamp(
680: 6,
681: new java.sql.Timestamp(cal.getTime()
682: .getTime()))
683: .setDouble(7, 86.7d)
684: .setString(8, "VALUE_THREE").setFloat(9, 13.4f)
685: .setFloat(10, 32.8f).setInt(11, 358).setLong(
686: 12, 9680L).setShort(13, (short) 12)
687: .setShort(14, (short) 78).setString(15,
688: "string'value").setTime(16,
689: new Time(cal.getTime().getTime()));
690: }
691: }));
692: }
693:
694: public void testInsertSubselectParamsMysql() {
695: // mysql doesn't support subqueries
696: }
697:
698: public void testCloneMysql() {
699: final Calendar cal = Calendar.getInstance();
700: cal.set(2002, 7, 19, 12, 17, 52);
701: cal.set(Calendar.MILLISECOND, 462);
702: Insert query = new Insert(mMysql);
703: query.hint("DELAYED").into("tablename").fieldParameter(
704: "nullColumn").field("propertyBigDecimal",
705: new BigDecimal("98347.876438637")).fieldParameter(
706: "propertyBoolean").fieldParameter("propertyByte")
707: .field("propertyCalendar", cal.getTime())
708: .fieldParameter("propertyChar").field("propertyDate",
709: cal).field("propertyDouble", 12.3d)
710: .fieldParameter("propertyFloat").fieldParameter(
711: "propertyInt").field("propertyShort",
712: (short) 12).fieldParameter("propertySqlDate")
713: .fieldParameter("propertyString").field(
714: "propertyStringbuffer",
715: new StringBuffer("stringbuffer'value")).field(
716: "propertyTime",
717: new Time(cal.getTime().getTime()))
718: .fieldParameter("propertyTimestamp")
719:
720: .fieldParameter("nullColumn").field(
721: "propertyBigDecimal",
722: new BigDecimal("98347.876438637"))
723: .fieldParameter("propertyBoolean").fieldParameter(
724: "propertyByte").field("propertyCalendar",
725: cal.getTime()).fieldParameter("propertyChar")
726: .field("propertyDate", cal).field("propertyDouble",
727: 12.3d).fieldParameter("propertyFloat")
728: .fieldParameter("propertyInt").field("propertyShort",
729: (short) 12).fieldParameter("propertySqlDate")
730: .fieldParameter("propertyString").field(
731: "propertyStringbuffer",
732: new StringBuffer("stringbuffer'value")).field(
733: "propertyTime",
734: new Time(cal.getTime().getTime()))
735: .fieldParameter("propertyTimestamp");
736:
737: Insert query_clone = query.clone();
738: assertEquals(query.getSql(), query_clone.getSql());
739: assertTrue(query != query_clone);
740:
741: assertTrue(execute(query, new DbPreparedStatementHandler() {
742: public void setParameters(DbPreparedStatement statement) {
743: statement.setString("nullColumn", null).setBoolean(
744: "propertyBoolean", true).setByte(
745: "propertyByte", (byte) 16).setString(
746: "propertyChar", "M").setFloat("propertyFloat",
747: 13.4f).setInt("propertyInt", 34).setDate(
748: "propertySqlDate",
749: new java.sql.Date(cal.getTime().getTime()))
750: .setString("propertyString", "string'value")
751: .setTimestamp("propertyTimestamp",
752: new Timestamp(cal.getTime().getTime()));
753: }
754: }));
755: }
756: }
|