001: /*
002: * $Id: TestInsertCommand.java,v 1.15 2005/12/20 18:32:27 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2004 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.engine.commands;
042:
043: import java.util.ArrayList;
044: import java.util.Arrays;
045:
046: import junit.framework.Test;
047: import junit.framework.TestCase;
048: import junit.framework.TestSuite;
049:
050: import org.axiondb.AxionCommand;
051: import org.axiondb.ColumnIdentifier;
052: import org.axiondb.Literal;
053: import org.axiondb.TableIdentifier;
054: import org.axiondb.engine.MemoryDatabase;
055: import org.axiondb.functions.FunctionIdentifier;
056: import org.axiondb.types.CharacterVaryingType;
057: import org.axiondb.types.IntegerType;
058:
059: /**
060: * @version $Revision: 1.15 $ $Date: 2005/12/20 18:32:27 $
061: * @author Chuck Burdick
062: * @author Ahimanikya Satapathy
063: */
064: public class TestInsertCommand extends TestCase {
065:
066: //------------------------------------------------------------ Conventional
067:
068: public TestInsertCommand(String testName) {
069: super (testName);
070: }
071:
072: public static void main(String args[]) {
073: String[] testCaseName = { TestInsertCommand.class.getName() };
074: junit.textui.TestRunner.main(testCaseName);
075: }
076:
077: public static Test suite() {
078: return new TestSuite(TestInsertCommand.class);
079: }
080:
081: //--------------------------------------------------------------- Lifecycle
082: org.axiondb.Database _db = null;
083:
084: public void setUp() throws Exception {
085: _db = new MemoryDatabase("testdb");
086: CreateTableCommand cmd = new CreateTableCommand("FOO");
087: cmd.addColumn("A", "varchar", "10");
088: cmd.execute(_db);
089:
090: //create emp source table
091: cmd = new CreateTableCommand("EMP");
092: cmd.addColumn("ID", "integer");
093: cmd.addColumn("FNAME", "varchar", "10");
094: cmd.addColumn("LNAME", "varchar", "10");
095: cmd.execute(_db);
096:
097: //create salary source table
098: cmd = new CreateTableCommand("salary");
099: cmd.addColumn("eid", "integer");
100: cmd.addColumn("base_salary", "integer");
101: cmd.execute(_db);
102:
103: //create emp target table 1
104: cmd = new CreateTableCommand("EMP_TGT1");
105: cmd.addColumn("ID", "integer");
106: cmd.addColumn("NAME", "varchar", "20");
107: cmd.execute(_db);
108:
109: //create emp target table 2
110: cmd = new CreateTableCommand("EMP_TGT2");
111: cmd.addColumn("ID", "integer");
112: cmd.addColumn("NAME", "varchar", "20");
113: cmd.execute(_db);
114:
115: //create emp target table 3
116: cmd = new CreateTableCommand("EMP_TGT3");
117: cmd.addColumn("ID", "integer");
118: cmd.addColumn("NAME", "varchar", "20");
119: cmd.execute(_db);
120: }
121:
122: public void tearDown() throws Exception {
123: _db.shutdown();
124: _db = null;
125: }
126:
127: //------------------------------------------------------------------- Tests
128:
129: public void testInsert() throws Exception {
130: assertEquals("Should have no rows", 0, _db.getTable("FOO")
131: .getRowCount());
132: AxionCommand cmd = new InsertCommand(
133: new TableIdentifier("FOO"),
134: Arrays
135: .asList(new ColumnIdentifier[] { new ColumnIdentifier(
136: "A") }), Arrays
137: .asList(new Literal[] { new Literal("BAR",
138: new CharacterVaryingType(10)) }));
139: cmd.execute(_db);
140: assertEquals("Should have 1 row", 1, _db.getTable("FOO")
141: .getRowCount());
142: }
143:
144: public void testMultiTableInsert() throws Exception {
145: InsertCommand cmd = null;
146:
147: //**START source table emp
148: //first insert some rows into emp table
149: TableIdentifier emp_src_tid = new TableIdentifier("EMP");
150: ArrayList emp_src_columns = new ArrayList();
151: ArrayList emp_src_values = new ArrayList();
152:
153: //table columns
154: ColumnIdentifier emp_src_id = new ColumnIdentifier("ID");
155: emp_src_id.setTableIdentifier(emp_src_tid);
156: emp_src_columns.add(emp_src_id);
157:
158: ColumnIdentifier emp_src_fname = new ColumnIdentifier("FNAME");
159: emp_src_fname.setTableIdentifier(emp_src_tid);
160: emp_src_columns.add(emp_src_fname);
161:
162: ColumnIdentifier emp_src_lname = new ColumnIdentifier("LNAME");
163: emp_src_lname.setTableIdentifier(emp_src_tid);
164: emp_src_columns.add(emp_src_lname);
165:
166: //first row of table values
167: Literal l1 = new Literal(new Integer(1), new IntegerType());
168: emp_src_values.add(l1);
169:
170: Literal l2 = new Literal("Amy", new CharacterVaryingType(3));
171: emp_src_values.add(l2);
172:
173: Literal l3 = new Literal("Mathews", new CharacterVaryingType(7));
174: emp_src_values.add(l3);
175:
176: //insert one row of value
177: cmd = new InsertCommand(emp_src_tid, emp_src_columns,
178: emp_src_values);
179: cmd.execute(_db);
180:
181: //second row of table values
182: emp_src_values = new ArrayList();
183: Literal l4 = new Literal(new Integer(2), new IntegerType());
184: emp_src_values.add(l4);
185:
186: Literal l5 = new Literal("Linda", new CharacterVaryingType(10));
187: emp_src_values.add(l5);
188:
189: Literal l6 = new Literal("Tracy", new CharacterVaryingType(10));
190: emp_src_values.add(l6);
191:
192: //insert second row of value
193: cmd = new InsertCommand(emp_src_tid, emp_src_columns,
194: emp_src_values);
195: cmd.execute(_db);
196:
197: //3rd row of table values
198: emp_src_values = new ArrayList();
199: Literal l7 = new Literal(new Integer(3), new IntegerType());
200: emp_src_values.add(l7);
201:
202: Literal l8 = new Literal("Linda22",
203: new CharacterVaryingType(10));
204: emp_src_values.add(l8);
205:
206: Literal l9 = new Literal("Tracy22",
207: new CharacterVaryingType(10));
208: emp_src_values.add(l9);
209:
210: //insert second row of value
211: cmd = new InsertCommand(emp_src_tid, emp_src_columns,
212: emp_src_values);
213: cmd.execute(_db);
214:
215: assertEquals("Should have 3 row", 3, _db.getTable("EMP")
216: .getRowCount());
217:
218: //**END source table emp
219:
220: //emp_tgt1 table
221: TableIdentifier emp_tgt1_tid = new TableIdentifier("EMP_TGT1");
222: ArrayList emp_tgt1_tableColumns = new ArrayList();
223:
224: //table columns
225: ColumnIdentifier emp_tgt1_id = new ColumnIdentifier("ID");
226: emp_tgt1_id.setTableIdentifier(emp_tgt1_tid);
227: emp_tgt1_tableColumns.add(emp_tgt1_id);
228:
229: ColumnIdentifier emp_tgt1_name = new ColumnIdentifier("NAME");
230: emp_tgt1_name.setTableIdentifier(emp_tgt1_tid);
231: emp_tgt1_tableColumns.add(emp_tgt1_name);
232:
233: //emp_tgt2 table
234: TableIdentifier emp_tgt2_tid = new TableIdentifier("EMP_TGT2");
235: ArrayList emp_tgt2_tableColumns = new ArrayList();
236:
237: //table columns
238: ColumnIdentifier emp_tgt2_id = new ColumnIdentifier("ID");
239: emp_tgt2_id.setTableIdentifier(emp_tgt2_tid);
240: emp_tgt2_tableColumns.add(emp_tgt2_id);
241:
242: ColumnIdentifier emp_tgt2_name = new ColumnIdentifier("NAME");
243: emp_tgt2_name.setTableIdentifier(emp_tgt2_tid);
244: emp_tgt2_tableColumns.add(emp_tgt2_name);
245:
246: //emp_tgt3 table
247: TableIdentifier emp_tgt3_tid = new TableIdentifier("EMP_TGT3");
248: ArrayList emp_tgt3_tableColumns = new ArrayList();
249:
250: //table columns
251: ColumnIdentifier emp_tgt3_id = new ColumnIdentifier("ID");
252: emp_tgt3_id.setTableIdentifier(emp_tgt3_tid);
253: emp_tgt3_tableColumns.add(emp_tgt3_id);
254:
255: ColumnIdentifier emp_tgt3_name = new ColumnIdentifier("NAME");
256: emp_tgt3_name.setTableIdentifier(emp_tgt3_tid);
257: emp_tgt3_tableColumns.add(emp_tgt3_name);
258:
259: //now invoke multi table insert
260: cmd = new InsertCommand();
261:
262: //first create sub select command which represents what we are trying to insert.
263: AxionQueryContext ctx = new AxionQueryContext();
264:
265: ctx.addFrom(emp_src_tid);
266: ctx.addSelect(emp_src_id);
267: ctx.addSelect(emp_src_fname);
268: ctx.addSelect(emp_src_lname);
269: SubSelectCommand subselect = new SubSelectCommand(ctx);
270: subselect.setAlias("S");
271: cmd.setSubSelect(subselect);
272:
273: ColumnIdentifier emp_id = new ColumnIdentifier("ID");
274: emp_id.setTableIdentifier(new TableIdentifier("S"));
275:
276: ColumnIdentifier emp_fname = new ColumnIdentifier("FNAME");
277: emp_fname.setTableIdentifier(new TableIdentifier("S"));
278:
279: ColumnIdentifier emp_lname = new ColumnIdentifier("LNAME");
280: emp_lname.setTableIdentifier(new TableIdentifier("S"));
281:
282: //create first when clause
283: //condition is emp.id = 1
284: FunctionIdentifier eq1 = new FunctionIdentifier("=");
285: eq1.addArgument(emp_id);
286: eq1.addArgument(new Literal(new Integer(1), new IntegerType()));
287:
288: DMLWhenClause w1 = new DMLWhenClause(eq1);
289:
290: //create second when clause
291: //condition is emp.id = 2
292: FunctionIdentifier eq2 = new FunctionIdentifier("=");
293: eq2.addArgument(emp_id);
294: eq2.addArgument(new Literal(new Integer(2), new IntegerType()));
295:
296: DMLWhenClause w2 = new DMLWhenClause(eq2);
297:
298: //insert values
299: ArrayList emp_tgt1_insertValues = new ArrayList();
300: ArrayList emp_tgt2_insertValues = new ArrayList();
301: ArrayList emp_tgt3_insertValues = new ArrayList();
302:
303: emp_tgt1_insertValues.add(emp_id);
304: //concat first name, last name
305: FunctionIdentifier cf1 = new FunctionIdentifier("||");
306: cf1.addArgument(emp_fname);
307: cf1.addArgument(emp_lname);
308: emp_tgt1_insertValues.add(cf1);
309:
310: emp_tgt2_insertValues.add(emp_id);
311: //concat first name, last name
312: FunctionIdentifier cf2 = new FunctionIdentifier("||");
313: cf2.addArgument(emp_fname);
314: cf2.addArgument(emp_lname);
315: emp_tgt2_insertValues.add(cf2);
316:
317: emp_tgt3_insertValues.add(emp_id);
318: //concat first name, last name
319: FunctionIdentifier cf3 = new FunctionIdentifier("||");
320: cf3.addArgument(emp_fname);
321: cf3.addArgument(emp_lname);
322: emp_tgt3_insertValues.add(cf3);
323:
324: //add multi table info
325: cmd.addInsertIntoClause(w1, emp_tgt1_tid,
326: emp_tgt1_tableColumns, emp_tgt1_insertValues);
327: cmd.addInsertIntoClause(w2, emp_tgt2_tid,
328: emp_tgt2_tableColumns, emp_tgt2_insertValues);
329:
330: assertFalse(cmd.isInsertIntoListEmpty());
331: if (!cmd.isInsertIntoListEmpty()) {
332: cmd.setElseClause(emp_tgt3_tid, emp_tgt3_tableColumns,
333: emp_tgt3_insertValues);
334: }
335:
336: cmd.setMultiTableEvaluationMode(InsertCommand.WHEN_ALL);
337:
338: //execute multitable insert
339: cmd.execute(_db);
340:
341: // emp_tgt1 should have one row
342: assertEquals("Should have 1 row", 1, _db.getTable("EMP_TGT1")
343: .getRowCount());
344:
345: //emp_tgt2 should have one row
346: assertEquals("Should have 1 row", 1, _db.getTable("EMP_TGT2")
347: .getRowCount());
348:
349: //emp_tgt2 should have one row
350: assertEquals("Should have 1 row", 1, _db.getTable("EMP_TGT3")
351: .getRowCount());
352:
353: }
354:
355: public void testMultiTableInsertWhenfirst() throws Exception {
356: InsertCommand cmd = null;
357:
358: //**START source table emp
359: //first insert some rows into emp table
360: TableIdentifier emp_src_tid = new TableIdentifier("EMP");
361: ArrayList emp_src_columns = new ArrayList();
362: ArrayList emp_src_values = new ArrayList();
363:
364: //table columns
365: ColumnIdentifier emp_src_id = new ColumnIdentifier("ID");
366: emp_src_id.setTableIdentifier(emp_src_tid);
367: emp_src_columns.add(emp_src_id);
368:
369: ColumnIdentifier emp_src_fname = new ColumnIdentifier("FNAME");
370: emp_src_fname.setTableIdentifier(emp_src_tid);
371: emp_src_columns.add(emp_src_fname);
372:
373: ColumnIdentifier emp_src_lname = new ColumnIdentifier("LNAME");
374: emp_src_lname.setTableIdentifier(emp_src_tid);
375: emp_src_columns.add(emp_src_lname);
376:
377: //first row of table values
378: Literal l1 = new Literal(new Integer(1), new IntegerType());
379: emp_src_values.add(l1);
380:
381: Literal l2 = new Literal("Amy", new CharacterVaryingType(3));
382: emp_src_values.add(l2);
383:
384: Literal l3 = new Literal("Mathews", new CharacterVaryingType(7));
385: emp_src_values.add(l3);
386:
387: //insert one row of value
388: cmd = new InsertCommand(emp_src_tid, emp_src_columns,
389: emp_src_values);
390: cmd.execute(_db);
391:
392: //second row of table values
393: emp_src_values = new ArrayList();
394: Literal l4 = new Literal(new Integer(2), new IntegerType());
395: emp_src_values.add(l4);
396:
397: Literal l5 = new Literal("Linda", new CharacterVaryingType(5));
398: emp_src_values.add(l5);
399:
400: Literal l6 = new Literal("Tracy", new CharacterVaryingType(5));
401: emp_src_values.add(l6);
402:
403: //insert second row of value
404: cmd = new InsertCommand(emp_src_tid, emp_src_columns,
405: emp_src_values);
406: cmd.execute(_db);
407:
408: //3rd row of table values
409: emp_src_values = new ArrayList();
410: Literal l7 = new Literal(new Integer(3), new IntegerType());
411: emp_src_values.add(l7);
412:
413: Literal l8 = new Literal("Linda22", new CharacterVaryingType(7));
414: emp_src_values.add(l8);
415:
416: Literal l9 = new Literal("Tracy22", new CharacterVaryingType(7));
417: emp_src_values.add(l9);
418:
419: //insert second row of value
420: cmd = new InsertCommand(emp_src_tid, emp_src_columns,
421: emp_src_values);
422: cmd.execute(_db);
423:
424: assertEquals("Should have 3 row", 3, _db.getTable("EMP")
425: .getRowCount());
426:
427: //**END source table emp
428:
429: //emp_tgt1 table
430: TableIdentifier emp_tgt1_tid = new TableIdentifier("EMP_TGT1");
431: ArrayList emp_tgt1_tableColumns = new ArrayList();
432:
433: //table columns
434: ColumnIdentifier emp_tgt1_id = new ColumnIdentifier("ID");
435: emp_tgt1_id.setTableIdentifier(emp_tgt1_tid);
436: emp_tgt1_tableColumns.add(emp_tgt1_id);
437:
438: ColumnIdentifier emp_tgt1_name = new ColumnIdentifier("NAME");
439: emp_tgt1_name.setTableIdentifier(emp_tgt1_tid);
440: emp_tgt1_tableColumns.add(emp_tgt1_name);
441:
442: //emp_tgt2 table
443: TableIdentifier emp_tgt2_tid = new TableIdentifier("EMP_TGT2");
444: ArrayList emp_tgt2_tableColumns = new ArrayList();
445:
446: //table columns
447: ColumnIdentifier emp_tgt2_id = new ColumnIdentifier("ID");
448: emp_tgt2_id.setTableIdentifier(emp_tgt2_tid);
449: emp_tgt2_tableColumns.add(emp_tgt2_id);
450:
451: ColumnIdentifier emp_tgt2_name = new ColumnIdentifier("NAME");
452: emp_tgt2_name.setTableIdentifier(emp_tgt2_tid);
453: emp_tgt2_tableColumns.add(emp_tgt2_name);
454:
455: //emp_tgt3 table
456: TableIdentifier emp_tgt3_tid = new TableIdentifier("EMP_TGT3");
457: ArrayList emp_tgt3_tableColumns = new ArrayList();
458:
459: //table columns
460: ColumnIdentifier emp_tgt3_id = new ColumnIdentifier("ID");
461: emp_tgt3_id.setTableIdentifier(emp_tgt3_tid);
462: emp_tgt3_tableColumns.add(emp_tgt3_id);
463:
464: ColumnIdentifier emp_tgt3_name = new ColumnIdentifier("NAME");
465: emp_tgt3_name.setTableIdentifier(emp_tgt3_tid);
466: emp_tgt3_tableColumns.add(emp_tgt3_name);
467:
468: //now invoke multi table insert
469: cmd = new InsertCommand();
470:
471: //first create sub select command which represents what we are trying to insert.
472: AxionQueryContext ctx = new AxionQueryContext();
473:
474: ctx.addFrom(emp_src_tid);
475: ctx.addSelect(emp_src_id);
476: ctx.addSelect(emp_src_fname);
477: ctx.addSelect(emp_src_lname);
478: SubSelectCommand subselect = new SubSelectCommand(ctx);
479: subselect.setAlias("S");
480: cmd.setSubSelect(subselect);
481:
482: ColumnIdentifier emp_id = new ColumnIdentifier("ID");
483: emp_id.setTableIdentifier(new TableIdentifier("S"));
484:
485: ColumnIdentifier emp_fname = new ColumnIdentifier("FNAME");
486: emp_fname.setTableIdentifier(new TableIdentifier("S"));
487:
488: ColumnIdentifier emp_lname = new ColumnIdentifier("LNAME");
489: emp_lname.setTableIdentifier(new TableIdentifier("S"));
490:
491: //create first when clause
492: //condition is emp.id = 1
493: FunctionIdentifier eq1 = new FunctionIdentifier("=");
494: eq1.addArgument(emp_id);
495: eq1.addArgument(new Literal(new Integer(1), new IntegerType()));
496:
497: DMLWhenClause w1 = new DMLWhenClause(eq1);
498:
499: //create second when clause
500: //condition is emp.id = 2
501: FunctionIdentifier eq2 = new FunctionIdentifier("=");
502: eq2.addArgument(emp_id);
503: eq2.addArgument(new Literal(new Integer(2), new IntegerType()));
504:
505: DMLWhenClause w2 = new DMLWhenClause(eq2);
506:
507: //insert values
508: ArrayList emp_tgt1_insertValues = new ArrayList();
509: ArrayList emp_tgt2_insertValues = new ArrayList();
510: ArrayList emp_tgt3_insertValues = new ArrayList();
511:
512: emp_tgt1_insertValues.add(emp_id);
513: //concat first name, last name
514: FunctionIdentifier cf1 = new FunctionIdentifier("||");
515: cf1.addArgument(emp_fname);
516: cf1.addArgument(emp_lname);
517: emp_tgt1_insertValues.add(cf1);
518:
519: emp_tgt2_insertValues.add(emp_id);
520: //concat first name, last name
521: FunctionIdentifier cf2 = new FunctionIdentifier("||");
522: cf2.addArgument(emp_fname);
523: cf2.addArgument(emp_lname);
524: emp_tgt2_insertValues.add(cf2);
525:
526: emp_tgt3_insertValues.add(emp_id);
527: //concat first name, last name
528: FunctionIdentifier cf3 = new FunctionIdentifier("||");
529: cf3.addArgument(emp_fname);
530: cf3.addArgument(emp_lname);
531: emp_tgt3_insertValues.add(cf3);
532:
533: //add multi table info
534: cmd.addInsertIntoClause(w1, emp_tgt1_tid,
535: emp_tgt1_tableColumns, emp_tgt1_insertValues);
536: cmd.addInsertIntoClause(w2, emp_tgt2_tid,
537: emp_tgt2_tableColumns, emp_tgt2_insertValues);
538:
539: assertFalse(cmd.isInsertIntoListEmpty());
540: if (!cmd.isInsertIntoListEmpty()) {
541: cmd.setElseClause(emp_tgt3_tid, emp_tgt3_tableColumns,
542: emp_tgt3_insertValues);
543: }
544:
545: cmd.setMultiTableEvaluationMode(InsertCommand.WHEN_FIRST);
546:
547: //execute multitable insert
548: cmd.execute(_db);
549:
550: // emp_tgt1 should have one row
551: assertEquals("Should have 1 row", 1, _db.getTable("EMP_TGT1")
552: .getRowCount());
553: }
554:
555: public void testMultiTableInsertBad() throws Exception {
556: // source table
557: TableIdentifier emp_src_tid = new TableIdentifier("EMP");
558:
559: //table columns of src table
560: ColumnIdentifier emp_src_id = new ColumnIdentifier("ID");
561: emp_src_id.setTableIdentifier(emp_src_tid);
562:
563: ColumnIdentifier emp_src_fname = new ColumnIdentifier("FNAME");
564: emp_src_fname.setTableIdentifier(emp_src_tid);
565:
566: ColumnIdentifier emp_src_lname = new ColumnIdentifier("LNAME");
567: emp_src_lname.setTableIdentifier(emp_src_tid);
568:
569: // first create sub select command which represents
570: // what we are trying to insert.
571: AxionQueryContext ctx = new AxionQueryContext();
572:
573: ctx.addFrom(emp_src_tid);
574: ctx.addSelect(emp_src_id);
575: ctx.addSelect(emp_src_fname);
576: ctx.addSelect(emp_src_lname);
577: SubSelectCommand subselect = new SubSelectCommand(ctx);
578: subselect.setAlias("S");
579:
580: //emp_tgt1 table
581: TableIdentifier emp_tgt1_tid = new TableIdentifier("EMP_TGT1");
582: ArrayList emp_tgt1_tableColumns = new ArrayList();
583:
584: //table columns of target table
585: ColumnIdentifier emp_tgt1_id = new ColumnIdentifier("ID");
586: emp_tgt1_id.setTableIdentifier(emp_tgt1_tid);
587: emp_tgt1_tableColumns.add(emp_tgt1_id);
588:
589: ColumnIdentifier emp_tgt1_name = new ColumnIdentifier("NAME");
590: emp_tgt1_name.setTableIdentifier(emp_tgt1_tid);
591: emp_tgt1_tableColumns.add(emp_tgt1_name);
592:
593: //insert values : to raise exception we have only one value here.
594: ArrayList emp_tgt1_insertValues = new ArrayList();
595: ColumnIdentifier emp_id = new ColumnIdentifier("ID");
596: emp_id.setTableIdentifier(new TableIdentifier("S"));
597: emp_tgt1_insertValues.add(emp_id);
598:
599: //create when clause for condition is emp.id = 1
600: FunctionIdentifier eq1 = new FunctionIdentifier("=");
601: eq1.addArgument(emp_id);
602: eq1.addArgument(new Literal(new Integer(1), new IntegerType()));
603: DMLWhenClause w1 = new DMLWhenClause(eq1);
604:
605: // now invoke multi table insert
606: InsertCommand cmd = new InsertCommand();
607: cmd.setSubSelect(subselect);
608: cmd.addInsertIntoClause(w1, emp_tgt1_tid,
609: emp_tgt1_tableColumns, emp_tgt1_insertValues);
610: cmd.setMultiTableEvaluationMode(InsertCommand.WHEN_ALL);
611:
612: //execute multitable insert
613: try {
614: cmd.execute(_db);
615: fail("Expected Exception for less number of values");
616: } catch (Exception e) {
617: // expected
618: }
619: }
620:
621: public void testExecuteQueryIsNotSupported() throws Exception {
622: try {
623: InsertCommand cmd = new InsertCommand();
624: cmd.executeQuery(_db);
625: fail("Expected UnsupportedOperationException");
626: } catch (UnsupportedOperationException e) {
627: // expected
628: }
629: }
630:
631: public void testInsertIntoClauseUnsupported() throws Exception {
632: class InsertIntoClauseImpl extends InsertIntoClause {
633: InsertIntoClauseImpl() {
634: super (null, null, null, null);
635: }
636: }
637: ;
638:
639: InsertIntoClauseImpl cmd = new InsertIntoClauseImpl();
640: assertFalse(cmd.isTargetTablePartOfSubQuery());
641: assertNotNull(cmd.getBindVariableIterator());
642: assertNotNull(cmd.getBindVariableIterator());
643: try {
644: cmd.executeQuery(_db);
645: fail("Expected UnsupportedOperationException");
646: } catch (UnsupportedOperationException e) {
647: // expected
648: }
649:
650: try {
651: cmd.execute(_db);
652: fail("Expected UnsupportedOperationException");
653: } catch (UnsupportedOperationException e) {
654: // expected
655: }
656:
657: try {
658: cmd.executeUpdate(_db);
659: fail("Expected UnsupportedOperationException");
660: } catch (UnsupportedOperationException e) {
661: // expected
662: }
663:
664: }
665: }
|