~desarrollokumbia/kumbia/ActiveRecord

« back to all changes in this revision

Viewing changes to db_pool/db_query.php

  • Committer: Deivinson Tejeda
  • Date: 2010-01-24 20:53:08 UTC
  • Revision ID: deivinsontejeda@gmail.com-20100124205308-hsovf0apo3pbg3s0
Mejorando el chain where, quedan ahora los metodos where() y whereOr y se puede concatenar los where, ademas se crea un método interno para el DbQuery quien ira armando el los where en el orden como se vayan llamando y para dejar mas facil el where y whereOr solo reciben string asi no liamos a los usarios con array, sin embargo seguimos evaluando
Eje...
    public function consulta()
    {
        $this->get()->where('id < :id2')
                    ->where('id > :id')
                    ->bind(array(':id'=>2, ':id2'=>5));
        return $this->find();
    }
la consulta generada del anterios chain es:
SELECT * FROM prueba WHERE  (id < :id2)  AND (id > :id)

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
     * Partes de la consulta sql
27
27
     *
28
28
     * @var array
29
 
     */
 
29
     **/
30
30
    protected $_sql = array();
31
31
 
32
32
    /**
34
34
     *
35
35
     * @param boolean $distinct
36
36
     * @return DbQuery
37
 
     */
 
37
     **/
38
38
    public function distinct($distinct) 
39
39
    {
40
40
        $this->_sql['distinct'] = $distinct;
52
52
        $this->_sql['where'][] = $this->_where($conditions);
53
53
        return $this;
54
54
    }
55
 
        
56
55
    /**
57
56
     * Clausula WHERE con OR
58
57
     * 
64
63
        $this->_sql['where'][] = $this->_where($conditions, FALSE);
65
64
        return $this;
66
65
    }
67
 
        
68
66
    /**
69
67
     * Método interno para crear la Clusula WHERE
70
68
     * 
87
85
    /**
88
86
     * Parámetros que seran enlazados a la setencia SQL
89
87
     * 
90
 
     * @param array $bind
91
88
     * @return DbQuery
92
89
     */
93
90
    public function bind($bind)
94
91
    {
 
92
        if(!is_array($bind)){
 
93
            throw new KumbiaException('Los parámetros para enlazar a la sentencia SQL debe ser un array');
 
94
        }
95
95
        foreach ($bind as $k => $v) {
96
 
                $this->_sql['bind'][":$k"] = $v;
 
96
                $this->_sql['bind'][$k] = $v;
97
97
        }
98
98
        return $this;
99
99
    }
100
 
        
101
 
    /**
102
 
     * Parámetro que sera enlazado a la setencia SQL
103
 
     * 
104
 
     * @param string $bind
105
 
         * @param string $value
106
 
     * @return DbQuery
107
 
     */
108
 
    public function bindValue($bind, $value)
109
 
    {
110
 
        $this->_sql['bind'][":$bind"] = $value;
111
 
        return $this;
112
 
    }
113
 
        
114
100
    /**
115
101
     * Retorna los elementos para ser enlazados
116
102
     * 
130
116
     * @param string $table nombre de tabla
131
117
     * @param string $conditions condiciones
132
118
     * @return DbQuery
133
 
     */
 
119
     **/
134
120
    public function join($table, $conditions) 
