InsertOrUpdateQuery.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2008 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 
00017     abstract class InsertOrUpdateQuery
00018         extends QuerySkeleton
00019         implements SQLTableName
00020     {
00021         protected $table    = null;
00022         protected $fields   = array();
00023         
00024         abstract public function setTable($table);
00025         
00026         public function getTable()
00027         {
00028             return $this->table;
00029         }
00030         
00031         public function getFieldsCount()
00032         {
00033             return count($this->fields);
00034         }
00035         
00039         public function set($field, $value = null)
00040         {
00041             $this->fields[$field] = $value;
00042             
00043             return $this;
00044         }
00045         
00050         public function drop($field)
00051         {
00052             if (!array_key_exists($field, $this->fields))
00053                 throw new MissingElementException("unknown field '{$field}'");
00054             
00055             unset($this->fields[$field]);
00056             
00057             return $this;
00058         }
00059         
00063         public function lazySet($field, /* Identifiable */ $object = null)
00064         {
00065             if ($object === null)
00066                 $this->set($field, null);
00067             elseif ($object instanceof Identifiable)
00068                 $this->set($field, $object->getId());
00069             elseif ($object instanceof Range)
00070                 $this->
00071                     set($field.'_min', $object->getMin())->
00072                     set($field.'_max', $object->getMax());
00073             elseif ($object instanceof DateRange)
00074                 $this->
00075                     set($field.'_start', $object->getStart())->
00076                     set($field.'_end', $object->getEnd());
00077             elseif ($object instanceof Time)
00078                 $this->set($field, $object->toFullString());
00079             elseif ($object instanceof Stringable)
00080                 $this->set($field, $object->toString());
00081             else
00082                 $this->set($field, $object);
00083             
00084             return $this;
00085         }
00086         
00090         public function setBoolean($field, $value = false)
00091         {
00092             try {
00093                 Assert::isTernaryBase($value);
00094                 $this->set($field, $value);
00095             } catch (WrongArgumentException $e) {/*_*/}
00096             
00097             return $this;
00098         }
00099         
00105         public function arraySet($fields)
00106         {
00107             Assert::isArray($fields);
00108             
00109             $this->fields = array_merge($this->fields, $fields);
00110             
00111             return $this;
00112         }
00113         
00114         public function toDialectString(Dialect $dialect)
00115         {
00116             $this->checkReturning($dialect);
00117             
00118             if (empty($this->returning))
00119                 return parent::toDialectString($dialect);
00120             
00121             $query =
00122                 parent::toDialectString($dialect)
00123                 .' RETURNING '
00124                 .$this->toDialectStringReturning($dialect);
00125             
00126             return $query;
00127         }
00128     }
00129 ?>