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

Commit 4590

User picture
  • Author: johanjanssens
  • 2012-04-03 21:01 (about 2 years ago)

re #121 : Re-added KDatabaseQuerySelect::count property and count() function to make the SELECT query perform a count operation. Allows to easily check if the query is a count query or not.

Files Affected

 
45894590
19
 */
19
 */
20
class KDatabaseQuerySelect extends KDatabaseQueryAbstract
20
class KDatabaseQuerySelect extends KDatabaseQueryAbstract
21
{   
21
{   
 
 
22
    /**
 
 
23
     * Count operation
 
 
24
     *
 
 
25
     * @var boolean
 
 
26
     */
 
 
27
    public $count      = false;
 
 
28
 
22
    /**
29
    /**
23
     * Distinct operation
30
     * Distinct operation
24
     * 
31
     * 
...
 
...
 
63
 
70
 
64
    /**
71
    /**
65
     * The having element
72
     * The having element
 
 
73
     * 
 
 
74
     * @var array
66
     */
75
     */
67
    public $having = array();
76
    public $having = array();
68
 
77
 
...
 
...
 
124
        $this->distinct = true;
133
        $this->distinct = true;
125
        return $this;
134
        return $this;
126
    }
135
    }
 
 
136
 
 
 
137
    /**
 
 
138
     * Built a count query
 
 
139
     *
 
 
140
     * @return KDatabaseQuery
 
 
141
     */
 
 
142
    public function count()
 
 
143
    {
 
 
144
        $this->count   = true;
 
 
145
        $this->columns = array();
 
 
146
        return $this;
 
 
147
    }
127
 
148
 
128
    /**
149
    /**
129
     * Built the from clause of the query
150
     * Built the from clause of the query
...
 
...
 
276
        $prefix  = $adapter->getTablePrefix();
297
        $prefix  = $adapter->getTablePrefix();
277
        $query   = 'SELECT';
298
        $query   = 'SELECT';
278
 
299
 
279
        if ($this->columns) 
300
        if ($this->columns || $this->count) 
280
        {
301
        {
281
            if ($this->distinct) {
302
            if ($this->distinct) {
282
                $query .= ' DISTINCT';
303
                $query .= ' DISTINCT';
283
            }
304
            }
284
 
305
 
285
            $columns = array();
306
            if(!$this->count) 
286
            foreach($this->columns as $alias => $column) 
 
 
287
            {
307
            {
288
                if ($column instanceof KDatabaseQuerySelect) {
308
                $columns = array();
289
                    $columns[] = '('.$column.')'.(is_string($alias) ? ' AS '.$adapter->quoteIdentifier($alias) : '');
309
                foreach($this->columns as $alias => $column) 
290
                } else {
310
                {
291
                    $columns[] = $adapter->quoteIdentifier($column.(is_string($alias) ? ' AS '.$alias : ''));
311
                    if ($column instanceof KDatabaseQuerySelect) {
 
 
312
                        $columns[] = '('.$column.')'.(is_string($alias) ? ' AS '.$adapter->quoteIdentifier($alias) : '');
 
 
313
                    } else {
 
 
314
                        $columns[] = $adapter->quoteIdentifier($column.(is_string($alias) ? ' AS '.$alias : ''));
 
 
315
                    }
292
                }
316
                }
 
 
317
 
 
 
318
                $query .= ' '.implode(', ', $columns);
293
            }
319
            }
294
 
320
            else $query .= ' COUNT(*)';
295
            $query .= ' '.implode(', ', $columns);
 
 
296
        }
321
        }
297
 
322
 
298
        if ($this->from) 
323
        if ($this->from) 
45894590
573
 
573
 
574
        if($query instanceof KDatabaseQuerySelect)
574
        if($query instanceof KDatabaseQuerySelect)
575
        {
575
        {
576
            $query->columns(array('count' => 'COUNT(*)'));
576
            $query->count();
577
 
577
 
578
            if(!count($query->from)) {
578
            if(!count($query->from)) {
579
                $query->from(array('tbl' => $this->getName()));
579
                $query->from(array('tbl' => $this->getName()));