0001: /*
0002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
0003: * Distributed under the terms of either:
0004: * - the common development and distribution license (CDDL), v1.0; or
0005: * - the GNU Lesser General Public License, v2.1 or later
0006: * $Id: TestSelectMckoi.java 3695 2007-03-16 09:26:50Z gbevin $
0007: */
0008: package com.uwyn.rife.database.queries;
0009:
0010: import java.math.BigDecimal;
0011: import java.sql.ResultSet;
0012: import java.sql.SQLException;
0013: import java.sql.Time;
0014: import java.sql.Timestamp;
0015: import java.util.Arrays;
0016: import java.util.Calendar;
0017:
0018: import com.uwyn.rife.database.*;
0019: import com.uwyn.rife.database.exceptions.TableNameOrFieldsRequiredException;
0020: import com.uwyn.rife.database.exceptions.UnsupportedSqlFeatureException;
0021:
0022: public class TestSelectMckoi extends TestSelect {
0023: public TestSelectMckoi(String name) {
0024: super (name);
0025: }
0026:
0027: public void testInstantiationMckoi() {
0028: Select query = new Select(mMckoi);
0029: assertNotNull(query);
0030: try {
0031: query.getSql();
0032: fail();
0033: } catch (TableNameOrFieldsRequiredException e) {
0034: assertEquals(e.getQueryName(), "Select");
0035: }
0036: }
0037:
0038: public void testIncompleteQueryMckoi() {
0039: Select query = new Select(mMckoi);
0040: try {
0041: query.getSql();
0042: fail();
0043: } catch (TableNameOrFieldsRequiredException e) {
0044: assertEquals(e.getQueryName(), "Select");
0045: }
0046: query.from("tablename");
0047: assertNotNull(query.getSql());
0048:
0049: query = new Select(mMckoi);
0050: try {
0051: query.getSql();
0052: fail();
0053: } catch (TableNameOrFieldsRequiredException e) {
0054: assertEquals(e.getQueryName(), "Select");
0055: }
0056: query.field("field");
0057: assertNotNull(query.getSql());
0058: }
0059:
0060: public void testClearMckoi() {
0061: Select query = new Select(mMckoi);
0062: query.from("tablename");
0063: assertNotNull(query.getSql());
0064: query.clear();
0065: try {
0066: query.getSql();
0067: fail();
0068: } catch (TableNameOrFieldsRequiredException e) {
0069: assertEquals(e.getQueryName(), "Select");
0070: }
0071: }
0072:
0073: public void testBasicMckoi() {
0074: Select query = new Select(mMckoi);
0075: query.from("tablename");
0076: assertEquals(query.getSql(), "SELECT * FROM tablename");
0077: assertTrue(execute(query));
0078: }
0079:
0080: public void testHintMckoi() {
0081: Select query = new Select(mMckoi);
0082: query.hint("NO_INDEX").from("tablename");
0083: try {
0084: query.getSql();
0085: fail();
0086: } catch (UnsupportedSqlFeatureException e) {
0087: assertTrue(true);
0088: }
0089: }
0090:
0091: public void testOrderByAscendingMckoi() {
0092: Select query = new Select(mMckoi);
0093: query.from("tablename").orderBy("propertyInt", Select.ASC);
0094: assertEquals(query.getSql(),
0095: "SELECT * FROM tablename ORDER BY propertyInt ASC");
0096: assertTrue(execute(query));
0097: }
0098:
0099: public void testOrderByDescendingMckoi() {
0100: Select query = new Select(mMckoi);
0101: query.from("tablename").orderBy("propertyInt", Select.DESC);
0102: assertEquals(query.getSql(),
0103: "SELECT * FROM tablename ORDER BY propertyInt DESC");
0104: assertTrue(execute(query));
0105: }
0106:
0107: public void testBeanMckoi() {
0108: Select query = new Select(mMckoi);
0109: query.from("tablename").fields(BeanImpl.class);
0110: assertEquals(
0111: query.getSql(),
0112: "SELECT 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 FROM tablename");
0113: assertTrue(execute(query));
0114: }
0115:
0116: public void testBeanConstrainedMckoi() {
0117: Select query = new Select(mMckoi, BeanImplConstrained.class);
0118: query.from("tablename");
0119: assertEquals(query.getSql(),
0120: "SELECT * FROM tablename ORDER BY propertyString ASC, propertyInt DESC");
0121: assertTrue(execute(query));
0122:
0123: query = new Select(mMckoi, BeanImplConstrained.class);
0124: query.from("tablename").orderBy("propertyByte");
0125: assertEquals(query.getSql(),
0126: "SELECT * FROM tablename ORDER BY propertyByte ASC");
0127: assertTrue(execute(query));
0128: }
0129:
0130: public void testBeanExcludedMckoi() {
0131: Select query = new Select(mMckoi);
0132: query.from("tablename").fieldsExcluded(
0133: BeanImpl.class,
0134: new String[] { "propertyCalendar", "propertyFloat",
0135: "propertyShort" });
0136: assertEquals(
0137: query.getSql(),
0138: "SELECT propertyBigDecimal, propertyBoolean, propertyBooleanObject, propertyByte, propertyByteObject, propertyChar, propertyCharacterObject, propertyDate, propertyDouble, propertyDoubleObject, propertyEnum, propertyFloatObject, propertyInt, propertyIntegerObject, propertyLong, propertyLongObject, propertyShortObject, propertySqlDate, propertyString, propertyStringbuffer, propertyTime, propertyTimestamp FROM tablename");
0139: assertTrue(execute(query));
0140: }
0141:
0142: public void testWhereTypedMckoi() {
0143: Select query = new Select(mMckoi);
0144: query.from("tablename");
0145:
0146: Calendar cal = Calendar.getInstance();
0147: cal.set(2003, 2, 3, 10, 1, 28);
0148: cal.set(Calendar.MILLISECOND, 154);
0149:
0150: query.where("propertyBigDecimal", ">=",
0151: new BigDecimal("53443433.9784567")).whereAnd(
0152: "propertyBoolean", "=", false).whereOr("propertyByte",
0153: "=", (byte) 54).whereAnd("propertyCalendar", "<=", cal)
0154: .whereOr("propertyChar", "=", 'f').whereAnd(
0155: "propertyDate", "=", cal.getTime()).whereAnd(
0156: "propertyDouble", "!=", 73453.71d).whereOr(
0157: "propertyFloat", ">=", 1987.14f).whereAnd(
0158: "propertyInt", "=", 973).whereAnd(
0159: "propertyLong", "<", 347678L).whereAnd(
0160: "propertyShort", "=", (short) 78).whereOr(
0161: "propertySqlDate", "=",
0162: new java.sql.Date(cal.getTime().getTime()))
0163: .whereAnd("propertyString", "LIKE", "someotherstring%")
0164: .whereAnd("propertyStringbuffer", "=",
0165: new StringBuffer("someotherstringbuff"))
0166: .whereOr("propertyTime", "=",
0167: new Time(cal.getTime().getTime())).whereAnd(
0168: "propertyTimestamp", "<=",
0169: new Timestamp(cal.getTime().getTime()));
0170:
0171: assertEquals(
0172: query.getSql(),
0173: "SELECT * FROM tablename WHERE propertyBigDecimal >= 53443433.9784567 AND propertyBoolean = false OR propertyByte = 54 AND propertyCalendar <= TIMESTAMP '2003-03-03 10:01:28.154' OR propertyChar = 'f' AND propertyDate = TIMESTAMP '2003-03-03 10:01:28.154' AND propertyDouble != 73453.71 OR propertyFloat >= 1987.14 AND propertyInt = 973 AND propertyLong < 347678 AND propertyShort = 78 OR propertySqlDate = DATE '2003-03-03' AND propertyString LIKE 'someotherstring%' AND propertyStringbuffer = 'someotherstringbuff' OR propertyTime = TIME '10:01:28' AND propertyTimestamp <= TIMESTAMP '2003-03-03 10:01:28.154'");
0174: assertFalse(execute(query));
0175: }
0176:
0177: public void testWhereTypedMixedMckoi() {
0178: Select query = new Select(mMckoi);
0179: query.from("tablename");
0180:
0181: final Calendar cal = Calendar.getInstance();
0182: cal.set(2003, 2, 3, 10, 1, 28);
0183: cal.set(Calendar.MILLISECOND, 154);
0184:
0185: query.where("propertyBigDecimal", ">=",
0186: new BigDecimal("53443433.9784567")).whereAnd(
0187: "propertyBoolean", "=", false).whereOr(
0188: "propertyByte = 54").whereAnd("propertyCalendar", "<=",
0189: cal).whereOr("propertyChar", "=", 'f').whereAnd(
0190: "propertyDate", "=", cal.getTime()).whereAnd(
0191: "propertyDouble", "!=", 73453.71d).whereOr(
0192: "propertyFloat >= 1987.14").whereAnd("propertyInt",
0193: "=", 973).whereAnd("propertyLong", "<", 347678L)
0194: .whereAnd("propertyShort", "=", (short) 78)
0195: .whereParameterOr("propertySqlDate", "=").whereAnd(
0196: "propertyString", "LIKE", "someotherstring%")
0197: .whereAnd("propertyStringbuffer", "=",
0198: new StringBuffer("someotherstringbuff"))
0199: .whereOr("propertyTime", "=",
0200: new Time(cal.getTime().getTime())).whereAnd(
0201: "propertyTimestamp", "<=",
0202: new Timestamp(cal.getTime().getTime()));
0203:
0204: assertEquals(
0205: query.getSql(),
0206: "SELECT * FROM tablename WHERE propertyBigDecimal >= 53443433.9784567 AND propertyBoolean = false OR propertyByte = 54 AND propertyCalendar <= TIMESTAMP '2003-03-03 10:01:28.154' OR propertyChar = 'f' AND propertyDate = TIMESTAMP '2003-03-03 10:01:28.154' AND propertyDouble != 73453.71 OR propertyFloat >= 1987.14 AND propertyInt = 973 AND propertyLong < 347678 AND propertyShort = 78 OR propertySqlDate = ? AND propertyString LIKE 'someotherstring%' AND propertyStringbuffer = 'someotherstringbuff' OR propertyTime = TIME '10:01:28' AND propertyTimestamp <= TIMESTAMP '2003-03-03 10:01:28.154'");
0207:
0208: assertFalse(execute(query, new DbPreparedStatementHandler() {
0209: public void setParameters(DbPreparedStatement statement) {
0210: statement.setDate("propertySqlDate", new java.sql.Date(
0211: cal.getTime().getTime()));
0212: }
0213: }));
0214: }
0215:
0216: public void testWhereParametersMckoi() {
0217: Select query = new Select(mMckoi);
0218: query.from("tablename");
0219:
0220: assertNull(query.getParameters());
0221:
0222: query.whereParameter("propertyInt", "=").whereParameterAnd(
0223: "propertyLong", "<").whereParameterOr("propertyChar",
0224: "=");
0225:
0226: assertEquals(query.getParameters().getOrderedNames().size(), 3);
0227: assertEquals(query.getParameters().getOrderedNames().get(0),
0228: "propertyInt");
0229: assertEquals(query.getParameters().getOrderedNames().get(1),
0230: "propertyLong");
0231: assertEquals(query.getParameters().getOrderedNames().get(2),
0232: "propertyChar");
0233: assertTrue(Arrays.equals(query.getParameters()
0234: .getOrderedNamesArray(), new String[] { "propertyInt",
0235: "propertyLong", "propertyChar" }));
0236:
0237: assertEquals(
0238: query.getSql(),
0239: "SELECT * FROM tablename WHERE propertyInt = ? AND propertyLong < ? OR propertyChar = ?");
0240: assertTrue(execute(query, new DbPreparedStatementHandler() {
0241: public void setParameters(DbPreparedStatement statement) {
0242: statement.setInt(1, 545).setLong(2, 50000).setString(3,
0243: "v");
0244: }
0245: }));
0246:
0247: query.where("propertyInt = 545");
0248:
0249: assertNull(query.getParameters());
0250: assertEquals(query.getSql(),
0251: "SELECT * FROM tablename WHERE propertyInt = 545");
0252: }
0253:
0254: public void testWhereParametersMixedMckoi() {
0255: Select query = new Select(mMckoi);
0256: query.from("tablename").where("propertyInt = 545")
0257: .whereParameterAnd("propertyLong", "<")
0258: .whereParameterOr("propertyChar", "=");
0259:
0260: assertEquals(query.getParameters().getOrderedNames().get(0),
0261: "propertyLong");
0262: assertEquals(query.getParameters().getOrderedNames().get(1),
0263: "propertyChar");
0264: assertTrue(Arrays.equals(query.getParameters()
0265: .getOrderedNamesArray(), new String[] { "propertyLong",
0266: "propertyChar" }));
0267:
0268: assertEquals(
0269: query.getSql(),
0270: "SELECT * FROM tablename WHERE propertyInt = 545 AND propertyLong < ? OR propertyChar = ?");
0271: assertTrue(execute(query, new DbPreparedStatementHandler() {
0272: public void setParameters(DbPreparedStatement statement) {
0273: statement.setLong(1, 50000).setString(2, "v");
0274: }
0275: }));
0276: }
0277:
0278: public void testWhereConstructionMckoi() {
0279: Select query = new Select(mMckoi);
0280: query.from("tablename").where("propertyInt = 545").whereAnd(
0281: "propertyLong < 50000").whereOr("propertyChar = 'v'");
0282: assertEquals(
0283: query.getSql(),
0284: "SELECT * FROM tablename WHERE propertyInt = 545 AND propertyLong < 50000 OR propertyChar = 'v'");
0285: assertTrue(execute(query));
0286: }
0287:
0288: public void testWhereConstructionGroupMckoi() {
0289: Select query = new Select(mMckoi);
0290: query.from("tablename").startWhere().where("propertyInt", "=",
0291: 545).whereAnd("propertyByte", "=", 89).end().whereAnd(
0292: "propertyLong < 50000").startWhereOr().whereParameter(
0293: "propertyString", "=").whereAnd("propertyByte", "<=",
0294: (byte) 0).startWhereAnd().where("propertyBoolean",
0295: "!=", true).whereParameterOr("propertyStringbuffer",
0296: "LIKE").end().end().whereOr("propertyChar = 'v'");
0297:
0298: assertEquals(query.getParameters().getOrderedNames().get(0),
0299: "propertyString");
0300: assertEquals(query.getParameters().getOrderedNames().get(1),
0301: "propertyStringbuffer");
0302: assertTrue(Arrays.equals(query.getParameters()
0303: .getOrderedNamesArray(), new String[] {
0304: "propertyString", "propertyStringbuffer" }));
0305:
0306: assertEquals(
0307: query.getSql(),
0308: "SELECT * FROM tablename WHERE (propertyInt = 545 AND propertyByte = 89) AND propertyLong < 50000 OR (propertyString = ? AND propertyByte <= 0 AND (propertyBoolean != true OR propertyStringbuffer LIKE ?)) OR propertyChar = 'v'");
0309:
0310: assertTrue(execute(query, new DbPreparedStatementHandler() {
0311: public void setParameters(DbPreparedStatement statement) {
0312: statement
0313: .setString("propertyString", "someotherstring")
0314: .setString("propertyStringbuffer", "stringbuff");
0315: }
0316: }));
0317: }
0318:
0319: public void testWhereBeanMckoi() {
0320: Select query = new Select(mMckoi);
0321: query.from("tablename").where(BeanImpl.getPopulatedBean());
0322: assertEquals(
0323: query.getSql(),
0324: "SELECT * FROM tablename WHERE propertyBigDecimal = 219038743.392874 AND propertyBoolean = true AND propertyBooleanObject = false AND propertyByte = 89 AND propertyByteObject = 34 AND propertyCalendar = TIMESTAMP '2002-06-18 15:26:14.764' AND propertyChar = 'v' AND propertyCharacterObject = 'r' AND propertyDate = TIMESTAMP '2002-06-18 15:26:14.764' AND propertyDouble = 53348.34 AND propertyDoubleObject = 143298.692 AND propertyEnum = 'VALUE_THREE' AND propertyFloat = 98634.2 AND propertyFloatObject = 8734.7 AND propertyInt = 545 AND propertyIntegerObject = 968 AND propertyLong = 34563 AND propertyLongObject = 66875 AND propertyShort = 43 AND propertyShortObject = 68 AND propertySqlDate = DATE '2002-06-18' AND propertyString = 'someotherstring' AND propertyStringbuffer = 'someotherstringbuff' AND propertyTime = TIME '15:26:14' AND propertyTimestamp = TIMESTAMP '2002-06-18 15:26:14.764'");
0325: assertTrue(execute(query));
0326: }
0327:
0328: public void testWhereBeanConstrainedMckoi() {
0329: Select query = new Select(mMckoi);
0330: query.from("tablename").where(
0331: BeanImplConstrained.getPopulatedBean());
0332: assertEquals(
0333: query.getSql(),
0334: "SELECT * FROM tablename WHERE propertyBigDecimal = 219038743.392874 AND propertyBoolean = true AND propertyBooleanObject = false AND propertyByte = 89 AND propertyByteObject = 34 AND propertyCalendar = TIMESTAMP '2002-06-18 15:26:14.764' AND propertyChar = 'v' AND propertyCharacterObject = 'r' AND propertyDate = TIMESTAMP '2002-06-18 15:26:14.764' AND propertyDouble = 53348.34 AND propertyDoubleObject = 143298.692 AND propertyFloat = 98634.2 AND propertyFloatObject = 8734.7 AND propertyInt = 545 AND propertyIntegerObject = 968 AND propertyLongObject = 66875 AND propertyShort = 43 AND propertySqlDate = DATE '2002-06-18' AND propertyString = 'someotherstring' AND propertyStringbuffer = 'someotherstringbuff' AND propertyTime = TIME '15:26:14' AND propertyTimestamp = TIMESTAMP '2002-06-18 15:26:14.764'");
0335: assertTrue(execute(query));
0336: }
0337:
0338: public void testWhereBeanNullValuesMckoi() {
0339: Select query = new Select(mMckoi);
0340: query.from("tablename").where(BeanImpl.getNullBean());
0341: assertEquals(
0342: query.getSql(),
0343: "SELECT * FROM tablename WHERE propertyBoolean = false AND propertyBooleanObject = false AND propertyByte = 0 AND propertyByteObject = 0 AND propertyDouble = 0.0 AND propertyDoubleObject = 0.0 AND propertyFloat = 0.0 AND propertyFloatObject = 0.0 AND propertyInt = 0 AND propertyIntegerObject = 0 AND propertyLong = 0 AND propertyLongObject = 0 AND propertyShort = 0 AND propertyShortObject = 0");
0344: assertTrue(execute(query));
0345: }
0346:
0347: public void testWhereBeanIncludedMckoi() {
0348: Select query = new Select(mMckoi);
0349: query.from("tablename").whereIncluded(
0350: BeanImpl.getPopulatedBean(),
0351: new String[] { "propertyByte", "propertyDouble",
0352: "propertyShort", "propertyStringbuffer",
0353: "propertyTime" });
0354: assertEquals(
0355: query.getSql(),
0356: "SELECT * FROM tablename WHERE propertyByte = 89 AND propertyDouble = 53348.34 AND propertyShort = 43 AND propertyStringbuffer = 'someotherstringbuff' AND propertyTime = TIME '15:26:14'");
0357: assertTrue(execute(query));
0358: }
0359:
0360: public void testWhereBeanExcludedMckoi() {
0361: Select query = new Select(mMckoi);
0362: query.from("tablename").whereExcluded(
0363: BeanImpl.getPopulatedBean(),
0364: new String[] { "propertyByte", "propertyDouble",
0365: "propertyShort", "propertyStringbuffer",
0366: "propertyTime" });
0367: assertEquals(
0368: query.getSql(),
0369: "SELECT * FROM tablename WHERE propertyBigDecimal = 219038743.392874 AND propertyBoolean = true AND propertyBooleanObject = false AND propertyByteObject = 34 AND propertyCalendar = TIMESTAMP '2002-06-18 15:26:14.764' AND propertyChar = 'v' AND propertyCharacterObject = 'r' AND propertyDate = TIMESTAMP '2002-06-18 15:26:14.764' AND propertyDoubleObject = 143298.692 AND propertyEnum = 'VALUE_THREE' AND propertyFloat = 98634.2 AND propertyFloatObject = 8734.7 AND propertyInt = 545 AND propertyIntegerObject = 968 AND propertyLong = 34563 AND propertyLongObject = 66875 AND propertyShortObject = 68 AND propertySqlDate = DATE '2002-06-18' AND propertyString = 'someotherstring' AND propertyTimestamp = TIMESTAMP '2002-06-18 15:26:14.764'");
0370: assertTrue(execute(query));
0371: }
0372:
0373: public void testWhereBeanFilteredMckoi() {
0374: Select query = new Select(mMckoi);
0375: query.from("tablename").whereFiltered(
0376: BeanImpl.getPopulatedBean(),
0377: new String[] { "propertyByte", "propertyDouble",
0378: "propertyShort", "propertyStringbuffer",
0379: "propertyTime" },
0380: new String[] { "propertyByte", "propertyShort",
0381: "propertyTime" });
0382: assertEquals(
0383: query.getSql(),
0384: "SELECT * FROM tablename WHERE propertyDouble = 53348.34 AND propertyStringbuffer = 'someotherstringbuff'");
0385: assertTrue(execute(query));
0386: }
0387:
0388: public void testWhereParametersBeanMckoi() {
0389: Select query = new Select(mMckoi);
0390: query.from("tablename").whereParameters(BeanImpl.class);
0391: assertEquals(
0392: query.getSql(),
0393: "SELECT * FROM tablename WHERE propertyBigDecimal = ? AND propertyBoolean = ? AND propertyBooleanObject = ? AND propertyByte = ? AND propertyByteObject = ? AND propertyCalendar = ? AND propertyChar = ? AND propertyCharacterObject = ? AND propertyDate = ? AND propertyDouble = ? AND propertyDoubleObject = ? AND propertyEnum = ? AND propertyFloat = ? AND propertyFloatObject = ? AND propertyInt = ? AND propertyIntegerObject = ? AND propertyLong = ? AND propertyLongObject = ? AND propertyShort = ? AND propertyShortObject = ? AND propertySqlDate = ? AND propertyString = ? AND propertyStringbuffer = ? AND propertyTime = ? AND propertyTimestamp = ?");
0394:
0395: assertEquals(query.getParameters().getOrderedNames().size(), 25);
0396: assertEquals(query.getParameters().getOrderedNames().get(0),
0397: "propertyBigDecimal");
0398: assertEquals(query.getParameters().getOrderedNames().get(1),
0399: "propertyBoolean");
0400: assertEquals(query.getParameters().getOrderedNames().get(2),
0401: "propertyBooleanObject");
0402: assertEquals(query.getParameters().getOrderedNames().get(3),
0403: "propertyByte");
0404: assertEquals(query.getParameters().getOrderedNames().get(4),
0405: "propertyByteObject");
0406: assertEquals(query.getParameters().getOrderedNames().get(5),
0407: "propertyCalendar");
0408: assertEquals(query.getParameters().getOrderedNames().get(6),
0409: "propertyChar");
0410: assertEquals(query.getParameters().getOrderedNames().get(7),
0411: "propertyCharacterObject");
0412: assertEquals(query.getParameters().getOrderedNames().get(8),
0413: "propertyDate");
0414: assertEquals(query.getParameters().getOrderedNames().get(9),
0415: "propertyDouble");
0416: assertEquals(query.getParameters().getOrderedNames().get(10),
0417: "propertyDoubleObject");
0418: assertEquals(query.getParameters().getOrderedNames().get(11),
0419: "propertyEnum");
0420: assertEquals(query.getParameters().getOrderedNames().get(12),
0421: "propertyFloat");
0422: assertEquals(query.getParameters().getOrderedNames().get(13),
0423: "propertyFloatObject");
0424: assertEquals(query.getParameters().getOrderedNames().get(14),
0425: "propertyInt");
0426: assertEquals(query.getParameters().getOrderedNames().get(15),
0427: "propertyIntegerObject");
0428: assertEquals(query.getParameters().getOrderedNames().get(16),
0429: "propertyLong");
0430: assertEquals(query.getParameters().getOrderedNames().get(17),
0431: "propertyLongObject");
0432: assertEquals(query.getParameters().getOrderedNames().get(18),
0433: "propertyShort");
0434: assertEquals(query.getParameters().getOrderedNames().get(19),
0435: "propertyShortObject");
0436: assertEquals(query.getParameters().getOrderedNames().get(20),
0437: "propertySqlDate");
0438: assertEquals(query.getParameters().getOrderedNames().get(21),
0439: "propertyString");
0440: assertEquals(query.getParameters().getOrderedNames().get(22),
0441: "propertyStringbuffer");
0442: assertEquals(query.getParameters().getOrderedNames().get(23),
0443: "propertyTime");
0444: assertEquals(query.getParameters().getOrderedNames().get(24),
0445: "propertyTimestamp");
0446: assertTrue(Arrays.equals(query.getParameters()
0447: .getOrderedNamesArray(), new String[] {
0448: "propertyBigDecimal", "propertyBoolean",
0449: "propertyBooleanObject", "propertyByte",
0450: "propertyByteObject", "propertyCalendar",
0451: "propertyChar", "propertyCharacterObject",
0452: "propertyDate", "propertyDouble",
0453: "propertyDoubleObject", "propertyEnum",
0454: "propertyFloat", "propertyFloatObject", "propertyInt",
0455: "propertyIntegerObject", "propertyLong",
0456: "propertyLongObject", "propertyShort",
0457: "propertyShortObject", "propertySqlDate",
0458: "propertyString", "propertyStringbuffer",
0459: "propertyTime", "propertyTimestamp" }));
0460:
0461: assertTrue(execute(query, new DbPreparedStatementHandler() {
0462: public void setParameters(DbPreparedStatement statement) {
0463: Calendar cal = Calendar.getInstance();
0464: cal.set(2002, 5, 18, 15, 26, 14);
0465: cal.set(Calendar.MILLISECOND, 764);
0466: java.sql.Date date = new java.sql.Date(102, 5, 18);
0467: Time time = new Time(15, 26, 14);
0468: statement.setBigDecimal(1,
0469: new BigDecimal("219038743.392874")).setBoolean(
0470: 2, true).setBoolean(3, false).setByte(4,
0471: (byte) 89).setByte(5, (byte) 34)
0472: .setTimestamp(
0473: 6,
0474: new java.sql.Timestamp(cal.getTime()
0475: .getTime())).setString(7, "v")
0476: .setString(8, "r").setTimestamp(
0477: 9,
0478: new java.sql.Timestamp(cal.getTime()
0479: .getTime())).setDouble(10,
0480: 53348.34d).setDouble(11, 143298.692d)
0481: .setString(12, "VALUE_THREE").setFloat(13,
0482: 98634.2f).setFloat(14, 8734.7f).setInt(
0483: 15, 545).setInt(16, 968).setLong(17,
0484: 34563L).setLong(18, 66875L).setShort(
0485: 19, (short) 43)
0486: .setShort(20, (short) 68).setDate(21, date)
0487: .setString(22, "someotherstring").setString(23,
0488: "someotherstringbuff")
0489: .setTime(24, time).setTimestamp(25,
0490: new Timestamp(cal.getTime().getTime()));
0491: }
0492: }));
0493: }
0494:
0495: public void testWhereParametersBeanConstrainedMckoi() {
0496: Select query = new Select(mMckoi);
0497: query.from("tablename").whereParameters(
0498: BeanImplConstrained.class);
0499: assertEquals(
0500: query.getSql(),
0501: "SELECT * FROM tablename WHERE propertyBigDecimal = ? AND propertyBoolean = ? AND propertyBooleanObject = ? AND propertyByte = ? AND propertyByteObject = ? AND propertyCalendar = ? AND propertyChar = ? AND propertyCharacterObject = ? AND propertyDate = ? AND propertyDouble = ? AND propertyDoubleObject = ? AND propertyFloat = ? AND propertyFloatObject = ? AND propertyInt = ? AND propertyIntegerObject = ? AND propertyLongObject = ? AND propertyShort = ? AND propertySqlDate = ? AND propertyString = ? AND propertyStringbuffer = ? AND propertyTime = ? AND propertyTimestamp = ?");
0502:
0503: assertEquals(query.getParameters().getOrderedNames().size(), 22);
0504: assertEquals(query.getParameters().getOrderedNames().get(0),
0505: "propertyBigDecimal");
0506: assertEquals(query.getParameters().getOrderedNames().get(1),
0507: "propertyBoolean");
0508: assertEquals(query.getParameters().getOrderedNames().get(2),
0509: "propertyBooleanObject");
0510: assertEquals(query.getParameters().getOrderedNames().get(3),
0511: "propertyByte");
0512: assertEquals(query.getParameters().getOrderedNames().get(4),
0513: "propertyByteObject");
0514: assertEquals(query.getParameters().getOrderedNames().get(5),
0515: "propertyCalendar");
0516: assertEquals(query.getParameters().getOrderedNames().get(6),
0517: "propertyChar");
0518: assertEquals(query.getParameters().getOrderedNames().get(7),
0519: "propertyCharacterObject");
0520: assertEquals(query.getParameters().getOrderedNames().get(8),
0521: "propertyDate");
0522: assertEquals(query.getParameters().getOrderedNames().get(9),
0523: "propertyDouble");
0524: assertEquals(query.getParameters().getOrderedNames().get(10),
0525: "propertyDoubleObject");
0526: assertEquals(query.getParameters().getOrderedNames().get(11),
0527: "propertyFloat");
0528: assertEquals(query.getParameters().getOrderedNames().get(12),
0529: "propertyFloatObject");
0530: assertEquals(query.getParameters().getOrderedNames().get(13),
0531: "propertyInt");
0532: assertEquals(query.getParameters().getOrderedNames().get(14),
0533: "propertyIntegerObject");
0534: assertEquals(query.getParameters().getOrderedNames().get(15),
0535: "propertyLongObject");
0536: assertEquals(query.getParameters().getOrderedNames().get(16),
0537: "propertyShort");
0538: assertEquals(query.getParameters().getOrderedNames().get(17),
0539: "propertySqlDate");
0540: assertEquals(query.getParameters().getOrderedNames().get(18),
0541: "propertyString");
0542: assertEquals(query.getParameters().getOrderedNames().get(19),
0543: "propertyStringbuffer");
0544: assertEquals(query.getParameters().getOrderedNames().get(20),
0545: "propertyTime");
0546: assertEquals(query.getParameters().getOrderedNames().get(21),
0547: "propertyTimestamp");
0548: assertTrue(Arrays.equals(query.getParameters()
0549: .getOrderedNamesArray(), new String[] {
0550: "propertyBigDecimal", "propertyBoolean",
0551: "propertyBooleanObject", "propertyByte",
0552: "propertyByteObject", "propertyCalendar",
0553: "propertyChar", "propertyCharacterObject",
0554: "propertyDate", "propertyDouble",
0555: "propertyDoubleObject", "propertyFloat",
0556: "propertyFloatObject", "propertyInt",
0557: "propertyIntegerObject", "propertyLongObject",
0558: "propertyShort", "propertySqlDate", "propertyString",
0559: "propertyStringbuffer", "propertyTime",
0560: "propertyTimestamp" }));
0561:
0562: assertTrue(execute(query, new DbPreparedStatementHandler() {
0563: public void setParameters(DbPreparedStatement statement) {
0564: Calendar cal = Calendar.getInstance();
0565: cal.set(2002, 5, 18, 15, 26, 14);
0566: cal.set(Calendar.MILLISECOND, 764);
0567: java.sql.Date date = new java.sql.Date(102, 5, 18);
0568: Time time = new Time(15, 26, 14);
0569: statement.setBigDecimal(1,
0570: new BigDecimal("219038743.392874")).setBoolean(
0571: 2, true).setBoolean(3, false).setByte(4,
0572: (byte) 89).setByte(5, (byte) 34)
0573: .setTimestamp(
0574: 6,
0575: new java.sql.Timestamp(cal.getTime()
0576: .getTime())).setString(7, "v")
0577: .setString(8, "r").setTimestamp(
0578: 9,
0579: new java.sql.Timestamp(cal.getTime()
0580: .getTime())).setDouble(10,
0581: 53348.34d).setDouble(11, 143298.692d)
0582: .setFloat(12, 98634.2f).setFloat(13, 8734.7f)
0583: .setInt(14, 545).setInt(15, 968).setLong(16,
0584: 66875L).setShort(17, (short) 43)
0585: .setDate(18, date).setString(19,
0586: "someotherstring").setString(20,
0587: "someotherstringbuff")
0588: .setTime(21, time).setTimestamp(22,
0589: new Timestamp(cal.getTime().getTime()));
0590: }
0591: }));
0592: }
0593:
0594: public void testWhereParametersBeanExcludedMckoi() {
0595: Select query = new Select(mMckoi);
0596: query.from("tablename").whereParametersExcluded(
0597: BeanImpl.class,
0598: new String[] { "propertyBoolean", "propertyByte",
0599: "propertyChar", "propertyDouble",
0600: "propertyInt", "propertyLong",
0601: "propertySqlDate", "propertyStringbuffer",
0602: "propertyTimestamp" });
0603: assertEquals(
0604: query.getSql(),
0605: "SELECT * FROM tablename WHERE propertyBigDecimal = ? AND propertyBooleanObject = ? AND propertyByteObject = ? AND propertyCalendar = ? AND propertyCharacterObject = ? AND propertyDate = ? AND propertyDoubleObject = ? AND propertyEnum = ? AND propertyFloat = ? AND propertyFloatObject = ? AND propertyIntegerObject = ? AND propertyLongObject = ? AND propertyShort = ? AND propertyShortObject = ? AND propertyString = ? AND propertyTime = ?");
0606:
0607: assertEquals(query.getParameters().getOrderedNames().size(), 16);
0608: assertEquals(query.getParameters().getOrderedNames().get(0),
0609: "propertyBigDecimal");
0610: assertEquals(query.getParameters().getOrderedNames().get(1),
0611: "propertyBooleanObject");
0612: assertEquals(query.getParameters().getOrderedNames().get(2),
0613: "propertyByteObject");
0614: assertEquals(query.getParameters().getOrderedNames().get(3),
0615: "propertyCalendar");
0616: assertEquals(query.getParameters().getOrderedNames().get(4),
0617: "propertyCharacterObject");
0618: assertEquals(query.getParameters().getOrderedNames().get(5),
0619: "propertyDate");
0620: assertEquals(query.getParameters().getOrderedNames().get(6),
0621: "propertyDoubleObject");
0622: assertEquals(query.getParameters().getOrderedNames().get(7),
0623: "propertyEnum");
0624: assertEquals(query.getParameters().getOrderedNames().get(8),
0625: "propertyFloat");
0626: assertEquals(query.getParameters().getOrderedNames().get(9),
0627: "propertyFloatObject");
0628: assertEquals(query.getParameters().getOrderedNames().get(10),
0629: "propertyIntegerObject");
0630: assertEquals(query.getParameters().getOrderedNames().get(11),
0631: "propertyLongObject");
0632: assertEquals(query.getParameters().getOrderedNames().get(12),
0633: "propertyShort");
0634: assertEquals(query.getParameters().getOrderedNames().get(13),
0635: "propertyShortObject");
0636: assertEquals(query.getParameters().getOrderedNames().get(14),
0637: "propertyString");
0638: assertEquals(query.getParameters().getOrderedNames().get(15),
0639: "propertyTime");
0640: assertTrue(Arrays.equals(query.getParameters()
0641: .getOrderedNamesArray(), new String[] {
0642: "propertyBigDecimal", "propertyBooleanObject",
0643: "propertyByteObject", "propertyCalendar",
0644: "propertyCharacterObject", "propertyDate",
0645: "propertyDoubleObject", "propertyEnum",
0646: "propertyFloat", "propertyFloatObject",
0647: "propertyIntegerObject", "propertyLongObject",
0648: "propertyShort", "propertyShortObject",
0649: "propertyString", "propertyTime" }));
0650:
0651: assertTrue(execute(query, new DbPreparedStatementHandler() {
0652: public void setParameters(DbPreparedStatement statement) {
0653: Calendar cal = Calendar.getInstance();
0654: cal.set(2002, 5, 18, 15, 26, 14);
0655: cal.set(Calendar.MILLISECOND, 764);
0656: Time time = new Time(15, 26, 14);
0657: statement.setBigDecimal(1,
0658: new BigDecimal("219038743.392874")).setBoolean(
0659: 2, false).setByte(3, (byte) 34)
0660: .setTimestamp(
0661: 4,
0662: new java.sql.Timestamp(cal.getTime()
0663: .getTime())).setString(5, "r")
0664: .setTimestamp(
0665: 6,
0666: new java.sql.Timestamp(cal.getTime()
0667: .getTime())).setDouble(7,
0668: 143298.692d)
0669: .setString(8, "VALUE_THREE").setFloat(9,
0670: 98634.2f).setFloat(10, 8734.7f).setInt(
0671: 11, 968).setLong(12, 66875L).setShort(
0672: 13, (short) 43)
0673: .setShort(14, (short) 68).setString(15,
0674: "someotherstring").setTime(16, time);
0675: }
0676: }));
0677: }
0678:
0679: public void testDistinctMckoi() {
0680: Select query = new Select(mMckoi);
0681: query.from("tablename").distinct().where("propertyByte = 89")
0682: .orderBy("propertyDouble").orderBy("propertyShort")
0683: .orderBy("propertyTime");
0684: assertEquals(
0685: query.getSql(),
0686: "SELECT DISTINCT * FROM tablename WHERE propertyByte = 89 ORDER BY propertyDouble ASC, propertyShort ASC, propertyTime ASC");
0687: assertTrue(execute(query));
0688: }
0689:
0690: public void testDistinctOnMckoi() {
0691: Select query = new Select(mMckoi);
0692: query.from("tablename").distinctOn("propertyDouble")
0693: .distinctOn("propertyShort").distinctOn("propertyTime")
0694: .where("propertyByte = 89").orderBy("propertyDouble")
0695: .orderBy("propertyShort").orderBy("propertyTime");
0696: try {
0697: query.getSql();
0698: fail();
0699: } catch (UnsupportedSqlFeatureException e) {
0700: assertTrue(true);
0701: }
0702: }
0703:
0704: public void testComplexMckoi() {
0705: Select query = new Select(mMckoi);
0706: query.from("tablename").field("field1").field("field2").field(
0707: "field3").join("table2").joinCross("table3").where(
0708: "this = that").groupBy("gexpr1").groupBy("gexpr2")
0709: .having("hexpr1").having("hexpr2").distinct().unionAll(
0710: "uexpr1").union("uexpr2").limit(3).offset(1);
0711: try {
0712: query.getSql();
0713: fail();
0714: } catch (UnsupportedSqlFeatureException e) {
0715: assertTrue(true);
0716: }
0717: }
0718:
0719: public void testGroupByBeanMckoi() {
0720: Select query = new Select(mMckoi);
0721: query.from("tablename").fields(BeanImpl.class).groupBy(
0722: BeanImpl.class);
0723: assertEquals(
0724: query.getSql(),
0725: "SELECT 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 FROM tablename GROUP BY 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");
0726: assertTrue(execute(query));
0727: }
0728:
0729: public void testGroupByBeanExcludedMckoi() {
0730: Select query = new Select(mMckoi);
0731: query.from("tablename").fieldsExcluded(
0732: BeanImpl.class,
0733: new String[] { "propertyCalendar", "propertyFloat",
0734: "propertyShort" }).groupByExcluded(
0735: BeanImpl.class,
0736: new String[] { "propertyCalendar", "propertyFloat",
0737: "propertyShort" });
0738: assertEquals(
0739: query.getSql(),
0740: "SELECT propertyBigDecimal, propertyBoolean, propertyBooleanObject, propertyByte, propertyByteObject, propertyChar, propertyCharacterObject, propertyDate, propertyDouble, propertyDoubleObject, propertyEnum, propertyFloatObject, propertyInt, propertyIntegerObject, propertyLong, propertyLongObject, propertyShortObject, propertySqlDate, propertyString, propertyStringbuffer, propertyTime, propertyTimestamp FROM tablename GROUP BY propertyBigDecimal, propertyBoolean, propertyBooleanObject, propertyByte, propertyByteObject, propertyChar, propertyCharacterObject, propertyDate, propertyDouble, propertyDoubleObject, propertyEnum, propertyFloatObject, propertyInt, propertyIntegerObject, propertyLong, propertyLongObject, propertyShortObject, propertySqlDate, propertyString, propertyStringbuffer, propertyTime, propertyTimestamp");
0741: assertTrue(execute(query));
0742: }
0743:
0744: public void testJoinMckoi() {
0745: Select query = new Select(mMckoi);
0746: query.from("tablename").join("table2").join("table3");
0747: assertEquals(query.getSql(),
0748: "SELECT * FROM tablename, table2, table3");
0749: assertTrue(execute(query));
0750: }
0751:
0752: public void testJoinCustomMckoi() {
0753: Select query = new Select(mMckoi);
0754: query
0755: .from("tablename")
0756: .joinCustom(
0757: "JOIN table3 ON (tablename.propertyInt = table3.propertyInt)")
0758: .joinCustom(
0759: "JOIN table2 ON (table3.propertyInt = table2.propertyInt)");
0760: assertEquals(
0761: query.getSql(),
0762: "SELECT * FROM tablename JOIN table3 ON (tablename.propertyInt = table3.propertyInt) JOIN table2 ON (table3.propertyInt = table2.propertyInt)");
0763: assertTrue(execute(query));
0764: }
0765:
0766: public void testJoinCrossMckoi() {
0767: Select query = new Select(mMckoi);
0768: query.from("tablename").joinCross("table2").joinCross("table3");
0769: try {
0770: query.getSql();
0771: fail();
0772: } catch (UnsupportedSqlFeatureException e) {
0773: assertTrue(true);
0774: }
0775: }
0776:
0777: public void testJoinInnerMckoi() {
0778: Select query = new Select(mMckoi);
0779: query.from("tablename").joinInner("table2", Select.NATURAL,
0780: null);
0781: try {
0782: query.getSql();
0783: fail();
0784: } catch (UnsupportedSqlFeatureException e) {
0785: assertTrue(true);
0786: }
0787: query.clear();
0788: query.from("tablename").joinInner("table2", Select.ON,
0789: "tablename.propertyInt = table2.propertyInt");
0790: assertEquals(
0791: query.getSql(),
0792: "SELECT * FROM tablename JOIN table2 ON (tablename.propertyInt = table2.propertyInt)");
0793: assertTrue(execute(query));
0794: query.clear();
0795: query.from("tablename").joinInner("table2", Select.USING,
0796: "propertyInt");
0797: try {
0798: query.getSql();
0799: fail();
0800: } catch (UnsupportedSqlFeatureException e) {
0801: assertTrue(true);
0802: }
0803: }
0804:
0805: public void testJoinOuterMckoi() {
0806: Select query = new Select(mMckoi);
0807:
0808: query.from("tablename").joinOuter("table2", Select.FULL,
0809: Select.NATURAL, null);
0810: try {
0811: query.getSql();
0812: fail();
0813: } catch (UnsupportedSqlFeatureException e) {
0814: assertTrue(true);
0815: }
0816: query.clear();
0817: query.from("tablename").joinOuter("table2", Select.LEFT,
0818: Select.NATURAL, null);
0819: try {
0820: query.getSql();
0821: fail();
0822: } catch (UnsupportedSqlFeatureException e) {
0823: assertTrue(true);
0824: }
0825: query.clear();
0826: query.from("tablename").joinOuter("table2", Select.RIGHT,
0827: Select.NATURAL, null);
0828: try {
0829: query.getSql();
0830: fail();
0831: } catch (UnsupportedSqlFeatureException e) {
0832: assertTrue(true);
0833: }
0834: query.clear();
0835:
0836: query.from("tablename")
0837: .joinOuter("table2", Select.FULL, Select.ON,
0838: "tablename.propertyInt = table2.propertyInt");
0839: try {
0840: query.getSql();
0841: fail();
0842: } catch (UnsupportedSqlFeatureException e) {
0843: assertTrue(true);
0844: }
0845: query.clear();
0846: query.from("tablename")
0847: .joinOuter("table2", Select.LEFT, Select.ON,
0848: "tablename.propertyInt = table2.propertyInt");
0849: assertEquals(
0850: query.getSql(),
0851: "SELECT * FROM tablename LEFT OUTER JOIN table2 ON (tablename.propertyInt = table2.propertyInt)");
0852: assertTrue(execute(query));
0853: query.clear();
0854: query.from("tablename")
0855: .joinOuter("table2", Select.RIGHT, Select.ON,
0856: "tablename.propertyInt = table2.propertyInt");
0857: assertEquals(
0858: query.getSql(),
0859: "SELECT * FROM tablename RIGHT OUTER JOIN table2 ON (tablename.propertyInt = table2.propertyInt)");
0860: assertTrue(execute(query));
0861: query.clear();
0862:
0863: query.from("tablename").joinOuter("table2", Select.FULL,
0864: Select.USING, "propertyInt");
0865: try {
0866: query.getSql();
0867: fail();
0868: } catch (UnsupportedSqlFeatureException e) {
0869: assertTrue(true);
0870: }
0871: query.clear();
0872: query.from("tablename").joinOuter("table2", Select.LEFT,
0873: Select.USING, "propertyInt");
0874: try {
0875: query.getSql();
0876: fail();
0877: } catch (UnsupportedSqlFeatureException e) {
0878: assertTrue(true);
0879: }
0880: query.clear();
0881: query.from("tablename").joinOuter("table2", Select.RIGHT,
0882: Select.USING, "propertyInt");
0883: try {
0884: query.getSql();
0885: fail();
0886: } catch (UnsupportedSqlFeatureException e) {
0887: assertTrue(true);
0888: }
0889: query.clear();
0890: }
0891:
0892: public void testLimitMckoi() {
0893: Select query = new Select(mMckoi);
0894: query.from("tablename").limit(3);
0895: try {
0896: query.getSql();
0897: fail();
0898: } catch (UnsupportedSqlFeatureException e) {
0899: assertTrue(true);
0900: }
0901: query.offset(1);
0902: try {
0903: query.getSql();
0904: fail();
0905: } catch (UnsupportedSqlFeatureException e) {
0906: assertTrue(true);
0907: }
0908: query.clear();
0909: query.from("tablename").offset(10);
0910: assertEquals(query.getSql(), "SELECT * FROM tablename");
0911: assertTrue(execute(query));
0912: }
0913:
0914: public void testLimitCapabilitiesMckoi() {
0915: DbQueryManager manager = setupQuery(mMckoi);
0916: try {
0917: Select query = new Select(mMckoi).from("tablename")
0918: .limit(3);
0919:
0920: final int[] count1 = new int[] { 0 };
0921: final int[] integers1 = new int[] { 0, 0, 0 };
0922: manager.executeFetchAll(query, new DbRowProcessor() {
0923: public boolean processRow(ResultSet resultSet)
0924: throws SQLException {
0925: integers1[count1[0]] = resultSet
0926: .getInt("propertyInt");
0927: count1[0]++;
0928:
0929: return true;
0930: }
0931: });
0932: assertEquals(query.getSql(), "SELECT * FROM tablename");
0933: assertEquals(count1[0], 3);
0934: assertEquals(integers1[0], 545);
0935: assertEquals(integers1[1], 0);
0936: assertEquals(integers1[2], 3);
0937:
0938: query.offset(1);
0939:
0940: final int[] count2 = new int[] { 0 };
0941: final int[] integers2 = new int[] { 0, 0, 0 };
0942: manager.executeFetchAll(query, new DbRowProcessor() {
0943: public boolean processRow(ResultSet resultSet)
0944: throws SQLException {
0945: integers2[count2[0]] = resultSet
0946: .getInt("propertyInt");
0947: count2[0]++;
0948:
0949: return true;
0950: }
0951: });
0952: assertEquals(query.getSql(), "SELECT * FROM tablename");
0953: assertEquals(count2[0], 3);
0954: assertEquals(integers2[0], 0);
0955: assertEquals(integers2[1], 3);
0956: assertEquals(integers2[2], 4);
0957:
0958: query.clear();
0959: query.from("tablename").offset(10);
0960: final int[] count3 = new int[] { 0 };
0961: final int[] integers3 = new int[] { 0, 0, 0, 0, 0 };
0962: manager.executeFetchAll(query, new DbRowProcessor() {
0963: public boolean processRow(ResultSet resultSet)
0964: throws SQLException {
0965: integers3[count3[0]] = resultSet
0966: .getInt("propertyInt");
0967: count3[0]++;
0968:
0969: return true;
0970: }
0971: });
0972: assertEquals(query.getSql(), "SELECT * FROM tablename");
0973: assertEquals(count3[0], 5);
0974: assertEquals(integers3[0], 545);
0975: assertEquals(integers3[1], 0);
0976: assertEquals(integers3[2], 3);
0977: assertEquals(integers3[3], 4);
0978: assertEquals(integers3[4], 5);
0979: } finally {
0980: cleanupQuery(manager);
0981: }
0982: }
0983:
0984: public void testLimitParameterMckoi() {
0985: Select query = new Select(mMckoi);
0986: query.from("tablename").limitParameter("limit");
0987: try {
0988: query.getSql();
0989: fail();
0990: } catch (UnsupportedSqlFeatureException e) {
0991: assertTrue(true);
0992: }
0993:
0994: query.offsetParameter("offset");
0995: try {
0996: query.getSql();
0997: fail();
0998: } catch (UnsupportedSqlFeatureException e) {
0999: assertTrue(true);
1000: }
1001:
1002: query.clear();
1003: query.from("tablename").offsetParameter("offset");
1004: assertEquals(query.getSql(), "SELECT * FROM tablename");
1005: assertTrue(execute(query));
1006: }
1007:
1008: public void testSubselectParamsMckoi() {
1009: Select fieldquery = new Select(mMckoi);
1010: fieldquery.from("table2").field("max(propertyLong)")
1011: .whereParameter("propertyInt", ">");
1012: Select tablequery = new Select(mMckoi);
1013: tablequery.from("table2").whereParameter("propertyLong", "<");
1014: Select wherequery = new Select(mMckoi);
1015: wherequery.from("table3").field("max(propertyShort)")
1016: .whereParameter("propertyShort", "!=");
1017: Select unionquery1 = new Select(mMckoi);
1018: unionquery1.from("table2").field("propertyString").field(
1019: "max(propertyByte)")
1020: .whereParameter("propertyByte", "=").groupBy(
1021: "propertyString");
1022: Select unionquery2 = new Select(mMckoi);
1023: unionquery2.from("table2").field("propertyStringbuffer").field(
1024: "min(propertyByte)")
1025: .whereParameter("propertyByte", ">").groupBy(
1026: "propertyStringbuffer");
1027:
1028: // Manual subselect creation
1029: Select query = new Select(mMckoi);
1030: // shuffled the structure around a bit to test the correct order usage
1031: query.unionAll(unionquery1).union(unionquery2).where(
1032: "tablename.propertyShort >= (" + wherequery + ")")
1033: .whereSubselect(wherequery).whereParameterOr(
1034: "tablename.propertyString", "propertyString",
1035: "=").from("tablename").join(
1036: "(" + tablequery + ") AS tablesubselect")
1037: .tableSubselect(tablequery).field(
1038: "tablename.propertyString");
1039: // McKoi doesn't support field subselects
1040: // .field("("+fieldquery+") AS propertyLong")
1041: // .fieldSubselect(fieldquery);
1042: assertEquals(
1043: query.getSql(),
1044: "SELECT tablename.propertyString FROM tablename, (SELECT * FROM table2 WHERE propertyLong < ?) AS tablesubselect WHERE tablename.propertyShort >= (SELECT max(propertyShort) FROM table3 WHERE propertyShort != ?) OR tablename.propertyString = ? UNION ALL SELECT propertyString, max(propertyByte) FROM table2 WHERE propertyByte = ? GROUP BY propertyString UNION ALL SELECT propertyStringbuffer, min(propertyByte) FROM table2 WHERE propertyByte > ? GROUP BY propertyStringbuffer");
1045: String[] parameters = query.getParameters()
1046: .getOrderedNamesArray();
1047: assertEquals(5, parameters.length);
1048: assertEquals(parameters[0], "propertyLong");
1049: assertEquals(parameters[1], "propertyShort");
1050: assertEquals(parameters[2], "propertyString");
1051: assertEquals(parameters[3], "propertyByte");
1052: assertEquals(parameters[4], "propertyByte");
1053:
1054: assertTrue(execute(query, new DbPreparedStatementHandler() {
1055: public void setParameters(DbPreparedStatement statement) {
1056: statement.setLong("propertyLong", 99999999).setShort(
1057: "propertyShort", (short) 5).setString(
1058: "propertyString", "thestring").setByte(
1059: "propertyByte", (byte) 4);
1060: }
1061: }));
1062:
1063: //Automated subselect creation
1064: query = new Select(mMckoi);
1065: query.union(unionquery1).union(unionquery2).where(
1066: "tablename.propertyShort", ">=", wherequery)
1067: .whereParameterOr("tablename.propertyString",
1068: "propertyString", "=").whereOr(
1069: "tablename.propertyFloat",
1070: ">",
1071: new Select(mMckoi).from("table3").field(
1072: "max(propertyLong)").whereParameter(
1073: "propertyLong", "!=")).from(
1074: "tablename",
1075: new Select(mMckoi).from("tablename")).join(
1076: "tablesubselect", tablequery).whereAnd(
1077: "tablename.propertyDouble",
1078: "<=",
1079: new Select(mMckoi).from("table2").field(
1080: "max(propertyFloat)").whereParameter(
1081: "propertyFloat", "!=")).field(
1082: "tablename.propertyString");
1083: // McKoi doesn't support field subselects
1084: // .field("propertyLong", fieldquery);
1085: assertEquals(
1086: query.getSql(),
1087: "SELECT tablename.propertyString FROM (SELECT * FROM tablename) tablename, (SELECT * FROM table2 WHERE propertyLong < ?) tablesubselect WHERE tablename.propertyShort >= (SELECT max(propertyShort) FROM table3 WHERE propertyShort != ?) OR tablename.propertyString = ? OR tablename.propertyFloat > (SELECT max(propertyLong) FROM table3 WHERE propertyLong != ?) AND tablename.propertyDouble <= (SELECT max(propertyFloat) FROM table2 WHERE propertyFloat != ?) UNION ALL SELECT propertyString, max(propertyByte) FROM table2 WHERE propertyByte = ? GROUP BY propertyString UNION ALL SELECT propertyStringbuffer, min(propertyByte) FROM table2 WHERE propertyByte > ? GROUP BY propertyStringbuffer");
1088: parameters = query.getParameters().getOrderedNamesArray();
1089: assertEquals(7, parameters.length);
1090: assertEquals(parameters[0], "propertyLong");
1091: assertEquals(parameters[1], "propertyShort");
1092: assertEquals(parameters[2], "propertyString");
1093: assertEquals(parameters[3], "propertyLong");
1094: assertEquals(parameters[4], "propertyFloat");
1095: assertEquals(parameters[5], "propertyByte");
1096: assertEquals(parameters[6], "propertyByte");
1097:
1098: assertTrue(execute(query, new DbPreparedStatementHandler() {
1099: public void setParameters(DbPreparedStatement statement) {
1100: statement.setLong("propertyLong", 99999999).setShort(
1101: "propertyShort", (short) 5).setString(
1102: "propertyString", "thestring").setFloat(
1103: "propertyFloat", -1f).setByte("propertyByte",
1104: (byte) 4);
1105: }
1106: }));
1107: }
1108:
1109: public void testCloneMckoi() {
1110: Select fieldquery = new Select(mMckoi);
1111: fieldquery.from("table2").field("propertyLong").whereParameter(
1112: "propertyInt", ">").limit(1);
1113: Select tablequery = new Select(mMckoi);
1114: tablequery.from("table2").whereParameter("propertyLong", "<");
1115: Select wherequery = new Select(mMckoi);
1116: wherequery.from("table3").field("max(propertyShort)")
1117: .whereParameter("propertyShort", "!=");
1118: Select unionquery1 = new Select(mMckoi);
1119: unionquery1.from("table2").field("propertyString").field(
1120: "max(propertyByte)")
1121: .whereParameter("propertyByte", "=").groupBy(
1122: "propertyString");
1123: Select unionquery2 = new Select(mMckoi);
1124: unionquery2.from("table2").field("propertyStringbuffer").field(
1125: "min(propertyByte)")
1126: .whereParameter("propertyByte", ">").groupBy(
1127: "propertyStringbuffer");
1128: Select query = new Select(mMckoi);
1129: query
1130: .from("tablename")
1131: .join("(" + tablequery + ") AS tablesubselect")
1132: .tableSubselect(tablequery)
1133: .join("table3")
1134: .joinOuter("table2", Select.RIGHT, Select.ON,
1135: "table3.propertyInt = table2.propertyInt")
1136: .distinct()
1137: // .distinctOn("tablename.propertyShort")
1138: .field("tablename.propertyString")
1139: // .field("("+fieldquery+") AS propertyLong")
1140: // .fieldSubselect(fieldquery)
1141: .where(
1142: "tablename.propertyShort >= (" + wherequery
1143: + ")").whereSubselect(wherequery)
1144: .whereParameterOr("tablename.propertyString",
1145: "propertyString", "=").whereOr(
1146: "tablename.propertyByte", "=", (byte) 54)
1147: .whereAnd("tablename.propertyDouble", "!=", 73453.71d)
1148: .whereParameterOr("tablename.propertyInt",
1149: "propertyInt", "=").whereParameterAnd(
1150: "tablename.propertyLong", "propertyLong", "<")
1151: .whereParameterOr("tablename.propertyChar",
1152: "propertyChar", "=").groupBy(
1153: "tablename.propertyShort").groupBy(
1154: "tablename.propertyLong").groupBy(
1155: "tablename.propertyString").having(
1156: "tablename.propertyLong = 1").unionAll(
1157: unionquery1).union(unionquery2);
1158: // .limit(3)
1159: // .offset(1);
1160: Select query_clone = query.clone();
1161: assertEquals(query.getSql(), query_clone.getSql());
1162: assertTrue(query != query_clone);
1163: execute(query, new DbPreparedStatementHandler() {
1164: public void setParameters(DbPreparedStatement statement) {
1165: statement.setInt("propertyInt", 1).setLong(
1166: "propertyLong", 99999999).setShort(
1167: "propertyShort", (short) 5).setString(
1168: "propertyString", "thestring").setByte(
1169: "propertyByte", (byte) 4).setString(
1170: "propertyChar", "c");
1171: }
1172: });
1173: }
1174: }
|