Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Criteria.php 1262 2009-10-26 20:54:39Z francois $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information please see
19
 * <http://propel.phpdb.org>.
20
 */
21
 
22
/**
23
 * This is a utility class for holding criteria information for a query.
24
 *
25
 * BasePeer constructs SQL statements based on the values in this class.
26
 *
27
 * @author     Hans Lellelid <hans@xmpl.org> (Propel)
28
 * @author     Kaspars Jaudzems <kaspars.jaudzems@inbox.lv> (Propel)
29
 * @author     Frank Y. Kim <frank.kim@clearink.com> (Torque)
30
 * @author     John D. McNally <jmcnally@collab.net> (Torque)
31
 * @author     Brett McLaughlin <bmclaugh@algx.net> (Torque)
32
 * @author     Eric Dobbs <eric@dobbse.net> (Torque)
33
 * @author     Henning P. Schmiedehausen <hps@intermeta.de> (Torque)
34
 * @author     Sam Joseph <sam@neurogrid.com> (Torque)
35
 * @version    $Revision: 1262 $
36
 * @package    propel.util
37
 */
38
class Criteria implements IteratorAggregate {
39
 
40
	/** Comparison type. */
41
	const EQUAL = "=";
42
 
43
	/** Comparison type. */
44
	const NOT_EQUAL = "<>";
45
 
46
	/** Comparison type. */
47
	const ALT_NOT_EQUAL = "!=";
48
 
49
	/** Comparison type. */
50
	const GREATER_THAN = ">";
51
 
52
	/** Comparison type. */
53
	const LESS_THAN = "<";
54
 
55
	/** Comparison type. */
56
	const GREATER_EQUAL = ">=";
57
 
58
	/** Comparison type. */
59
	const LESS_EQUAL = "<=";
60
 
61
	/** Comparison type. */
62
	const LIKE = " LIKE ";
63
 
64
	/** Comparison type. */
65
	const NOT_LIKE = " NOT LIKE ";
66
 
67
	/** PostgreSQL comparison type */
68
	const ILIKE = " ILIKE ";
69
 
70
	/** PostgreSQL comparison type */
71
	const NOT_ILIKE = " NOT ILIKE ";
72
 
73
	/** Comparison type. */
74
	const CUSTOM = "CUSTOM";
75
 
76
	/** Comparison type for update */
77
	const CUSTOM_EQUAL = "CUSTOM_EQUAL";
78
 
79
	/** Comparison type. */
80
	const DISTINCT = "DISTINCT ";
81
 
82
	/** Comparison type. */
83
	const IN = " IN ";
84
 
85
	/** Comparison type. */
86
	const NOT_IN = " NOT IN ";
87
 
88
	/** Comparison type. */
89
	const ALL = "ALL ";
90
 
91
	/** Comparison type. */
92
	const JOIN = "JOIN";
93
 
94
	/** Binary math operator: AND */
95
	const BINARY_AND = "&";
96
 
97
	/** Binary math operator: OR */
98
	const BINARY_OR = "|";
99
 
100
	/** "Order by" qualifier - ascending */
101
	const ASC = "ASC";
102
 
103
	/** "Order by" qualifier - descending */
104
	const DESC = "DESC";
105
 
106
	/** "IS NULL" null comparison */
107
	const ISNULL = " IS NULL ";
108
 
109
	/** "IS NOT NULL" null comparison */
110
	const ISNOTNULL = " IS NOT NULL ";
111
 
112
	/** "CURRENT_DATE" ANSI SQL function */
113
	const CURRENT_DATE = "CURRENT_DATE";
114
 
115
	/** "CURRENT_TIME" ANSI SQL function */
116
	const CURRENT_TIME = "CURRENT_TIME";
117
 
118
	/** "CURRENT_TIMESTAMP" ANSI SQL function */
119
	const CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";
120
 
121
	/** "LEFT JOIN" SQL statement */
122
	const LEFT_JOIN = "LEFT JOIN";
123
 
124
	/** "RIGHT JOIN" SQL statement */
125
	const RIGHT_JOIN = "RIGHT JOIN";
126
 
127
	/** "INNER JOIN" SQL statement */
128
	const INNER_JOIN = "INNER JOIN";
129
 
130
	private $ignoreCase = false;
131
	private $singleRecord = false;
132
	private $selectModifiers = array();
133
	private $selectColumns = array();
134
	private $orderByColumns = array();
135
	private $groupByColumns = array();
136
	private $having = null;
137
	private $asColumns = array();
138
	private $joins = array();
139
 
140
	/** The name of the database. */
141
	private $dbName;
142
 
143
	/**
144
	 * The primary table for this Criteria.
145
	 * Useful in cases where there are no select or where
146
	 * columns.
147
	 * @var        string
148
	 */
149
	private $primaryTableName;
150
 
151
	/** The name of the database as given in the contructor. */
152
	private $originalDbName;
153
 
154
	/**
155
	 * To limit the number of rows to return.  <code>0</code> means return all
156
	 * rows.
157
	 */
158
	private $limit = 0;
159
 
160
	/** To start the results at a row other than the first one. */
161
	private $offset = 0;
162
 
163
	// flag to note that the criteria involves a blob.
164
	private $blobFlag = null;
165
 
166
	private $aliases = array();
167
 
168
	private $useTransaction = false;
169
 
170
	/**
171
	 * Primary storage of criteria data.
172
	 * @var        array
173
	 */
174
	private $map = array();
175
 
176
	/**
177
	 * Creates a new instance with the default capacity which corresponds to
178
	 * the specified database.
179
	 *
180
	 * @param      dbName The dabase name.
181
	 */
182
	public function __construct($dbName = null)
183
	{
184
		$this->setDbName($dbName);
185
		$this->originalDbName = $dbName;
186
	}
187
 
188
	/**
189
	 * Implementing SPL IteratorAggregate interface.  This allows
190
	 * you to foreach () over a Criteria object.
191
	 */
192
	public function getIterator()
193
	{
194
		return new CriterionIterator($this);
195
	}
196
 
197
	/**
198
	 * Get the criteria map.
199
	 * @return     array
200
	 */
201
	public function getMap()
202
	{
203
		return $this->map;
204
	}
205
 
206
	/**
207
	 * Brings this criteria back to its initial state, so that it
208
	 * can be reused as if it was new. Except if the criteria has grown in
209
	 * capacity, it is left at the current capacity.
210
	 * @return     void
211
	 */
212
	public function clear()
213
	{
214
		$this->map = array();
215
		$this->ignoreCase = false;
216
		$this->singleRecord = false;
217
		$this->selectModifiers = array();
218
		$this->selectColumns = array();
219
		$this->orderByColumns = array();
220
		$this->groupByColumns = array();
221
		$this->having = null;
222
		$this->asColumns = array();
223
		$this->joins = array();
224
		$this->dbName = $this->originalDbName;
225
		$this->offset = 0;
226
		$this->limit = -1;
227
		$this->blobFlag = null;
228
		$this->aliases = array();
229
		$this->useTransaction = false;
230
	}
231
 
232
	/**
233
	 * Add an AS clause to the select columns. Usage:
234
	 *
235
	 * <code>
236
	 * Criteria myCrit = new Criteria();
237
	 * myCrit->addAsColumn("alias", "ALIAS(".MyPeer::ID.")");
238
	 * </code>
239
	 *
240
	 * @param      string $name Wanted Name of the column (alias).
241
	 * @param      string $clause SQL clause to select from the table
242
	 *
243
	 * If the name already exists, it is replaced by the new clause.
244
	 *
245
	 * @return     Criteria A modified Criteria object.
246
	 */
247
	public function addAsColumn($name, $clause)
248
	{
249
		$this->asColumns[$name] = $clause;
250
		return $this;
251
	}
252
 
253
	/**
254
	 * Get the column aliases.
255
	 *
256
	 * @return     array An assoc array which map the column alias names
257
	 * to the alias clauses.
258
	 */
259
	public function getAsColumns()
260
	{
261
		return $this->asColumns;
262
	}
263
 
264
		/**
265
	 * Returns the column name associated with an alias (AS-column).
266
	 *
267
	 * @param      string $alias
268
	 * @return     string $string
269
	 */
270
	public function getColumnForAs($as)
271
	{
272
		if (isset($this->asColumns[$as])) {
273
			return $this->asColumns[$as];
274
		}
275
	}
276
 
277
	/**
278
	 * Allows one to specify an alias for a table that can
279
	 * be used in various parts of the SQL.
280
	 *
281
	 * @param      string $alias
282
	 * @param      string $table
283
	 * @return     void
284
	 */
285
	public function addAlias($alias, $table)
286
	{
287
		$this->aliases[$alias] = $table;
288
	}
289
 
290
	/**
291
	 * Returns the table name associated with an alias.
292
	 *
293
	 * @param      string $alias
294
	 * @return     string $string
295
	 */
296
	public function getTableForAlias($alias)
297
	{
298
		if (isset($this->aliases[$alias])) {
299
			return $this->aliases[$alias];
300
		}
301
	}
302
 
303
	/**
304
	 * Get the keys for the criteria map.
305
	 * @return     array
306
	 */
307
	public function keys()
308
	{
309
		return array_keys($this->map);
310
	}
311
 
312
	/**
313
	 * Does this Criteria object contain the specified key?
314
	 *
315
	 * @param      string $column [table.]column
316
	 * @return     boolean True if this Criteria object contain the specified key.
317
	 */
318
	public function containsKey($column)
319
	{
320
		// must use array_key_exists() because the key could
321
		// exist but have a NULL value (that'd be valid).
322
		return array_key_exists($column, $this->map);
323
	}
324
 
325
	/**
326
	 * Does this Criteria object contain the specified key and does it have a value set for the key
327
	 *
328
	 * @param      string $column [table.]column
329
	 * @return     boolean True if this Criteria object contain the specified key and a value for that key
330
	 */
331
	public function keyContainsValue($column)
332
	{
333
		// must use array_key_exists() because the key could
334
		// exist but have a NULL value (that'd be valid).
335
		return (array_key_exists($column, $this->map) && ($this->map[$column]->getValue() !== null) );
336
	}
337
 
338
	/**
339
	 * Will force the sql represented by this criteria to be executed within
340
	 * a transaction.  This is here primarily to support the oid type in
341
	 * postgresql.  Though it can be used to require any single sql statement
342
	 * to use a transaction.
343
	 * @return     void
344
	 */
345
	public function setUseTransaction($v)
346
	{
347
		$this->useTransaction = (boolean) $v;
348
	}
349
 
350
	/**
351
	 * Whether the sql command specified by this criteria must be wrapped
352
	 * in a transaction.
353
	 *
354
	 * @return     boolean
355
	 */
356
	public function isUseTransaction()
357
	{
358
		return $this->useTransaction;
359
	}
360
 
361
	/**
362
	 * Method to return criteria related to columns in a table.
363
	 *
364
	 * @param      string $column Column name.
365
	 * @return     Criterion A Criterion or null if $column is invalid.
366
	 */
367
	public function getCriterion($column)
368
	{
369
		if ( isset ( $this->map[$column] ) ) {
370
			return $this->map[$column];
371
		}
372
		return null;
373
	}
374
 
375
	/**
376
	 * Method to return criterion that is not added automatically
377
	 * to this Criteria.  This can be used to chain the
378
	 * Criterions to form a more complex where clause.
379
	 *
380
	 * @param      string $column Full name of column (for example TABLE.COLUMN).
381
	 * @param      mixed $value
382
	 * @param      string $comparison
383
	 * @return     Criterion
384
	 */
385
	public function getNewCriterion($column, $value, $comparison = null)
386
	{
387
		return new Criterion($this, $column, $value, $comparison);
388
	}
389
 
390
	/**
391
	 * Method to return a String table name.
392
	 *
393
	 * @param      string $name Name of the key.
394
	 * @return     string The value of the object at key.
395
	 */
396
	public function getColumnName($name)
397
	{
398
		if (isset($this->map[$name])) {
399
			return $this->map[$name]->getColumn();
400
		}
401
		return null;
402
	}
403
 
404
	/**
405
	 * Shortcut method to get an array of columns indexed by table.
406
	 * @return     array array(table => array(table.column1, table.column2))
407
	 */
408
	public function getTablesColumns()
409
	{
410
		$tables = array();
411
		foreach ( array_keys ( $this->map ) as $key) {
412
			$t = substr ( $key, 0, strrpos ( $key, '.' ) );
413
			if ( ! isset ( $tables[$t] ) ) {
414
				$tables[$t] = array( $key );
415
			} else {
416
				$tables[$t][] = $key;
417
			}
418
		}
419
		return $tables;
420
	}
421
 
422
	/**
423
	 * Method to return a comparison String.
424
	 *
425
	 * @param      string $key String name of the key.
426
	 * @return     string A String with the value of the object at key.
427
	 */
428
	public function getComparison($key)
429
	{
430
		if ( isset ( $this->map[$key] ) ) {
431
			return $this->map[$key]->getComparison();
432
		}
433
		return null;
434
	}
435
 
436
	/**
437
	 * Get the Database(Map) name.
438
	 *
439
	 * @return     string A String with the Database(Map) name.
440
	 */
441
	public function getDbName()
442
	{
443
		return $this->dbName;
444
	}
445
 
446
	/**
447
	 * Set the DatabaseMap name.  If <code>null</code> is supplied, uses value
448
	 * provided by <code>Propel::getDefaultDB()</code>.
449
	 *
450
	 * @param      string $dbName The Database (Map) name.
451
	 * @return     void
452
	 */
453
	public function setDbName($dbName = null)
454
	{
455
		$this->dbName = ($dbName === null ? Propel::getDefaultDB() : $dbName);
456
	}
457
 
458
	/**
459
	 * Get the primary table for this Criteria.
460
	 *
461
	 * This is useful for cases where a Criteria may not contain
462
	 * any SELECT columns or WHERE columns.  This must be explicitly
463
	 * set, of course, in order to be useful.
464
	 *
465
	 * @return     string
466
	 */
467
	public function getPrimaryTableName()
468
	{
469
		return $this->primaryTableName;
470
	}
471
 
472
	/**
473
	 * Sets the primary table for this Criteria.
474
	 *
475
	 * This is useful for cases where a Criteria may not contain
476
	 * any SELECT columns or WHERE columns.  This must be explicitly
477
	 * set, of course, in order to be useful.
478
	 *
479
	 * @param      string $v
480
	 */
481
	public function setPrimaryTableName($tableName)
482
	{
483
		$this->primaryTableName = $tableName;
484
	}
485
 
486
	/**
487
	 * Method to return a String table name.
488
	 *
489
	 * @param      string $name The name of the key.
490
	 * @return     string The value of table for criterion at key.
491
	 */
492
	public function getTableName($name)
493
	{
494
		if (isset($this->map[$name])) {
495
			return $this->map[$name]->getTable();
496
		}
497
		return null;
498
	}
499
 
500
	/**
501
	 * Method to return the value that was added to Criteria.
502
	 *
503
	 * @param      string $name A String with the name of the key.
504
	 * @return     mixed The value of object at key.
505
	 */
506
	public function getValue($name)
507
	{
508
		if (isset($this->map[$name])) {
509
			return $this->map[$name]->getValue();
510
		}
511
		return null;
512
	}
513
 
514
	/**
515
	 * An alias to getValue() -- exposing a Hashtable-like interface.
516
	 *
517
	 * @param      string $key An Object.
518
	 * @return     mixed The value within the Criterion (not the Criterion object).
519
	 */
520
	public function get($key)
521
	{
522
		return $this->getValue($key);
523
	}
524
 
525
	/**
526
	 * Overrides Hashtable put, so that this object is returned
527
	 * instead of the value previously in the Criteria object.
528
	 * The reason is so that it more closely matches the behavior
529
	 * of the add() methods. If you want to get the previous value
530
	 * then you should first Criteria.get() it yourself. Note, if
531
	 * you attempt to pass in an Object that is not a String, it will
532
	 * throw a NPE. The reason for this is that none of the add()
533
	 * methods support adding anything other than a String as a key.
534
	 *
535
	 * @param      string $key
536
	 * @param      mixed $value
537
	 * @return     Instance of self.
538
	 */
539
	public function put($key, $value)
540
	{
541
		return $this->add($key, $value);
542
	}
543
 
544
	/**
545
	 * Copies all of the mappings from the specified Map to this Criteria
546
	 * These mappings will replace any mappings that this Criteria had for any
547
	 * of the keys currently in the specified Map.
548
	 *
549
	 * if the map was another Criteria, its attributes are copied to this
550
	 * Criteria, overwriting previous settings.
551
	 *
552
	 * @param      mixed $t Mappings to be stored in this map.
553
	 */
554
	public function putAll($t)
555
	{
556
 
557
		if (is_array($t)) {
558
 
559
			foreach ($t as $key=>$value) {
560
 
561
				if ($value instanceof Criterion) {
562
 
563
					$this->map[$key] = $value;
564
 
565
				} else {
566
 
567
					$this->put($key, $value);
568
 
569
				}
570
 
571
			}
572
 
573
		} elseif ($t instanceof Criteria) {
574
 
575
			$this->joins = $t->joins;
576
 
577
		}
578
 
579
	}
580
 
581
 
582
	/**
583
	 * This method adds a new criterion to the list of criterias.
584
	 * If a criterion for the requested column already exists, it is
585
	 * replaced. If is used as follow:
586
	 *
587
	 * <p>
588
	 * <code>
589
	 * $crit = new Criteria();
590
	 * $crit->add(&quot;column&quot;,
591
	 *                                      &quot;value&quot;
592
	 *                                      &quot;Criteria::GREATER_THAN&quot;);
593
	 * </code>
594
	 *
595
	 * Any comparison can be used.
596
	 *
597
	 * The name of the table must be used implicitly in the column name,
598
	 * so the Column name must be something like 'TABLE.id'. If you
599
	 * don't like this, you can use the add(table, column, value) method.
600
	 *
601
	 * @param      string $critOrColumn The column to run the comparison on, or Criterion object.
602
	 * @param      mixed $value
603
	 * @param      string $comparison A String.
604
	 *
605
	 * @return     A modified Criteria object.
606
	 */
607
	public function add($p1, $value = null, $comparison = null)
608
	{
609
		if ($p1 instanceof Criterion) {
610
			$this->map[$p1->getTable() . '.' . $p1->getColumn()] = $p1;
611
		} else {
612
			$this->map[$p1] = new Criterion($this, $p1, $value, $comparison);
613
		}
614
		return $this;
615
	}
616
 
617
	/**
618
	 * This is the way that you should add a straight (inner) join of two tables.  For
619
	 * example:
620
	 *
621
	 * <p>
622
	 * AND PROJECT.PROJECT_ID=FOO.PROJECT_ID
623
	 * <p>
624
	 *
625
	 * left = PROJECT.PROJECT_ID
626
	 * right = FOO.PROJECT_ID
627
	 *
628
	 * @param      mixed $left A String with the left side of the join.
629
	 * @param      mixed $right A String with the right side of the join.
630
	 * @param      mixed $operator A String with the join operator e.g. LEFT JOIN, ...
631
   *
632
	 * @return     Criteria A modified Criteria object.
633
	 */
634
	public function addJoin($left, $right, $operator = null)
635
	{
636
		$join = new Join();
637
    if (!is_array($left)) {
638
      // simple join
639
      $join->addCondition($left, $right);
640
    } else {
641
      // join with multiple conditions
642
      // deprecated: use addMultipleJoin() instead
643
      foreach ($left as $key => $value)
644
      {
645
        $join->addCondition($value, $right[$key]);
646
      }
647
    }
648
		$join->setJoinType($operator);
649
 
650
		return $this->addJoinObject($join);
651
	}
652
 
653
	/**
654
	 * Add a join with multiple conditions
655
	 * see http://propel.phpdb.org/trac/ticket/167, http://propel.phpdb.org/trac/ticket/606
656
	 *
657
	 * Example usage:
658
	 * $c->addMultipleJoin(array(
659
	 *     array(LeftPeer::LEFT_COLUMN, RightPeer::RIGHT_COLUMN),  // if no third argument, defaults to Criteria::EQUAL
660
	 *     array(FoldersPeer::alias( 'fo', FoldersPeer::LFT ), FoldersPeer::alias( 'parent', FoldersPeer::RGT ), Criteria::LESS_EQUAL )
661
	 *   ),
662
	 *   Criteria::LEFT_JOIN
663
 	 * );
664
	 *
665
	 * @see        addJoin()
666
	 * @param      array $conditions An array of conditions, each condition being an array (left, right, operator)
667
	 * @param      string $joinType  A String with the join operator. Defaults to an implicit join.
668
	 *
669
	 * @return     Criteria A modified Criteria object.
670
	 */
671
	public function addMultipleJoin($conditions, $joinType = null)
672
  {
673
		$join = new Join();
674
		foreach ($conditions as $condition) {
675
		  $join->addCondition($condition[0], $condition[1], isset($condition[2]) ? $condition[2] : Criteria::EQUAL);
676
		}
677
		$join->setJoinType($joinType);
678
 
679
		return $this->addJoinObject($join);
680
	}
681
 
682
	/**
683
	 * Add a join object to the Criteria
684
	 *
685
	 * @param Join $join A join object
686
	 *
687
	 * @return Criteria A modified Criteria object
688
	 */
689
	public function addJoinObject(Join $join)
690
	{
691
	  if (!in_array($join, $this->joins)) { // compare equality, NOT identity
692
			$this->joins[] = $join;
693
		}
694
		return $this;
695
	}
696
 
697
 
698
	/**
699
	 * Get the array of Joins.
700
	 * @return     array Join[]
701
	 */
702
	public function getJoins()
703
	{
704
		return $this->joins;
705
	}
706
 
707
	/**
708
	 * Adds "ALL" modifier to the SQL statement.
709
	 * @return     Criteria Modified Criteria object (for fluent API)
710
	 */
711
	public function setAll()
712
	{
713
		$this->selectModifiers[] = self::ALL;
714
		return $this;
715
	}
716
 
717
	/**
718
	 * Adds "DISTINCT" modifier to the SQL statement.
719
	 * @return     Criteria Modified Criteria object (for fluent API)
720
	 */
721
	public function setDistinct()
722
	{
723
		$this->selectModifiers[] = self::DISTINCT;
724
		return $this;
725
	}
726
 
727
	/**
728
	 * Sets ignore case.
729
	 *
730
	 * @param      boolean $b True if case should be ignored.
731
	 * @return     Criteria Modified Criteria object (for fluent API)
732
	 */
733
	public function setIgnoreCase($b)
734
	{
735
		$this->ignoreCase = (boolean) $b;
736
		return $this;
737
	}
738
 
739
	/**
740
	 * Is ignore case on or off?
741
	 *
742
	 * @return     boolean True if case is ignored.
743
	 */
744
	public function isIgnoreCase()
745
	{
746
		return $this->ignoreCase;
747
	}
748
 
749
	/**
750
	 * Set single record?  Set this to <code>true</code> if you expect the query
751
	 * to result in only a single result record (the default behaviour is to
752
	 * throw a PropelException if multiple records are returned when the query
753
	 * is executed).  This should be used in situations where returning multiple
754
	 * rows would indicate an error of some sort.  If your query might return
755
	 * multiple records but you are only interested in the first one then you
756
	 * should be using setLimit(1).
757
	 *
758
	 * @param      boolean $b Set to TRUE if you expect the query to select just one record.
759
	 * @return     Criteria Modified Criteria object (for fluent API)
760
	 */
761
	public function setSingleRecord($b)
762
	{
763
		$this->singleRecord = (boolean) $b;
764
		return $this;
765
	}
766
 
767
	/**
768
	 * Is single record?
769
	 *
770
	 * @return     boolean True if a single record is being returned.
771
	 */
772
	public function isSingleRecord()
773
	{
774
		return $this->singleRecord;
775
	}
776
 
777
	/**
778
	 * Set limit.
779
	 *
780
	 * @param      limit An int with the value for limit.
781
	 * @return     Criteria Modified Criteria object (for fluent API)
782
	 */
783
	public function setLimit($limit)
784
	{
785
		// TODO: do we enforce int here? 32bit issue if we do
786
		$this->limit = $limit;
787
		return $this;
788
	}
789
 
790
	/**
791
	 * Get limit.
792
	 *
793
	 * @return     int An int with the value for limit.
794
	 */
795
	public function getLimit()
796
	{
797
		return $this->limit;
798
	}
799
 
800
	/**
801
	 * Set offset.
802
	 *
803
	 * @param      int $offset An int with the value for offset.  (Note this values is
804
	 * 							cast to a 32bit integer and may result in truncatation)
805
	 * @return     Criteria Modified Criteria object (for fluent API)
806
	 */
807
	public function setOffset($offset)
808
	{
809
		$this->offset = (int) $offset;
810
		return $this;
811
	}
812
 
813
	/**
814
	 * Get offset.
815
	 *
816
	 * @return     An int with the value for offset.
817
	 */
818
	public function getOffset()
819
	{
820
		return $this->offset;
821
	}
822
 
823
	/**
824
	 * Add select column.
825
	 *
826
	 * @param      string $name Name of the select column.
827
	 * @return     Criteria Modified Criteria object (for fluent API)
828
	 */
829
	public function addSelectColumn($name)
830
	{
831
		$this->selectColumns[] = $name;
832
		return $this;
833
	}
834
 
835
	/**
836
	 * Whether this Criteria has any select columns.
837
	 *
838
	 * This will include columns added with addAsColumn() method.
839
	 *
840
	 * @return     boolean
841
	 * @see        addAsColumn()
842
	 * @see        addSelectColumn()
843
	 */
844
	public function hasSelectClause()
845
	{
846
		return (!empty($this->selectColumns) || !empty($this->asColumns));
847
	}
848
 
849
	/**
850
	 * Get select columns.
851
	 *
852
	 * @return     array An array with the name of the select
853
	 * columns.
854
	 */
855
	public function getSelectColumns()
856
	{
857
		return $this->selectColumns;
858
	}
859
 
860
	/**
861
	 * Clears current select columns.
862
	 *
863
	 * @return     Criteria Modified Criteria object (for fluent API)
864
	 */
865
	public function clearSelectColumns() {
866
		$this->selectColumns = $this->asColumns = array();
867
		return $this;
868
	}
869
 
870
	/**
871
	 * Get select modifiers.
872
	 *
873
	 * @return     An array with the select modifiers.
874
	 */
875
	public function getSelectModifiers()
876
	{
877
		return $this->selectModifiers;
878
	}
879
 
880
	/**
881
	 * Add group by column name.
882
	 *
883
	 * @param      string $groupBy The name of the column to group by.
884
	 * @return     A modified Criteria object.
885
	 */
886
	public function addGroupByColumn($groupBy)
887
	{
888
		$this->groupByColumns[] = $groupBy;
889
		return $this;
890
	}
891
 
892
	/**
893
	 * Add order by column name, explicitly specifying ascending.
894
	 *
895
	 * @param      name The name of the column to order by.
896
	 * @return     A modified Criteria object.
897
	 */
898
	public function addAscendingOrderByColumn($name)
899
	{
900
		$this->orderByColumns[] = $name . ' ' . self::ASC;
901
		return $this;
902
	}
903
 
904
	/**
905
	 * Add order by column name, explicitly specifying descending.
906
	 *
907
	 * @param      string $name The name of the column to order by.
908
	 * @return     Criteria Modified Criteria object (for fluent API)
909
	 */
910
	public function addDescendingOrderByColumn($name)
911
	{
912
		$this->orderByColumns[] = $name . ' ' . self::DESC;
913
		return $this;
914
	}
915
 
916
	/**
917
	 * Get order by columns.
918
	 *
919
	 * @return     array An array with the name of the order columns.
920
	 */
921
	public function getOrderByColumns()
922
	{
923
		return $this->orderByColumns;
924
	}
925
 
926
	/**
927
	 * Clear the order-by columns.
928
	 *
929
	 * @return     Criteria Modified Criteria object (for fluent API)
930
	 */
931
	public function clearOrderByColumns()
932
	{
933
		$this->orderByColumns = array();
934
		return $this;
935
	}
936
 
937
	/**
938
	 * Clear the group-by columns.
939
	 *
940
	 * @return     Criteria
941
	 */
942
	public function clearGroupByColumns()
943
	{
944
		$this->groupByColumns = array();
945
		return $this;
946
	}
947
 
948
	/**
949
	 * Get group by columns.
950
	 *
951
	 * @return     array
952
	 */
953
	public function getGroupByColumns()
954
	{
955
		return $this->groupByColumns;
956
	}
957
 
958
	/**
959
	 * Get Having Criterion.
960
	 *
961
	 * @return     Criterion A Criterion object that is the having clause.
962
	 */
963
	public function getHaving()
964
	{
965
		return $this->having;
966
	}
967
 
968
	/**
969
	 * Remove an object from the criteria.
970
	 *
971
	 * @param      string $key A string with the key to be removed.
972
	 * @return     mixed The removed value.
973
	 */
974
	public function remove($key)
975
	{
976
		if ( isset ( $this->map[$key] ) ) {
977
			$removed = $this->map[$key];
978
			unset ( $this->map[$key] );
979
			if ( $removed instanceof Criterion ) {
980
				return $removed->getValue();
981
			}
982
			return $removed;
983
		}
984
	}
985
 
986
	/**
987
	 * Build a string representation of the Criteria.
988
	 *
989
	 * @return     string A String with the representation of the Criteria.
990
	 */
991
	public function toString()
992
	{
993
 
994
		$sb = "Criteria:";
995
		try {
996
 
997
			$params = array();
998
			$sb .= "\nSQL (may not be complete): "
999
			  . BasePeer::createSelectSql($this, $params);
1000
 
1001
			$sb .= "\nParams: ";
1002
			$paramstr = array();
1003
			foreach ($params as $param) {
1004
				$paramstr[] = $param['table'] . '.' . $param['column'] . ' => ' . var_export($param['value'], true);
1005
			}
1006
			$sb .= implode(", ", $paramstr);
1007
 
1008
		} catch (Exception $exc) {
1009
			$sb .= "(Error: " . $exc->getMessage() . ")";
1010
		}
1011
 
1012
		return $sb;
1013
	}
1014
 
1015
	/**
1016
	 * Returns the size (count) of this criteria.
1017
	 * @return     int
1018
	 */
1019
	public function size()
1020
	{
1021
		return count($this->map);
1022
	}
1023
 
1024
	/**
1025
	 * This method checks another Criteria to see if they contain
1026
	 * the same attributes and hashtable entries.
1027
	 * @return     boolean
1028
	 */
1029
	public function equals($crit)
1030
	{
1031
		$isEquiv = false;
1032
		if ($crit === null || !($crit instanceof Criteria)) {
1033
			$isEquiv = false;
1034
		} elseif ($this === $crit) {
1035
			$isEquiv = true;
1036
		} elseif ($this->size() === $crit->size()) {
1037
 
1038
			// Important: nested criterion objects are checked
1039
 
1040
			$criteria = $crit; // alias
1041
			if ($this->offset === $criteria->getOffset()
1042
				&& $this->limit === $criteria->getLimit()
1043
				&& $this->ignoreCase === $criteria->isIgnoreCase()
1044
				&& $this->singleRecord === $criteria->isSingleRecord()
1045
				&& $this->dbName === $criteria->getDbName()
1046
				&& $this->selectModifiers === $criteria->getSelectModifiers()
1047
				&& $this->selectColumns === $criteria->getSelectColumns()
1048
				&& $this->orderByColumns === $criteria->getOrderByColumns()
1049
				&& $this->groupByColumns === $criteria->getGroupByColumns()
1050
			   )
1051
			{
1052
				$isEquiv = true;
1053
				foreach ($criteria->keys() as $key) {
1054
					if ($this->containsKey($key)) {
1055
						$a = $this->getCriterion($key);
1056
						$b = $criteria->getCriterion($key);
1057
						if (!$a->equals($b)) {
1058
							$isEquiv = false;
1059
							break;
1060
						}
1061
					} else {
1062
						$isEquiv = false;
1063
						break;
1064
					}
1065
				}
1066
			}
1067
		}
1068
		return $isEquiv;
1069
	}
1070
 
1071
	/**
1072
	 * This method adds a prepared Criterion object to the Criteria as a having clause.
1073
	 * You can get a new, empty Criterion object with the
1074
	 * getNewCriterion() method.
1075
	 *
1076
	 * <p>
1077
	 * <code>
1078
	 * $crit = new Criteria();
1079
	 * $c = $crit->getNewCriterion(BasePeer::ID, 5, Criteria::LESS_THAN);
1080
	 * $crit->addHaving($c);
1081
	 * </code>
1082
	 *
1083
	 * @param      having A Criterion object
1084
	 *
1085
	 * @return     A modified Criteria object.
1086
	 */
1087
	public function addHaving(Criterion $having)
1088
	{
1089
		$this->having = $having;
1090
		return $this;
1091
	}
1092
 
1093
	/**
1094
	 * This method adds a new criterion to the list of criterias.
1095
	 * If a criterion for the requested column already exists, it is
1096
	 * "AND"ed to the existing criterion.
1097
	  *
1098
	 * addAnd(column, value, comparison)
1099
	 * <code>
1100
	 * $crit = $orig_crit->addAnd(&quot;column&quot;,
1101
	 *                                      &quot;value&quot;
1102
	 *                                      &quot;Criterion::GREATER_THAN&quot;);
1103
	 * </code>
1104
	 *
1105
	 * addAnd(column, value)
1106
	 * <code>
1107
	 * $crit = $orig_crit->addAnd(&quot;column&quot;, &quot;value&quot;);
1108
	 * </code>
1109
	 *
1110
	 * addAnd(Criterion)
1111
	 * <code>
1112
	 * $crit = new Criteria();
1113
	 * $c = $crit->getNewCriterion(BasePeer::ID, 5, Criteria::LESS_THAN);
1114
	 * $crit->addAnd($c);
1115
	 * </code>
1116
	 *
1117
	 * Any comparison can be used, of course.
1118
	 *
1119
	 *
1120
	 * @return     Criteria A modified Criteria object.
1121
	 */
1122
	public function addAnd($p1, $p2 = null, $p3 = null)
1123
	{
1124
		if ($p3 !== null) {
1125
			// addAnd(column, value, comparison)
1126
			$oc = $this->getCriterion($p1);
1127
			$nc = new Criterion($this, $p1, $p2, $p3);
1128
			if ( $oc === null) {
1129
				$this->map[$p1] = $nc;
1130
			} else {
1131
				$oc->addAnd($nc);
1132
			}
1133
		} elseif ($p2 !== null) {
1134
			// addAnd(column, value)
1135
			$this->addAnd($p1, $p2, self::EQUAL);
1136
		} elseif ($p1 instanceof Criterion) {
1137
			// addAnd(Criterion)
1138
			$oc = $this->getCriterion($p1->getTable() . '.' . $p1->getColumn());
1139
			if ($oc === null) {
1140
				$this->add($p1);
1141
			} else {
1142
				$oc->addAnd($p1);
1143
			}
1144
		} elseif ($p2 === null && $p3 === null) {
1145
			// client has not specified $p3 (comparison)
1146
			// which means Criteria::EQUAL but has also specified $p2 == null
1147
			// which is a valid combination we should handle by creating "IS NULL"
1148
			$this->addAnd($p1, $p2, self::EQUAL);
1149
		}
1150
		return $this;
1151
	}
1152
 
1153
	/**
1154
	 * This method adds a new criterion to the list of criterias.
1155
	 * If a criterion for the requested column already exists, it is
1156
	 * "OR"ed to the existing criterion.
1157
	 *
1158
	 * Any comparison can be used.
1159
	 *
1160
	 * Supports a number of different signatures:
1161
	 *
1162
	 * addOr(column, value, comparison)
1163
	 * <code>
1164
	 * $crit = $orig_crit->addOr(&quot;column&quot;,
1165
	 *                                      &quot;value&quot;
1166
	 *                                      &quot;Criterion::GREATER_THAN&quot;);
1167
	 * </code>
1168
	 *
1169
	 * addOr(column, value)
1170
	 * <code>
1171
	 * $crit = $orig_crit->addOr(&quot;column&quot;, &quot;value&quot;);
1172
	 * </code>
1173
	 *
1174
	 * addOr(Criterion)
1175
	 *
1176
	 * @return     Criteria A modified Criteria object.
1177
	 */
1178
	public function addOr($p1, $p2 = null, $p3 = null)
1179
	{
1180
		if ($p3 !== null) {
1181
			// addOr(column, value, comparison)
1182
			$nc = new Criterion($this, $p1, $p2, $p3);
1183
			$oc = $this->getCriterion($p1);
1184
			if ($oc === null) {
1185
				$this->map[$p1] = $nc;
1186
			} else {
1187
				$oc->addOr($nc);
1188
			}
1189
		} elseif ($p2 !== null) {
1190
			// addOr(column, value)
1191
			$this->addOr($p1, $p2, self::EQUAL);
1192
		} elseif ($p1 instanceof Criterion) {
1193
			// addOr(Criterion)
1194
			$oc = $this->getCriterion($p1->getTable() . '.' . $p1->getColumn());
1195
			if ($oc === null) {
1196
				$this->add($p1);
1197
			} else {
1198
				$oc->addOr($p1);
1199
			}
1200
		} elseif ($p2 === null && $p3 === null) {
1201
			// client has not specified $p3 (comparison)
1202
			// which means Criteria::EQUAL but has also specified $p2 == null
1203
			// which is a valid combination we should handle by creating "IS NULL"
1204
			$this->addOr($p1, $p2, self::EQUAL);
1205
		}
1206
 
1207
		return $this;
1208
	}
1209
}
1210
 
