~desarrollokumbia/kumbia/ActiveRecord

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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
/**
 * KumbiaPHP web & app Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://wiki.kumbiaphp.com/Licencia
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@kumbiaphp.com so we can send you a copy immediately.
 *
 * Clase para consultas SQL
 * 
 * @category   Kumbia
 * @package    DbPool 
 * @copyright  Copyright (c) 2005-2009 Kumbia Team (http://www.kumbiaphp.com)
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
 */

class DbQuery
{
    /**
     * Nombre de tabla asociada al modelo
     *
     * @var string
     **/
    protected $_table;

    /**
     * Nombre del esquema donde se ubica la tabla
     *
     * @var string
     **/
    protected $_schema;

    /**
     * Condicion para WHERE
     *
     * @var string
     **/
    protected $_where = null;

    /**
     * INNER JOIN a realizar
     *
     * @var array
     **/
    protected $_join = array();

    /**
     * LEFT OUTER JOIN a realizar
     *
     * @var array
     **/
    protected $_leftJoin = array();

    /**
     * RIGHT OUTER JOIN a realizar
     *
     * @var array
     **/
    protected $_rightJoin = array();
    
    /**
     * FULL JOIN a realizar
     *
     * @var array
     **/
    protected $_fullJoin = array();

    /**
     * GROUP a realizar
     *
     * @var array
     **/
    protected $_group = null;
    
    /**
     * Condicion para HAVING
     *
     * @var string
     **/
    protected $_having = null;
    
    /**
     * Criterio de ordenamiento
     *
     * @var string
     **/
    protected $_order = null;

    /**
     * OFFSET
     *
     * @var string
     **/
    protected $_offset = null;

    /**
     * LIMIT
     *
     * @var string
     **/
    protected $_limit = null;

    /**
     * Nombre de conexion
     *
     * @var string
     **/
    protected $_connection;

    /**
     * Constructor
     *
     * @param string $connection nombre de conexion en databases.ini
     **/
    public function __construct($connection)
    {
        $this->_connection = $connection;
    }

    /**
     * Genera la fuente de datos
     *
     * @return string
     **/
    protected function _getSource()
    {
        $source = $this->_table;
        if($this->_schema) {
            $source = $this->_schema . '.' . $source;
        }
        
        return $source;
    }

    /**
     * Clausula WHERE
     *
     * @param string | array $conditions condiciones
     * @return SqlQuery
     **/
    public function where($conditions) 
    {
        // para cuando se pasa por array
        if(is_array($conditions)) {
            $buff = array();
            foreach($conditions as $column => $value) {
                $buff[] = "$column=\"" . DbPool::factory($this->_connection)->quote($value) . '"'; 
            }
            $conditions = implode(' AND ', $buff);
        }
        
        $this->_where = $conditions;
        return $this;
    }
    
    /**
     * Clausula INNER JOIN
     *
     * @param string $table nombre de tabla
     * @param string $conditions condiciones
     * @return SqlQuery
     **/
    public function join($table, $conditions) 
    {
        $this->_join[] = array('table' => $table, 'conditions' => $conditions);
        return $this;
    }
    
    /**
     * Clausula LEFT OUTER JOIN
     *
     * @param string $table nombre de tabla
     * @param string $conditions condiciones
     * @return SqlQuery
     **/
    public function leftJoin($table, $conditions) 
    {
        $this->_leftJoin[] = array('table' => $table, 'conditions' => $conditions);
        return $this;
    }
    
    /**
     * Clausula RIGHT OUTER JOIN
     *
     * @param string $table nombre de tabla
     * @param string $conditions condiciones
     * @return SqlQuery
     **/
    public function rightJoin($table, $conditions) 
    {
        $this->_rightJoin[] = array('table' => $table, 'conditions' => $conditions);
        return $this;
    }
    
    /**
     * Clausula FULL JOIN
     *
     * @param string $table nombre de tabla
     * @param string $conditions condiciones
     * @return SqlQuery
     **/
    public function fullJoin($table, $conditions) 
    {
        $this->_fullJoin[] = array('table' => $table, 'conditions' => $conditions);
        return $this;
    }
    
    /**
     * Columnas de la consulta
     *
     * @param string $table nombre de tabla
     * @return SqlQuery
     **/
    public function table($table) 
    {
        $this->_table = $table;
        return $this;
    }
        
    /**
     * Clausula SELECT
     *
     * @param string $criteria criterio de ordenamiento
     * @return SqlQuery
     **/
    public function order($criteria) 
    {
        $this->_order = $criteria;
        return $this;
    }
    
