~desarrollokumbia/kumbia/ActiveRecord

« back to all changes in this revision

Viewing changes to db_pool/db_query.php

  • Committer: Deivinson Tejeda
  • Date: 2010-01-22 22:44:49 UTC
  • Revision ID: deivinsontejeda@gmail.com-20100122224449-8a03r26ppfuz3963
1->Arreglando el find() para que funcione con y sin chain
2->En el DbQuery 
        ->En el método where se recibe un array para realizar las consultas preparadas (Prepared Statement)
        ->se agrega método para obetener los params para enlazar al SQL
        ->Se agrega método column para indicar las columnas que se quiere en la consulta

3->En el DbAdapter en la construcción del Query en el where se hace un implode directo ya que la consulta es preparada

Ejemplo
//Modelo
    public function buscar()
    {
        $this->get()->columns('nombre, id')
                    ->where(array('id > ?'=>2, 'id<?'=>5))
                    ->limit(2);

        //ejecuta un find() personalizado via chain
        return $this->find();
                         
    }

//Otra forma
    public function buscar()
    {
        //ejecuta un find() por defecto
        return $this->find();
    }

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;
42
42
    }
43
43
 
44
44
    /**
45
 
     * Clausula WHERE con AND
 
45
     * Clausula WHERE
46
46
     *
47
 
     * @param string $conditions condiciones AND
 
47
     * @param string | array $conditions condiciones
48
48
     * @return DbQuery
49
 
     */
 
49
     **/
50
50
    public function where($conditions) 
51
51
    {
52
 
        $this->_sql['where'][] = $this->_where($conditions);
53
 
        return $this;
54
 
    }
55
 
        
56
 
    /**
57
 
     * Clausula WHERE con OR
58
 
     * 
59
 
     * @param string $conditions condiciones OR
60
 
     * @return DbQuery
61
 
     */
62
 
    public function whereOr($conditions)
63
 
    {
64
 
        $this->_sql['where'][] = $this->_where($conditions, FALSE);
65
 
        return $this;
66
 
    }
67
 
        
68
 
    /**
69
 
     * Método interno para crear la Clusula WHERE
70
 
     * 
71
 
     * @param string $conditions
72
 
     * @param bool   $type TRUE = AND; FALSE = OR
73
 
     * @return string clausula
74
 
     */
75
 
    protected function _where($conditions, $type=TRUE)
