Example #1
0
 public function __call($name, $args)
 {
     if (strpos($name, 'addDimension') === 0) {
         //addSegmentCity('Manila')
         $key = Eden_Type_String::i($name)->substr(12)->strtolower()->get();
         if (!isset(self::$_options[$key], $args[0])) {
             return $this;
         }
         $this->_dimensions[] = self::$_options[$key];
         return $this;
     }
     if (strpos($name, 'addSegment') === 0) {
         //addSegmentCity('Manila')
         $key = Eden_Type_String::i($name)->substr(10)->strtolower()->get();
         if (!isset(self::$_options[$key], $args[0])) {
             return $this;
         }
         $this->_segment[] = self::$_options[$key];
         return $this;
     }
     if (strpos($name, 'sortBy') === 0) {
         //sortByCity('Manila')
         $key = Eden_Type_String::i($name)->substr(6)->strtolower()->get();
         if (!isset(self::$_options[$key], $args[0])) {
             return $this;
         }
         $this->_sort[] = self::$_options[$key];
         return $this;
     }
     try {
         return parent::__call($name, $args);
     } catch (Eden_Error $e) {
         Eden_Sql_Error::i($e->getMessage())->trigger();
     }
 }
Example #2
0
 /**
  * Where clause
  *
  * @param array|string where
  * @return	this
  * @notes loads a where phrase into registry
  */
 public function where($where)
 {
     //Argument 1 must be a string or array
     Eden_Sql_Error::i()->argument(1, 'string', 'array');
     if (is_string($where)) {
         $where = array($where);
     }
     $this->_where = array_merge($this->_where, $where);
     return $this;
 }
Example #3
0
 /**
  * Set clause that assigns a given field name to a given value.
  *
  * @param string
  * @param string
  * @return this
  * @notes loads a set into registry
  */
 public function set($key, $value)
 {
     //argument test
     Eden_Sql_Error::i()->argument(1, 'string')->argument(2, 'scalar', 'null');
     //Argument 2 must be scalar or null
     if (is_null($value)) {
         $value = 'NULL';
     } else {
         if (is_bool($value)) {
             $value = $value ? 'TRUE' : 'FALSE';
         }
     }
     $this->_set[$key] = $value;
     return $this;
 }
Example #4
0
 /**
  * Set clause that assigns a given field name to a given value.
  * You can also use this to add multiple rows in one call
  *
  * @param string
  * @param string
  * @return this
  * @notes loads a set into registry
  */
 public function set($key, $value, $index = 0)
 {
     //argument test
     Eden_Sql_Error::i()->argument(1, 'string')->argument(2, 'scalar', 'null');
     //Argument 2 must be scalar or null
     if (!in_array($key, $this->_setKey)) {
         $this->_setKey[] = $key;
     }
     if (is_null($value)) {
         $value = 'NULL';
     } else {
         if (is_bool($value)) {
             $value = $value ? 'TRUE' : 'FALSE';
         }
     }
     $this->_setVal[$index][] = $value;
     return $this;
 }
Example #5
0
 /**
  * Updates rows that match a filter given the update settings
  *
  * @param string table
  * @param array setting
  * @param array filter
  * @return var
  */
 public function updateRows($table, array $setting, $filters = NULL, $bind = true)
 {
     //Argument 1 must be a string
     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) {
             //array('post_id=%s AND post_title IN %s', 123, array('asd'));
             $format = array_shift($filter);
             foreach ($filter as $j => $value) {
                 $filter[$j] = $this->bind($value);
             }
             $filters[$i] = vsprintf($format, $filter);
         }
     }
     $query->where($filters);
     //run the query
     $this->query($query, $this->getBinds());
     $this->trigger($table, $setting, $filters);
     return $this;
 }
Example #6
0
 /**
  * Sets the default database
  *
  * @param string
  */
 public function setTable($table)
 {
     //Argument 1 must be a string
     Eden_Sql_Error::i()->argument(1, 'string');
     $this->_table = $table;
     //for each row
     foreach ($this->_list as $row) {
         if (!is_object($row) || !method_exists($row, __FUNCTION__)) {
             continue;
         }
         //let the row handle this
         $row->setTable($table);
     }
     return $this;
 }
Example #7
0
 /**
  * Set the table name in which you want to delete from
  *
  * @param string name
  * @return this
  */
 public function setTable($table)
 {
     //Argument 1 must be a string
     Eden_Sql_Error::i()->argument(1, 'string');
     $this->_table = $table;
     return $this;
 }
