Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Groupby.php 7490 2010-03-29 19:53:27Z jwage $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Doctrine_Query_Groupby
24
 *
25
 * @package     Doctrine
26
 * @subpackage  Query
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28
 * @link        www.doctrine-project.org
29
 * @since       1.0
30
 * @version     $Revision: 7490 $
31
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
32
 */
33
class Doctrine_Query_Groupby extends Doctrine_Query_Part
34
{
35
    /**
36
     * DQL GROUP BY PARSER
37
     * parses the group by part of the query string
38
     *
39
     * @param string $str
40
     * @return void
41
     */
42
    public function parse($clause, $append = false)
43
    {
44
        $terms = $this->_tokenizer->clauseExplode($clause, array(' ', '+', '-', '*', '/', '<', '>', '=', '>=', '<='));
45
        $str = '';
46
 
47
        foreach ($terms as $term) {
48
            $pos = strpos($term[0], '(');
49
            $hasComma = false;
50
 
51
            if ($pos !== false) {
52
                $name = substr($term[0], 0, $pos);
53
 
54
                $term[0] = $this->query->parseFunctionExpression($term[0]);
55
            } else {
56
                if (substr($term[0], 0, 1) !== "'" && substr($term[0], -1) !== "'") {
57
 
58
                    if (strpos($term[0], '.') !== false) {
59
                        if ( ! is_numeric($term[0])) {
60
                            $e = explode('.', $term[0]);
61
 
62
                            $field = array_pop($e);
63
 
64
                            // Check if field name still has comma
65
                            if (($pos = strpos($field, ',')) !== false) {
66
                                $field = substr($field, 0, $pos);
67
                                $hasComma = true;
68
                            }
69
 
70
                            // Grab query connection
71
                            $conn = $this->query->getConnection();
72
 
73
                            if ($this->query->getType() === Doctrine_Query::SELECT) {
74
                                $componentAlias = implode('.', $e);
75
 
76
                                if (empty($componentAlias)) {
77
                                    $componentAlias = $this->query->getRootAlias();
78
                                }
79
 
80
                                $this->query->load($componentAlias);
81
 
82
                                // check the existence of the component alias
83
                                $queryComponent = $this->query->getQueryComponent($componentAlias);
84
 
85
                                $table = $queryComponent['table'];
86
 
87
                                $def = $table->getDefinitionOf($field);
88
 
89
                                // get the actual field name from alias
90
                                $field = $table->getColumnName($field);
91
 
92
                                // check column existence
93
                                if ( ! $def) {
94
                                    throw new Doctrine_Query_Exception('Unknown column ' . $field);
95
                                }
96
 
97
                                if (isset($def['owner'])) {
98
                                    $componentAlias = $componentAlias . '.' . $def['owner'];
99
                                }
100
 
101
                                $tableAlias = $this->query->getSqlTableAlias($componentAlias);
102
 
103
                                // build sql expression
104
                                $term[0] = $conn->quoteIdentifier($tableAlias) . '.' . $conn->quoteIdentifier($field);
105
                            } else {
106
                                // build sql expression
107
                                $field = $this->query->getRoot()->getColumnName($field);
108
                                $term[0] = $conn->quoteIdentifier($field);
109
                            }
110
                        }
111
                    } else {
112
                        if ( ! empty($term[0]) &&
113
                             ! is_numeric($term[0]) &&
114
                            $term[0] !== '?' && substr($term[0], 0, 1) !== ':') {
115
 
116
                            $componentAlias = $this->query->getRootAlias();
117
 
118
                            $found = false;
119
 
120
                            // Check if field name still has comma
121
                            if (($pos = strpos($term[0], ',')) !== false) {
122
                                $term[0] = substr($term[0], 0, $pos);
123
                                $hasComma = true;
124
                            }
125
 
126
                            if ($componentAlias !== false &&
127
                                $componentAlias !== null) {
128
                                $queryComponent = $this->query->getQueryComponent($componentAlias);
129
 
130
                                $table = $queryComponent['table'];
131
 
132
                                // check column existence
133
                                if ($table->hasField($term[0])) {
134
                                    $found = true;
135
 
136
                                    $def = $table->getDefinitionOf($term[0]);
137
 
138
                                    // get the actual column name from field name
139
                                    $term[0] = $table->getColumnName($term[0]);
140
 
141
 
142
                                    if (isset($def['owner'])) {
143
                                        $componentAlias = $componentAlias . '.' . $def['owner'];
144
                                    }
145
 
146
                                    $tableAlias = $this->query->getSqlTableAlias($componentAlias);
147
                                    $conn = $this->query->getConnection();
148
 
149
                                    if ($this->query->getType() === Doctrine_Query::SELECT) {
150
                                        // build sql expression
151
                                        $term[0] = $conn->quoteIdentifier($tableAlias)
152
                                                 . '.' . $conn->quoteIdentifier($term[0]);
153
                                    } else {
154
                                        // build sql expression
155
                                        $term[0] = $conn->quoteIdentifier($term[0]);
156
                                    }
157
                                } else {
158
                                    $found = false;
159
                                }
160
                            }
161
 
162
                            if ( ! $found) {
163
                                $term[0] = $this->query->getSqlAggregateAlias($term[0]);
164
                            }
165
                        }
166
                    }
167
                }
168
            }
169
 
170
            $str .= $term[0] . ($hasComma ? ',' : '') . $term[1];
171
        }
172
 
173
        return $str;
174
    }
175
}