-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathORMCollection.php
More file actions
323 lines (254 loc) · 6.9 KB
/
Copy pathORMCollection.php
File metadata and controls
323 lines (254 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/*
* Esta clase contiene una lista de objetos ORMBase
* obtenidos generalmente por medio de getAll.
* Sin embargo los objetos no son obtenidos de la base de datos,
* sino hasta que realmente son requeridos
* */
include_once("ORMCondition.php");
class ORMCollection implements Iterator, SeekableIterator
{
public static $OFFSET=null;
protected $data=array();
private $begin = null;
private $offset = 50;
private $pointer = 0;
protected $whereCondition = null;
public function get($i)
{
$this->count();
$offset = (is_null(self::$OFFSET))?$this->offset:self::$OFFSET;
if ( $offset < 1 ) $offset = 1;
if ( $this->begin === null || $i >= $this->begin+$offset )
{
$conn = $this->obj->getConnection();
$this->generateSQL();
$rs = $conn->SelectLimit($this->sql,$offset,$i,$this->params);
$this->data = array();
foreach($rs as $item)
{
$this->data[] = $item;
}
$this->begin = $i;
}
$obj = clone $this->obj;
if ( count($this->data) < 2 )
{
$obj->setFields($this->data[0]);
}else
{
$obj->setFields($this->data[$i-$this->begin]);
}
return $obj;
}
public function __construct($obj)
{
$this->obj = $obj;
$this->tablename = $obj->getTableName();
$this->sql = null;
}
public function WhereAnd($property,$value=null)
{
if ( $this->whereCondition == null ) // No existe condiciones creadas
{
$this->WhereCondition($property,$value);
}else{ // Agregar condiciones con And
$this->whereCondition = $this->whereCondition->andCondition($property,$value);
}
return $this;
}
public function WhereOr($property,$value=null)
{
if ( $this->whereCondition == null ) // No existe condiciones creadas
{
$this->WhereCondition($property,$value);
}else{ // Agregar condiciones con And
$this->whereCondition = $this->whereCondition->orCondition($property,$value);
}
return $this;
}
public function Orderby($property,$asc=null)
{
if ( is_array($property) )
foreach($property as $key => $value )
$this->orderby[$key]=$value;
else
$this->orderby[$property] = $asc;
$this->sql = null;
return $this;
}
public function GroupBy($property)
{
if ( is_array($property) )
foreach($property as $key )
$this->groupby[] = $key;
else
$this->groupby[] = $property;
$this->sql = null;
return $this;
}
public function AddSelect($property)
{
}
private function generateSQL()
{
if ( $this->sql != null ) return $this->sql;
$groupby = "";
if ( isset( $this->groupby ) )
{
$groupby = array();
foreach($this->groupby as $group)
{ $groupby[] = $group; }
$groupby = " GROUP BY ".implode(",", $groupby);
}
$orderby = "";
if ( isset($this->orderby) )
{
$orderby = array();
foreach($this->orderby as $key=>$value)
$orderby[$key] = ($value==true)?" $key ASC " :" $key DESC ";
$orderby = "ORDER BY ".implode(",",$orderby);
}
$this->params = array();
/*$where = "";
if ( isset($this->where))
{
$where = array();
foreach($this->where as $key => $value )
{
$where[] = $key." ?";
$args[] = $value;
}
$where = "WHERE 1=1 ".implode(" ",$where);
}*/
$where = $this->_whereCondition();
$sql = " $this->tablename $where $orderby $groupby";
$this->from = $sql;
$this->sql = "SELECT * from $this->tablename $where $orderby $groupby";
$this->countsql = "SELECT count(*) from $this->tablename $where $orderby $groupby";
//$this->params = $args;
//print_r($this->params);
return $sql;
}
public function rewind() { $this->pointer = 0; }
public function current() { return $this->get($this->pointer); }
public function key() { return $this->pointer; }
public function next() { $this->pointer++; }
public function valid() { return ($this->pointer < $this->count() ); }
public function seek($pos) { $this->pointer = ($this->count() > $pos)?$pos:$this->count() - 1; }
public function count()
{
if ( ! isset($this->count) )
{
$this->generateSQL();
$conn = $this->obj->getConnection();
$this->count = (int)$conn->GetOne($this->countsql,$this->params);
}
return $this->count;
}
public function delete($i=null)
{
//echo "delete $this->from";
}
public function getArray($i=null,$level=0)
{
if ( $i == null ) $i = $this->count();
$i = (int) $i;
if ( $i < 0 ) $i = 0;
if ( $i > $this->count() ) $i = $this->count();
$conn = $this->obj->getConnection();
$this->generateSQL();
$rs = $conn->SelectLimit($this->sql,$i,$this->pointer,$this->params);
$response = array();
foreach($rs as $item)
{
$aux = clone $this->obj;
$aux->setFields($item);
$response[] = $aux;
}
return $response;
}
public function toJSON($i=null)
{
if ( is_null($i) ) $i = $this->count();
if ( $i <= 0) return json_encode(array());
if ( $i > $this->count()) $i = $this->count();
$conn = $this->obj->getConnection();
$this->generateSQL();
$rs = $conn->SelectLimit($this->sql, $i, $this->pointer, $this->params);
$response = array();
foreach($rs as $item)
{
$response[] = $item;
}
return json_encode($response);
}
// ORMCollection::update
// Permite hacer actualizaciones a distintos campos de nuestra tabla
// para todos los objetos seleccionados en el ORMCollection
public function update($params)
{
if ( ! is_array($params) )
{
throw new Exception("ORMCollection::update esperaba un array");
}
foreach( $params as $key => $value)
{
}
}
public function WhereCondition($key,$value=null)
{
if (! is_a($key ,"ORMCondition") )
{
$condition = new ORMCondition($key,$value);
}else
$condition = $key;
$this->whereCondition = $condition;
return $this;
}
private function _whereCondition()
{
$cond = &$this->whereCondition;
if ( $cond == null ) return "";
if ( ! is_a($cond,"ORMCondition")) return "";
$where = "WHERE ";
$con = $this->_iterCondition($cond);
return "WHERE $con";
}
private function _iterCondition($cond)
{
if ( is_a($cond,"ORMCondition")) return $this->_iterCondition($cond->conditions);
if ( ! is_array($cond)) return "";
$strcond = "";
if ( ! array_key_exists("op",$cond) )
{
foreach( $cond as $c )
$strcond .= $this->_iterCondition($c);
return " ( $strcond ) ";
}
$op = $cond["op"]; // Operador
if ( array_key_exists("cond",$cond))
return "$op ".$this->_iterCondition($cond["cond"]);
else{
if ( is_array( $cond["value"] )) // Entonces pueden ser un between o un in
{
$this->params = array_merge($this->params,$cond["value"]);
list($field, $fop )=explode(" ",trim($cond["key"]),2);
switch (strtolower(trim($fop)))
{
case "in":
$cond["key"] .= " ( ".join(",",array_fill(0,count($cond["value"]),"?"))." ) ";
break;
case "between":
$cond["key"] .= " ? and ? ";
break;
}
return " $op $cond[key] ";
}else{
$this->params[] = $cond["value"] ;
return "$op $cond[key] ? ";
}
}
}
}
?>