1 | <?php |
||
13 | class WhereStatement extends Statement |
||
14 | { |
||
15 | /** |
||
16 | * @var bool |
||
17 | */ |
||
18 | protected $having; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $conditions = []; |
||
24 | |||
25 | /** |
||
26 | * @param bool $having when true, statement becomes a having statement |
||
27 | */ |
||
28 | public function __construct($having = false) |
||
32 | |||
33 | /** |
||
34 | * Tells whether this statement is a HAVING statement. |
||
35 | * |
||
36 | * @return bool true: is HAVING, false: is WHERE |
||
37 | */ |
||
38 | public function isHaving() |
||
42 | |||
43 | /** |
||
44 | * Adds a condition to the statement. |
||
45 | * |
||
46 | * Accepts the following forms: |
||
47 | * 1. Equality comparison: |
||
48 | * addCondition('username', 'john') |
||
49 | * 2. Comparison with custom operator: |
||
50 | * addCondition('balance', 100, '>') |
||
51 | * 3. IN statement: |
||
52 | * addCondition('group', ['admin', 'owner']) |
||
53 | * 4. SQL fragment: |
||
54 | * addCondition('name LIKE "%john%"') |
||
55 | * 5. List of conditions to add: |
||
56 | * addCondition([['balance', 100, '>'], |
||
57 | * ['user_id', 5]]) |
||
58 | * 6. Map of equality comparisons: |
||
59 | * addCondition(['username' => 'john', |
||
60 | * 'user_id' => 5]) |
||
61 | * 7. List of SQL fragments: |
||
62 | * addCondition(['first_name LIKE "%john%"', |
||
63 | * 'last_name LIKE "%doe%"']) |
||
64 | * |
||
65 | * @param array|string $field |
||
66 | * @param string|bool $value condition value (optional) |
||
67 | * @param string $operator operator (optional) |
||
68 | * |
||
69 | * @return self |
||
70 | */ |
||
71 | public function addCondition($field, $value = false, $operator = '=') |
||
106 | |||
107 | /** |
||
108 | * Gets the conditions for this statement. |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | public function getConditions() |
||
116 | |||
117 | /** |
||
118 | * Builds a parameterized and escaped SQL fragment |
||
119 | * for a condition that uses our own internal |
||
120 | * representation. |
||
121 | * |
||
122 | * A condition is represented by an array, and can be |
||
123 | * have one of the following forms: |
||
124 | * i) ['SQL fragment'] |
||
125 | * ii) ['identifier', '=', 'value'] |
||
126 | * |
||
127 | * @param array $cond |
||
128 | * |
||
129 | * @return string generated SQL fragment |
||
130 | */ |
||
131 | protected function buildClause(array $cond) |
||
163 | |||
164 | public function build() |
||
186 | } |
||
187 |