aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/iciql/Query.java
blob: fb193a063eb11f47afa14e426104911d5739a760 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
/*
 * Copyright 2004-2011 H2 Group.
 * 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;

import java.lang.reflect.Field;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;

import com.iciql.Iciql.EnumType;
import com.iciql.bytecode.ClassReader;
import com.iciql.util.JdbcUtils;
import com.iciql.util.IciqlLogger;
import com.iciql.util.Utils;

/**
 * This class represents a query.
 * 
 * @param <T>
 *            the return type
 */

public class Query<T> {

	private Db db;
	private SelectTable<T> from;
	private ArrayList<Token> conditions = Utils.newArrayList();
	private ArrayList<UpdateColumn> updateColumnDeclarations = Utils.newArrayList();
	private ArrayList<SelectTable<T>> joins = Utils.newArrayList();
	private final IdentityHashMap<Object, SelectColumn<T>> aliasMap = Utils.newIdentityHashMap();
	private ArrayList<OrderExpression<T>> orderByList = Utils.newArrayList();
	private ArrayList<Object> groupByExpressions = Utils.newArrayList();
	private long limit;
	private long offset;

	private Query(Db db) {
		this.db = db;
	}

	/**
	 * from() is a static factory method to build a Query object.
	 * 
	 * @param db
	 * @param alias
	 * @return a query object
	 */
	@SuppressWarnings("unchecked")
	static <T> Query<T> from(Db db, T alias) {
		Query<T> query = new Query<T>(db);
		TableDefinition<T> def = (TableDefinition<T>) db.define(alias.getClass());
		query.from = new SelectTable<T>(db, query, alias, false);
		def.initSelectObject(query.from, alias, query.aliasMap);
		return query;
	}

	public long selectCount() {
		SQLStatement stat = getSelectStatement(false);
		stat.appendSQL("COUNT(*) ");
		appendFromWhere(stat);
		ResultSet rs = stat.executeQuery();
		try {
			rs.next();
			long value = rs.getLong(1);
			return value;
		} catch (SQLException e) {
			throw IciqlException.fromSQL(stat.getSQL(), e);
		} finally {
			JdbcUtils.closeSilently(rs, true);
		}
	}

	public List<T> select() {
		return select(false);
	}

	public T selectFirst() {
		return select(false).get(0);
	}

	public List<T> selectDistinct() {
		return select(true);
	}

	@SuppressWarnings("unchecked")
	public <X, Z> X selectFirst(Z x) {
		List<X> list = (List<X>) select(x);
		return list.isEmpty() ? null : list.get(0);
	}

	public String getSQL() {
		SQLStatement stat = getSelectStatement(false);
		stat.appendSQL("*");
		appendFromWhere(stat);
		return stat.getSQL().trim();
	}

	/**
	 * toSQL returns a static string version of the query with runtime variables
	 * properly encoded. This method is also useful when combined with the where
	 * clause methods like isParameter() or atLeastParameter() which allows
	 * iciql to generate re-usable parameterized string statements.
	 * 
	 * @return the sql query as plain text
	 */
	public String toSQL() {
		return toSQL(false);
	}

	/**
	 * toSQL returns a static string version of the query with runtime variables
	 * properly encoded. This method is also useful when combined with the where
	 * clause methods like isParameter() or atLeastParameter() which allows
	 * iciql to generate re-usable parameterized string statements.
	 * 
	 * @param distinct
	 *            if true SELECT DISTINCT is used for the query
	 * @return the sql query as plain text
	 */
	public String toSQL(boolean distinct) {
		return toSQL(distinct, null);
	}

