Nooku Framework is an open source project powered by Assembla

Assembla offers free public and private SVN/Git repositories and project hosting with bug/issue tracking and collaboration tools.

nooku-framework

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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
/**
 * @version        $Id$
 * @category    Koowa
 * @package     Koowa_Database
 * @subpackage  Query
 * @copyright    Copyright (C) 2007 - 2010 Johan Janssens and Mathias Verraes. All rights reserved.
 * @license        GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
 * @link         http://www.koowa.org
 */

/**
 * Database Select Class for database select statement generation
 *
 * @author        Johan Janssens <johan@koowa.org>
 * @category    Koowa
 * @package     Koowa_Database
 * @subpackage  Query
 */
class KDatabaseQuery extends KObject
{
    /**
     * The operation to perform
     *
     * @var array
     */
    public $operation = '';

    /**
     * The columns
     *
     * @var array
     */
    public $columns = array();

    /**
     * The from element
     *
     * @var array
     */
    public $from = array();

    /**
     * The join element
     *
     * @var array
     */
    public $join = array();

    /**
     * The where element
     *
     * @var array
     */
    public $where = array();

    /**
     * The group element
     *
     * @var array
     */
    public $group = array();

    /**
     * The having element
     *
     * @var array
     */
    public $having = array();

    /**
     * The order element
     *
     * @var string
     */
    public $order = array();

    /**
     * The limit element
     *
     * @var integer
     */
    public $limit = 0;

    /**
     * The limit offset element
     *
     * @var integer
     */
    public $offset = 0;

    /**
     * Data to bind into the query as key => value pairs.
     *
     * @var array
     */
    protected $_bind = array();

    /**
     * Database connector
     *
     * @var        object
     */
    protected $_adapter;

    /**
     * Object constructor
     *
     * Can be overloaded/supplemented by the child class
     *
     * @param     object     An optional KConfig object with configuration options.
     */
    public function __construct( KConfig $config = null)
    {
        //If no config is passed create it
        if(!isset($config)) $config = new KConfig();

        parent::__construct($config);

        //set the model adapter
        $this->_adapter = $config->adapter;
    }


    /**
     * Initializes the options for the object
     *
     * @param     object     An optional KConfig object with configuration options.
     */
    protected function _initialize(KConfig $config)
    {
        $config->append(array(
            'adapter' => KFactory::get('lib.koowa.database')
        ));

        parent::_initialize($config);
    }

    /**
     * Gets the database adapter for this particular KDatabaseQuery object.
     *
     * @return KDatabaseAdapterInterface
     */
    public function getAdapter()
    {
        return $this->_adapter;
    }


    /**
     * Built a select query
     *
     * @param    array|string    A string or an array of column names
     * @return     KDatabaseQuery
     */
    public function select( $columns = '*')
    {
        settype($columns, 'array'); //force to an array

        //Quote the identifiers
        $columns = $this->_adapter->quoteName($columns);

        $this->operation = 'SELECT';
        $this->columns   = array_unique( array_merge( $this->columns, $columns ) );
        return $this;
    }

    /**
     * Built a count query
     *
     * @return KDatabaseQuery
     */
    public function count()
    {
        $this->operation = 'SELECT COUNT(*) ';
        $this->columns    = array();
        return $this;
    }

    /**
     * Make the query distinct
     *
     * @return KDatabaseQuery
     */
    public function distinct()
    {
        $this->operation = 'SELECT DISTINCT ';
        return $this;
    }

    /**
     * Built the from clause of the query
     *
     * @param    array|string    A string or array of table names
     * @return     KDatabaseQuery
     */
    public function from( $tables )
    {
        settype($tables, 'array'); //force to an array

        //Prepent the table prefix
        array_walk($tables, array($this, '_prefix'));

        //Quote the identifiers
        $tables = $this->_adapter->quoteName($tables);

        $this->from = array_unique( array_merge( $this->from, $tables ) );
        return $this;
    }

    /**
     * Built the join clause of the query
     *
     * @param string         The type of join; empty for a plain JOIN, or "LEFT", "INNER", etc.
     * @param string         The table name to join to.
     * @param string|array     Join on this condition.
     * @return KDatabaseQuery
     */
    public function join($type, $table, $condition)
    {
        settype($condition, 'array'); //force to an array

        $this->_prefix($table); //add a prefix to the table

        //Quote the identifiers
        $table     = $this->_adapter->quoteName($table);
        $condition = $this->_adapter->quoteName($condition);

        $this->join[] = array(
            'type'      => strtoupper($type),
            'table'     => $table,
            'condition' => $condition,
        );

        return $this;
    }

    /**
     * Built the where clause of the query
     *
     * @param   string             The name of the property the constraint applies too, or a SQL function or statement
     * @param    string          The comparison used for the constraint
     * @param    string|array    The value compared to the property value using the constraint
     * @param    string            The where condition, defaults to 'AND'
     * @return     KDatabaseQuery
     */
    public function where( $property, $constraint = null, $value = null, $condition = 'AND' )
    {
        if(empty($property)) {
            return $this;
        }

        // Apply quotes to the property name
        $where = $this->_adapter->quoteName($property);

        if(isset($constraint) && isset($value))
        {
            $constraint    = strtoupper($constraint);
            $condition    = strtoupper($condition);

            // Apply quotes to the value
            $value = $this->_adapter->quoteValue($value);

               //Create the where clause
            if(in_array($constraint, array('IN', 'NOT IN'))) {
                $value = ' ( '.$value. ' ) ';
            }

            $where = $where.' '.$constraint.' '.$value;
        }

        //Prepend the condition
        if(count($this->where)) {
            $where = $condition .' '. $where;
        }

        $this->where = array_unique( array_merge( $this->where, array($where) ));
        return $this;
    }