1211
// --------------------------------------------------------------------
1212
// Criterion Iterator class -- allows foreach ($criteria as $criterion)
1213
// --------------------------------------------------------------------
1214
 
1215
/**
1216
 * Class that implements SPL Iterator interface.  This allows foreach () to
1217
 * be used w/ Criteria objects.  Probably there is no performance advantage
1218
 * to doing it this way, but it makes sense -- and simpler code.
1219
 *
1220
 * @author     Hans Lellelid <hans@xmpl.org>
1221
 * @package    propel.util
1222
 */
1223
class CriterionIterator implements Iterator {
1224
 
1225
	private $idx = 0;
1226
	private $criteria;
1227
	private $criteriaKeys;
1228
	private $criteriaSize;
1229
 
1230
	public function __construct(Criteria $criteria) {
1231
		$this->criteria = $criteria;
1232
		$this->criteriaKeys = $criteria->keys();
1233
		$this->criteriaSize = count($this->criteriaKeys);
1234
	}
1235
 
1236
	public function rewind() {
1237
		$this->idx = 0;
1238
	}
1239
 
1240
	public function valid() {
1241
		return $this->idx < $this->criteriaSize;
1242
	}
1243
 
1244
	public function key() {
1245
		return $this->criteriaKeys[$this->idx];
1246
	}
1247
 
1248
	public function current() {
1249
		return $this->criteria->getCriterion($this->criteriaKeys[$this->idx]);
1250
	}
1251
 
1252
	public function next() {
1253
		$this->idx++;
1254
	}
1255
 
1256
}
1257
 