	/**
	 * toSQL returns a static string version of the query with runtime variables
	 * properly encoded. This method is also useful when combined with the where
	 * clause methods like isParameter() or atLeastParameter() which allows
	 * iciql to generate re-usable parameterized string statements.
	 * 
	 * @param distinct
	 *            if true SELECT DISTINCT is used for the query
	 * @param k
	 *            k is used to select only the columns of the specified alias
	 *            for an inner join statement. An example of a generated
	 *            statement is: SELECT DISTINCT t1.* FROM sometable AS t1 INNER
	 *            JOIN othertable AS t2 ON t1.id = t2.id WHERE t2.flag = true
	 *            without the alias parameter the statement would start with
	 *            SELECT DISTINCT * FROM...
	 * @return the sql query as plain text
	 */
	public <K> String toSQL(boolean distinct, K k) {
		SQLStatement stat = new SQLStatement(getDb());
		stat.appendSQL("SELECT ");
		if (distinct) {
			stat.appendSQL("DISTINCT ");
		}
		if (k != null) {
			SelectTable<?> sel = getSelectTable(k);
			if (sel == null) {
				// unknown alias, use wildcard
				IciqlLogger.warn("Alias {0} is not defined in the statement!", k.getClass());
				stat.appendSQL("*");
			} else if (isJoin()) {
				// join query, use AS alias
				String as = sel.getAs();
				stat.appendSQL(as + ".*");
			} else {
				// schema.table.*
				String schema = sel.getAliasDefinition().schemaName;
				String table = sel.getAliasDefinition().tableName;
				String as = getDb().getDialect().prepareTableName(schema, table);
				stat.appendSQL(as + ".*");
			}
		} else {
			// alias unspecified, use wildcard
			stat.appendSQL("*");
		}
		appendFromWhere(stat);
		return stat.toSQL().trim();
	}

	private List<T> select(boolean distinct) {
		List<T> result = Utils.newArrayList();
		TableDefinition<T> def = from.getAliasDefinition();
		SQLStatement stat = getSelectStatement(distinct);
		def.appendSelectList(stat);
		appendFromWhere(stat);
		ResultSet rs = stat.executeQuery();
		try {
			int[] columns = def.mapColumns(false, rs);
			while (rs.next()) {
				T item = from.newObject();
				def.readRow(item, rs, columns);
				result.add(item);
			}
		} catch (SQLException e) {
			throw IciqlException.fromSQL(stat.getSQL(), e);
		} finally {
			JdbcUtils.closeSilently(rs, true);
		}
		return result;
	}

	public int delete() {
		SQLStatement stat = new SQLStatement(db);
		stat.appendSQL("DELETE FROM ");
		from.appendSQL(stat);
		appendWhere(stat);
		IciqlLogger.delete(stat.getSQL());
		return stat.executeUpdate();
	}

	public <A> UpdateColumnSet<T, A> set(A field) {
		from.getAliasDefinition().checkMultipleEnums(field);
		return new UpdateColumnSet<T, A>(this, field);
	}

	public UpdateColumnSet<T, Boolean> set(boolean field) {
		from.getAliasDefinition().checkMultipleBooleans();
		return setPrimitive(field);
	}

	public UpdateColumnSet<T, Byte> set(byte field) {
		return setPrimitive(field);
	}

	public UpdateColumnSet<T, Short> set(short field) {
		return setPrimitive(field);
	}

	public UpdateColumnSet<T, Integer> set(int field) {
		return setPrimitive(field);
	}

	public UpdateColumnSet<T, Long> set(long field) {
		return setPrimitive(field);
	}

	public UpdateColumnSet<T, Float> set(float field) {
		return setPrimitive(field);
	}

	public UpdateColumnSet<T, Double> set(double field) {
		return setPrimitive(field);
	}

	private <A> UpdateColumnSet<T, A> setPrimitive(A field) {
		A alias = getPrimitiveAliasByValue(field);
		if (alias == null) {
			// this will result in an unmapped field exception
			return set(field);
		}
		return set(alias);
	}

	public <A> UpdateColumnIncrement<T, A> increment(A field) {
		return new UpdateColumnIncrement<T, A>(this, field);
	}

	public UpdateColumnIncrement<T, Byte> increment(byte field) {
		return incrementPrimitive(field);
	}

	public UpdateColumnIncrement<T, Short> increment(short field) {
		return incrementPrimitive(field);
	}

	public UpdateColumnIncrement<T, Integer> increment(int field) {
		return incrementPrimitive(field);
	}

	public UpdateColumnIncrement<T, Long> increment(long field) {
		return incrementPrimitive(field);
	}

	public UpdateColumnIncrement<T, Float> increment(float field) {
		return incrementPrimitive(field);
	}

