From 955537cdb726c33890598c485524c80787d085b6 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 29 Feb 2012 18:33:21 -0500 Subject: [PATCH] Added limit clause to db drivers --- sys/common/db_pdo.php | 12 ++++++++++-- sys/databases/firebird_sql.php | 25 +++++++++++++++++++++++++ sys/databases/mysql_sql.php | 18 ++++++++++++++++++ sys/databases/odbc_sql.php | 13 +++++++++++++ sys/databases/pgsql_sql.php | 20 ++++++++++++++++++++ sys/databases/sqlite_sql.php | 18 +++++++++++------- 6 files changed, 97 insertions(+), 9 deletions(-) diff --git a/sys/common/db_pdo.php b/sys/common/db_pdo.php index d295f78..72d940c 100644 --- a/sys/common/db_pdo.php +++ b/sys/common/db_pdo.php @@ -213,7 +213,6 @@ abstract class DB_SQL { * @param array $columns * @param array $constraints * @param array $indexes - * * @return string */ 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 * * @param string $name - * * @return string */ 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 \ No newline at end of file diff --git a/sys/databases/firebird_sql.php b/sys/databases/firebird_sql.php index 800a749..83b1638 100644 --- a/sys/databases/firebird_sql.php +++ b/sys/databases/firebird_sql.php @@ -85,5 +85,30 @@ class Firebird_SQL extends DB_SQL { { 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 \ No newline at end of file diff --git a/sys/databases/mysql_sql.php b/sys/databases/mysql_sql.php index cc77203..ee4ad3b 100644 --- a/sys/databases/mysql_sql.php +++ b/sys/databases/mysql_sql.php @@ -41,6 +41,24 @@ public function delete_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 \ No newline at end of file diff --git a/sys/databases/odbc_sql.php b/sys/databases/odbc_sql.php index b6dfa47..6567ab5 100644 --- a/sys/databases/odbc_sql.php +++ b/sys/databases/odbc_sql.php @@ -27,5 +27,18 @@ class ODBC_SQL extends DB_SQL { { 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 \ No newline at end of file diff --git a/sys/databases/pgsql_sql.php b/sys/databases/pgsql_sql.php index c530d54..2c673a6 100644 --- a/sys/databases/pgsql_sql.php +++ b/sys/databases/pgsql_sql.php @@ -27,5 +27,25 @@ class pgSQL_SQL extends DB_SQL { 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 \ No newline at end of file diff --git a/sys/databases/sqlite_sql.php b/sys/databases/sqlite_sql.php index e23fd99..c5165e0 100644 --- a/sys/databases/sqlite_sql.php +++ b/sys/databases/sqlite_sql.php @@ -86,17 +86,21 @@ class SQLite_SQL extends DB_SQL { } /** - * Create an sqlite database file - * - * @param $path + * Limit clause + * + * @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( ! file_exists($path)) + if ( ! is_numeric($offset)) { - touch($path); + return $sql." LIMIT {$limit}"; } + + return $sql." LIMIT {$offset}, {$limit}"; } } //End of sqlite_sql.php \ No newline at end of file