    /**
     * Built the group clause of the query
     *
     * @param    array|string    A string or array of ordering columns
     * @return     KDatabaseQuery
     */
    public function group( $columns )
    {
        settype($columns, 'array'); //force to an array

        //Quote the identifiers
        $columns = $this->_adapter->quoteName($columns);

        $this->group = array_unique( array_merge( $this->group, $columns));
        return $this;
    }

    /**
     * Built the having clause of the query
     *
     * @param    array|string    A string or array of ordering columns
     * @return     KDatabaseQuery
     */
    public function having( $columns )
    {
        settype($columns, 'array'); //force to an array

        //Quote the identifiers
        $columns = $this->_adapter->quoteName($columns);

        $this->having = array_unique( array_merge( $this->having, $columns ));
        return $this;
    }

    /**
     * Built the order clause of the query
     *
     * @param    array|string  A string or array of ordering columns
     * @param    string          Either DESC or ASC
     * @return     KDatabaseQuery
     */
    public function order( $columns, $direction = 'ASC' )
    {
        settype($columns, 'array'); //force to an array

        //Quote the identifiers
        $columns = $this->_adapter->quoteName($columns);

        foreach($columns as $column)
        {
            $this->order[] = array(
                'column'      => $column,
                'direction' => $direction
            );
        }

        return $this;
    }

    /**
     * Built the limit element of the query
     *
     * @param     integer Number of items to fetch.
     * @param     integer Offset to start fetching at.
     * @return     KDatabaseQuery
     */
    public function limit( $limit, $offset = 0 )
    {
        $this->limit  = $limit;
        $this->offset = $offset;
        return $this;
    }

    /**
     * Adds data to bind into the query.
     *
     * @param     mixed     The replacement key in the query.  If this is an
     *                     array or object, the $val parameter is ignored,
     *                     and all the key-value pairs in the array (or all
     *                   properties of the object) are added to the bind.
     * @param     mixed     The value to use for the replacement key.
     * @return     KDatabaseQuery
     */
    public function bind($key, $val = null)
    {
        if (is_array($key)) {
            $this->_bind = array_merge($this->_bind, $key);
        } elseif (is_object($key)) {
            $this->_bind = array_merge((array) $this->_bind, $key);
        } else {
            $this->_bind[$key] = $val;
        }

        return $this;
    }

    /**
     * Unsets bound data.
     *
     * @param     mixed     The key to unset.  If a string, unsets that one
     *                     bound value; if an array, unsets the list of values;
     *                     if empty, unsets all bound values (the default).
     * @return     KDatabaseQuery
     */
    public function unbind($spec = null)
    {
        if (empty($spec)) {
            $this->_bind = array();
        } else {
            settype($spec, 'array');
            foreach ($spec as $key) {
                unset($this->_bind[$key]);
            }
        }

        return $this;
    }

    /*
     * Callback for array_walk to prefix elements of array with given prefix
     *
     * @param string The data to be prefixed
     */
    protected function _prefix(&$data)
    {
        // Prepend the table modifier
        $prefix = $this->_adapter->getTablePrefix();
        $data = $prefix.$data;
    }

    /**
     * Render the query to a string
     *
     * @return    string    The completed query
     */
    public function __toString()
    {
        $query = '';

        $query .= $this->operation.PHP_EOL;

        if (!empty($this->columns)) {
            $query .= implode(' , ', $this->columns).PHP_EOL;
        }

        if (!empty($this->from)) {
            $query .= ' FROM '.implode(' , ', $this->from).PHP_EOL;
        }

        if (!empty($this->join))
        {
            $joins = array();
            foreach ($this->join as $join)
            {
                $tmp = '';

                if (! empty($join['type'])) {
                    $tmp .= $join['type'] . ' ';
                }

                $tmp .= 'JOIN ' . $join['table'];
                $tmp .= ' ON ' . implode(' AND ', $join['condition']);

                $joins[] = $tmp;
            }

            $query .= implode(PHP_EOL, $joins) .PHP_EOL;
        }

        if (!empty($this->where)) {
            $query .= ' WHERE '.implode(' ', $this->where).PHP_EOL;
        }

        if (!empty($this->group)) {
            $query .= ' GROUP BY '.implode(' , ', $this->group).PHP_EOL;
        }

        if (!empty($this->having)) {
            $query .= ' HAVING '.implode(' , ', $this->having).PHP_EOL;
        }

        if (!empty($this->order) )
        {
            $query .= 'ORDER BY ';

            $list = array();
            foreach ($this->order as $order) {
                $list[] = $order['column'].' '.$order['direction'];
            }

            $query .= implode(' , ', $list) . PHP_EOL;
        }

        if (!empty($this->limit)) {
            $query .= ' LIMIT '.$this->offset.' , '.$this->limit.PHP_EOL;
        }

        return $query;
    }
}
Ajax-loader Loading, please wait...