aboutsummaryrefslogtreecommitdiffstats
path: root/tests/com/iciql/test/RuntimeQueryTest.java
blob: bb43a4f0b3d29e758b789379fb67efcb553d6ef4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright 2011 James Moger.
 *
 * 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
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.iciql.test;

import static org.junit.Assert.assertEquals;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import org.junit.Assume;
import org.junit.Test;

import com.iciql.Db;
import com.iciql.QueryWhere;
import com.iciql.test.models.EnumModels.Tree;
import com.iciql.test.models.Product;
import com.iciql.test.models.StaticQueries;
import com.iciql.util.JdbcUtils;
import com.iciql.util.Utils;

/**
 * Tests the runtime dynamic query function.
 */
public class RuntimeQueryTest {

	@Test
	public void testParameters() {
		Db db = IciqlSuite.openNewDb();
		
		// do not test non-H2 databases because dialects will get in the way
		// e.g. column quoting, etc
		Assume.assumeTrue(IciqlSuite.isH2(db));

		Product p = new Product();
		String q1 = db.from(p).where(p.unitsInStock).isParameter().and(p.productName).likeParameter().orderBy(p.productId).toSQL();
		String q2 = db.from(p).where(p.unitsInStock).lessThan(100).and(p.productName).like("test").or(p.productName).likeParameter().orderBy(p.productId).toSQL();
		
		StaticQueries.StaticModel1 m1 = new StaticQueries.StaticModel1();
		String q3 = db.from(m1).where(m1.myTree).is(Tree.MAPLE).and(m1.myTree).isParameter().toSQL();
		
		StaticQueries.StaticModel2 m2 = new StaticQueries.StaticModel2();
		String q4 = db.from(m2).where(m2.myTree).is(Tree.MAPLE).and(m2.myTree).isParameter().toSQL();

		StaticQueries.StaticModel3 m3 = new StaticQueries.StaticModel3();
		String q5 = db.from(m3).where(m3.myTree).is(Tree.MAPLE).and(m3.myTree).isParameter().toSQL();

		long now = System.currentTimeMillis();
		java.sql.Date aDate = new java.sql.Date(now);
		java.sql.Time aTime = new java.sql.Time(now);
		java.sql.Timestamp aTimestamp = new java.sql.Timestamp(now);
		
		String q6 = db.from(m1).where(m1.myDate).is(aDate).and(m1.myDate).isParameter().toSQL();
		String q7 = db.from(m1).where(m1.myTime).is(aTime).and(m1.myTime).isParameter().toSQL();
		String q8 = db.from(m1).where(m1.myTimestamp).is(aTimestamp).and(m1.myTimestamp).isParameter().toSQL();

		db.close();
		assertEquals("SELECT * FROM Product WHERE unitsInStock = ? AND productName LIKE ?  ORDER BY productId", q1);
		assertEquals("SELECT * FROM Product WHERE unitsInStock < 100 AND productName LIKE 'test' OR productName LIKE ?  ORDER BY productId", q2);
		
		assertEquals("SELECT * FROM StaticQueryTest1 WHERE myTree = 'MAPLE' AND myTree = ?", q3);
		assertEquals("SELECT * FROM StaticQueryTest2 WHERE myTree = 50 AND myTree = ?", q4);
		assertEquals("SELECT * FROM StaticQueryTest3 WHERE myTree = 4 AND myTree = ?", q5);

		java.util.Date refDate = new java.util.Date(now);
		assertEquals("SELECT * FROM StaticQueryTest1 WHERE myDate = '" + new SimpleDateFormat("yyyy-MM-dd").format(refDate) + "' AND myDate = ?", q6);
		assertEquals("SELECT * FROM StaticQueryTest1 WHERE myTime = '" + new SimpleDateFormat("HH:mm:ss").format(refDate) + "' AND myTime = ?", q7);
		assertEquals("SELECT * FROM StaticQueryTest1 WHERE myTimestamp = '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(refDate) + "' AND myTimestamp = ?", q8);
	}
	
	@Test
	public void testRuntimeSelectWildcards() {
		Db db = IciqlSuite.openNewDb();
		
		// do not test non-H2 databases because dialects will get in the way
		// e.g. column quoting, etc
		Assume.assumeTrue(IciqlSuite.isH2(db));
		
		StaticQueries.StaticModel1 m1 = new StaticQueries.StaticModel1();
		StaticQueries.StaticModel2 m2 = new StaticQueries.StaticModel2();
		StaticQueries.StaticModel2 m3 = new StaticQueries.StaticModel2();
		
		int t0 = Utils.AS_COUNTER.get() + 1;
		int t1 = t0 + 1;
		
		QueryWhere<?> where = db.from(m1).innerJoin(m2).on(m1.id).is(m2.id).where(m2.myTree).is(Tree.MAPLE); 
		String q1 = where.toSQL(false);
		String q2 = where.toSQL(true);
		String q3 = where.toSQL(false, m1);
		String q4 = where.toSQL(true, m1);
		String q5 = where.toSQL(false, m2);
		String q6 = where.toSQL(true, m2);
		
		// test unused alias
		String q7 = where.toSQL(true, m3);
		
		db.close();
		
		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);
		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);
		
		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);
		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);
		
		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);
		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);
		
		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);
	}
	
	@Test
	public void testRuntimeQuery() {
		Db db = IciqlSuite.openNewDb();
		db.insertAll(Product.getList());

		Product p = new Product();
		List<Product> products = db.from(p).where("unitsInStock=?", 120).orderBy(p.productId).select();
		assertEquals(1, products.size());

		products = db.from(p).where("unitsInStock=? and productName like ? order by productId", 0, "Chef%")
				.select();
		assertEquals(1, products.size());

		db.close();
	}

	@Test
	public void testExecuteQuery() throws SQLException {
		Db db = IciqlSuite.openNewDb();
		db.insertAll(Product.getList());

		// test plain statement
		List<Product> products = db.executeQuery(Product.class,
				"select * from product where unitsInStock=120");
		assertEquals(1, products.size());
		assertEquals("Condiments", products.get(0).category);

		// test prepared statement
		products = db.executeQuery(Product.class, "select * from product where unitsInStock=?", 120);
		assertEquals(1, products.size());
		assertEquals("Condiments", products.get(0).category);

		db.close();
	}

	@Test
	public void testBuildObjects() throws SQLException {
		Db db = IciqlSuite.openNewDb();
		db.insertAll(Product.getList());

		// test plain statement
		ResultSet rs = db.executeQuery("select * from product where unitsInStock=120");
		List<Product> products = db.buildObjects(Product.class, rs);
		JdbcUtils.closeSilently(rs, true);

		assertEquals(1, products.size());
		assertEquals("Condiments", products.get(0).category);

		// test prepared statement
		rs = db.executeQuery("select * from product where unitsInStock=?", 120);
		products = db.buildObjects(Product.class, rs);
		JdbcUtils.closeSilently(rs, true);

		assertEquals(1, products.size());
		assertEquals("Condiments", products.get(0).category);

		db.close();
	}
}