Example #8
0
if(!class_exists('Eden_Sql_Search')){class Eden_Sql_Search extends Eden_Class{const LEFT='LEFT';const RIGHT='RIGHT';const INNER='INNER';const OUTER='OUTER';const ASC='ASC';const DESC='DESC';protected $_database=NULL;protected $_table=NULL;protected $_columns=array();protected $_join=array();protected $_filter=array();protected $_sort=array();protected $_group=array();protected $_start=0;protected $_range=0;protected $_model=Eden_Sql_Database::MODEL;protected $_collection=Eden_Sql_Database::COLLECTION;public static function i(){return self::_getMultiple(__CLASS__);}public function __call($name,$args){if(strpos($name,'filterBy')===0){$separator='_';if(isset($args[1]) && is_scalar($args[1])){$separator=(string) $args[1];}$key=Eden_Type_String::i($name)->substr(8)->preg_replace("/([A-Z0-9])/",$separator."$1")->substr(strlen($separator))->strtolower()->get();if(!isset($args[0])){$args[0]=NULL;}$key=$key.'=%s';$this->addFilter($key,$args[0]);return $this;}if(strpos($name,'sortBy')===0){$separator='_';if(isset($args[1]) && is_scalar($args[1])){$separator=(string) $args[1];}$key=Eden_Type_String::i($name)->substr(6)->preg_replace("/([A-Z0-9])/",$separator."$1")->substr(strlen($separator))->strtolower()->get();if(!isset($args[0])){$args[0]=self::ASC;}$this->addSort($key,$args[0]);return $this;}try{return parent::__call($name,$args);}catch(Eden_Error $e){Eden_Sql_Error::i($e->getMessage())->trigger();}}public function __construct(Eden_Sql_Database $database){$this->_database=$database;}public function addFilter(){Eden_Sql_Error::i()->argument(1,'string');$this->_filter[]=func_get_args();return $this;}public function addInnerJoinOn($table,$where){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');$where=func_get_args();$table=array_shift($where);$this->_join[]=array(self::INNER,$table,$where,false);return $this;}public function addInnerJoinUsing($table,$where){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');$where=func_get_args();$table=array_shift($where);$this->_join[]=array(self::INNER,$table,$where,true);return $this;}public function addLeftJoinOn($table,$where){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');$where=func_get_args();$table=array_shift($where);$this->_join[]=array(self::LEFT,$table,$where,false);return $this;}public function addLeftJoinUsing($table,$where){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');$where=func_get_args();$table=array_shift($where);$this->_join[]=array(self::LEFT,$table,$where,true);return $this;}public function addOuterJoinOn($table,$where){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');$where=func_get_args();$table=array_shift($where);$this->_join[]=array(self::OUTER,$table,$where,false);return $this;}public function addOuterJoinUsing($table,$where){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');$where=func_get_args();$table=array_shift($where);$this->_join[]=array(self::OUTER,$table,$where,true);return $this;}public function addRightJoinOn($table,$where){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');$where=func_get_args();$table=array_shift($where);$this->_join[]=array(self::RIGHT,$table,$where,false);return $this;}public function addRightJoinUsing($table,$where){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');$where=func_get_args();$table=array_shift($where);$this->_join[]=array(self::RIGHT,$table,$where,true);return $this;}public function addSort($column,$order=self::ASC){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');if($order !=self::DESC){$order=self::ASC;}$this->_sort[$column]=$order;return $this;}public function getCollection(){$collection=$this->_collection;return $this->$collection()->setDatabase($this->_database)->setTable($this->_table)->setModel($this->_model)->set($this->getRows());}public function getModel($index=0){Eden_Sql_Error::i()->argument(1,'int');return $this->getCollection()->offsetGet($index);}public function getRow($index=0,$column=NULL){Eden_Sql_Error::i()->argument(1,'int','string')->argument(2,'string','null');if(is_string($index)){$column=$index;$index=0;}$rows=$this->getRows();if(!is_null($column) && isset($rows[$index][$column])){return $rows[$index][$column];}else if(is_null($column) && isset($rows[$index])){return $rows[$index];}return NULL;}public function getRows(){$query=$this->_getQuery();if(!empty($this->_columns)){$query->select(implode(',',$this->_columns));}foreach($this->_sort as $key=>$value){$query->sortBy($key,$value);}if($this->_range){$query->limit($this->_start,$this->_range);}if(!empty($this->_group)){$query->groupBy($this->_group);}return $this->_database->query($query,$this->_database->getBinds());}public function getTotal(){$query=$this->_getQuery()->select('COUNT(*) as total');$rows=$this->_database->query($query,$this->_database->getBinds());if(!isset($rows[0]['total'])){return 0;}return $rows[0]['total'];}public function innerJoinOn($table,$where){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');$where=func_get_args();$table=array_shift($where);$this->_join[]=array(self::INNER,$table,$where,false);return $this;}public function innerJoinUsing($table,$where){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');$where=func_get_args();$table=array_shift($where);$this->_join[]=array(self::INNER,$table,$where,true);return $this;}public function leftJoinOn($table,$where){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');$where=func_get_args();$table=array_shift($where);$this->_join[]=array(self::LEFT,$table,$where,false);return $this;}public function leftJoinUsing($table,$where){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');$where=func_get_args();$table=array_shift($where);$this->_join[]=array(self::LEFT,$table,$where,true);return $this;}public function outerJoinOn($table,$where){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');$where=func_get_args();$table=array_shift($where);$this->_join[]=array(self::OUTER,$table,$where,false);return $this;}public function outerJoinUsing($table,$where){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');$where=func_get_args();$table=array_shift($where);$this->_join[]=array(self::OUTER,$table,$where,true);return $this;}public function rightJoinOn($table,$where){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');$where=func_get_args();$table=array_shift($where);$this->_join[]=array(self::RIGHT,$table,$where,false);return $this;}public function rightJoinUsing($table,$where){Eden_Sql_Error::i()->argument(1,'string')->argument(2,'string');$where=func_get_args();$table=array_shift($where);$this->_join[]=array(self::RIGHT,$table,$where,true);return $this;}public function setColumns($columns){if(!is_array($columns)){$columns=func_get_args();}$this->_columns=$columns;return $this;}public function setCollection($collection){$error=Eden_Sql_Error::i()->argument(1,'string');if(!is_subclass_of($collection,'Eden_Collection')){$error->setMessage(Eden_Sql_Error::NOT_SUB_COLLECTION)->addVariable($collection)->trigger();}$this->_collection=$collection;return $this;}public function setGroup($group){Eden_Sql_Error::i()->argument(1,'string','array');if(is_string($group)){$group=array($group);}$this->_group=$group;return $this;}public function setModel($model){$error=Eden_Sql_Error::i()->argument(1,'string');if(!is_subclass_of($model,'Eden_Model')){$error->setMessage(Eden_Sql_Error::NOT_SUB_MODEL)->addVariable($model)->trigger();}$this->_model=$model;return $this;}public function setPage($page){Eden_Sql_Error::i()->argument(1,'int');if($page < 1){$page=1;}$this->_start=($page - 1) * $this->_range;return $this;}public function setRange($range){Eden_Sql_Error::i()->argument(1,'int');if($range < 0){$range=25;}$this->_range=$range;return $this;}public function setStart($start){Eden_Sql_Error::i()->argument(1,'int');if($start < 0){$start=0;}$this->_start=$start;return $this;}public function setTable($table){Eden_Sql_Error::i()->argument(1,'string');$this->_table=$table;return $this;}protected function _getQuery(){$query=$this->_database->select()->from($this->_table);foreach($this->_join as $join){if(!is_array($join[2])){$join[2]=array($join[2]);}$where=array_shift($join[2]);if(!empty($join[2])){foreach($join[2] as $i=>$value){$join[2][$i]=$this->_database->bind($value);}$where=vsprintf($where,$join[2]);}$query->join($join[0],$join[1],$where,$join[3]);}foreach($this->_filter as $i=>$filter){$where=array_shift($filter);if(!empty($filter)){foreach($filter as $i=>$value){$filter[$i]=$this->_database->bind($value);}$where=vsprintf($where,$filter);}$query->where($where);}return $query;}}}
Example #9
0
 /**
  * Updates model to database
  *
  * @param string
  * @param Eden_Sql_Database
  * @param string|array|null
  * @return this
  */
 public function update($table = NULL, Eden_Sql_Database $database = NULL, $primary = NULL)
 {
     //Argument 1 must be a string
     $error = Eden_Sql_Error::i()->argument(1, 'string', 'null');
     //if no table
     if (is_null($table)) {
         //if no default table either
         if (!$this->_table) {
             //throw error
             $error->setMessage(Eden_Sql_Error::TABLE_NOT_SET)->trigger();
         }
         $table = $this->_table;
     }
     //if no database
     if (is_null($database)) {
         //and no default database
         if (!$this->_database) {
             $error->setMessage(Eden_Sql_Error::DATABASE_NOT_SET)->trigger();
         }
         $database = $this->_database;
     }
     //get the meta data, the valid column values and whether is primary is set
     $meta = $this->_getMeta($table, $database);
     $data = $this->_getValidColumns(array_keys($meta[self::COLUMNS]));
     //update original data
     $this->_original = $this->_data;
     //from here it means that this table has primary
     //columns and all primary values are set
     if (is_null($primary)) {
         $primary = $meta[self::PRIMARY];
     }
     if (is_string($primary)) {
         $primary = array($primary);
     }
     $filter = array();
     //for each primary key
     foreach ($primary as $column) {
         //add the condition to the filter
         $filter[] = array($column . '=%s', $data[$column]);
     }
     //we update it
     $database->updateRows($table, $data, $filter);
     return $this;
 }