source: pro-violet-viettel/sourcecode/api.violet.vn/www/plugins/sfPropel13Plugin/lib/vendor/propel/util/Criteria.php

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