Changeset 3
Author: m4rw3r
(2008/06/30 15:36) Over 3 years ago
A bit more on the migrate controller Thanks to the inparo, elitemedia and the Linkster guidelines, I've added a lot more empty lines and comments to IgnitedQuery to improve readability :P A bit more on the IgnitedQuery unit test select() now has a switch for protect_identifiers unions of arrays didn't work as expected, switched to array_merge() added select_count(), is it possible? _select_func() also has this protect_identifiers switch or_where_not_in() was missing Started on INSERT
class irtest extends Controller{class irtest extends Controller{function irtest(){function irtest(){$this->load->library('unit_test');$this->load->library('unit_test');function index(){function index(){PRIMARY KEY ("id")PRIMARY KEY ("id")PRIMARY KEY ("id")PRIMARY KEY ("id")$this->unit->run(class_exists('IgnitedRecord'),$this->unit->run(class_exists('IgnitedRecord'),$this->post = IgnitedRecord::factory('test_posts',$this->post = IgnitedRecord::factory('test_posts',array('belongs_to' => 'test_users',array('belongs_to' => 'test_users','habtm' => array('name' => 'lol','table' => 'test_habtm','model' => 'user')'habtm' => array('name' => 'lol','table' => 'test_habtm','model' => 'user')class migrate extends Controller{class migrate extends Controller{var $ignore_classes = array('array','array_object','migrate','controller');function migrate(){function migrate(){$this->load->helper('url');function index(){function index(){$classes = array();foreach(get_declared_classes() as $class){if(stripos($class,'migrate') !== false && $class != 'migrate' && class_exists($class)){$classes[] = $class;}}?><ul><?php foreach($classes as $class){$version = substr($class,8);echo "<li>".anchor('migrate/to/'.$version,$version)."</li>";}?></ul><?phpfunction to($version){if($version == $this->current){for($i = $this->current + $inc; ($i <= $version && $inc == 2) || ($i > $version && $inc == 0); $i = $i - 1 + $inc){class amigrate_001 extends IRM{class migrate_002{function up(){function up($CI){table('testaaaa')echo "going to version 2";}function down($CI){echo "going down from version 2";}}class migrate_001 extends IRM{function up(&$CI){echo "going to version 1";/*table('testaaaa')->column('id')->column('id')->type('int')->type('int')->options(array('auto_increment' => true,->options(array('auto_increment' => true,->type('varchar')->type('varchar')->options(array('constraint' => 100))->options(array('constraint' => 100))->execute();->execute();*/abstract function up(&$CI);function down(){function down(&$CI){return '!!!!';echo '!!!!';class IRM_table{class IRM_table{class test extends Controller{class test extends Controller{function test(){function test(){$this->load->library('ignitedquery');$this->load->library('ignitedquery');///////////////////////////////////////////////////////////////////////////// =============================// EMPTY //// = ========= EMPTY ========= =///////////////////////////////////////////////////////////////////////////// =============================$this->unit->run($iq->_build_get(),false,'Testing empty object');$this->unit->run($iq->_build_get_query(),false,'Empty object');///////////////////////////////////////////////////////////////////////////$iq->from('foo');// FROM //$iq->reset();///////////////////////////////////////////////////////////////////////////$this->unit->run($iq->_build_get_query(), false, 'Resetting Object');$iq->alias('bar');$this->unit->run($iq->from('foo')->_build_get_query(),$this->unit->run($iq->from('users')->_build_get(),$this->unit->run($iq->from('users')->_build_get_query(),FROM `users`', 'SELECT * FROM users');FROM `users`', 'From single table');$iq->from('users');$iq->from('users');$this->unit->run($iq->from('posts')->_build_get(),$this->unit->run($iq->from('posts')->_build_get_query(),FROM `users`, `posts`', 'SELECT * FROM users, posts (sequence)');FROM `users`, `posts`', 'From multiple tables (sequence)');$this->unit->run($iq->from(array('users','posts'))->_build_get(),$this->unit->run($iq->from(array('users','posts'))->_build_get_query(),FROM `users`, `posts`','FROM: SELECT * FROM users, posts (array)');FROM `users`, `posts`','From multiple tables (array)');$this->unit->run($iq->from()->from('users')->end()->_build_get(),$iq->from('data');$this->unit->run($iq->from(array('users','posts'))->_build_get_query(),->from('users')$obj->from('users');$obj->from('users');$this->unit->run($iq->from($obj)->_build_get_query(),///////////////////////////////////////////////////////////////////////////// =============================// SUBQUERIES //// = ========= SELECT ======== =///////////////////////////////////////////////////////////////////////////// =============================$iq->from('foo');$this->unit->run($iq->select('lol')->_build_get_query(),$iq->select('lol')->from('foo');$this->unit->run($iq->select('longcat')->_build_get_query(),$iq->from('foo');$this->unit->run($iq->select(array('lol','longcat'))->_build_get_query(),$iq->select('bar')->from('foo');$this->unit->run($iq->select(array('lol','longcat'))->_build_get_query(),->from('users')->from('users')->where('id',1)->where('id',1)->_build_get_query(),->select('username')->select('username')->from('users')->from('users')->_build_get_query());u(str_replace("\n",' ',u(str_replace("\n",' ',$this->db->select(array('a','b','c'))$this->db->select(array('a','b','c'))->select_avg('depth')->select_avg('depth')->group_by('aaa')->group_by('aaa')->_build_get_query()));* @{class IgnitedQuery{class IgnitedQuery{// flags/*** The data used by insert() and update().** @access private*/var $set_data = array();// =============================// = ========= FLAGS ========= =// =============================/*** The parent node of the subquery (if this is one).** If not null or false*/function IgnitedQuery(&$parent = null){function IgnitedQuery(&$parent = null){$this->reset();/*function __call($method,$args){/*function __call($method,$args){show_error("IgnitedQuery: Method $method is not found.");show_error("IgnitedQuery: Method $method is not found.");function reset(){function reset(){function &select($cols = false){function &select($cols = false, $protect_identifiers = true){if($cols == false){if($cols === false){if(is_string($this->select)){if(is_string($this->select)){if(is_array($cols)){if(is_array($cols)){array_walk($cols,array($this,'_protect_identifiers_walk'));// many columns in array$this->select = $this->select + $cols;if($protect_identifiers)array_walk($cols,array($this,'_protect_identifiers_walk'));$this->select = array_merge($this->select,$cols);elseif(is_object($cols) && is_a($cols,'IgnitedQuery')){elseif(is_object($cols) && is_a($cols,'IgnitedQuery')){else{else{$this->select[] =& $this->_protect_identifiers($cols);if($protect_identifiers)$cols = $this->_protect_identifiers($cols);$this->select[] = $cols;function &select_max($col = false,$alias = ''){function &select_max($col = false,$alias = '', $protect_identifiers = true){return $this->_select_func($col,$alias,'MAX');return $this->_select_func($col, $alias, 'MAX', $protect_identifiers);function &select_min($col = false,$alias = ''){function &select_min($col = false,$alias = '', $protect_identifiers = true){return $this->_select_func($col,$alias,'MIN');return $this->_select_func($col, $alias, 'MIN', $protect_identifiers);function &select_avg($col = false,$alias = ''){function &select_avg($col = false,$alias = '', $protect_identifiers = true){return $this->_select_func($col,$alias,'AVG');return $this->_select_func($col, $alias, 'AVG', $protect_identifiers);function &select_sum($col = false,$alias = ''){function &select_sum($col = false,$alias = '', $protect_identifiers = true){return $this->_select_func($col,$alias,'SUM');return $this->_select_func($col, $alias, 'SUM', $protect_identifiers);function &select_count($col = false, $alias = '', $protect_identifiers = true){function &_select_func($col, $alias, $func){function &_select_func($col, $alias, $func, $protect_identifiers){if(is_string($this->select)){if(is_string($this->select)){if($col == false){if($col == false){$this->select[] = '('.$this->_protect_identifiers($col).') AS '.($alias != '' ? $this->_protect_identifiers($alias) : $this->_protect_identifiers($col));if($protect_identifiers)$col = $this->_protect_identifiers($col);$this->select[] = '('.$col.') AS '.($alias != '' ? $this->_protect_identifiers($alias) :$this->_protect_identifiers($col));function &from($tables = false){function &from($tables = false){if($tables == false){if($tables === false){if(is_array($tables)){if(is_array($tables)){$this->from = $this->from + $tables;$this->from = array_merge($this->from, $tables);elseif(is_object($tables) && is_a($tables,'IgnitedQuery')){elseif(is_object($tables) && is_a($tables,'IgnitedQuery')){else{else{function &where($where = false,$value = null,$escape = true){function &where($where = false,$value = null,$escape = true){if($where == false){if($where === false){if(is_object($where) && is_a($where,'IgnitedQuery')){if(is_object($where) && is_a($where,'IgnitedQuery')){if(!is_array($where) && $escape)if( ! is_array($where) && $escape){foreach((Array)$where as $k => $val){foreach((Array)$where as $k => $val){if(is_numeric($k))// numeric tells us that we have a finished where statementif(is_numeric($k)){else{else{if ($this->_has_operator($k)){// process key => value whereif($this->_has_operator($k)){$k = preg_replace("/([A-Za-z_0-9]+)/",$k = preg_replace("/([A-Za-z_0-9]+)/",$this->_protect_identifiers('$1'), $k);$this->_protect_identifiers('$1'), $k);else{else{function &or_where($where = false,$value = null,$escape = true){function &or_where($where = false,$value = null,$escape = true){function &where_in($key,$values = false,$not = false){function &where_in($key,$values = false,$not = false){if($values == false){if($values === false){if(is_object($values) && is_a($values,'IgnitedQuery')){if(is_object($values) && is_a($values,'IgnitedQuery')){foreach((Array)$values as $key => $col){foreach((Array)$values as $key => $col){$this->where[] = '('.$str.')';$this->where[] = '('.$str.')';function &or_where_in($key, $values = false){function &or_where_in($key, $values = false){function &where_not_in($key, $values = false){function &where_not_in($key, $values = false){function &or_where_not_in($key, $values = false){* @param $col* @access public* @param $col The column to match* @param $match The match string* @param $side Which side the '%' sign should be on,* Accepts 'both', 'before' and 'after' (default: 'both')* @param $not If to add NOT to the statement (default: false)function like($col, $match = '', $side = 'both', $not = false){function &like($col, $match = '', $side = 'both', $not = false){/* if(!is_array($col) && $match == ''){/* if(!is_array($col) && $match == ''){foreach($col as $k => $v){foreach($col as $k => $v){$this->where[] = '%'.$v;$this->where[] = "%$v";$this->where[] = $v.'%';$this->where[] = "$v%";$this->where[] = '%'$v'%';$this->where[] = "%$v%";function not_like($col, $match = '', $side = 'both'){function not_like($col, $match = '', $side = 'both'){function or_like($col, $match = '', $side = 'both', $not = false){function or_like($col, $match = '', $side = 'both', $not = false){function or_not_like($col, $match = '', $side = 'both'){return $this->or_like($col, $match, $side, true);// --------------------------------------------------------------------}* Specifies an OR WHERE NOT IN in the WHERE part of the query.* Produces an OR NOT LIKE sql statement.* Works like where_not_in()* @access public* @param $col The column to match* @param $match The match string* @param $side Which side the '%' sign should be on,* Accepts 'both', 'before' and 'after' (default: 'both')* @param $key The column to check* @return $this* @param $values The values to check against* If set to false, a new IgnitedQuery is returned which acts like a subquery** @return $this or a new IgnitedQueryfunction &or_where_not_in($key, $values = false){function or_not_like($col, $match = '', $side = 'both'){if(count($this->where))return $this->or_like($col, $match, $side, true);$this->where[] = 'OR';return $this->where_in($key,$values,true);function set($key, $value = '', $escape = true){if( ! is_array($key)){foreach($key as $key => $value){if($escape === true && ! is_object($value)){if(is_object($value) && is_a($value,'IgnitedQuery')){function &end(){function &end(){function &get($table = '', $limit = null, $offset = null){function &get_where($table, $where, $limit = null, $offset = null){function insert($table = '', $set = null){if($table == ''){foreach($this->set_data as $field => $value){function _build_get(){function _build_get_query(){return '('.$this->build_where($this->where).')';return '('.$this->build_where($this->where).')';if(is_numeric($this->limit)){if(is_numeric($this->limit)){if(isset($this->parent)){if(isset($this->parent)){return '('.$query.')'.// this is a subquery$query = '('.$query.')'.* Runs the query.*/function &get($table = '', $limit = null, $offset = null){$this->limit($limit);$this->offset($offset);if($table != '')$this->from($table);$CI =& get_instance();return $CI->db->query($this->_build_get());}/****/function &get_where($table, $where, $limit = null, $offset = null){$this->where($where);$this->limit($limit);$this->offset($offset);return $this->get($table);}/**function _build_select($select){function _build_select($select){if(is_object($select)){if(is_object($select)){return $select->_build_get_query();elseif(is_array($select)){elseif(is_array($select)){foreach($select as $key => $col){foreach($select as $key => $col){if(in_array($col,array('MIN','MAX','AVG','SUM'))){$str .= "$col";// these are db functions, just echo themif(in_array($col,array('MIN','MAX','AVG','SUM','COUNT'))){$str .= $col;elsereturn $select;function _build_from($from){function _build_from($from){if(is_object($from)){if(is_object($from)){return $from->_build_get_query();elseif(is_array($from)){elseif(is_array($from)){foreach($from as $key => $col){foreach($from as $key => $col){elsereturn $from;function _build_where($where){function _build_where($where){if(is_object($where)){if(is_object($where)){return $where->_build_get_query();elseif(is_array($where)){elseif(is_array($where)){foreach($where as $key => $col){foreach($where as $key => $col){if(in_array($col,array('OR','NOT','IN','LIKE'))){if(in_array($col,array('OR','NOT','IN','LIKE'))){if(!is_numeric($key)){if(!is_numeric($key)){if(!$this->_has_operator($key)){// do we need to add an equal sign?if( ! $this->_has_operator($key)){else{}} // endforeachelsereturn $where;function _has_operator($str){function _has_operator($str){if (!preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str)){if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str)){function escape($str){function escape($str){function _protect_identifiers($str){function _protect_identifiers($str){function _protect_identifiers_walk(&$str){function _protect_identifiers_walk(&$str){</IfModule>