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: Set.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
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_Set extends Doctrine_Query_Part
34
{
35
    public function parse($dql)
36
    {
37
	    $terms = $this->_tokenizer->sqlExplode($dql, ' ');
38
        $termsTranslation = array();
39
 
40
        foreach ($terms as $term) {
41
	        $termOriginal = $term;
42
 
43
	        // We need to check for agg functions here
44
            $matches = array();
45
            $hasAggExpression = $this->_processPossibleAggExpression($term, $matches);
46
 
47
            $lftExpr = (($hasAggExpression) ? $matches[1] . '(' : '');
48
            $rgtExpr = (($hasAggExpression) ? $matches[3] . ')' : '');
49
 
50
	        preg_match_all("/^([a-zA-Z0-9_]+[\.[a-zA-Z0-9_]+]*)(\sAS\s[a-zA-Z0-9_]+)?/i", $term, $m, PREG_SET_ORDER);
51
 
52
            if (isset($m[0])) {
53
                $processed = array();
54
 
55
                foreach ($m as $piece) {
56
                    $part = $piece[1];
57
                    $e = explode('.', trim($part));
58
 
59
                    $fieldName  = array_pop($e);
60
                    $reference  = (count($e) > 0) ? implode('.', $e) : $this->query->getRootAlias();
61
                    $aliasMap   = $this->query->getQueryComponent($reference);
62
 
63
                    if ($aliasMap['table']->hasField($fieldName)) {
64
	                    $columnName = $aliasMap['table']->getColumnName($fieldName);
65
                        $columnName = $aliasMap['table']->getConnection()->quoteIdentifier($columnName);
66
 
67
                        $part = $columnName;
68
                    }
69
 
70
                    $processed[] = $part . (isset($piece[2]) ? $piece[2] : '');
71
                }
72
 
73
                $termsTranslation[$termOriginal] = $lftExpr . implode(' ', $processed) . $rgtExpr;
74
            }
75
        }
76
 
77
        return strtr($dql, $termsTranslation);
78
    }
79
 
80
 
81
    protected function _processPossibleAggExpression(& $expr, & $matches = array())
82
    {
83
        $hasAggExpr = preg_match('/(.*[^\s\(\=])\(([^\)]*)\)(.*)/', $expr, $matches);
84
 
85
        if ($hasAggExpr) {
86
            $expr = $matches[2];
87
 
88
            // We need to process possible comma separated items
89
            if (substr(trim($matches[3]), 0, 1) == ',') {
90
                $xplod = $this->_tokenizer->sqlExplode(trim($matches[3], ' )'), ',');
91
 
92
                $matches[3] = array();
93
 
94
                foreach ($xplod as $part) {
95
                    if ($part != '') {
96
                        $matches[3][] = $this->parseLiteralValue($part);
97
                    }
98
                }
99
 
100
                $matches[3] = '), ' . implode(', ', $matches[3]);
101
            }
102
        }
103
 
104
        return $hasAggExpr;
105
    }
106
}