InsertQuery.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     final class InsertQuery extends InsertOrUpdateQuery
00016     {
00020         protected $select = null;
00021         
00025         public function into($table)
00026         {
00027             $this->table = $table;
00028             
00029             return $this;
00030         }
00031         
00037         public function setTable($table)
00038         {
00039             return $this->into($table);
00040         }
00041         
00045         public function setSelect(SelectQuery $select)
00046         {
00047             $this->select = $select;
00048             
00049             return $this;
00050         }
00051         
00052         public function toDialectString(Dialect $dialect)
00053         {
00054             $query = 'INSERT INTO '.$dialect->quoteTable($this->table).' ';
00055             
00056             if ($this->select === null) {
00057                 $query = $this->toDialectStringValues($query, $dialect);
00058             } else {
00059                 $query = $this->toDialectStringSelect($query, $dialect);
00060             }
00061             
00062             $query .= parent::toDialectString($dialect);
00063             
00064             return $query;
00065         }
00066         
00067         public function where(LogicalObject $exp, $logic = null)
00068         {
00069             throw new UnsupportedMethodException();
00070         }
00071         
00072         public function andWhere(LogicalObject $exp)
00073         {
00074             throw new UnsupportedMethodException();
00075         }
00076         
00077         public function orWhere(LogicalObject $exp)
00078         {
00079             throw new UnsupportedMethodException();
00080         }
00081         
00082         protected function toDialectStringValues($query, Dialect $dialect)
00083         {
00084             $fields = array();
00085             $values = array();
00086             
00087             foreach ($this->fields as $var => $val) {
00088                 $fields[] = $dialect->quoteField($var);
00089                 
00090                 if ($val === null)
00091                     $values[] = 'NULL';
00092                 elseif (true === $val)
00093                     $values[] = 'TRUE';
00094                 elseif (false === $val)
00095                     $values[] = 'FALSE';
00096                 elseif ($val instanceof DialectString)
00097                     $values[] = $val->toDialectString($dialect);
00098                 else
00099                     $values[] = $dialect->quoteValue($val);
00100             }
00101             
00102             if (!$fields || !$values)
00103                 throw new WrongStateException('what should i insert?');
00104             
00105             $fields = implode(', ', $fields);
00106             $values = implode(', ', $values);
00107             
00108             return $query . "({$fields}) VALUES ({$values})";
00109         }
00110         
00111         protected function toDialectStringSelect($query, Dialect $dialect)
00112         {
00113             $fields = array();
00114             
00115             foreach ($this->fields as $var => $val) {
00116                 $fields[] = $dialect->quoteField($var);
00117             }
00118             
00119             if (!$fields)
00120                 throw new WrongStateException('what should i insert?');
00121             if ($this->select->getFieldsCount() != count($fields))
00122                 throw new WrongStateException('count of select fields must be equal with count of insert fields');
00123             
00124             $fields = implode(', ', $fields);
00125             
00126             return $query . "({$fields}) ("
00127                 .$this->select->toDialectString($dialect).")";
00128         }
00129     }
00130 ?>