Added random function syntax to each driver

This commit is contained in:
Timothy Warren 2012-03-13 18:40:51 -04:00
parent 382664ff6d
commit 3767956481
10 changed files with 153 additions and 10 deletions

View File

@ -277,5 +277,12 @@ abstract class DB_SQL {
* @return string * @return string
*/ */
abstract public function limit($sql, $limit, $offset=FALSE); abstract public function limit($sql, $limit, $offset=FALSE);
/**
* Get the sql for random ordering
*
* @return string
*/
abstract public function random();
} }
// End of db_pdo.php // End of db_pdo.php

View File

@ -74,6 +74,8 @@ class Firebird_SQL extends DB_SQL {
return $sql; return $sql;
} }
// --------------------------------------------------------------------------
/** /**
* Drop the selected table * Drop the selected table
@ -85,6 +87,8 @@ class Firebird_SQL extends DB_SQL {
{ {
return 'DROP TABLE "'.$name.'"'; return 'DROP TABLE "'.$name.'"';
} }
// --------------------------------------------------------------------------
/** /**
* Limit clause * Limit clause
@ -110,5 +114,17 @@ class Firebird_SQL extends DB_SQL {
return $sql; return $sql;
} }
// --------------------------------------------------------------------------
/**
* Random ordering keyword
*
* @return string
*/
public function random()
{
return FALSE;
}
} }
//End of firebird_sql.php //End of firebird_sql.php

View File

@ -32,6 +32,8 @@
//TODO: implement //TODO: implement
} }
// --------------------------------------------------------------------------
/** /**
* Convience public function for droping a MySQL table * Convience public function for droping a MySQL table
* *
@ -42,6 +44,8 @@
{ {
return "DROP TABLE `{$name}`"; return "DROP TABLE `{$name}`";
} }
// --------------------------------------------------------------------------
/** /**
* Limit clause * Limit clause
@ -59,6 +63,18 @@
} }
return $sql." LIMIT {$offset}, {$limit}"; return $sql." LIMIT {$offset}, {$limit}";
}
// --------------------------------------------------------------------------
/**
* Random ordering keyword
*
* @return string
*/
public function random()
{
return ' RAND()';
} }
} }
//End of mysql_sql.php //End of mysql_sql.php

View File

@ -23,10 +23,20 @@ class ODBC_SQL extends DB_SQL {
return FALSE; return FALSE;
} }
// --------------------------------------------------------------------------
/**
* Remove a table from the database
*
* @param string $name
* @return string
*/
public function delete_table($name) public function delete_table($name)
{ {
return "DROP TABLE {$name}"; return "DROP TABLE {$name}";
} }
// --------------------------------------------------------------------------
/** /**
* Limit clause * Limit clause
@ -38,7 +48,19 @@ class ODBC_SQL extends DB_SQL {
*/ */
public function limit($sql, $limit, $offset=FALSE) public function limit($sql, $limit, $offset=FALSE)
{ {
return FALSE;
}
// --------------------------------------------------------------------------
/**
* Random ordering keyword
*
* @return string
*/
public function random()
{
return FALSE;
} }
} }
// End of odbc_sql.php // End of odbc_sql.php

View File

@ -21,11 +21,15 @@ class pgSQL_SQL extends DB_SQL {
{ {
//TODO: implement //TODO: implement
} }
// --------------------------------------------------------------------------
public function delete_table($name) public function delete_table($name)
{ {
return 'DROP TABLE "'.$name.'"'; return 'DROP TABLE "'.$name.'"';
} }
// --------------------------------------------------------------------------
/** /**
* Limit clause * Limit clause
@ -46,6 +50,18 @@ class pgSQL_SQL extends DB_SQL {
return $sql; return $sql;
} }
// --------------------------------------------------------------------------
/**
* Random ordering keyword
*
* @return string
*/
public function random()
{
return ' RANDOM()';
}
} }
//End of pgsql_manip.php //End of pgsql_manip.php

View File

@ -73,6 +73,8 @@ class SQLite_SQL extends DB_SQL {
return $sql; return $sql;
} }
// --------------------------------------------------------------------------
/** /**
* SQL to drop the specified table * SQL to drop the specified table
@ -84,6 +86,8 @@ class SQLite_SQL extends DB_SQL {
{ {
return 'DROP TABLE IF EXISTS "'.$name.'"'; return 'DROP TABLE IF EXISTS "'.$name.'"';
} }
// --------------------------------------------------------------------------
/** /**
* Limit clause * Limit clause
@ -102,5 +106,17 @@ class SQLite_SQL extends DB_SQL {
return $sql." LIMIT {$offset}, {$limit}"; return $sql." LIMIT {$offset}, {$limit}";
} }
// --------------------------------------------------------------------------
/**
* Random ordering keyword
*
* @return string
*/
public function random()
{
return ' RANDOM()';
}
} }
//End of sqlite_sql.php //End of sqlite_sql.php

