RawSql.php 7.73 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
21
Doctrine::autoload('Doctrine_Hydrate');
22 23 24
/**
 * Doctrine_RawSql
 *
25 26 27 28 29 30 31 32
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
33 34 35 36 37 38 39 40 41 42 43 44 45 46
class Doctrine_RawSql extends Doctrine_Hydrate {
    /**
     * @var array $fields
     */
    private $fields;
    /**
     * __call
     * method overloader
     *
     * @param string $name
     * @param array $args
     * @return Doctrine_RawSql
     */
    public function __call($name, $args) {
47
        if( ! isset($this->parts[$name]))
48
            throw new Doctrine_RawSql_Exception("Unknown overload method $name. Availible overload methods are ".implode(" ",array_keys($this->parts)));
49 50 51 52 53 54

        if($name == 'select') {
            preg_match_all('/{([^}{]*)}/U', $args[0], $m);

            $this->fields = $m[1];
            $this->parts["select"] = array();
55
        } else
56 57 58 59
            $this->parts[$name][] = $args[0];

        return $this;
    }
zYne's avatar
zYne committed
60 61 62 63
    /**
     * get
     */
    public function get($name) {
64 65
        if( ! isset($this->parts[$name]))
            throw new Doctrine_RawSql_Exception('Unknown query part '.$name);
zYne's avatar
zYne committed
66 67 68
            
        return $this->parts[$name];
    }
69 70 71 72
    /**
     * parseQuery
     *
     * @param string $query
doctrine's avatar
doctrine committed
73
     * @return Doctrine_RawSql
74 75
     */
    public function parseQuery($query) {
76
        preg_match_all('/{([^}{]*)}/U', $query, $m);
77 78 79 80

        $this->fields = $m[1];
        $this->clear();

81
        $e = Doctrine_Query::sqlExplode($query,' ');
82 83 84 85 86 87 88 89 90 91 92

        foreach($e as $k => $part):
            $low = strtolower($part);
            switch(strtolower($part)):
                case "select":
                case "from":
                case "where":
                case "limit":
                case "offset":
                case "having":
                    $p = $low;
zYne's avatar
zYne committed
93 94
                    if( ! isset($parts[$low]))
                        $parts[$low] = array();
zYne's avatar
zYne committed
95

96 97 98 99 100
                break;
                case "order":
                case "group":
                    $i = ($k + 1);
                    if(isset($e[$i]) && strtolower($e[$i]) === "by") {
101 102 103 104
                        $p = $low;
                        $p .= "by";
                        $parts[$low."by"] = array();

105 106 107 108 109 110
                    } else
                        $parts[$p][] = $part;
                break;
                case "by":
                    continue;
                default:
zYne's avatar
zYne committed
111 112 113 114
                    if( ! isset($parts[$p][0])) 
                        $parts[$p][0] = $part;
                    else
                        $parts[$p][0] .= ' '.$part;
115 116 117 118 119
            endswitch;
        endforeach;

        $this->parts = $parts;
        $this->parts["select"] = array();
doctrine's avatar
doctrine committed
120 121
        
        return $this;
122 123 124
    }
    /**
     * getQuery
125 126
     *
     *
127 128 129 130 131 132
     * @return string
     */
    public function getQuery() {
        foreach($this->fields as $field) {
            $e = explode(".", $field);
            if( ! isset($e[1]))
133
                throw new Doctrine_RawSql_Exception("All selected fields in Sql query must be in format tableAlias.fieldName");
134

135 136 137
            if( ! isset($this->tables[$e[0]])) {
                try {
                    $this->addComponent($e[0], ucwords($e[0]));
138
                } catch(Doctrine_Exception $exception) {
139
                    throw new Doctrine_RawSql_Exception("The associated component for table alias $e[0] couldn't be found.");
140 141 142 143 144 145 146 147 148 149 150 151 152
                }
            }

            if($e[1] == '*') {
                foreach($this->tables[$e[0]]->getColumnNames() as $name) {
                    $field = $e[0].".".$name;
                    $this->parts["select"][$field] = $field." AS ".$e[0]."__".$name;
                }
            } else {
                $field = $e[0].".".$e[1];
                $this->parts["select"][$field] = $field." AS ".$e[0]."__".$e[1];
            }
        }
153

154 155 156 157 158 159 160 161 162 163
        // force-add all primary key fields

        foreach($this->tableAliases as $alias) {
            foreach($this->tables[$alias]->getPrimaryKeys() as $key) {
                $field = $alias.".".$key;
                if( ! isset($this->parts["select"][$field]))
                    $this->parts["select"][$field] = $field." AS ".$alias."__".$key;
            }
        }

164
        $q = 'SELECT '.implode(', ', $this->parts['select']);
165

166 167
        $string = $this->applyInheritance();
        if( ! empty($string))
168
            $this->parts['where'][] = $string;
169 170 171 172

        $copy = $this->parts;
        unset($copy['select']);

173 174 175 176 177
        $q .= ( ! empty($this->parts['from']))?    ' FROM '     . implode(' ', $this->parts['from']) : '';
        $q .= ( ! empty($this->parts['where']))?   ' WHERE '    . implode(' AND ', $this->parts['where']) : '';
        $q .= ( ! empty($this->parts['groupby']))? ' GROUP BY ' . implode(', ', $this->parts['groupby']) : '';
        $q .= ( ! empty($this->parts['having']))?  ' HAVING '   . implode(' ', $this->parts['having']) : '';
        $q .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(' ', $this->parts['orderby']) : '';
178 179 180 181 182

        if( ! empty($string))
            array_pop($this->parts['where']);

        return $q;
183 184 185 186 187 188 189 190 191 192 193 194 195 196
    }
    /**
     * getFields
     *
     * @return array
     */
    public function getFields() {
        return $this->fields;
    }
    /**
     * addComponent
     *
     * @param string $tableAlias
     * @param string $componentName
doctrine's avatar
doctrine committed
197
     * @return Doctrine_RawSql
198 199
     */
    public function addComponent($tableAlias, $componentName) {
zYne's avatar
zYne committed
200
        $e = explode('.', $componentName);
201

202
        $currPath = '';
pookey's avatar
pookey committed
203
        $table = null;
204 205

        foreach($e as $k => $component) {
zYne's avatar
zYne committed
206
            $currPath .= '.' . $component;
207
            if($k == 0)
208 209 210 211 212 213 214
                $currPath = substr($currPath,1);

            if(isset($this->tableAliases[$currPath]))
                $alias = $this->tableAliases[$currPath];
            else
                $alias = $tableAlias;

215
            if($table) {
zYne's avatar
zYne committed
216 217 218 219

                $tableName = $table->getAliasName($component);
                

220
                $table = $this->conn->getTable($tableName);
pookey's avatar
pookey committed
221
            } else {
222
                $table = $this->conn->getTable($component);
pookey's avatar
pookey committed
223
            }
224 225 226 227 228 229 230 231 232 233
            $this->tables[$alias]           = $table;
            $this->fetchModes[$alias]       = Doctrine::FETCH_IMMEDIATE;
            $this->tableAliases[$currPath]  = $alias;

            if($k !== 0)
                $this->joins[$alias]        = $prevAlias;

            $prevAlias = $alias;
            $prevPath  = $currPath;
        }
doctrine's avatar
doctrine committed
234 235
        
        return $this;
236 237 238
    }

}
239