	public UpdateColumnIncrement<T, Double> increment(double field) {
		return incrementPrimitive(field);
	}

	private <A> UpdateColumnIncrement<T, A> incrementPrimitive(A field) {
		A alias = getPrimitiveAliasByValue(field);
		if (alias == null) {
			// this will result in an unmapped field exception
			return increment(field);
		}
		return increment(alias);
	}

	public int update() {
		if (updateColumnDeclarations.size() == 0) {
			throw new IciqlException("Missing set or increment call.");
		}
		SQLStatement stat = new SQLStatement(db);
		stat.appendSQL("UPDATE ");
		from.appendSQL(stat);
		stat.appendSQL(" SET ");
		int i = 0;
		for (UpdateColumn declaration : updateColumnDeclarations) {
			if (i++ > 0) {
				stat.appendSQL(", ");
			}
			declaration.appendSQL(stat);
		}
		appendWhere(stat);
		IciqlLogger.update(stat.getSQL());
		return stat.executeUpdate();
	}

	public <X, Z> List<X> selectDistinct(Z x) {
		return select(x, true);
	}

	public <X, Z> List<X> select(Z x) {
		return select(x, false);
	}

	@SuppressWarnings("unchecked")
	private <X, Z> List<X> select(Z x, boolean distinct) {
		Class<?> clazz = x.getClass();
		if (Utils.isSimpleType(clazz)) {
			return selectSimple((X) x, distinct);
		}
		Class<?> enclosingClass = clazz.getEnclosingClass();
		if (enclosingClass != null) {
			// anonymous inner class
			clazz = clazz.getSuperclass();
		}
		return select((Class<X>) clazz, (X) x, distinct);
	}