View File

@ -28,14 +28,16 @@ class Query_Builder {
$insert_string, $insert_string,
$update_string, $update_string,
$set_string, $set_string,
$order_string; $order_string,
$group_string;
// Key value pairs // Key value pairs
private $where_array, private $where_array,
$like_array, $like_array,
$set_array, $set_array,
$set_array_keys, $set_array_keys,
$order_array; $order_array,
$group_array;
// Query-global components // Query-global components
private $limit, private $limit,
@ -233,10 +235,10 @@ class Query_Builder {
* Where clause prefixed with "OR" * Where clause prefixed with "OR"
* *
* @param string $field * @param string $field
* @param mixed $value * @param mixed $val
* @return $this * @return $this
*/ */
public function or_where($field, $value) public function or_where($field, $val=array())
{ {
// @todo Implement or_where method // @todo Implement or_where method
return $this; return $this;
@ -251,7 +253,7 @@ class Query_Builder {
* @param mixed $val * @param mixed $val
* @return $this * @return $this
*/ */
public function where_in($field, $val) public function where_in($field, $val=array())
{ {
// @todo Implement Where_in method // @todo Implement Where_in method
return $this; return $this;
@ -266,7 +268,7 @@ class Query_Builder {
* @param mixed $val * @param mixed $val
* @return $this * @return $this
*/ */
public function or_where_in($field, $val) public function or_where_in($field, $val=array())
{ {
// @todo Implement or_where_in method // @todo Implement or_where_in method
return $this; return $this;
@ -281,7 +283,7 @@ class Query_Builder {
* @param mixed $val * @param mixed $val
* @return $this * @return $this
*/ */
public function where_not_in($field, $val) public function where_not_in($field, $val=array())
{ {
// @todo Implement where_not_in method // @todo Implement where_not_in method
return $this; return $this;
@ -296,7 +298,7 @@ class Query_Builder {
* @param mixed $val * @param mixed $val
* @return $this * @return $this
*/ */
public function or_where_not_in($field, $val) public function or_where_not_in($field, $val=array())
{ {
// @todo Implement or_where_not_in method // @todo Implement or_where_not_in method
return $this; return $this;
@ -328,7 +330,17 @@ class Query_Builder {
*/ */
public function group_by($field) public function group_by($field)
{ {
// @todo Implement group_by method if ( ! is_scalar($field))
{
$this->group_array = array_map(array($this->db, 'quote_ident'), $field);
}
else
{
$this->group_array[] = $this->db->quote_ident($field);
}
$this->group_string = ' GROUP BY '.implode(', ', $this->group_array);
return $this; return $this;
} }
@ -615,6 +627,12 @@ class Query_Builder {
$sql .= $this->where_string; $sql .= $this->where_string;
} }
// Set the group_by string
if ( ! empty($this->group_string))
{
$sql .= $this->group_string;
}
// Set the order_by string // Set the order_by string
if ( ! empty($this->order_string)) if ( ! empty($this->order_string))
{ {

View File

@ -120,6 +120,22 @@ class FirebirdQBTest extends UnitTestCase {
$this->assertTrue(is_resource($query)); $this->assertTrue(is_resource($query));
} }
/*function TestGroupBy()
{
$query = $this->qb->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
->group_by('k')
->group_by('val')
->order_by('id', 'DESC')
->order_by('k', 'ASC')
->limit(5,2)
->get();
$this->assertTrue(is_resource($query));
}*/
function TestInsert() function TestInsert()
{ {
$query = $this->qb->set('id', 4) $query = $this->qb->set('id', 4)

View File

@ -114,6 +114,22 @@
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
function TestGroupBy()
{
$query = $this->qb->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
->group_by('k')
->group_by('val')
->order_by('id', 'DESC')
->order_by('k', 'ASC')
->limit(5,2)
->get();
$this->assertIsA($query, 'PDOStatement');
}
function TestInsert() function TestInsert()
{ {
$query = $this->qb->set('id', 4) $query = $this->qb->set('id', 4)

Binary file not shown.