Added 'get_where' method to query builder

Also added prep work for select_* methods
This commit is contained in:
Timothy Warren 2012-04-13 11:53:11 -04:00
parent ffb454e95f
commit 15e7ef0832
9 changed files with 183 additions and 76 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
test_config.json
index.html
tests/db_files/*
._*

View File

@ -45,7 +45,7 @@ Create a connection array or object similar to this:
The parameters required depend on the database.
### Running Queries
Query uses the same interface as CodeIgniter's [Active Record class](http://codeigniter.com/user_guide/database/active_record.html). However, it does not implement the `select_` methods, `count_all_results`, `distinct`, `having`, `or_having`, `get_compiled_query`, `insert_batch`, `update_batch`, or `count_all` methods.
Query uses the same interface as CodeIgniter's [Active Record class](http://codeigniter.com/user_guide/database/active_record.html). However, it does not implement the `select_` methods, `count_all_results`, `distinct`, `having`, `or_having`, `insert_batch`, `update_batch`, or `count_all` methods.
#### Retrieving Results

View File

@ -17,6 +17,60 @@
*/
abstract class DB_SQL {
// --------------------------------------------------------------------------
// ! Methods to override
// --------------------------------------------------------------------------
/**
* Get the max keyword sql
*
* @return string
*/
public function max()
{
return ' MAX';
}
// --------------------------------------------------------------------------
/**
* Get the min keyword sql
*
* @return string
*/
public function min()
{
return ' MIN';
}
// --------------------------------------------------------------------------
/**
* Get the 'distinct' keyword
*
* @return string
*/
public function distinct()
{
return ' DISTINCT';
}
// --------------------------------------------------------------------------
/**
* Get the 'average' keyword
*
* @return string
*/
public function avg()
{
return ' AVG';
}
// --------------------------------------------------------------------------
// ! Abstract Methods
// --------------------------------------------------------------------------
/**
* Get database-specific sql to create a new table
*

View File

@ -717,6 +717,82 @@ class Query_Builder {
return $this;
}
// --------------------------------------------------------------------------
// ! Query Grouping Methods
// --------------------------------------------------------------------------
/**
* Adds a paren to the current query for query grouping
*
* @return $this
*/
public function group_start()
{
$this->query_map[] = array(
'type' => 'group_start',
'conjunction' => '',
'string' => ' ('
);
return $this;
}
// --------------------------------------------------------------------------
/**
* Adds a paren to the current query for query grouping,
* prefixed with 'OR'
*
* @return $this
*/
public function or_group_start()
{
$this->query_map[] = array(
'type' => 'group_start',
'conjunction' => '',
'string' => ' OR ('
);
return $this;
}
// --------------------------------------------------------------------------
/**
* Adds a paren to the current query for query grouping,
* prefixed with 'OR NOT'
*
* @return $this
*/
public function or_not_group_start()
{
$this->query_map[] = array(
'type' => 'group_start',
'conjunction' => '',
'string' => ' OR NOT ('
);
return $this;
}
// --------------------------------------------------------------------------
/**
* Ends a query group
*
* @return $this
*/
public function group_end()
{
$this->query_map[] = array(
'type' => 'group_end',
'conjunction' => '',
'string' => ' ) '
);
return $this;
}
// --------------------------------------------------------------------------
// ! Query execution methods
// --------------------------------------------------------------------------
@ -765,6 +841,26 @@ class Query_Builder {
// --------------------------------------------------------------------------
/**
* Convience method for get() with a where clause
*
* @param string $table
* @param array $where
* @param int $limit
* @param int $offset
* @return object
*/
public function get_where($table, $where=array(), $limit=FALSE, $offset=FALSE)
{
// Create the where clause
$this->where($where);
// Return the result
return $this->get($table, $limit, $offset);
}
// --------------------------------------------------------------------------
/**
* Creates an insert clause, and executes it
*
@ -844,81 +940,6 @@ class Query_Builder {
return $res;
}
// --------------------------------------------------------------------------
// ! Query Grouping Methods
// --------------------------------------------------------------------------
/**
* Adds a paren to the current query for query grouping
*
* @return $this
*/
public function group_start()
{
$this->query_map[] = array(
'type' => 'group_start',
'conjunction' => '',
'string' => ' ('
);
return $this;
}
// --------------------------------------------------------------------------
/**
* Adds a paren to the current query for query grouping,
* prefixed with 'OR'
*
* @return $this
*/
public function or_group_start()
{
$this->query_map[] = array(
'type' => 'group_start',
'conjunction' => '',
'string' => ' OR ('
);
return $this;
}
// --------------------------------------------------------------------------
/**
* Adds a paren to the current query for query grouping,
* prefixed with 'OR NOT'
*
* @return $this
*/
public function or_not_group_start()
{
$this->query_map[] = array(
'type' => 'group_start',
'conjunction' => '',
'string' => ' OR NOT ('
);
return $this;
}
// --------------------------------------------------------------------------
/**
* Ends a query group
*
* @return $this
*/
public function group_end()
{
$this->query_map[] = array(
'type' => 'group_end',
'conjunction' => '',
'string' => ' ) '
);
return $this;
}
// --------------------------------------------------------------------------
// ! Miscellaneous Methods

View File

@ -21,6 +21,17 @@
*/
class ODBC extends DB_PDO {
// Don't define the escape char - or define it in sub-drivers in a refactor
protected $escape_char = '';
/**
* Use ODBC to connect to a database
*
* @param string $dsn
* @param string $username
* @param string $password
* @param array $options
*/
public function __construct($dsn, $username=null, $password=null, $options=array())
{
parent::__construct("odbc:$dsn", $username, $password, $options);
@ -33,6 +44,8 @@ class ODBC extends DB_PDO {
/**
* Doesn't apply to ODBC
*
* @return bool
*/
public function switch_db($name)
{

View File

@ -76,6 +76,15 @@ class FirebirdQBTest extends QBTest {
$this->assertIsA($query, 'Firebird_Result');
}
function TestGetWhere()
{
if (empty($this->db)) return;
$query = $this->db->get_where('create_test', array('id !=' => 1), 2, 1);
$this->assertIsA($query, 'Firebird_Result');
}
function TestSelectGet()
{
$query = $this->db->select('id, key as k, val')

Binary file not shown.

Binary file not shown.

View File

@ -107,6 +107,15 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement');
}
function TestGetWhere()
{
if (empty($this->db)) return;
$query = $this->db->get_where('create_test', array('id !=' => 1), 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
function TestSelectGet()
{
if (empty($this->db)) return;