/*
* Copyright (c) 2009-2014, Architector Inc., Japan
* All rights reserved.
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
package com.iciql.test;
import static org.junit.Assert.assertEquals;
+
import java.util.ArrayList;
+
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+
import com.iciql.Db;
import com.iciql.test.models.Customer;
import com.iciql.test.models.PrimitivesModel;
@SuppressWarnings("serial")
@Test
public void oneOfTest() {
+ String PrimitivesTest = db.getDialect().prepareTableName(null, "PrimitivesTest");
+ String Customer = db.getDialect().prepareTableName(null, "Customer");
+ String myInteger = db.getDialect().prepareColumnName("myInteger");
+ String customerId = db.getDialect().prepareColumnName("customerId");
+
PrimitivesModel p = new PrimitivesModel();
assertEquals(
+ String.format("SELECT * FROM %s WHERE %s IN(0)", PrimitivesTest, myInteger),
db.from(p)
.where(p.myInteger).oneOf(0)
- .toSQL(),
- "SELECT * FROM PrimitivesTest WHERE myInteger IN(0)");
+ .toSQL());
assertEquals(
+ String.format("SELECT * FROM %s WHERE %s IN(0, 1)", PrimitivesTest, myInteger),
db.from(p)
.where(p.myInteger).oneOf(0, 1)
- .toSQL(),
- "SELECT * FROM PrimitivesTest WHERE myInteger IN(0, 1)");
+ .toSQL());
Customer c = new Customer();
assertEquals(
+ String.format("SELECT * FROM %s WHERE %s IN('a')", Customer, customerId),
db.from(c)
.where(c.customerId).oneOf(new ArrayList<String>() {{
this.add("a");
}})
- .toSQL(),
- "SELECT * FROM Customer WHERE customerId IN('a')");
+ .toSQL());
assertEquals(
+ String.format("SELECT * FROM %s WHERE %s IN('a', 'b')", Customer, customerId),
db.from(c)
.where(c.customerId).oneOf(new ArrayList<String>() {{
this.add("a");
this.add("b");
}})
- .toSQL(),
- "SELECT * FROM Customer WHERE customerId IN('a', 'b')");
+ .toSQL());
}
@SuppressWarnings("serial")
@Test
public void noneOfTest() {
+ String PrimitivesTest = db.getDialect().prepareTableName(null, "PrimitivesTest");
+ String Customer = db.getDialect().prepareTableName(null, "Customer");
+ String myInteger = db.getDialect().prepareColumnName("myInteger");
+ String customerId = db.getDialect().prepareColumnName("customerId");
+
PrimitivesModel p = new PrimitivesModel();
assertEquals(
+ String.format("SELECT * FROM %s WHERE %s NOT IN(0)", PrimitivesTest, myInteger),
db.from(p)
.where(p.myInteger).noneOf(0)
- .toSQL(),
- "SELECT * FROM PrimitivesTest WHERE myInteger NOT IN(0)");
+ .toSQL());
assertEquals(
+ String.format("SELECT * FROM %s WHERE %s NOT IN(0, 1)", PrimitivesTest, myInteger),
db.from(p)
.where(p.myInteger).noneOf(0, 1)
- .toSQL(),
- "SELECT * FROM PrimitivesTest WHERE myInteger NOT IN(0, 1)");
+ .toSQL());
Customer c = new Customer();
assertEquals(
+ String.format("SELECT * FROM %s WHERE %s NOT IN('a')", Customer, customerId),
db.from(c)
.where(c.customerId).noneOf(new ArrayList<String>() {{
this.add("a");
}})
- .toSQL(),
- "SELECT * FROM Customer WHERE customerId NOT IN('a')");
+ .toSQL());
assertEquals(
+ String.format("SELECT * FROM %s WHERE %s NOT IN('a', 'b')", Customer, customerId),
db.from(c)
.where(c.customerId).noneOf(new ArrayList<String>() {{
this.add("a");
this.add("b");
}})
- .toSQL(),
- "SELECT * FROM Customer WHERE customerId NOT IN('a', 'b')");
+ .toSQL());
}
}
@Test\r
public void testParameters() {\r
Db db = IciqlSuite.openNewDb();\r
- \r
+\r
// do not test non-H2 databases because dialects will get in the way\r
// e.g. column quoting, etc\r
Assume.assumeTrue(IciqlSuite.isH2(db));\r
Product p = new Product();\r
String q1 = db.from(p).where(p.unitsInStock).isParameter().and(p.productName).likeParameter().orderBy(p.productId).toSQL();\r
String q2 = db.from(p).where(p.unitsInStock).lessThan(100).and(p.productName).like("test").or(p.productName).likeParameter().orderBy(p.productId).toSQL();\r
- \r
+\r
StaticQueries.StaticModel1 m1 = new StaticQueries.StaticModel1();\r
String q3 = db.from(m1).where(m1.myTree).is(Tree.MAPLE).and(m1.myTree).isParameter().toSQL();\r
- \r
+\r
StaticQueries.StaticModel2 m2 = new StaticQueries.StaticModel2();\r
String q4 = db.from(m2).where(m2.myTree).is(Tree.MAPLE).and(m2.myTree).isParameter().toSQL();\r
\r
java.sql.Date aDate = new java.sql.Date(now);\r
java.sql.Time aTime = new java.sql.Time(now);\r
java.sql.Timestamp aTimestamp = new java.sql.Timestamp(now);\r
- \r
+\r
String q6 = db.from(m1).where(m1.myDate).is(aDate).and(m1.myDate).isParameter().toSQL();\r
String q7 = db.from(m1).where(m1.myTime).is(aTime).and(m1.myTime).isParameter().toSQL();\r
String q8 = db.from(m1).where(m1.myTimestamp).is(aTimestamp).and(m1.myTimestamp).isParameter().toSQL();\r
db.close();\r
assertEquals("SELECT * FROM Product WHERE unitsInStock = ? AND productName LIKE ? ORDER BY productId", q1);\r
assertEquals("SELECT * FROM Product WHERE unitsInStock < 100 AND productName LIKE 'test' OR productName LIKE ? ORDER BY productId", q2);\r
- \r
+\r
assertEquals("SELECT * FROM StaticQueryTest1 WHERE myTree = 'MAPLE' AND myTree = ?", q3);\r
assertEquals("SELECT * FROM StaticQueryTest2 WHERE myTree = 50 AND myTree = ?", q4);\r
assertEquals("SELECT * FROM StaticQueryTest3 WHERE myTree = 4 AND myTree = ?", q5);\r
assertEquals("SELECT * FROM StaticQueryTest1 WHERE myTime = '" + new SimpleDateFormat("HH:mm:ss").format(refDate) + "' AND myTime = ?", q7);\r
assertEquals("SELECT * FROM StaticQueryTest1 WHERE myTimestamp = '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(refDate) + "' AND myTimestamp = ?", q8);\r
}\r
- \r
+\r
@Test\r
public void testRuntimeSet() {\r
Db db = IciqlSuite.openNewDb();\r
- \r
+\r
// do not test non-H2 databases because dialects will get in the way\r
// e.g. column quoting, etc\r
Assume.assumeTrue(IciqlSuite.isH2(db));\r
- \r
+\r
StaticQueries.StaticModel1 m = new StaticQueries.StaticModel1();\r
String q = db.from(m).set(m.myTimestamp).toParameter().where(m.id).isParameter().toSQL();\r
db.close();\r
- \r
+\r
assertEquals("UPDATE StaticQueryTest1 SET myTimestamp = ? WHERE id = ?", q);\r
}\r
- \r
+\r
@Test\r
public void testRuntimeSelectWildcards() {\r
Db db = IciqlSuite.openNewDb();\r
- \r
+\r
// do not test non-H2 databases because dialects will get in the way\r
// e.g. column quoting, etc\r
Assume.assumeTrue(IciqlSuite.isH2(db));\r
- \r
+\r
StaticQueries.StaticModel1 m1 = new StaticQueries.StaticModel1();\r
StaticQueries.StaticModel2 m2 = new StaticQueries.StaticModel2();\r
StaticQueries.StaticModel2 m3 = new StaticQueries.StaticModel2();\r
- \r
+\r
int t0 = Utils.AS_COUNTER.get() + 1;\r
int t1 = t0 + 1;\r
- \r
- QueryWhere<?> where = db.from(m1).innerJoin(m2).on(m1.id).is(m2.id).where(m2.myTree).is(Tree.MAPLE); \r
+\r
+ QueryWhere<?> where = db.from(m1).innerJoin(m2).on(m1.id).is(m2.id).where(m2.myTree).is(Tree.MAPLE);\r
String q1 = where.toSQL(false);\r
String q2 = where.toSQL(true);\r
String q3 = where.toSQL(false, m1);\r
String q4 = where.toSQL(true, m1);\r
String q5 = where.toSQL(false, m2);\r
String q6 = where.toSQL(true, m2);\r
- \r
+\r
// test unused alias\r
String q7 = where.toSQL(true, m3);\r
- \r
+\r
db.close();\r
- \r
+\r
assertEquals(MessageFormat.format("SELECT * FROM StaticQueryTest1 AS T{0,number,0} INNER JOIN StaticQueryTest2 AS T{1,number,0} ON T{0,number,0}.id = T{1,number,0}.id WHERE T{1,number,0}.myTree = 50", t0, t1), q1);\r
assertEquals(MessageFormat.format("SELECT DISTINCT * FROM StaticQueryTest1 AS T{0,number,0} INNER JOIN StaticQueryTest2 AS T{1,number,0} ON T{0,number,0}.id = T{1,number,0}.id WHERE T{1,number,0}.myTree = 50", t0, t1), q2);\r
- \r
+\r
assertEquals(MessageFormat.format("SELECT T{0,number,0}.* FROM StaticQueryTest1 AS T{0,number,0} INNER JOIN StaticQueryTest2 AS T{1,number,0} ON T{0,number,0}.id = T{1,number,0}.id WHERE T{1,number,0}.myTree = 50", t0, t1), q3);\r
assertEquals(MessageFormat.format("SELECT DISTINCT T{0,number,0}.* FROM StaticQueryTest1 AS T{0,number,0} INNER JOIN StaticQueryTest2 AS T{1,number,0} ON T{0,number,0}.id = T{1,number,0}.id WHERE T{1,number,0}.myTree = 50", t0, t1), q4);\r
- \r
+\r
assertEquals(MessageFormat.format("SELECT T{1,number,0}.* FROM StaticQueryTest1 AS T{0,number,0} INNER JOIN StaticQueryTest2 AS T{1,number,0} ON T{0,number,0}.id = T{1,number,0}.id WHERE T{1,number,0}.myTree = 50", t0, t1), q5);\r
assertEquals(MessageFormat.format("SELECT DISTINCT T{1,number,0}.* FROM StaticQueryTest1 AS T{0,number,0} INNER JOIN StaticQueryTest2 AS T{1,number,0} ON T{0,number,0}.id = T{1,number,0}.id WHERE T{1,number,0}.myTree = 50", t0, t1), q6);\r
- \r
+\r
assertEquals(MessageFormat.format("SELECT DISTINCT * FROM StaticQueryTest1 AS T{0,number,0} INNER JOIN StaticQueryTest2 AS T{1,number,0} ON T{0,number,0}.id = T{1,number,0}.id WHERE T{1,number,0}.myTree = 50", t0, t1), q7);\r
}\r
- \r
+\r
@Test\r
public void testRuntimeQuery() {\r
Db db = IciqlSuite.openNewDb();\r
db.insertAll(Product.getList());\r
\r
+ String unitsInStock = db.getDialect().prepareColumnName("unitsInStock");\r
+ String productName = db.getDialect().prepareColumnName("productName");\r
+ String productId = db.getDialect().prepareColumnName("productId");\r
+\r
Product p = new Product();\r
- List<Product> products = db.from(p).where("unitsInStock=?", 120).orderBy(p.productId).select();\r
+ List<Product> products = db.from(p).where(unitsInStock + "=?", 120).orderBy(p.productId).select();\r
assertEquals(1, products.size());\r
\r
- products = db.from(p).where("unitsInStock=? and productName like ? order by productId", 0, "Chef%")\r
+ products = db.from(p).where(String.format("%s=? and productName like ? order by productId",\r
+ unitsInStock, productName, productId), 0, "Chef%")\r
.select();\r
assertEquals(1, products.size());\r
\r
Db db = IciqlSuite.openNewDb();\r
db.insertAll(Product.getList());\r
\r
+ String product = db.getDialect().prepareTableName(null, "Product");\r
+ String unitsInStock = db.getDialect().prepareColumnName("unitsInStock");\r
+\r
// test plain statement\r
List<Product> products = db.executeQuery(Product.class,\r
- "select * from product where unitsInStock=120");\r
+ String.format("select * from %s where %s=120",\r
+ product, unitsInStock));\r
assertEquals(1, products.size());\r
assertEquals("Condiments", products.get(0).category);\r
\r
// test prepared statement\r
- products = db.executeQuery(Product.class, "select * from product where unitsInStock=?", 120);\r
+ products = db.executeQuery(Product.class, String.format("select * from %s where %s=?",\r
+ product, unitsInStock), 120);\r
assertEquals(1, products.size());\r
assertEquals("Condiments", products.get(0).category);\r
\r
Db db = IciqlSuite.openNewDb();\r
db.insertAll(Product.getList());\r
\r
+ String product = db.getDialect().prepareTableName(null, "Product");\r
+ String unitsInStock = db.getDialect().prepareColumnName("unitsInStock");\r
+\r
// test plain statement\r
- ResultSet rs = db.executeQuery("select * from product where unitsInStock=120");\r
+ ResultSet rs = db.executeQuery(String.format("select * from %s where %s=120",\r
+ product, unitsInStock));\r
List<Product> products = db.buildObjects(Product.class, rs);\r
JdbcUtils.closeSilently(rs, true);\r
\r
assertEquals("Condiments", products.get(0).category);\r
\r
// test prepared statement\r
- rs = db.executeQuery("select * from product where unitsInStock=?", 120);\r
+ rs = db.executeQuery(String.format("select * from %s where %s=?",\r
+ product, unitsInStock), 120);\r
products = db.buildObjects(Product.class, rs);\r
JdbcUtils.closeSilently(rs, true);\r
\r
@SuppressWarnings("serial")
@Test
public void andOrTest() {
+ String Customer = db.getDialect().prepareTableName(null, "Customer");
+ String customerId = db.getDialect().prepareColumnName("customerId");
+ String region = db.getDialect().prepareColumnName("region");
+
assertEquals(
search(null, (String[]) null),
- "SELECT * FROM Customer WHERE (true)");
+ String.format("SELECT * FROM %s WHERE (true)",
+ Customer));
assertEquals(
search(null, new String[0]),
- "SELECT * FROM Customer WHERE (true) AND ( (false) )");
+ String.format("SELECT * FROM %s WHERE (true) AND ( (false) )",
+ Customer));
assertEquals(
search(null, "0001"),
- "SELECT * FROM Customer WHERE (true) AND ( (false) OR customerId = '0001' )");
+ String.format("SELECT * FROM %s WHERE (true) AND ( (false) OR %s = '0001' )",
+ Customer, customerId));
assertEquals(
search(null, "0001", "0002"),
- "SELECT * FROM Customer WHERE (true) AND ( (false) OR customerId = '0001' OR customerId = '0002' )");
+ String.format("SELECT * FROM %s WHERE (true) AND ( (false) OR %s = '0001' OR %s = '0002' )",
+ Customer, customerId, customerId));
assertEquals(
search(Region.JP, (String[]) null),
- "SELECT * FROM Customer WHERE (true) AND region = 'JP'");
+ String.format("SELECT * FROM %s WHERE (true) AND %s = 'JP'",
+ Customer, region));
assertEquals(
search(Region.JP, new String[0]),
- "SELECT * FROM Customer WHERE (true) AND ( (false) ) AND region = 'JP'");
+ String.format("SELECT * FROM %s WHERE (true) AND ( (false) ) AND %s = 'JP'",
+ Customer, region));
assertEquals(
search(Region.JP, "0001"),
- "SELECT * FROM Customer WHERE (true) AND ( (false) OR customerId = '0001' ) AND region = 'JP'");
+ String.format("SELECT * FROM %s WHERE (true) AND ( (false) OR %s = '0001' ) AND %s = 'JP'",
+ Customer, customerId, region));
assertEquals(
search(Region.JP, "0001", "0002"),
- "SELECT * FROM Customer WHERE (true) AND ( (false) OR customerId = '0001' OR customerId = '0002' ) AND region = 'JP'");
+ String.format("SELECT * FROM %s WHERE (true) AND ( (false) OR %s = '0001' OR %s = '0002' ) AND %s = 'JP'",
+ Customer, customerId, customerId, region));
}
@Test
@Test
public void fluentTest() {
+ String Customer = db.getDialect().prepareTableName(null, "Customer");
+ String customerId = db.getDialect().prepareColumnName("customerId");
+ String region = db.getDialect().prepareColumnName("region");
+
final Customer model = new Customer();
assertEquals(
db.from(model).where(new And<Customer>(db, model) {{
or(model.region).is("LA");
}});
}}).toSQL(),
- "SELECT * FROM Customer WHERE (true) AND customerId = '0001' AND ( (false) OR region = 'CA' OR region = 'LA' )");
+ String.format("SELECT * FROM %s WHERE (true) AND %s = '0001' AND ( (false) OR %s = 'CA' OR %s = 'LA' )",
+ Customer, customerId, region, region));
assertEquals(
db.from(model).where(new Or<Customer>(db, model) {{
or(model.customerId).is("0001");
and(model.region).is("LA");
}});
}}).toSQL(),
- "SELECT * FROM Customer WHERE (false) OR customerId = '0001' OR ( (true) AND customerId = '0002' AND region = 'LA' )");
+ String.format("SELECT * FROM %s WHERE (false) OR %s = '0001' OR ( (true) AND %s = '0002' AND %s = 'LA' )",
+ Customer, customerId, customerId, region));
assertEquals(
db.from(model)
.where(model.customerId).isNotNull()
}})
.and(model.region).isNotNull()
.toSQL(),
- "SELECT * FROM Customer WHERE customerId IS NOT NULL AND ( (false) OR region = 'LA' OR region = 'CA' ) AND region IS NOT NULL");
+ String.format("SELECT * FROM %s WHERE %s IS NOT NULL AND ( (false) OR %s = 'LA' OR %s = 'CA' ) AND %s IS NOT NULL",
+ Customer, customerId, region, region, region));
}
}