76
 
    {
77
 
        $cond=NULL;
78
 
        if(isset($this->_sql['where'])){
79
 
            if($type===TRUE){
80
 
                $cond = ' AND ';
81
 
            }else{
82
 
                $cond = ' OR ';
 
52
        $where = array();
 
53
        if(is_array($conditions)){
 
54
            foreach ($conditions as $k => $v) {
 
55
                $this->_sql['params'][] = $v;
 
56
                $where[] = $k;
83
57
            }
84
58
        }
85
 
        return $cond . "($conditions)";
86
 
    }
87
 
    /**
88
 
     * Parámetros que seran enlazados a la setencia SQL
89
 
     * 
90
 
     * @param array $bind
91
 
     * @return DbQuery
92
 
     */
93
 
    public function bind($bind)
94
 
    {
95
 
        foreach ($bind as $k => $v) {
96
 
                $this->_sql['bind'][":$k"] = $v;
97
 
        }
98
 
        return $this;
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
 
    /**
115
 
     * Retorna los elementos para ser enlazados
116
 
     * 
117
 
     * @return array
118
 
     */
119
 
    public function getBind()
120
 
    {
121
 
        if(isset($this->_sql['bind'])){
122
 
            return $this->_sql['bind'];
 
59
        $this->_sql['where'] = $where;
 
60
        return $this;
 
61
    }
 
62
    /**
 
63
     * Parametros que seran enlazados a la setencia SQL
 
64
     * 
 
65
     * @return Array
 
66
     */
 
67
    public function params()
 
68
    {
 
69
        if(isset($this->_sql['params'])){
 
70
            return $this->_sql['params'];
123
71
        }
124
72
        return NULL;
125
73
    }
130
78
     * @param string $table nombre de tabla
131
79
     * @param string $conditions condiciones
132
80
     * @return DbQuery
133
 
     */
 
81
     **/
134
82
    public function join($table, $conditions) 
135
83
    {
136
84
        $this->_sql['join'][] = array('table' => $table, 'conditions' => $conditions);
143
91
     * @param string $table nombre de tabla
144
92
     * @param string $conditions condiciones
145
93
     * @return DbQuery
146
 
     */
 
94
     **/
147
95
    public function leftJoin($table, $conditions) 
148
96
    {
149
97
        $this->_sql['leftJoin'][] = array('table' => $table, 'conditions' => $conditions);
156
104
     * @param string $table nombre de tabla
157
105
     * @param string $conditions condiciones
158
106
     * @return DbQuery
159
 
     */
 
107
     **/
160
108
    public function rightJoin($table, $conditions) 
161
109
    {
162
110
        $this->_sql['rightJoin'][] = array('table' => $table, 'conditions' => $conditions);
169
117
     * @param string $table nombre de tabla
170
118
     * @param string $conditions condiciones
171
119
     * @return DbQuery
172
 
     */
 
120
     **/
173
121
    public function fullJoin($table, $conditions) 
174
122
    {
175
123
        $this->_sql['fullJoin'][] = array('table' => $table, 'conditions' => $conditions);
181
129
     *
182
130
     * @param string $table nombre de tabla
183
131
     * @return DbQuery
184
 
     */
 
132
     **/
185
133
    public function table($table) 
186
134
    {
187
135
        $this->_sql['table'] = $table;
193
141
     *
194
142
     * @param string $schema schema donde se ubica la tabla
195
143
     * @return DbQuery
196
 
     */
 
144
     **/
197
145
    public function schema($schema) 
198
146
    {
199
147
        $this->_sql['schema'] = $schema;
205
153
     *
206
154
     * @param string $criteria criterio de ordenamiento
207
155
     * @return DbQuery
208
 
     */
 
156
     **/
209
157
    public function order($criteria) 
210
158
    {
211
159
        $this->_sql['order'] = $criteria;
217
165
     *
218
166
     * @param string $columns columnas
219
167
     * @return DbQuery
220
 
     */
 
168
     **/
221
169
    public function group($columns) 
222
170
    {
223
171
        $this->_sql['group'] = $columns;
229
177
     *
230
178
     * @param string $conditions condiciones
231
179
     * @return DbQuery
232
 
     */
 
180
     **/
233
181
    public function having($conditions) 
234
182
    {
235
183
        $this->_sql['having'] = $conditions;
241
189
     *
242
190
     * @param int $limit
243
191
     * @return DbQuery
244
 
     */
 
192
     **/
245
193
    public function limit($limit) 
246
194
    {
247
195
        $this->_sql['limit'] = $limit;
253
201
     *
254
202
     * @param int $offset
255
203
     * @return DbQuery
256
 
     */
 
204
     **/
257
205
    public function offset($offset) 
258
206
    {
259
207
        $this->_sql['offset'] = $offset;
265
213
     *
266
214
     * @param string $columns columnas
267
215
     * @return DbQuery
268
 
     */
269
 
    public function select($columns = NULL) 
 
216
     **/
 
217
    public function select($columns='*') 
270
218
    {
271
 
        $this->_sql['command'] = 'select';
272
 
                
273
 
                if($columns) {
274
 
                        $this->columns($columns);
275
 
                }
276
 
        
 
219
        $this->_sql['select'] = $columns;
277
220
        return $this;
278
221
    }
279
222
    /**
280
223
     * Columnas a utilizar en el Query
281
 
         *
282
 
         * @param string $columns columnas
283
224
     * @return DbQuery
284
225
     */
285
226
    public function columns($columns)
286
227
    {
287
 
        $this->_sql['columns'] = $columns;
288
 
                return $this;
 
228
        $this->select($columns);
 
229
        return $this;
289
230
    }
290
 
        
291
231
    /**
292
232
     * Construye la consulta DELETE
293
233
     *
294
234
     * @return DbQuery
295
 
     */
 
235
     **/
296
236
    public function delete() 
297
237
    {
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';
 
238
        $this->_sql['delete'] = TRUE;
 
239
        return $this;
 
240
    }
 
241
    
 
242
    /**
 
243
     * Construye la consulta UPDATE
 
244
     *
 
245
     * @param string | array $values claves/valores
 
246
     * @return DbQuery
 
247
     **/
 
248
    public function update($values) 
 
249
    {
 
250
        $this->_sql['update'] = $values;
 
251
        return $this;
 
252
    }
 
253
    
 
254
    /**
 
255
     * Construye la consulta UPDATE
 
256
     *
 
257
     * @param string | array $columns columnas, o array de claves/valores
 
258
     * @param string $values 
 
259
     * @return DbQuery
 
260
     **/
 
261
    public function insert($columns, $values=null) 
 
262
    {
 
263
        $this->_sql['insert'] = array('columns' => $columns, 'values' => $values);
327
264
        return $this;
328
265
    }
329
266
    
331
268
     * Obtiene el array base con las partes de la consulta SQL
332
269
     *
333
270
     * @return array
334
 
     */
 
271
     **/
335
272
    public function getSqlArray()
336
273
    {
337
274
        return $this->_sql;