Added limit clause to db drivers

This commit is contained in:
Timothy Warren 2012-02-29 18:33:21 -05:00
parent dafbdbf4d2
commit 955537cdb7
6 changed files with 97 additions and 9 deletions

View File

@ -213,7 +213,6 @@ abstract class DB_SQL {
* @param array $columns * @param array $columns
* @param array $constraints * @param array $constraints
* @param array $indexes * @param array $indexes
*
* @return string * @return string
*/ */
abstract public function create_table($name, $columns, $constraints=array(), $indexes=array()); abstract public function create_table($name, $columns, $constraints=array(), $indexes=array());
@ -222,11 +221,20 @@ abstract class DB_SQL {
* Get database-specific sql to drop a table * Get database-specific sql to drop a table
* *
* @param string $name * @param string $name
*
* @return string * @return string
*/ */
abstract public function delete_table($name); abstract public function delete_table($name);
/**
* Get database specific sql for limit clause
*
* @param string $sql
* @param int $limiit
* @param int $offset
* @return string
*/
abstract public function limit($sql, $limit, $offset);
} }
// End of db_pdo.php // End of db_pdo.php

View File

@ -85,5 +85,30 @@ class Firebird_SQL extends DB_SQL {
{ {
return 'DROP TABLE "'.$name.'"'; return 'DROP TABLE "'.$name.'"';
} }
/**
* Limit clause
*
* @param string $sql
* @param int $limit
* @param int $offset
* @return string
*/
public function limit($sql, $limit, $offset=FALSE)
{
// Keep the current sql string safe for a moment
$orig_sql = $sql;
$sql = 'FIRST '. (int) $limit;
if ($offset > 0)
{
$sql .= ' SKIP'. (int) $offset;
}
$sql = preg_replace("`SELECT`i", "SELECT {$sql}", $orig_sql);
return $sql;
}
} }
//End of firebird_sql.php //End of firebird_sql.php

View File

@ -41,6 +41,24 @@
public function delete_table($name) public function delete_table($name)
{ {
return "DROP TABLE `{$name}`"; return "DROP TABLE `{$name}`";
}
/**
* Limit clause
*
* @param string $sql
* @param int $limit
* @param int $offset
* @return string
*/
public function limit($sql, $limit, $offset=FALSE)
{
if ( ! is_numeric($offset))
{
return $sql." LIMIT {$limit}";
}
return $sql." LIMIT {$offset}, {$limit}";
} }
} }
//End of mysql_sql.php //End of mysql_sql.php

View File

@ -27,5 +27,18 @@ class ODBC_SQL extends DB_SQL {
{ {
return "DROP TABLE {$name}"; return "DROP TABLE {$name}";
} }
/**
* Limit clause
*
* @param string $sql
* @param int $limit
* @param int $offset
* @return string
*/
public function limit($sql, $limit, $offset=FALSE)
{
}
} }
// End of odbc_sql.php // End of odbc_sql.php

View File

@ -27,5 +27,25 @@ class pgSQL_SQL extends DB_SQL {
return 'DROP TABLE "'.$name.'"'; return 'DROP TABLE "'.$name.'"';
} }
/**
* Limit clause
*
* @param string $sql
* @param int $limit
* @param int $offset
* @return string
*/
public function limit($sql, $limit, $offset=FALSE)
{
$sql .= " LIMIT {$limit}";
if(is_numeric($offset))
{
$sql .= " OFFSET {$offset}";
}
return $sql;
}
} }
//End of pgsql_manip.php //End of pgsql_manip.php

View File

@ -86,17 +86,21 @@ class SQLite_SQL extends DB_SQL {
} }
/** /**
* Create an sqlite database file * Limit clause
* *
* @param $path * @param string $sql
* @param int $limit
* @param int $offset
* @return string
*/ */
public function create_db($path) public function limit($sql, $limit, $offset=FALSE)
{ {
// Create the file if it doesn't exist if ( ! is_numeric($offset))
if( ! file_exists($path))
{ {
touch($path); return $sql." LIMIT {$limit}";
} }
return $sql." LIMIT {$offset}, {$limit}";
} }
} }
//End of sqlite_sql.php //End of sqlite_sql.php