Added 'get_where' method to query builder

Also added prep work for select_* methods
keep-around/97eccfd706a523136e243db5485bbc0c81bf7dd6
Timothy Warren 11 years ago
parent ffb454e95f
commit 15e7ef0832
  1. 1
      .gitignore
  2. 2
      README.md
  3. 54
      classes/db_sql.php
  4. 171
      classes/query_builder.php
  5. 13
      drivers/odbc/odbc_driver.php
  6. 9
      tests/databases/firebird/firebird-qb.php
  7. BIN
      tests/db_files/FB_TEST_DB.FDB
  8. BIN
      tests/db_files/test_sqlite.db
  9. 9
      tests/parent.php

1
.gitignore vendored

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

@ -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

@ -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
*

@ -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
// --------------------------------------------------------------------------
@ -763,6 +839,26 @@ class Query_Builder {
return $result;
}
// --------------------------------------------------------------------------
/**
* 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);
}
// --------------------------------------------------------------------------
/**
@ -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

@ -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)
{

@ -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.

@ -106,6 +106,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()
{

Loading…
Cancel
Save