1: <?php
2:
3: namespace zsql;
4:
5: 6: 7: 8:
9: abstract class ExtendedQuery extends Query
10: {
11: 12: 13: 14: 15:
16: protected $direction;
17:
18: 19: 20: 21: 22:
23: protected $group;
24:
25: 26: 27: 28: 29:
30: protected $limit;
31:
32: 33: 34: 35: 36:
37: protected $offset;
38:
39: 40: 41: 42: 43:
44: protected $order;
45:
46: 47: 48: 49: 50:
51: protected $where;
52:
53: 54: 55: 56: 57: 58:
59: public function group($column)
60: {
61: $this->group = $column;
62: return $this;
63: }
64:
65: 66: 67: 68: 69: 70: 71:
72: public function limit($limit, $offset = null)
73: {
74: $this->limit = (int) $limit;
75: if( null !== $offset ) {
76: $this->offset = (int) $offset;
77: }
78: return $this;
79: }
80:
81: 82: 83: 84: 85: 86: 87:
88: public function offset($offset, $limit = null)
89: {
90: $this->offset = (int) $offset;
91: if( null !== $limit ) {
92: $this->limit = (int) $limit;
93: }
94: return $this;
95: }
96:
97: 98: 99: 100: 101: 102: 103:
104: public function order($order, $direction = 'ASC')
105: {
106: $this->order = $order;
107: $this->direction = $direction;
108: return $this;
109: }
110:
111: 112: 113: 114: 115:
116: protected function pushLimit()
117: {
118: if( null !== $this->limit ) {
119: if( null !== $this->offset ) {
120: $this->parts[] = 'LIMIT ?, ?';
121: $this->params[] = $this->offset;
122: $this->params[] = $this->limit;
123: } else {
124: $this->parts[] = 'LIMIT ?';
125: $this->params[] = $this->limit;
126: }
127: }
128: return $this;
129: }
130:
131: 132: 133: 134: 135:
136: protected function pushGroup()
137: {
138: if( $this->group ) {
139: $this->parts[] = 'GROUP BY';
140: $this->parts[] = $this->quoteIdentifierIfNotExpression($this->group);
141: }
142: return $this;
143: }
144:
145: 146: 147: 148: 149:
150: protected function pushOrder()
151: {
152: if( $this->order ) {
153: $this->parts[] = 'ORDER BY';
154: $this->parts[] = $this->quoteIdentifierIfNotExpression($this->order);
155: $this->parts[] = $this->direction == 'DESC' ? 'DESC' : 'ASC';
156: }
157: return $this;
158: }
159:
160: 161: 162: 163: 164:
165: protected function pushWhere()
166: {
167: if( empty($this->where) ) {
168: return $this;
169: }
170:
171: $this->parts[] = 'WHERE';
172: foreach( $this->where as $w ) {
173: $where = $w[0];
174: $value = isset($w[1]) ? $w[1] : null;
175: $type = isset($w[2]) ? $w[2] : null;
176: if( $where instanceof Expression ) {
177: $this->parts[] = (string) $where;
178: } else if( count($w) == 3 ) {
179: $this->parts[] = $this->quoteIdentifierIfNotExpression($where);
180: $this->parts[] = $type;
181: $tmp = '';
182: foreach( $value as $val ) {
183: $tmp .= '?, ';
184: $this->params[] = $val;
185: }
186: $this->parts[] = '(' . substr($tmp, 0, -2) . ')';
187: } else if( false !== strpos($where, '?') ) {
188: $this->parts[] = $where;
189: $this->params[] = $value;
190: } else {
191: $this->parts[] = $this->quoteIdentifierIfNotExpression($where);
192: $this->parts[] = '=';
193: if( $value instanceof Expression ) {
194: $this->parts[] = (string) $value;
195: } else {
196: $this->parts[] = '?';
197: $this->params[] = $value;
198: }
199: }
200: $this->parts[] = '&&';
201: }
202: array_pop($this->parts);
203: return $this;
204: }
205:
206: 207: 208: 209: 210: 211: 212:
213: public function where($where, $value = null)
214: {
215: $nArgs = func_num_args();
216: if( $nArgs >= 2 ) {
217: $this->where[] = array($where, $value);
218: } else if( $nArgs >= 1 ) {
219: $this->where[] = array($where);
220: }
221: return $this;
222: }
223:
224: 225: 226: 227: 228: 229: 230:
231: public function whereExpr($where)
232: {
233: $this->where[] = array(new Expression((string) $where));
234: return $this;
235: }
236:
237: 238: 239: 240: 241: 242: 243:
244: public function whereIn($where, $value)
245: {
246: if( !is_array($value) ) {
247: $value = (array) $value;
248: }
249: if( count($value) <= 0 ) {
250: $this->where[] = array(new Expression('FALSE'));
251: } else {
252: $this->where[] = array($where, $value, 'IN');
253: }
254: return $this;
255: }
256: }