2012-03-19 13:24:36 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Query
|
|
|
|
*
|
|
|
|
* Free Query Builder / Database Abstraction Layer
|
|
|
|
*
|
2012-04-20 13:17:39 -04:00
|
|
|
* @package Query
|
|
|
|
* @author Timothy J. Warren
|
2013-01-02 14:26:42 -05:00
|
|
|
* @copyright Copyright (c) 2012 - 2013
|
2012-03-19 13:24:36 -04:00
|
|
|
* @link https://github.com/aviat4ion/Query
|
2012-04-20 13:17:39 -04:00
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
2012-03-19 13:24:36 -04:00
|
|
|
*/
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:38:49 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query builder parent test class
|
|
|
|
*/
|
2012-03-19 13:24:36 -04:00
|
|
|
abstract class QBTest extends UnitTestCase {
|
|
|
|
|
2013-02-28 12:22:55 -05:00
|
|
|
public function __destruct()
|
|
|
|
{
|
2013-05-01 15:59:23 -04:00
|
|
|
//echo '<pre>' . print_r($this->db->queries, TRUE) . '</pre>';
|
2013-02-28 12:22:55 -05:00
|
|
|
}
|
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-17 08:29:28 -04:00
|
|
|
// ! Get Tests
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2013-02-28 12:22:55 -05:00
|
|
|
|
2012-11-08 20:02:31 -05:00
|
|
|
public function TestQueryFunctionAlias()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2013-02-28 12:22:55 -05:00
|
|
|
|
2012-11-08 20:02:31 -05:00
|
|
|
$db = Query();
|
2013-02-28 12:22:55 -05:00
|
|
|
|
2012-11-08 20:02:31 -05:00
|
|
|
$this->assertReference($this->db, $db);
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2013-04-25 08:15:56 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function TestFunctionGet()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$query = $this->db->select('id, COUNT(id) as count')
|
|
|
|
->from('test')
|
|
|
|
->group_by('id')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestGet()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->get('create_test');
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-11-07 08:42:34 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-11-08 14:28:49 -05:00
|
|
|
|
2012-11-07 08:42:34 -05:00
|
|
|
public function TestPrefixGet()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$query = $this->db->from('test')->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-11-08 14:28:49 -05:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-09-27 13:41:29 -04:00
|
|
|
public function TestGetWNumRows()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$query = $this->db->get('create_test');
|
|
|
|
$numrows = count($query->fetchAll(PDO::FETCH_NUM));
|
|
|
|
|
|
|
|
$this->assertEqual($this->db->num_rows(), $numrows);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestGetLimit()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->get('create_test', 2);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestGetLimitSkip()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->get('create_test', 2, 1);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestGetWhere()
|
2012-04-17 08:29:28 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 08:29:28 -04:00
|
|
|
$query = $this->db->get_where('create_test', array('id !=' => 1), 2, 1);
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 08:29:28 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestHaving()
|
2012-04-17 14:22:51 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 14:22:51 -04:00
|
|
|
$query = $this->db->select('id')
|
|
|
|
->from('create_test')
|
|
|
|
->group_by('id')
|
|
|
|
->having(array('id >' => 1))
|
|
|
|
->having('id !=', 3)
|
|
|
|
->get();
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 14:22:51 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestOrHaving()
|
2012-04-17 14:22:51 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 14:22:51 -04:00
|
|
|
$query = $this->db->select('id')
|
|
|
|
->from('create_test')
|
|
|
|
->group_by('id')
|
|
|
|
->having(array('id >' => 1))
|
|
|
|
->or_having('id !=', 3)
|
|
|
|
->get();
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 14:22:51 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestGetViews()
|
2012-04-17 08:29:28 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 08:29:28 -04:00
|
|
|
$this->assertTrue(is_array($this->db->get_views()));
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-17 08:29:28 -04:00
|
|
|
// ! Select Tests
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-03-19 13:24:36 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestSelectWhereGet()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->select('id, key as k, val')
|
|
|
|
->where('id >', 1)
|
|
|
|
->where('id <', 900)
|
|
|
|
->get('create_test', 2, 1);
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestSelectWhereGet2()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->select('id, key as k, val')
|
|
|
|
->where('id !=', 1)
|
|
|
|
->get('create_test', 2, 1);
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestSelectMax()
|
2012-04-13 12:49:13 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-13 12:49:13 -04:00
|
|
|
$query = $this->db->select_max('id', 'di')
|
2012-11-07 08:42:34 -05:00
|
|
|
->get('test');
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-13 12:49:13 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestSelectMin()
|
2012-04-13 12:49:13 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-13 12:49:13 -04:00
|
|
|
$query = $this->db->select_min('id', 'di')
|
|
|
|
->get('create_test');
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-13 12:49:13 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-22 21:17:27 -04:00
|
|
|
|
|
|
|
public function TestMultiOrderBy()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$query = $this->db->from('create_test')
|
|
|
|
->order_by('id, key')
|
|
|
|
->get();
|
2012-08-24 12:17:08 -04:00
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function TestWhereIn()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$query = $this->db->from('create_test')
|
|
|
|
->where_in('id', array(0, 6, 56, 563, 341))
|
|
|
|
->get();
|
2012-08-22 21:17:27 -04:00
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestSelectAvg()
|
2012-04-13 12:49:13 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-13 12:49:13 -04:00
|
|
|
$query = $this->db->select_avg('id', 'di')
|
|
|
|
->get('create_test');
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-13 12:49:13 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestSelectSum()
|
2012-04-13 12:49:13 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-13 12:49:13 -04:00
|
|
|
$query = $this->db->select_sum('id', 'di')
|
|
|
|
->get('create_test');
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-13 12:49:13 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestSelectDistinct()
|
2012-04-13 16:50:05 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-13 16:50:05 -04:00
|
|
|
$query = $this->db->select_sum('id', 'di')
|
|
|
|
->distinct()
|
|
|
|
->get('create_test');
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-13 16:50:05 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-03-19 13:24:36 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestSelectGet()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->select('id, key as k, val')
|
|
|
|
->get('create_test', 2, 1);
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestSelectFromGet()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->select('id, key as k, val')
|
|
|
|
->from('create_test ct')
|
|
|
|
->where('id >', 1)
|
|
|
|
->get();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestSelectFromLimitGet()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->select('id, key as k, val')
|
|
|
|
->from('create_test ct')
|
|
|
|
->where('id >', 1)
|
|
|
|
->limit(3)
|
|
|
|
->get();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-17 08:29:28 -04:00
|
|
|
// ! Query modifier tests
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestOrderBy()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->select('id, key as k, val')
|
|
|
|
->from('create_test')
|
|
|
|
->where('id >', 0)
|
|
|
|
->where('id <', 9000)
|
|
|
|
->order_by('id', 'DESC')
|
|
|
|
->order_by('k', 'ASC')
|
|
|
|
->limit(5,2)
|
|
|
|
->get();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestOrderByRandom()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->select('id, key as k, val')
|
|
|
|
->from('create_test')
|
|
|
|
->where('id >', 0)
|
|
|
|
->where('id <', 9000)
|
|
|
|
->order_by('id', 'rand')
|
|
|
|
->limit(5,2)
|
|
|
|
->get();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestGroupBy()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->select('id, key as k, val')
|
|
|
|
->from('create_test')
|
|
|
|
->where('id >', 0)
|
|
|
|
->where('id <', 9000)
|
|
|
|
->group_by('k')
|
2012-04-10 14:06:34 -04:00
|
|
|
->group_by('id')
|
2012-03-19 13:24:36 -04:00
|
|
|
->group_by('val')
|
|
|
|
->order_by('id', 'DESC')
|
|
|
|
->order_by('k', 'ASC')
|
|
|
|
->limit(5,2)
|
|
|
|
->get();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestOrWhere()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->select('id, key as k, val')
|
|
|
|
->from('create_test')
|
|
|
|
->where(' id ', 1)
|
|
|
|
->or_where('key >', 0)
|
|
|
|
->limit(2, 1)
|
|
|
|
->get();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestLike()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->from('create_test')
|
|
|
|
->like('key', 'og')
|
|
|
|
->get();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestJoin()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-11-07 08:42:34 -05:00
|
|
|
$query = $this->db->from('create_test ct')
|
|
|
|
->join('join cj', 'cj.id = ct.id')
|
2012-03-19 13:24:36 -04:00
|
|
|
->get();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2013-05-01 15:59:23 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function TestLeftJoin()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$query = $this->db->from('create_test ct')
|
|
|
|
->join('join cj', 'cj.id = ct.id', 'left')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function TestInnerJoin()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$query = $this->db->from('create_test ct')
|
|
|
|
->join('join cj', 'cj.id = ct.id', 'inner')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-17 08:29:28 -04:00
|
|
|
// ! DB update tests
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestInsert()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->set('id', 4)
|
|
|
|
->set('key', 4)
|
|
|
|
->set('val', 5)
|
2012-11-07 08:42:34 -05:00
|
|
|
->insert('test');
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2013-05-01 15:59:23 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function TestInsertBatch()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$insert_array = array(
|
|
|
|
array(
|
|
|
|
'id' => 6,
|
|
|
|
'key' => 2,
|
|
|
|
'val' => 3
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 5,
|
|
|
|
'key' => 6,
|
|
|
|
'val' => 7
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 8,
|
|
|
|
'key' => 1,
|
|
|
|
'val' => 2
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$query = $this->db->insert_batch('test', $insert_array);
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestUpdate()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->set('id', 4)
|
|
|
|
->set('key', 'gogle')
|
|
|
|
->set('val', 'non-word')
|
|
|
|
->where('id', 4)
|
|
|
|
->update('create_test');
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-10-24 20:19:39 -04:00
|
|
|
public function TestSetArrayUpdate()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$array = array(
|
|
|
|
'id' => 4,
|
|
|
|
'key' => 'gogle',
|
|
|
|
'val' => 'non-word'
|
|
|
|
);
|
|
|
|
|
|
|
|
$query = $this->db->set($array)
|
|
|
|
->where('id', 4)
|
|
|
|
->update('create_test');
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function TestWhereSetUpdate()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$query = $this->db->where('id', 4)
|
|
|
|
->set('id', 4)
|
|
|
|
->set('key', 'gogle')
|
|
|
|
->set('val', 'non-word')
|
2012-11-07 08:42:34 -05:00
|
|
|
->update('test');
|
2012-10-24 20:19:39 -04:00
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestDelete()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->where('id', 4)->delete('create_test');
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-17 08:29:28 -04:00
|
|
|
// ! Non-data read queries
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestCountAll()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-11-07 08:42:34 -05:00
|
|
|
$query = $this->db->count_all('test');
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 08:29:28 -04:00
|
|
|
$this->assertTrue(is_numeric($query));
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestCountAllResults()
|
2012-04-17 11:37:39 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-11-07 08:42:34 -05:00
|
|
|
$query = $this->db->count_all_results('test');
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 11:37:39 -04:00
|
|
|
$this->assertTrue(is_numeric($query));
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestCountAllResults2()
|
2012-04-17 11:37:39 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 11:37:39 -04:00
|
|
|
$query = $this->db->select('id, key as k, val')
|
2012-11-07 08:42:34 -05:00
|
|
|
->from('test')
|
2012-04-17 11:37:39 -04:00
|
|
|
->where(' id ', 1)
|
|
|
|
->or_where('key >', 0)
|
|
|
|
->limit(2, 1)
|
|
|
|
->count_all_results();
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 11:37:39 -04:00
|
|
|
$this->assertTrue(is_numeric($query));
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-24 14:00:44 -04:00
|
|
|
public function TestNumRows()
|
2012-04-20 16:33:40 -04:00
|
|
|
{
|
|
|
|
$query = $this->db->get('create_test');
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-20 16:33:40 -04:00
|
|
|
$this->assertTrue(is_numeric($this->db->num_rows()));
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-07-26 13:21:24 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-07-27 12:05:29 -04:00
|
|
|
// ! Error Tests
|
2012-07-26 13:21:24 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-07-26 13:21:24 -04:00
|
|
|
/**
|
|
|
|
* Handles invalid drivers
|
|
|
|
*/
|
|
|
|
public function TestBadDriver()
|
|
|
|
{
|
|
|
|
$params = array(
|
|
|
|
'host' => '127.0.0.1',
|
|
|
|
'port' => '3306',
|
|
|
|
'database' => 'test',
|
|
|
|
'user' => 'root',
|
|
|
|
'pass' => NULL,
|
|
|
|
'type' => 'QGYFHGEG'
|
|
|
|
);
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-07-26 13:21:24 -04:00
|
|
|
$this->expectException('BadDBDriverException');
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-11-08 14:28:49 -05:00
|
|
|
$this->db = Query($params);
|
2012-07-26 13:21:24 -04:00
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-07-27 12:05:29 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-07-27 12:05:29 -04:00
|
|
|
public function TestBadConnection()
|
|
|
|
{
|
|
|
|
$params = array(
|
|
|
|
'host' => '127.0.0.1',
|
|
|
|
'port' => '3306',
|
|
|
|
'database' => 'test',
|
|
|
|
'user' => NULL,
|
|
|
|
'pass' => NULL,
|
2012-07-27 12:08:55 -04:00
|
|
|
'type' => 'pgsql'
|
2012-07-27 12:05:29 -04:00
|
|
|
);
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-07-27 12:05:29 -04:00
|
|
|
$this->expectException('BadConnectionException');
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-11-08 14:28:49 -05:00
|
|
|
$this->db = Query($params);
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-07-27 12:05:29 -04:00
|
|
|
}
|
2012-03-19 13:24:36 -04:00
|
|
|
}
|
|
|
|
|
2012-04-13 16:50:05 -04:00
|
|
|
// End of db_qb_test.php
|