From 57fa7a5177af3fa926a4264a8a33bc97308b2817 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 13 Mar 2012 13:25:47 -0400 Subject: [PATCH] Implement delete query builder method, add empty_table db method --- sys/db/db_pdo.php | 16 ++++++++++++++++ sys/db/query_builder.php | 20 +++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/sys/db/db_pdo.php b/sys/db/db_pdo.php index 4c4b129..dd52982 100644 --- a/sys/db/db_pdo.php +++ b/sys/db/db_pdo.php @@ -176,6 +176,22 @@ abstract class DB_PDO extends PDO { // ------------------------------------------------------------------------- + /** + * Deletes all the rows from a table. Does the same as the truncate + * method if the database does not support 'TRUNCATE'; + * + * @param string $table + * @return mixed + */ + public function empty_table($table) + { + $sql = 'DELETE FROM '.$this->quote_ident($table); + + return $this->query($sql); + } + + // ------------------------------------------------------------------------- + /** * Abstract public functions to override in child classes */ diff --git a/sys/db/query_builder.php b/sys/db/query_builder.php index fa88a4f..fce6e5b 100644 --- a/sys/db/query_builder.php +++ b/sys/db/query_builder.php @@ -503,11 +503,19 @@ class Query_Builder { * * @param string $table * @param mixed $where - * @return + * @return mixed */ public function delete($table, $where='') { - // @todo implement delete method + // Set the where clause + $this->where($where); + + // Create the SQL and parameters + $sql = $this->_compile("delete", $table); + $params = array_values($this->where_array); + + // Delete the table, and return the result + return $this->db->prepare_execute($sql, $params); } // -------------------------------------------------------------------------- @@ -600,7 +608,13 @@ class Query_Builder { break; case "delete": - // @todo Implement delete statements + $sql = 'DELETE FROM '.$this->db->quote_ident($table); + + if ( ! empty($this->where_string)) + { + $sql .= $this->where_string; + } + break; }