root/trunk/application/controllers/test.php

User picture

Author: m4rw3r

Revision: 279 («Previous)


File Size: 9.36 KB

(October 25, 2008 10:27 UTC) Over 3 years ago

Updated where() to use the slower storage of where data if a key already exists, this because of the problem which occurs if someone wants to write WHERE a > b AND a > c
Updated _protect_identifiers() to be aware of the operators (AS, <, >, etc.) and prevent escaping of those

 
Show/hide line numbers
<?php 
/*
 * Created on 2008 Jun 21
 * by Martin Wernstahl <m4rw3r@gmail.com>
 */
function u($s){
	echo "<pre>";
	var_dump($s);
	echo "</pre>";
}

class test extends Controller{
	function test()
	{
		parent::Controller();
		$this->load->database();
		$this->output->enable_profiler(TRUE);
		$this->load->library('unit_test');
		$this->unit->use_strict(TRUE);
	}
	
	function index()
	{
		$this->load->library('ignitedquery');
		$iq =& $this->ignitedquery;
		
		// =============================
		// = ========= EMPTY ========= =
		// =============================
		
		// need to use get_select_sql() on these two to avoid the show_error() function
		
		//$this->run($iq->get_select_sql(), false, 'Empty object');
		
		$iq->from('foo');
		$iq->reset();
		$this->run(empty($iq->q_from), true, 'Resetting Object');
		
		// =============================
		// = ========= FLAGS ========= =
		// =============================
		
		$iq->q_as = false;
		$iq->alias('bar');
		$this->unit->run($iq->q_as, false, 'As flag set to false');
		
		// =============================
		// = ========== FROM ========= =
		// =============================
	
		$this->run($iq->from('users')->_build_get_query(),
'SELECT *
FROM `users`', 'From single table');

		$iq->from('users');
		$this->run($iq->from('posts')->_build_get_query(),
'SELECT *
FROM `users`, `posts`', 'From multiple tables (sequence)');

		$this->run($iq->from(array('users','posts'))->_build_get_query(),
'SELECT *
FROM `users`, `posts`','From multiple tables (array)');
		
		$iq->from('data');
		$this->run($iq->from(array('users','posts'))->_build_get_query(),
'SELECT *
FROM `data`, `users`, `posts`','From multiple tables (sequence + array)');
		
		$this->run($iq->from()
							->from('users')
						->end()->_build_get_query(),
'SELECT *
FROM (SELECT *
FROM `users`)', 'Subquery from from()');
		
		$obj = new IgnitedQuery();
		$obj->from('users');
		$this->run($iq->from($obj)->_build_get_query(),
'SELECT *
FROM (SELECT *
FROM `users`)', 'Subquery as parameter to from()');
		
		// =============================
		// = ========= SELECT ======== =
		// =============================
		
		$iq->from('foo');
		$this->run($iq->select('lol')->_build_get_query(),
'SELECT `lol`
FROM `foo`', 'Select single column');
		
		$iq->select('lol')->from('foo');
		$this->run($iq->select('longcat')->_build_get_query(),
'SELECT `lol`, `longcat`
FROM `foo`', 'Select multiple columns (sequence)');
		
		$iq->from('foo');
		$this->run($iq->select(array('lol','longcat'))->_build_get_query(),
'SELECT `lol`, `longcat`
FROM `foo`', 'Select multiple columns (array)');
		
		$iq->select('bar')->from('foo');
		$this->run($iq->select(array('lol','longcat'))->_build_get_query(),
'SELECT `bar`, `lol`, `longcat`
FROM `foo`', 'Select multiple columns (sequence + array)');
		
		$iq->select('foo AS bar');
		$iq->from('test AS lol');
		$this->run($iq->_build_get_query(),
'SELECT `foo` AS `bar`
FROM `test` AS `lol`', 'Select and from escaping with aliases');
		
		// =============================
		// = ========== WHERE ======== =
		// =============================
		
		$iq->from('foo');
		$iq->where('a','b');
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE `a` = \'b\'', 'WHERE(\'a\',\'b\')');
		
		$iq->from('foo');
		$iq->where('a >','b');
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE `a` > \'b\'', 'WHERE(\'a >\',\'b\')');
		
		$iq->from('foo');
		$iq->where('a > b');
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE `a` > `b`', 'WHERE(\'a > b\'), escape');
		
		$iq->from('foo');
		$iq->where('a > b',null,false);
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE a > b', 'WHERE(\'a > b\'), no escape');
		
		$iq->from('foo');
		$iq->where('a > b + 1',null,false);
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE a > b + 1', 'WHERE(\'a > b + 1\'), no escape');
		
		$iq->from('foo');
		$iq->where('a','b');
		$iq->where('c','d');
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE `a` = \'b\' AND `c` = \'d\'', 'WHERE(\'a\',\'b\') AND (\'c\',\'d\')');
		
		$iq->from('foo');
		$iq->where('a','b');
		$iq->or_where('c','d');
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE `a` = \'b\' OR `c` = \'d\'', 'WHERE(\'a\',\'b\') OR (\'c\',\'d\')');
		
		$iq->from('foo');
		$iq->or_where('c','d');
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE `c` = \'d\'', 'WHERE OR (\'c\',\'d\')');
		
		$iq->from('foo');
		$iq->not_where('a','b');
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE NOT `a` = \'b\'', 'NOT_WHERE(\'a\',\'b\')');
		
		$iq->from('foo');
		$iq->where('a','b');
		$iq->not_where('c','d');
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE `a` = \'b\' AND NOT `c` = \'d\'', 'WHERE(\'a\',\'b\') AND NOT (\'c\',\'d\')');
		
		$iq->from('foo');
		$iq->where('a','b');
		$iq->or_not_where('c','d');
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE `a` = \'b\' OR NOT `c` = \'d\'', 'WHERE(\'a\',\'b\') OR NOT (\'c\',\'d\')');
		
		$iq->from('foo');
		$iq->or_not_where('c','d');
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE NOT `c` = \'d\'', 'WHERE OR NOT (\'c\',\'d\')');
		
		$iq->from('foo');
		$sub =& $iq->where();
		$sub->where('a','b');
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE (`a` = \'b\')', 'WHERE SUB (\'a\',\'b\')');
		
		$iq->from('foo');
		$sub =& $iq->not_where();
		$sub->where('a','b');
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE NOT (`a` = \'b\')', 'WHERE NOT SUB (\'a\',\'b\')');
		
		$iq->from('foo');
		$iq->where('c','d');
		$sub =& $iq->or_not_where();
		$sub->where('a','b');
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE `c` = \'d\' OR NOT (`a` = \'b\')', 'WHERE (\'c\',\'d\') OR NOT SUB (\'a\',\'b\')');
		
		$iq->from('foo');
		$sub =& $iq->or_not_where();
		$sub->where('a','b');
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE NOT (`a` = \'b\')', 'WHERE OR NOT SUB (\'a\',\'b\')');
		
		$iq->from('foo');
		$sub = new IgnitedQuery();
		$sub->where('a','b');
		$sub->or_where('a','c');
		$sub->from('bar');
		$iq->where($sub);
		$iq->where('b','c');
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE (`a` = \'b\' OR `a` = \'c\') AND `b` = \'c\'', 'WHERE SUB ((\'a\',\'b\') OR (\'a\',\'c\')) AND (\'b\',\'c\')');
		
		// =============================
		// = ======== WHERE IN ======= =
		// =============================
		
		// =============================
		// = ========== LIKE ========= =
		// =============================
		
		$iq->from('foo');
		$iq->like('bar', 'str');
		$this->run($iq->_build_get_query(),
'SELECT *
FROM `foo`
WHERE `bar` LIKE \'%str%\'', 'LIKE');
		
		// =============================
		// = ===== QUERY CACHING ===== =
		// =============================
		
		$iq->start_cache();
		$iq->select('field1');
		$iq->stop_cache();
		$iq->from('tablename');
		$this->run($iq->_build_get_query(),
'SELECT `field1` FROM `tablename`', 'Caching field1, no cache of tablename');

		$iq->select('field2');
		$iq->from('tablename');
		$this->run($iq->_build_get_query(),
'SELECT `field1`, `field2` FROM `tablename`', 'No cache, uses data from the cache');
		
		$iq->flush_cache();
		
		$iq->select('field2');
		$iq->from('tablename');
	$this->run($iq->_build_get_query(),
'SELECT `field2` FROM `tablename`', 'Flushed cache');
		
		// =============================
		// = ======= SUBQUERIES ====== =
		// =============================
		
		// FROM
		
		$this->run($iq->from()
							->from('users')
							->where('id',1)
						->end()
					->_build_get_query(),
'SELECT *
FROM (SELECT *
FROM `users`
WHERE `id` = 1)','SELECT * FROM (SELECT * FROM users WHERE id = 1)');

		/*
		u($iq->select(array('a','b','bb' => 'c'))
			 	->from()
			 		->from('tree_table')
			 		->alias('red')
			 	->end()
				->where(array('b = 2','c = 3','d' => 4))
				->or_where()
					->where('code =',2)
					->where('data =', 'blaa')
				->end()
				->where_in('title',array('about','lol'))
				->where_not_in('blaa')
					->select('username')
					->from('users')
				->end()
			->_build_get_query());
		u(str_replace("\n",' ',
		$this->db->select(array('a','b','c'))
				->select_avg('depth')
				->from('table')
				->where('a','b')
				->where('b !=','c')
				->or_where('a','c')
				->or_where_in('a',array('alpha','beta','delta'))
				->group_by('aaa')
				->limit(2)
				->offset(4)
			->_compile_select()));
		u(str_replace("\n",' ',
		$iq->select(array('a','b','c'))
			 	->select_avg('depth')
				->from('table')
				->where('a','b')
				->where('b !=','c')
				->or_where('a','c')
				->or_where_in('a',array('alpha','beta','delta'))
				->group_by('aaa')
				->limit(2)
				->offset(4)
			->_build_get_query()));
		*/
		
		echo $this->unit->summary_report();
	}
	
	// --------------------------------------------------------------------
		
	/**
	 * A special function that uses regex that does not care if there are one space, or 20.
	 * 
	 * @param $result The result to compare with $expected
	 * @param $expected The expected result
	 * @param $message The message
	 */
	function run($result, $expected, $message = '')
	{
		$to_cmp = '@^'.preg_replace('@\s+@', '\s+', preg_quote($expected)).'$@';
		$bool = (preg_match($to_cmp, $result) == 1);
		if( ! $bool){
			echo "<pre>\nFailed: $message\nActual Result:   ";
			var_dump(preg_replace('@\s@',' ',$result));
			echo "Expected Result: ";
			var_dump(preg_replace('@\s@',' ',$expected));
			echo "</pre>";
		}
		$this->unit->run($bool, true, $message);
	}
}
?>