	private <X> List<X> select(Class<X> clazz, X x, boolean distinct) {
		List<X> result = Utils.newArrayList();
		TableDefinition<X> def = db.define(clazz);
		SQLStatement stat = getSelectStatement(distinct);
		def.appendSelectList(stat, this, x);
		appendFromWhere(stat);
		ResultSet rs = stat.executeQuery();
		try {
			int[] columns = def.mapColumns(false, rs);
			while (rs.next()) {
				X row = Utils.newObject(clazz);
				def.readRow(row, rs, columns);
				result.add(row);
			}
		} catch (SQLException e) {
			throw IciqlException.fromSQL(stat.getSQL(), e);
		} finally {
			JdbcUtils.closeSilently(rs, true);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	private <X> List<X> selectSimple(X x, boolean distinct) {
		SQLStatement stat = getSelectStatement(distinct);
		appendSQL(stat, null, x);
		appendFromWhere(stat);
		ResultSet rs = stat.executeQuery();
		List<X> result = Utils.newArrayList();
		try {
			while (rs.next()) {
				X value;
				Object o = rs.getObject(1);
				// Convert CLOB and BLOB now because we close the resultset
				if (Clob.class.isAssignableFrom(o.getClass())) {
					value = (X) Utils.convert(o, String.class);
				} else if (Blob.class.isAssignableFrom(o.getClass())) {
					value = (X) Utils.convert(o, byte[].class);
				} else {
					value = (X) o;
				}
				result.add(value);
			}
		} catch (Exception e) {
			throw IciqlException.fromSQL(stat.getSQL(), e);
		} finally {
			JdbcUtils.closeSilently(rs, true);
		}
		return result;
	}

	private SQLStatement getSelectStatement(boolean distinct) {
		SQLStatement stat = new SQLStatement(db);
		stat.appendSQL("SELECT ");
		if (distinct) {
			stat.appendSQL("DISTINCT ");
		}
		return stat;
	}

	/**
	 * Begin a primitive boolean field condition clause.
	 * 
	 * @param x
	 *            the primitive boolean field to query
	 * @return a query condition to continue building the condition
	 */
	public QueryCondition<T, Boolean> where(boolean x) {
		from.getAliasDefinition().checkMultipleBooleans();
		return wherePrimitive(x);
	}

	/**
	 * Begin a primitive short field condition clause.
	 * 
	 * @param x
	 *            the primitive short field to query
	 * @return a query condition to continue building the condition
	 */
	public QueryCondition<T, Byte> where(byte x) {
		return wherePrimitive(x);
	}

	/**
	 * Begin a primitive short field condition clause.
	 * 
	 * @param x
	 *            the primitive short field to query
	 * @return a query condition to continue building the condition
	 */
	public QueryCondition<T, Short> where(short x) {
		return wherePrimitive(x);
	}

	/**
	 * Begin a primitive int field condition clause.
	 * 
	 * @param x
	 *            the primitive int field to query
	 * @return a query condition to continue building the condition
	 */
	public QueryCondition<T, Integer> where(int x) {
		return wherePrimitive(x);
	}

	/**
	 * Begin a primitive long field condition clause.
	 * 
	 * @param x
	 *            the primitive long field to query
	 * @return a query condition to continue building the condition
	 */
	public QueryCondition<T, Long> where(long x) {
		return wherePrimitive(x);
	}

	/**
	 * Begin a primitive float field condition clause.
	 * 
	 * @param x
	 *            the primitive float field to query
	 * @return a query condition to continue building the condition
	 */
	public QueryCondition<T, Float> where(float x) {
		return wherePrimitive(x);
	}

	/**
	 * Begin a primitive double field condition clause.
	 * 
	 * @param x
	 *            the primitive double field to query
	 * @return a query condition to continue building the condition
	 */
	public QueryCondition<T, Double> where(double x) {
		return wherePrimitive(x);
	}

	/**
	 * Begins a primitive field condition clause.
	 * 
	 * @param value
	 * @return a query condition to continue building the condition
	 */
	private <A> QueryCondition<T, A> wherePrimitive(A value) {
		A alias = getPrimitiveAliasByValue(value);
		if (alias == null) {
			// this will result in an unmapped field exception
			return where(value);
		}
		return where(alias);
	}

	/**
	 * Begin an Object field condition clause.
	 * 
	 * @param x
	 *            the mapped object to query
	 * @return a query condition to continue building the condition
	 */
	public <A> QueryCondition<T, A> where(A x) {
		from.getAliasDefinition().checkMultipleEnums(x);
		return new QueryCondition<T, A>(this, x);
	}

	public <A> QueryWhere<T> where(Filter filter) {
		HashMap<String, Object> fieldMap = Utils.newHashMap();
		for (Field f : filter.getClass().getDeclaredFields()) {
			f.setAccessible(true);
			try {
				Object obj = f.get(filter);
				if (obj == from.getAlias()) {
					List<TableDefinition.FieldDefinition> fields = from.getAliasDefinition().getFields();
					String name = f.getName();
					for (TableDefinition.FieldDefinition field : fields) {
						String n = name + "." + field.field.getName();
						Object o = field.field.get(obj);
						fieldMap.put(n, o);
					}
				}
				fieldMap.put(f.getName(), f.get(filter));
			} catch (Exception e) {
				throw new IciqlException(e);
			}
		}
		Token filterCode = new ClassReader().decompile(filter, fieldMap, "where");
		// String filterQuery = filterCode.toString();
		conditions.add(filterCode);
		return new QueryWhere<T>(this);
	}

	public QueryWhere<T> where(String fragment, List<?> args) {
		return this.where(fragment, args.toArray());
	}

	public QueryWhere<T> where(String fragment, Object... args) {
		conditions.add(new RuntimeToken(fragment, args));
		return new QueryWhere<T>(this);
	}

	public QueryWhere<T> whereTrue(Boolean condition) {
		Token token = new Function("", condition);
		addConditionToken(token);
		return new QueryWhere<T>(this);
	}

	/**
	 * Sets the Limit and Offset of a query.
	 * 
	 * @return the query
	 */

	public Query<T> limit(long limit) {
		this.limit = limit;
		return this;
	}

	public Query<T> offset(long offset) {
		this.offset = offset;
		return this;
	}

	public Query<T> orderBy(boolean field) {
		from.getAliasDefinition().checkMultipleBooleans();
		return orderByPrimitive(field);
	}

	public Query<T> orderBy(byte field) {
		return orderByPrimitive(field);
	}

	public Query<T> orderBy(short field) {
		return orderByPrimitive(field);
	}

	public Query<T> orderBy(int field) {
		return orderByPrimitive(field);
	}

	public Query<T> orderBy(long field) {
		return orderByPrimitive(field);
	}

	public Query<T> orderBy(float field) {
		return orderByPrimitive(field);
	}

	public Query<T> orderBy(double field) {
		return orderByPrimitive(field);
	}

	Query<T> orderByPrimitive(Object field) {
		Object alias = getPrimitiveAliasByValue(field);
		if (alias == null) {
			return orderBy(field);
		}
		return orderBy(alias);
	}

	public Query<T> orderBy(Object expr) {
		from.getAliasDefinition().checkMultipleEnums(expr);
		OrderExpression<T> e = new OrderExpression<T>(this, expr, false, false, false);
		addOrderBy(e);
		return this;
	}

	/**
	 * Order by a number of columns.
	 * 
	 * @param expressions
	 *            the columns
	 * @return the query
	 */

	public Query<T> orderBy(Object... expressions) {
		for (Object expr : expressions) {
			from.getAliasDefinition().checkMultipleEnums(expr);
			OrderExpression<T> e = new OrderExpression<T>(this, expr, false, false, false);
			addOrderBy(e);
		}
		return this;
	}

	public Query<T> orderByDesc(Object expr) {
		OrderExpression<T> e = new OrderExpression<T>(this, expr, true, false, false);
		addOrderBy(e);
		return this;
	}

	public Query<T> groupBy(boolean field) {
		from.getAliasDefinition().checkMultipleBooleans();
		return groupByPrimitive(field);
	}

	public Query<T> groupBy(byte field) {
		return groupByPrimitive(field);
	}

	public Query<T> groupBy(short field) {
		return groupByPrimitive(field);
	}

	public Query<T> groupBy(int field) {
		return groupByPrimitive(field);
	}

	public Query<T> groupBy(long field) {
		return groupByPrimitive(field);
	}

	public Query<T> groupBy(float field) {
		return groupByPrimitive(field);
	}

	public Query<T> groupBy(double field) {
		return groupByPrimitive(field);
	}

	Query<T> groupByPrimitive(Object field) {
		Object alias = getPrimitiveAliasByValue(field);
		if (alias == null) {
			return groupBy(field);
		}
		return groupBy(alias);
	}

	public Query<T> groupBy(Object expr) {
		from.getAliasDefinition().checkMultipleEnums(expr);
		groupByExpressions.add(expr);
		return this;
	}

	public Query<T> groupBy(Object... groupBy) {
		this.groupByExpressions.addAll(Arrays.asList(groupBy));
		return this;
	}

	/**
	 * INTERNAL
	 * 
	 * @param stat
	 *            the statement
	 * @param alias
	 *            the alias object (can be null)
	 * @param value
	 *            the value
	 */
	public void appendSQL(SQLStatement stat, Object alias, Object value) {
		if (Function.count() == value) {
			stat.appendSQL("COUNT(*)");
			return;
		}
		if (RuntimeParameter.PARAMETER == value) {
			stat.appendSQL("?");
			addParameter(stat, alias, value);
			return;
		}
		Token token = Db.getToken(value);
		if (token != null) {
			token.appendSQL(stat, this);
			return;
		}
		if (alias != null && value.getClass().isEnum()) {
			// special case:
			// value is first enum constant which is also the alias object.
			// the first enum constant is used as the alias because we can not
			// instantiate an enum reflectively.
			stat.appendSQL("?");
			addParameter(stat, alias, value);
			return;
		}
		SelectColumn<T> col = getColumnByReference(value);
		if (col != null) {
			col.appendSQL(stat);
			return;
		}
		stat.appendSQL("?");
		addParameter(stat, alias, value);
	}

	/**
	 * INTERNAL
	 * 
	 * @param stat
	 *            the statement
	 * @param alias
	 *            the alias object (can be null)
	 * @param valueLeft
	 *            the value on the left of the compound clause
	 * @param valueRight
	 *            the value on the right of the compound clause
	 * @param compareType
	 *            the current compare type (e.g. BETWEEN)
	 */
	public void appendSQL(SQLStatement stat, Object alias, Object valueLeft, Object valueRight,
			CompareType compareType) {
		stat.appendSQL("?");
		stat.appendSQL(" ");
		switch (compareType) {
		case BETWEEN:
			stat.appendSQL("AND");
			break;
		}
		stat.appendSQL(" ");
		stat.appendSQL("?");
		addParameter(stat, alias, valueLeft);
		addParameter(stat, alias, valueRight);
	}

	private void addParameter(SQLStatement stat, Object alias, Object value) {
		if (alias != null && value.getClass().isEnum()) {
			SelectColumn<T> col = getColumnByReference(alias);
			EnumType type = col.getFieldDefinition().enumType;
			Enum<?> anEnum = (Enum<?>) value;
			Object y = Utils.convertEnum(anEnum, type);
			stat.addParameter(y);
		} else {
			stat.addParameter(value);
		}
	}

	void addConditionToken(Token condition) {
		conditions.add(condition);
	}

	void addUpdateColumnDeclaration(UpdateColumn declaration) {
		updateColumnDeclarations.add(declaration);
	}

	void appendWhere(SQLStatement stat) {
		if (!conditions.isEmpty()) {
			stat.appendSQL(" WHERE ");
			for (Token token : conditions) {
				token.appendSQL(stat, this);
				stat.appendSQL(" ");
			}
		}
	}

	void appendFromWhere(SQLStatement stat) {
		stat.appendSQL(" FROM ");
		from.appendSQL(stat);
		for (SelectTable<T> join : joins) {
			join.appendSQLAsJoin(stat, this);
		}
		appendWhere(stat);
		if (!groupByExpressions.isEmpty()) {
			stat.appendSQL(" GROUP BY ");
			int i = 0;
			for (Object obj : groupByExpressions) {
				if (i++ > 0) {
					stat.appendSQL(", ");
				}
				appendSQL(stat, null, obj);
				stat.appendSQL(" ");
			}
		}
		if (!orderByList.isEmpty()) {
			stat.appendSQL(" ORDER BY ");
			int i = 0;
			for (OrderExpression<T> o : orderByList) {
				if (i++ > 0) {
					stat.appendSQL(", ");
				}
				o.appendSQL(stat);
				stat.appendSQL(" ");
			}
		}
		db.getDialect().appendLimitOffset(stat, limit, offset);
		IciqlLogger.select(stat.getSQL());
	}

	/**
	 * Join another table.
	 * 
	 * @param alias
	 *            an alias for the table to join
	 * @return the joined query
	 */

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public <A> QueryJoin<T> innerJoin(A alias) {
		TableDefinition<T> def = (TableDefinition<T>) db.define(alias.getClass());
		SelectTable<T> join = new SelectTable(db, this, alias, false);
		def.initSelectObject(join, alias, aliasMap);
		joins.add(join);
		return new QueryJoin(this, join);
	}

	Db getDb() {
		return db;
	}

	SelectTable<T> getFrom() {
		return from;
	}

	boolean isJoin() {
		return !joins.isEmpty();
	}

	SelectTable<?> getSelectTable(Object alias) {
		if (from.getAlias() == alias) {
			return from;
		} else {
			for (SelectTable<?> join : joins) {
				if (join.getAlias() == alias) {
					return join;
				}
			}
		}
		return null;
	}

	/**
	 * This method returns a mapped Object field by its reference.
	 * 
	 * @param obj
	 * @return
	 */
	private SelectColumn<T> getColumnByReference(Object obj) {
		SelectColumn<T> col = aliasMap.get(obj);
		return col;
	}

	/**
	 * This method returns the alias of a mapped primitive field by its value.
	 * 
	 * @param obj
	 * @return
	 */
	@SuppressWarnings("unchecked")
	<A> A getPrimitiveAliasByValue(A obj) {
		for (Object alias : aliasMap.keySet()) {
			if (alias.equals(obj)) {
				SelectColumn<T> match = aliasMap.get(alias);
				if (match.getFieldDefinition().isPrimitive) {
					return (A) alias;
				}
			}
		}
		return null;
	}

	void addOrderBy(OrderExpression<T> expr) {
		orderByList.add(expr);
	}

}