~desarrollokumbia/kumbia/ActiveRecord

« back to all changes in this revision

Viewing changes to db_pool/db_query.php

  • Committer: Emilio Silveira
  • Date: 2010-09-05 05:44:16 UTC
  • Revision ID: emilio.rst@gmail.com-20100905054416-k0zvmrxtvajdl59v
Adicionados metodos
delete y deleteByPK

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
        
55
56
    /**
56
57
     * Clausula WHERE con OR
57
58
     * 
63
64
        $this->_sql['where'][] = $this->_where($conditions, FALSE);
64
65
        return $this;
65
66
    }
 
67
        
66
68
    /**
67
69
     * Método interno para crear la Clusula WHERE
68
70
     * 
85
87
    /**
86
88
     * Parámetros que seran enlazados a la setencia SQL
87
89
     * 
 
90
     * @param array $bind
88
91
     * @return DbQuery
89
92
     */
90
93
    public function bind($bind)
91
94
    {
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
        
100
114
    /**
101
115
     * Retorna los elementos para ser enlazados
102
116
     * 
116
130
     * @param string $table nombre de tabla
117
131
     * @param string $conditions condiciones
118
132
     * @return DbQuery
119
 
     **/
 
133
     */
120
134
    public function join($table, $conditions) 
121
135
    {
122
136
        $this->_sql['join'][] = array('table' => $table, 'conditions' => $conditions);
129
143
     * @param string $table nombre de tabla
130
144
     * @param string $conditions condiciones
131
145
     * @return DbQuery
132
 
     **/
 
146
     */
133
147
    public function leftJoin($table, $conditions) 
134
148
    {
135
149
        $this->_sql['leftJoin'][] = array('table' => $table, 'conditions' => $conditions);
142
156
     * @param string $table nombre de tabla
143
157
     * @param string $conditions condiciones
144
158
     * @return DbQuery
145
 
     **/
 
159
     */
146
160
    public function rightJoin($table, $conditions) 
147
161
    {
148
162
        $this->_sql['rightJoin'][] = array('table' => $table, 'conditions' => $conditions);
155
169
     * @param string $table nombre de tabla
156
170
     * @param string $conditions condiciones
157
171
     * @return DbQuery
158
 
     **/
 
172
     */
159
173
    public function fullJoin($table, $conditions) 
160
174
    {
161
175
        $this->_sql['fullJoin'][] = array('table' => $table, 'conditions' => $conditions);
167
181
     *
168
182
     * @param string $table nombre de tabla
169
183
     * @return DbQuery
170
 
     **/
 
184
     */
171
185
    public function table($table) 
172
186
    {
173
187
        $this->_sql['table'] = $table;
179
193
     *
180
194
     * @param string $schema schema donde se ubica la tabla
181
195
     * @return DbQuery
182
 
     **/
 
196
     */
183
197
    public function schema($schema) 
184
198
    {
185
199
        $this->_sql['schema'] = $schema;
191
205
     *
192
206
     * @param string $criteria criterio de ordenamiento
193
207
     * @return DbQuery
194
 
     **/
 
208
     */
195
209
    public function order($criteria) 
196
210
    {
197
211
        $this->_sql['order'] = $criteria;
203
217
     *
204
218
     * @param string $columns columnas
205
219
     * @return DbQuery
206
 
     **/
 
220
     */
207
221
    public function group($columns) 
208
222
    {
209
223
        $this->_sql['group'] = $columns;
215
229
     *
216
230
     * @param string $conditions condiciones
217
231
     * @return DbQuery
218
 
     **/
 
232
     */
219
233
    public function having($conditions) 
220
234
    {
221
235
        $this->_sql['having'] = $conditions;
227
241
     *
228
242
     * @param int $limit
229
243
     * @return DbQuery
230
 
     **/
 
244
     */
231
245
    public function limit($limit) 
232
246
    {
233
247
        $this->_sql['limit'] = $limit;
239
253
     *
240
254
     * @param int $offset
241
255
     * @return DbQuery
242
 
     **/
 
256
     */
243
257
    public function offset($offset) 
244
258
    {
245
259
        $this->_sql['offset'] = $offset;
251
265
     *
252
266
     * @param string $columns columnas
253
267
     * @return DbQuery
254
 
     **/
255
 
    public function select($columns='*') 
 
268
     */
 
269
    public function select($columns = NULL) 
256
270
    {
257
 
        $this->_sql['select'] = $columns;
 
271
        $this->_sql['command'] = 'select';
 
272
                
 
273
                if($columns) {
 
274
                        $this->columns($columns);
 
275
                }
 
276
        
258
277
        return $this;
259
278
    }
260
279
    /**
261
280
     * Columnas a utilizar en el Query
 
281
         *
 
282
         * @param string $columns columnas
262
283
     * @return DbQuery
263
284
     */
264
285
    public function columns($columns)
265
286
    {
266
 
        $this->select($columns);
267
 
        return $this;
 
287
        $this->_sql['columns'] = $columns;
 
288
                return $this;
268
289
    }
 
290
        
269
291
    /**
270
292
     * Construye la consulta DELETE
271
293
     *
272
294
     * @return DbQuery
273
 
     **/
 
295
     */
274
296
    public function delete() 
275
297
    {
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);
 
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';
302
327
        return $this;
303
328
    }
304
329
    
306
331
     * Obtiene el array base con las partes de la consulta SQL
307
332
     *
308
333
     * @return array
309
 
     **/
 
334
     */
310
335
    public function getSqlArray()
311
336
    {
312
337
        return $this->_sql;