135
121
    {
136
122
        $this->_sql['join'][] = array('table' => $table, 'conditions' => $conditions);
143
129
     * @param string $table nombre de tabla
144
130
     * @param string $conditions condiciones
145
131
     * @return DbQuery
146
 
     */
 
132
     **/
147
133
    public function leftJoin($table, $conditions) 
148
134
    {
149
135
        $this->_sql['leftJoin'][] = array('table' => $table, 'conditions' => $conditions);
156
142
     * @param string $table nombre de tabla
157
143
     * @param string $conditions condiciones
158
144
     * @return DbQuery
159
 
     */
 
145
     **/
160
146
    public function rightJoin($table, $conditions) 
161
147
    {
162
148
        $this->_sql['rightJoin'][] = array('table' => $table, 'conditions' => $conditions);
169
155
     * @param string $table nombre de tabla
170
156
     * @param string $conditions condiciones
171
157
     * @return DbQuery
172
 
     */
 
158
     **/
173
159
    public function fullJoin($table, $conditions) 
174
160
    {
175
161
        $this->_sql['fullJoin'][] = array('table' => $table, 'conditions' => $conditions);
181
167
     *
182
168
     * @param string $table nombre de tabla
183
169
     * @return DbQuery
184
 
     */
 
170
     **/
185
171
    public function table($table) 
186
172
    {
187
173
        $this->_sql['table'] = $table;
193
179
     *
194
180
     * @param string $schema schema donde se ubica la tabla
195
181
     * @return DbQuery
196
 
     */
 
182
     **/
197
183
    public function schema($schema) 
198
184
    {
199
185
        $this->_sql['schema'] = $schema;
205
191
     *
206
192
     * @param string $criteria criterio de ordenamiento
207
193
     * @return DbQuery
208
 
     */
 
194
     **/
209
195
    public function order($criteria) 
210
196
    {
211
197
        $this->_sql['order'] = $criteria;
217
203
     *
218
204
     * @param string $columns columnas
219
205
     * @return DbQuery
220
 
     */
 
206
     **/
221
207
    public function group($columns) 
222
208
    {
223
209
        $this->_sql['group'] = $columns;
229
215
     *
230
216
     * @param string $conditions condiciones
231
217
     * @return DbQuery
232
 
     */
 
218
     **/
233
219
    public function having($conditions) 
234
220
    {
235
221
        $this->_sql['having'] = $conditions;
241
227
     *
242
228
     * @param int $limit
243
229
     * @return DbQuery
244
 
     */
 
230
     **/
245
231
    public function limit($limit) 
246
232
    {
247
233
        $this->_sql['limit'] = $limit;
253
239
     *
254
240
     * @param int $offset
255
241
     * @return DbQuery
256
 
     */
 
242
     **/
257
243
    public function offset($offset) 
258
244
    {
259
245
        $this->_sql['offset'] = $offset;
265
251
     *
266
252
     * @param string $columns columnas
267
253
     * @return DbQuery
268
 
     */
269
 
    public function select($columns = NULL) 
 
254
     **/
 
255
    public function select($columns='*') 
270
256
    {
271
 
        $this->_sql['command'] = 'select';
272
 
                
273
 
                if($columns) {
274
 
                        $this->columns($columns);
275
 
                }
276
 
        
 
257
        $this->_sql['select'] = $columns;
277
258
        return $this;
278
259
    }
279
260
    /**
280
261
     * Columnas a utilizar en el Query
281
 
         *
282
 
         * @param string $columns columnas
283
262
     * @return DbQuery
284
263
     */
285
264
    public function columns($columns)
286
265
    {
287
 
        $this->_sql['columns'] = $columns;
288
 
                return $this;
 
266
        $this->select($columns);
 
267
        return $this;
289
268
    }
290
 
        
291
269
    /**
292
270
     * Construye la consulta DELETE
293
271
     *
294
272
     * @return DbQuery
295
 
     */
 
273
     **/
296
274
    public function delete() 
297
275
    {
298
 
        $this->_sql['command'] = 'delete';
299
 
        return $this;
300
 
    }
301
 
 
302
 
    /**
303
 
     * Construye la consulta UPDATE
304
 
     *
305
 
     * @param array $data claves/valores
306
 
     * @return DbQuery
307
 
     */
308
 
    public function update($data) 
309
 
    {
310
 
        $this->bind($data);
311
 
        $this->_sql['data'] = $data;
312
 
                $this->_sql['command'] = 'update';
313
 
        return $this;
314
 
    }
315
 
    
316
 
    /**
317
 
     * Construye la consulta UPDATE
318
 
     *
319
 
     * @param string | array $data columnas, o array de claves/valores
320
 
     * @return DbQuery
321
 
     */
322
 
    public function insert($data) 
323
 
    {
324
 
        $this->bind($data);
325
 
        $this->_sql['data'] = $data;
326
 
                $this->_sql['command'] = 'insert';
 
276
        $this->_sql['delete'] = TRUE;
 
277
        return $this;
 
278
    }
 
279
    
 
280
    /**
 
281
     * Construye la consulta UPDATE
 
282
     *
 
283
     * @param string | array $values claves/valores
 
284
     * @return DbQuery
 
285
     **/
 
286
    public function update($values) 
 
287
    {
 
288
        $this->_sql['update'] = $values;
 
289
        return $this;
 
290
    }
 
291
    
 
292
    /**
 
293
     * Construye la consulta UPDATE
 
294
     *
 
295
     * @param string | array $columns columnas, o array de claves/valores
 
296
     * @param string $values 
 
297
     * @return DbQuery
 
298
     **/
 
299
    public function insert($columns, $values=null) 
 
300
    {
 
301
        $this->_sql['insert'] = array('columns' => $columns, 'values' => $values);
327
302
        return $this;
328
303
    }
329
304
    
331
306
     * Obtiene el array base con las partes de la consulta SQL
332
307
     *
333
308
     * @return array
334
 
     */
 
309
     **/
335
310
    public function getSqlArray()
336
311
    {
337
312
        return $this->_sql;