1258
// --------------------------------------------------------------------
1259
// Criterion "inner" class
1260
// --------------------------------------------------------------------
1261
 
1262
/**
1263
 * This is an "inner" class that describes an object in the criteria.
1264
 *
1265
 * In Torque this is an inner class of the Criteria class.
1266
 *
1267
 * @author     Hans Lellelid <hans@xmpl.org> (Propel)
1268
 * @package    propel.util
1269
 */
1270
class Criterion  {
1271
 
1272
	const UND = " AND ";
1273
	const ODER = " OR ";
1274
 
1275
	/** Value of the CO. */
1276
	private $value;
1277
 
1278
	/** Comparison value.
1279
	 * @var        SqlEnum
1280
	 */
1281
	private $comparison;
1282
 
1283
	/** Table name. */
1284
	private $table;
1285
 
1286
	/** Real table name */
1287
	private $realtable;
1288
 
1289
	/** Column name. */
1290
	private $column;
1291
 
1292
	/** flag to ignore case in comparision */
1293
	private $ignoreStringCase = false;
1294
 
1295
	/**
1296
	 * The DBAdaptor which might be used to get db specific
1297
	 * variations of sql.
1298
	 */
1299
	private $db;
1300
 
1301
	/**
1302
	 * other connected criteria and their conjunctions.
1303
	 */
1304
	private $clauses = array();
1305
	private $conjunctions = array();
1306
 
1307
	/** "Parent" Criteria class */
1308
	private $parent;
1309
 
1310
	/**
1311
	 * Create a new instance.
1312
	 *
1313
	 * @param      Criteria $parent The outer class (this is an "inner" class).
1314
	 * @param      string $column TABLE.COLUMN format.
1315
	 * @param      mixed $value
1316
	 * @param      string $comparison
1317
	 */
1318
	public function __construct(Criteria $outer, $column, $value, $comparison = null)
1319
	{
1320
		$this->value = $value;
1321
		$dotPos = strrpos($column,'.');
1322
		if ($dotPos === false) {
1323
			// no dot => aliased column
1324
			$this->table = null;
1325
			$this->column = $column;
1326
		} else {
1327
			$this->table = substr($column, 0, $dotPos);
1328
			$this->column = substr($column, $dotPos+1, strlen($column));
1329
		}
1330
		$this->comparison = ($comparison === null ? Criteria::EQUAL : $comparison);
1331
		$this->init($outer);
1332
	}
1333
 
1334
	/**
1335
	* Init some properties with the help of outer class
1336
	* @param      Criteria $criteria The outer class
1337
	*/
1338
	public function init(Criteria $criteria)
1339
	{
1340
		//init $this->db
1341
		try {
1342
			$db = Propel::getDB($criteria->getDbName());
1343
			$this->setDB($db);
1344
		} catch (Exception $e) {
1345
			// we are only doing this to allow easier debugging, so
1346
			// no need to throw up the exception, just make note of it.
1347
			Propel::log("Could not get a DBAdapter, sql may be wrong", Propel::LOG_ERR);
1348
		}
1349
 
1350
		//init $this->realtable
1351
		$realtable = $criteria->getTableForAlias($this->table);
1352
		if (! strlen ( $realtable ) ) {
1353
			$realtable = $this->table;
1354
		}
1355
		$this->realtable = $realtable;
1356
 
1357
	}
1358
 
1359
	/**
1360
	 * Get the column name.
1361
	 *
1362
	 * @return     string A String with the column name.
1363
	 */
1364
	public function getColumn()
1365
	{
1366
		return $this->column;
1367
	}
1368
 
1369
	/**
1370
	 * Set the table name.
1371
	 *
1372
	 * @param      name A String with the table name.
1373
	 * @return     void
1374
	 */
1375
	public function setTable($name)
1376
	{
1377
		$this->table = $name;
1378
	}
1379
 
1380
	/**
1381
	 * Get the table name.
1382
	 *
1383
	 * @return     string A String with the table name.
1384
	 */
1385
	public function getTable()
1386
	{
1387
		return $this->table;
1388
	}
1389
 
1390
	/**
1391
	 * Get the comparison.
1392
	 *
1393
	 * @return     string A String with the comparison.
1394
	 */
1395
	public function getComparison()
1396
	{
1397
		return $this->comparison;
1398
	}
1399
 
1400
	/**
1401
	 * Get the value.
1402
	 *
1403
	 * @return     mixed An Object with the value.
1404
	 */
1405
	public function getValue()
1406
	{
1407
		return $this->value;
1408
	}
1409
 
1410
	/**
1411
	 * Get the value of db.
1412
	 * The DBAdapter which might be used to get db specific
1413
	 * variations of sql.
1414
	 * @return     DBAdapter value of db.
1415
	 */
1416
	public function getDB()
1417
	{
1418
		return $this->db;
1419
	}
1420
 
1421
	/**
1422
	 * Set the value of db.
1423
	 * The DBAdapter might be used to get db specific variations of sql.
1424
	 * @param      DBAdapter $v Value to assign to db.
1425
	 * @return     void
1426
	 */
1427
	public function setDB(DBAdapter $v)
1428
	{
1429
		$this->db = $v;
1430
		foreach ( $this->clauses as $clause ) {
1431
			$clause->setDB($v);
1432
		}
1433
	}
1434
 
1435
	/**
1436
	 * Sets ignore case.
1437
	 *
1438
	 * @param      boolean $b True if case should be ignored.
1439
	 * @return     Criterion A modified Criterion object.
1440
	 */
1441
	public function setIgnoreCase($b)
1442
	{
1443
		$this->ignoreStringCase = (boolean) $b;
1444
		return $this;
1445
	}
1446
 
1447
	/**
1448
	 * Is ignore case on or off?
1449
	 *
1450
	 * @return     boolean True if case is ignored.
1451
	 */
1452
	 public function isIgnoreCase()
1453
	 {
1454
		 return $this->ignoreStringCase;
1455
	 }
1456
 
1457
	/**
1458
	 * Get the list of clauses in this Criterion.
1459
	 * @return     array
1460
	 */
1461
	private function getClauses()
1462
	{
1463
		return $this->clauses;
1464
	}
1465
 
1466
	/**
1467
	 * Get the list of conjunctions in this Criterion
1468
	 * @return     array
1469
	 */
1470
	public function getConjunctions()
1471
	{
1472
		return $this->conjunctions;
1473
	}
1474
 
1475
	/**
1476
	 * Append an AND Criterion onto this Criterion's list.
1477
	 */
1478
	public function addAnd(Criterion $criterion)
1479
	{
1480
		$this->clauses[] = $criterion;
1481
		$this->conjunctions[] = self::UND;
1482
		return $this;
1483
	}
1484
 
1485
	/**
1486
	 * Append an OR Criterion onto this Criterion's list.
1487
	 * @return     Criterion
1488
	 */
1489
	public function addOr(Criterion $criterion)
1490
	{
1491
		$this->clauses[] = $criterion;
1492
		$this->conjunctions[] = self::ODER;
1493
		return $this;
1494
	}
1495
 
1496
	/**
1497
	 * Appends a Prepared Statement representation of the Criterion
1498
	 * onto the buffer.
1499
	 *
1500
	 * @param      string &$sb The string that will receive the Prepared Statement
1501
	 * @param      array $params A list to which Prepared Statement parameters
1502
	 * will be appended
1503
	 * @return     void
1504
	 * @throws     PropelException - if the expression builder cannot figure out how to turn a specified
1505
	 *                           expression into proper SQL.
1506
	 */
1507
	public function appendPsTo(&$sb, array &$params)
1508
	{
1509
		if ($this->column === null) {
1510
			return;
1511
		}
1512
 
1513
		$db = $this->getDb();
1514
		$sb .= str_repeat ( '(', count($this->clauses) );
1515
 
1516
		if (Criteria::CUSTOM === $this->comparison) {
1517
			if ($this->value !== "") {
1518
				$sb .= (string) $this->value;
1519
			}
1520
		} else {
1521
 
1522
			if  ($this->table === null) {
1523
				$field = $this->column;
1524
			} else {
1525
				$field = $this->table . '.' . $this->column;
1526
			}
1527
 
1528
			// Check to see if table is an alias & store real name, if so
1529
			// (real table name is needed for the returned $params array)
1530
			$realtable = $this->realtable;
1531
 
1532
			// There are several different types of expressions that need individual handling:
1533
			// IN/NOT IN, LIKE/NOT LIKE, and traditional expressions.
1534
 
1535
			// OPTION 1:  table.column IN (?, ?) or table.column NOT IN (?, ?)
1536
			if ($this->comparison === Criteria::IN || $this->comparison === Criteria::NOT_IN) {
1537
 
1538
				$_bindParams = array(); // the param names used in query building
1539
				$_idxstart = count($params);
1540
				$valuesLength = 0;
1541
				foreach ( (array) $this->value as $value ) {
1542
					$valuesLength++; // increment this first to correct for wanting bind params to start with :p1
1543
					$params[] = array('table' => $realtable, 'column' => $this->column, 'value' => $value);
1544
					$_bindParams[] = ':p'.($_idxstart + $valuesLength);
1545
				}
1546
				if ( $valuesLength !== 0 ) {
1547
					$sb .= $field . $this->comparison . '(' . implode(',', $_bindParams) . ')';
1548
				} else {
1549
					$sb .= ($this->comparison === Criteria::IN) ? "1<>1" : "1=1";
1550
				}
1551
				unset ( $value, $valuesLength );
1552
 
1553
			// OPTION 2:  table.column LIKE ? or table.column NOT LIKE ?  (or ILIKE for Postgres)
1554
			} elseif ($this->comparison === Criteria::LIKE || $this->comparison === Criteria::NOT_LIKE
1555
				|| $this->comparison === Criteria::ILIKE || $this->comparison === Criteria::NOT_ILIKE) {
1556
				// Handle LIKE, NOT LIKE (and related ILIKE, NOT ILIKE for Postgres)
1557
 
1558
				// If selection is case insensitive use ILIKE for PostgreSQL or SQL
1559
				// UPPER() function on column name for other databases.
1560
				if ($this->ignoreStringCase) {
1561
					if ($db instanceof DBPostgres) {
1562
						if ($this->comparison === Criteria::LIKE) {
1563
							$this->comparison = Criteria::ILIKE;
1564
						} elseif ($this->comparison === Criteria::NOT_LIKE) {
1565
							$this->comparison = Criteria::NOT_ILIKE;
1566
						}
1567
					} else {
1568
						$field = $db->ignoreCase($field);
1569
					}
1570
				}
1571
 
1572
				$params[] = array('table' => $realtable, 'column' => $this->column, 'value' => $this->value);
1573
 
1574
				$sb .= $field . $this->comparison;
1575
 
1576
				// If selection is case insensitive use SQL UPPER() function
1577
				// on criteria or, if Postgres we are using ILIKE, so not necessary.
1578
				if ($this->ignoreStringCase && !($db instanceof DBPostgres)) {
1579
					$sb .= $db->ignoreCase(':p'.count($params));
1580
				} else {
1581
					$sb .= ':p'.count($params);
1582
				}
1583
 
1584
			// OPTION 3:  table.column = ? or table.column >= ? etc. (traditional expressions, the default)
1585
			} else {
1586
 
1587
				// NULL VALUES need special treatment because the SQL syntax is different
1588
				// i.e. table.column IS NULL rather than table.column = null
1589
				if ($this->value !== null) {
1590
 
1591
					// ANSI SQL functions get inserted right into SQL (not escaped, etc.)
1592
					if ($this->value === Criteria::CURRENT_DATE || $this->value === Criteria::CURRENT_TIME || $this->value === Criteria::CURRENT_TIMESTAMP) {
1593
						$sb .= $field . $this->comparison . $this->value;
1594
					} else {
1595
 
1596
						$params[] = array('table' => $realtable, 'column' => $this->column, 'value' => $this->value);
1597
 
1598
						// default case, it is a normal col = value expression; value
1599
						// will be replaced w/ '?' and will be inserted later using PDO bindValue()
1600
						if ($this->ignoreStringCase) {
1601
							$sb .= $db->ignoreCase($field) . $this->comparison . $db->ignoreCase(':p'.count($params));
1602
						} else {
1603
							$sb .= $field . $this->comparison . ':p'.count($params);
1604
						}
1605
 
1606
					}
1607
				} else {
1608
 
1609
					// value is null, which means it was either not specified or specifically
1610
					// set to null.
1611
					if ($this->comparison === Criteria::EQUAL || $this->comparison === Criteria::ISNULL) {
1612
						$sb .= $field . Criteria::ISNULL;
1613
					} elseif ($this->comparison === Criteria::NOT_EQUAL || $this->comparison === Criteria::ISNOTNULL) {
1614
						$sb .= $field . Criteria::ISNOTNULL;
1615
					} else {
1616
						// for now throw an exception, because not sure how to interpret this
1617
						throw new PropelException("Could not build SQL for expression: $field " . $this->comparison . " NULL");
1618
					}
1619
 
1620
				}
1621
 
1622
			}
1623
		}
1624
 
1625
		foreach ( $this->clauses as $key=>$clause ) {
1626
			$sb .= $this->conjunctions[$key];
1627
			$clause->appendPsTo($sb, $params);
1628
			$sb .= ')';
1629
		}
1630
	}
1631
 
1632
	/**
1633
	 * This method checks another Criteria to see if they contain
1634
	 * the same attributes and hashtable entries.
1635
	 * @return     boolean
1636
	 */
1637
	public function equals($obj)
1638
	{
1639
		// TODO: optimize me with early outs
1640
		if ($this === $obj) {
1641
			return true;
1642
		}
1643
 
1644
		if (($obj === null) || !($obj instanceof Criterion)) {
1645
			return false;
1646
		}
1647
 
1648
		$crit = $obj;
1649
 
1650
		$isEquiv = ( ( ($this->table === null && $crit->getTable() === null)
1651
			|| ( $this->table !== null && $this->table === $crit->getTable() )
1652
						  )
1653
			&& $this->column === $crit->getColumn()
1654
			&& $this->comparison === $crit->getComparison());
1655
 
1656
		// check chained criterion
1657
 
1658
		$clausesLength = count($this->clauses);
1659
		$isEquiv &= (count($crit->getClauses()) == $clausesLength);
1660
		$critConjunctions = $crit->getConjunctions();
1661
		$critClauses = $crit->getClauses();
1662
		for ($i=0; $i < $clausesLength && $isEquiv; $i++) {
1663
			$isEquiv &= ($this->conjunctions[$i] === $critConjunctions[$i]);
1664
			$isEquiv &= ($this->clauses[$i] === $critClauses[$i]);
1665
		}
1666
 
1667
		if ($isEquiv) {
1668
			$isEquiv &= $this->value === $crit->getValue();
1669
		}
1670
 
1671
		return $isEquiv;
1672
	}
1673
 
1674
	/**
1675
	 * Returns a hash code value for the object.
1676
	 */
1677
	public function hashCode()
1678
	{
1679
		$h = crc32(serialize($this->value)) ^ crc32($this->comparison);
1680
 
1681
		if ($this->table !== null) {
1682
			$h ^= crc32($this->table);
1683
		}
1684
 
1685
		if ($this->column !== null) {
1686
			$h ^= crc32($this->column);
1687
		}
1688
 
1689
		foreach ( $this->clauses as $clause ) {
1690
			// TODO: i KNOW there is a php incompatibility with the following line
1691
			// but i dont remember what it is, someone care to look it up and
1692
			// replace it if it doesnt bother us?
1693
			// $clause->appendPsTo($sb='',$params=array());
1694
			$sb = '';
1695
			$params = array();
1696
			$clause->appendPsTo($sb,$params);
1697
			$h ^= crc32(serialize(array($sb,$params)));
1698
			unset ( $sb, $params );
1699
		}
1700
 
1701
		return $h;
1702
	}
1703
 
1704
	/**
1705
	 * Get all tables from nested criterion objects
1706
	 * @return     array
1707
	 */
1708
	public function getAllTables()
1709
	{
1710
		$tables = array();
1711
		$this->addCriterionTable($this, $tables);
1712
		return $tables;
1713
	}
1714
 
1715
	/**
1716
	 * method supporting recursion through all criterions to give
1717
	 * us a string array of tables from each criterion
1718
	 * @return     void
1719
	 */
1720
	private function addCriterionTable(Criterion $c, array &$s)
1721
	{
1722
		$s[] = $c->getTable();
1723
		foreach ( $c->getClauses() as $clause ) {
1724
			$this->addCriterionTable($clause, $s);
1725
		}
1726
	}
1727
 
1728
	/**
1729
	 * get an array of all criterion attached to this
1730
	 * recursing through all sub criterion
1731
	 * @return     array Criterion[]
1732
	 */
1733
	public function getAttachedCriterion()
1734
	{
1735
		$crits = array();
1736
		$this->traverseCriterion($this, $crits);
1737
		return $crits;
1738
	}
1739
 
1740
	/**
1741
	 * method supporting recursion through all criterions to give
1742
	 * us an array of them
1743
	 * @param      Criterion $c
1744
	 * @param      array &$a
1745
	 * @return     void
1746
	 */
1747
	private function traverseCriterion(Criterion $c, array &$a)
1748
	{
1749
		$a[] = $c;
1750
		foreach ( $c->getClauses() as $clause ) {
1751
			$this->traverseCriterion($clause, $a);
1752
		}
1753
	}
1754
}