Exemple #1
0
 /**
  * Returns the delete query builder
  *
  * @return Eden_Sql_Delete
  */
 public function delete($table = NULL)
 {
     //Argument 1 must be a string or null
     Eden_Sql_Error::i()->argument(1, 'string', 'null');
     return Eden_Sql_Delete::i($table);
 }
Exemple #2
0
if(!class_exists('Eden_Sql_Database')){abstract class Eden_Sql_Database extends Eden_Event{const QUERY='Eden_Sql_Query';const FIRST='first';const LAST='last';const MODEL='Eden_Sql_Model';const COLLECTION='Eden_Sql_Collection';protected $_queries=array();protected $_connection=NULL;protected $_binds=array();protected $_model=self::MODEL;protected $_collection=self::COLLECTION;abstract public function connect(array $options=array());public function bind($value){Eden_Sql_Error::i()->argument(1,'array','string','numeric','null');if(is_array($value)){foreach($value as $i=>$item){$value[$i]=$this->bind($item);}return '('.implode(",",$value).')';}else if(is_numeric($value)){return $value;}$name=':bind'.count($this->_binds).'bind';$this->_binds[$name]=$value;return $name;}public function collection(array $data=array()){$collection=$this->_collection;return $this->$collection()->setDatabase($this)->setModel($this->_model)->set($data);}public function delete($table=NULL){Eden_Sql_Error::i()->argument(1,'string','null');return Eden_Sql_Delete::i($table);}public function deleteRows($table,$filters=NULL){Eden_Sql_Error::i()->argument(1,'string');$query=$this->delete($table);if(is_array($filters)){foreach($filters as $i=>$filter){$format=array_shift($filter);foreach($filter as $j=>$value){$filter[$j]=$this->bind($value);}$filters[$i]=vsprintf($format,$filter);}}$query->where($filters);$this->query($query,$this->getBinds());$this->trigger($table,$filters);return $this;}public function getBinds(){return $this->_binds;}public function getCollection($table,array $joins=array(),$filters=NULL,array $sort=array(),$start=0,$range=0,$index=NULL){Eden_Sql_Error::i()->argument(1,'string')->argument(3,'string','array','null')->argument(5,'numeric')->argument(6,'numeric')->argument(7,'numeric','null');$results=$this->getRows($table,$joins,$filters,$sort,$start,$range,$index);$collection=$this->collection()->setTable($table)->setModel($this->_model);if(is_null($results)){return $collection;}if(!is_null($index)){return $this->model($results)->setTable($table);}return $collection->set($results);}public function getConnection(){if(!$this->_connection){$this->connect();}return $this->_connection;}public function getLastInsertedId(){return $this->getConnection()->lastInsertId();}public function getModel($table,$name,$value){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string')->argument(3,'string','numeric');$result=$this->getRow($table,$name,$value);$model=$this->model()->setTable($table);if(is_null($result)){return $model;}return $model->set($result);}public function getQueries($index=NULL){if(is_null($index)){return $this->_queries;}if($index==self::FIRST){$index=0;}if($index==self::LAST){$index=count($this->_queries) - 1;}if(isset($this->_queries[$index])){return $this->_queries[$index];}return NULL;}public function getRow($table,$name,$value){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string')->argument(3,'string','numeric');$query=$this->select()->from($table)->where($name.'='.$this->bind($value))->limit(0,1);$results=$this->query($query,$this->getBinds());$this->trigger($table,$name,$value,$results);return isset($results[0]) ? $results[0] : NULL;}public function getRows($table,array $joins=array(),$filters=NULL,array $sort=array(),$start=0,$range=0,$index=NULL){Eden_Sql_Error::i()->argument(1,'string')->argument(3,'string','array','null')->argument(5,'numeric')->argument(6,'numeric')->argument(7,'numeric','null');$query=$this->select()->from($table);foreach($joins as $join){if(!is_array($join) || count($join) < 3){continue;}if(count($join)==3){$join[]=true;}$query->join($join[0],$join[1],$join[2],$join[3]);}if(is_array($filters)){foreach($filters as $i=>$filter){$format=array_shift($filter);foreach($filter as $j=>$value){$filter[$j]=$this->bind($value);}$filters[$i]=vsprintf($format,$filter);}}if(!is_null($filters)){$query->where($filters);}if(!empty($sort)){foreach($sort as $key=>$value){if(is_string($key) && trim($key)){$query->sortBy($key,$value);}}}if($range){$query->limit($start,$range);}$results=$this->query($query,$this->getBinds());if(!is_null($index)){if(empty($results)){$results=NULL;}else{if($index==self::FIRST){$index=0;}if($index==self::LAST){$index=count($results)-1;}if(isset($results[$index])){$results=$results[$index];}else{$results=NULL;}}}$this->trigger($table,$joins,$filters,$sort,$start,$range,$index,$results);return $results;}public function getRowsCount($table,array $joins=array(),$filters=NULL){Eden_Sql_Error::i()->argument(1,'string')->argument(3,'string','array','null');$query=$this->select('COUNT(*) as count')->from($table);foreach($joins as $join){if(!is_array($join) || count($join) < 3){continue;}if(count($join)==3){$join[]=true;}$query->join($join[0],$join[1],$join[2],$join[3]);}if(is_array($filters)){foreach($filters as $i=>$filter){$format=array_shift($filter);$filter=$this->bind($filter);$filters[$i]=vsprintf($format,$filter);}}$query->where($filters);$results=$this->query($query,$this->getBinds());if(isset($results[0]['count'])){$this->trigger($table,$joins,$filters,$results[0]['count']);return $results[0]['count'];}$this->trigger($table,$joins,$filters,false);return false;}public function insert($table=NULL){Eden_Sql_Error::i()->argument(1,'string','null');return Eden_Sql_Insert::i($table);}public function insertRow($table,array $setting,$bind=true){Eden_Sql_Error::i()->argument(1,'string')->argument(3,'array','bool');$query=$this->insert($table);foreach($setting as $key=>$value){if(is_null($value) || is_bool($value)){$query->set($key,$value);continue;}if((is_bool($bind) && $bind) || (is_array($bind) && in_array($key,$bind))){$value=$this->bind($value);}$query->set($key,$value);}$this->query($query,$this->getBinds());$this->trigger($table,$setting);return $this;}public function insertRows($table,array $settings,$bind=true){Eden_Sql_Error::i()->argument(1,'string')->argument(3,'array','bool');$query=$this->insert($table);foreach($settings as $index=>$setting){foreach($setting as $key=>$value){if(is_null($value) || is_bool($value)){$query->set($key,$value,$index);continue;}if((is_bool($bind) && $bind) || (is_array($bind) && in_array($key,$bind))){$value=$this->bind($value);}$query->set($key,$value,$index);}}$this->query($query,$this->getBinds());$this->trigger($table,$settings);return $this;}public function model(array $data=array()){$model=$this->_model;return $this->$model($data)->setDatabase($this);}public function query($query,array $binds=array()){Eden_Sql_Error::i()->argument(1,'string',self::QUERY);$connection=$this->getConnection();$query=(string) $query;$stmt=$connection->prepare($query);foreach($binds as $key=>$value){$stmt->bindValue($key,$value);}if(!$stmt->execute()){$error=$stmt->errorInfo();foreach($binds as $key=>$value){$query=str_replace($key,"'$value'",$query);}Eden_Sql_Error::i()->setMessage(Eden_Sql_Error::QUERY_ERROR)->addVariable($query)->addVariable($error[2])->trigger();}$results=$stmt->fetchAll( PDO::FETCH_ASSOC );$this->_queries[]=array( 'query'=>$query,'binds'=>$binds,'results'=>$results);$this->_binds=array();$this->trigger($query,$binds,$results);return $results;}public function search($table=NULL){Eden_Sql_Error::i()->argument(1,'string','null');$search=Eden_Sql_Search::i($this)->setCollection($this->_collection)->setModel($this->_model);if($table){$search->setTable($table);}return $search;}public function select($select='*'){Eden_Sql_Error::i()->argument(1,'string','array');return Eden_Sql_Select::i($select);}public function setBinds(array $binds){$this->_binds=$binds;return $this;}public function setCollection($collection){$error=Eden_Sql_Error::i()->argument(1,'string');if(!is_subclass_of($collection,self::COLLECTION)){$error->setMessage(Eden_Sql_Error::NOT_SUB_COLLECTION)->addVariable($collection)->trigger();}$this->_collection=$collection;return $this;}public function setModel($model){$error=Eden_Sql_Error::i()->argument(1,'string');if(!is_subclass_of($model,self::MODEL)){$error->setMessage(Eden_Sql_Error::NOT_SUB_MODEL)->addVariable($model)->trigger();}$this->_model=$model;return $this;}public function setRow($table,$name,$value,array $setting){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string')->argument(3,'string','numeric');$row=$this->getRow($table,$name,$value);if(!$row){$setting[$name]=$value;return $this->insertRow($table,$setting);}else{return $this->updateRows($table,$setting,array(array($name.'=%s',$value)));}}public function update($table=NULL){Eden_Sql_Error::i()->argument(1,'string','null');return Eden_Sql_Update::i($table);}public function updateRows($table,array $setting,$filters=NULL,$bind=true){Eden_Sql_Error::i()->argument(1,'string')->argument(4,'array','bool');$query=$this->update($table);foreach($setting as $key=>$value){if(is_null($value) || is_bool($value)){$query->set($key,$value);continue;}if((is_bool($bind) && $bind) || (is_array($bind) && in_array($key,$bind))){$value=$this->bind($value);}$query->set($key,$value);}if(is_array($filters)){foreach($filters as $i=>$filter){$format=array_shift($filter);foreach($filter as $j=>$value){$filter[$j]=$this->bind($value);}$filters[$i]=vsprintf($format,$filter);}}$query->where($filters);$this->query($query,$this->getBinds());$this->trigger($table,$setting,$filters);return $this;}}}