    /**
     * Clausula GROUP
     *
     * @param string $columns columnas
     * @return SqlQuery
     **/
    public function group($columns) 
    {
        $this->_group = $columns;
        return $this;
    }    

    /**
     * Clausula HAVING
     *
     * @param string $conditions condiciones
     * @return SqlQuery
     **/
    public function having($conditions) 
    {
        $this->_having = $conditions;
        return $this;
    }

    /**
     * Clausula LIMIT
     *
     * @param int $limit
     * @return SqlQuery
     **/
    public function limit($limit) 
    {
        $this->_limit = $limit;
        return $this;
    }   

    /**
     * Clausula OFFSET
     *
     * @param int $offset
     * @return SqlQuery
     **/
    public function offset($offset) 
    {
        $this->_offset = $offset;
        return $this;
    }  

    /**
     * Une con las clausulas adicionales de consulta
     *
     * @param string $sql consulta sql donde se unira las clausulas
     * @return string
     **/
    protected function _joinClausules($sql)
    {
        foreach($this->_join as $join) {
            $sql .= " INNER JOIN {$join['table']} ON ({$join['conditions']})";
        }

        foreach($this->_leftJoin as $join) {
            $sql .= " LEFT OUTER JOIN {$join['table']} ON ({$join['conditions']})";
        }

        foreach($this->_rightJoin as $join) {
            $sql .= " RIGHT OUTER JOIN {$join['table']} ON ({$join['conditions']})";
        }

        foreach($this->_fullJoin as $join) {
            $sql .= " FULL JOIN {$join['table']} ON ({$join['conditions']})";
        }

        if($this->_where) {
            $sql .= " WHERE $this->_where";
        }
    
        if($this->_group) {
            $sql .= " GROUP BY $this->_group";
        }

        if($this->_having) {
            $sql .= " HAVING $this->_having";
        }

        if($this->_order) {
            $sql .= " ORDER BY $this->_order";
        }

        if(!is_null($this->_limit)) {
            $sql .= " LIMIT $this->_limit";
        }

        if(!is_null($this->_offset)) {
            $sql .= " OFFSET $this->_offset";
        }
        
        return $sql;
    }

    /**
     * Construye la consulta SELECT
     *
     * @param string $columns columnas
     * @return string
     **/
    public function select($columns='*') 
    {
        return $this->_joinClausules("SELECT $columns FROM {$this->_getSource()}");
    }
    
    /**
     * Construye la consulta DELETE
     *
     * @return string
     **/
    public function delete() 
    {
        return $this->_joinClausules("DELETE FROM {$this->_getSource()}");
    }
    
    /**
     * Construye la consulta UPDATE
     *
     * @param string | array $values claves/valores
     * @return string
     **/
    public function update($values) 
    {
        // para cuando se pasa por array
        if(is_array($values)) {
            $buff = array();
            foreach($values as $column => $value) {
                $buff[] = "$column=\"" . DbPool::factory($this->_connection)->quote($value) . '"'; 
            }
            $values = implode(', ', $buff);
        }
    
        return $this->_joinClausules("UPDATE {$this->_getSource()} SET $values");
    }
    
    /**
     * Construye la consulta UPDATE
     *
     * @param string | array $columns columnas, o array de claves/valores
     * @param string $values 
     * @return string
     **/
    public function insert($columns, $values=null) 
    {
        // para cuando se pasa por array
        if(is_array($columns)) {
            $buff = array();
            $values = array();
            
            foreach($columns as $column => $value) {
                $buff[] = $column;
                $values[] = '"' . DbPool::factory($this->_connection)->quote($value) . '"';
            }
            
            $columns = implode(', ', $buff);
            $values = implode(', ', $values);
        }
    
        return "INSERT INTO {$this->_getSource()} ($columns) VALUES ($values)";
    }
    
    /**
     * Obtiene instancia de query en funcion de la conexion
     *
     * @param string $connection conexion a base de datos en databases.ini
     * @return DbQuery
     * @throw KumbiaException
     **/
    public static function factory($connection)
    {
        // lee la configuracion de base de datos
        $databases = Config::read('databases.ini');
        if(!isset($databases[$connection])) {
            throw new KumbiaException("No existe la conexion $connection en databases.ini");
        }
    
        // genera el nombre de clase
        $class = ucfirst($databases['type']) . 'Query';
    
        // si no existe la clase la carga
        if(!class_exists($class, false)) {
            // carga la clase
            require CORE_PATH . "libs/ActiveRecord/db_pool/adapthers/{$databases['type']}_query.php";
        }
        
        return new $class($connection);
    }
}