1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24:
25: class DibiDataSource extends DibiObject implements IDataSource
26: {
27:
28: private $connection;
29:
30:
31: private $sql;
32:
33:
34: private $result;
35:
36:
37: private $count;
38:
39:
40: private $totalCount;
41:
42:
43: private $cols = array();
44:
45:
46: private $sorting = array();
47:
48:
49: private $conds = array();
50:
51:
52: private $offset;
53:
54:
55: private $limit;
56:
57:
58:
59: 60: 61: 62:
63: public function __construct($sql, DibiConnection $connection)
64: {
65: if (strpbrk($sql, " \t\r\n") === FALSE) {
66: $this->sql = $connection->getDriver()->escape($sql, dibi::IDENTIFIER); 67: } else {
68: $this->sql = '(' . $sql . ') t'; 69: }
70: $this->connection = $connection;
71: }
72:
73:
74:
75: 76: 77: 78: 79: 80:
81: public function select($col, $as = NULL)
82: {
83: if (is_array($col)) {
84: $this->cols = $col;
85: } else {
86: $this->cols[$col] = $as;
87: }
88: $this->result = NULL;
89: return $this;
90: }
91:
92:
93:
94: 95: 96: 97: 98:
99: public function where($cond)
100: {
101: if (is_array($cond)) {
102: 103: $this->conds[] = $cond;
104: } else {
105: $this->conds[] = func_get_args();
106: }
107: $this->result = $this->count = NULL;
108: return $this;
109: }
110:
111:
112:
113: 114: 115: 116: 117: 118:
119: public function orderBy($row, $sorting = 'ASC')
120: {
121: if (is_array($row)) {
122: $this->sorting = $row;
123: } else {
124: $this->sorting[$row] = $sorting;
125: }
126: $this->result = NULL;
127: return $this;
128: }
129:
130:
131:
132: 133: 134: 135: 136: 137:
138: public function applyLimit($limit, $offset = NULL)
139: {
140: $this->limit = $limit;
141: $this->offset = $offset;
142: $this->result = $this->count = NULL;
143: return $this;
144: }
145:
146:
147:
148: 149: 150: 151:
152: final public function getConnection()
153: {
154: return $this->connection;
155: }
156:
157:
158:
159:
160:
161:
162:
163: 164: 165: 166:
167: public function getResult()
168: {
169: if ($this->result === NULL) {
170: $this->result = $this->connection->nativeQuery($this->__toString());
171: }
172: return $this->result;
173: }
174:
175:
176:
177: 178: 179:
180: public function getIterator()
181: {
182: return $this->getResult()->getIterator();
183: }
184:
185:
186:
187: 188: 189: 190:
191: public function fetch()
192: {
193: return $this->getResult()->fetch();
194: }
195:
196:
197:
198: 199: 200: 201:
202: public function fetchSingle()
203: {
204: return $this->getResult()->fetchSingle();
205: }
206:
207:
208:
209: 210: 211: 212:
213: public function fetchAll()
214: {
215: return $this->getResult()->fetchAll();
216: }
217:
218:
219:
220: 221: 222: 223: 224:
225: public function fetchAssoc($assoc)
226: {
227: return $this->getResult()->fetchAssoc($assoc);
228: }
229:
230:
231:
232: 233: 234: 235: 236: 237:
238: public function fetchPairs($key = NULL, $value = NULL)
239: {
240: return $this->getResult()->fetchPairs($key, $value);
241: }
242:
243:
244:
245: 246: 247: 248:
249: public function release()
250: {
251: $this->result = $this->count = $this->totalCount = NULL;
252: }
253:
254:
255:
256:
257:
258:
259:
260: 261: 262: 263:
264: public function toFluent()
265: {
266: return $this->connection->select('*')->from('(%SQL) AS t', $this->__toString());
267: }
268:
269:
270:
271: 272: 273: 274:
275: public function toDataSource()
276: {
277: return new self($this->__toString(), $this->connection);
278: }
279:
280:
281:
282: 283: 284: 285:
286: public function __toString()
287: {
288: return $this->connection->translate('
289: SELECT %n', (empty($this->cols) ? '*' : $this->cols), '
290: FROM %SQL', $this->sql, '
291: %ex', $this->conds ? array('WHERE %and', $this->conds) : NULL, '
292: %ex', $this->sorting ? array('ORDER BY %by', $this->sorting) : NULL, '
293: %ofs %lmt', $this->offset, $this->limit
294: );
295: }
296:
297:
298:
299:
300:
301:
302:
303: 304: 305: 306:
307: public function count()
308: {
309: if ($this->count === NULL) {
310: $this->count = $this->conds || $this->offset || $this->limit
311: ? (int) $this->connection->nativeQuery(
312: 'SELECT COUNT(*) FROM (' . $this->__toString() . ') AS t'
313: )->fetchSingle()
314: : $this->getTotalCount();
315: }
316: return $this->count;
317: }
318:
319:
320:
321: 322: 323: 324:
325: public function getTotalCount()
326: {
327: if ($this->totalCount === NULL) {
328: $this->totalCount = (int) $this->connection->nativeQuery(
329: 'SELECT COUNT(*) FROM ' . $this->sql
330: )->fetchSingle();
331: }
332: return $this->totalCount;
333: }
334:
335: }
336: