From 68774532e13eec0346104e2c05e6198b71c6b7a1 Mon Sep 17 00:00:00 2001 From: Sergey Tsalkov Date: Fri, 22 Apr 2011 23:15:14 -0400 Subject: [PATCH] add delete() function -- mostly for completeness with insert(), update(), etc --- db.class.php | 9 +++++++++ simpletest/BasicTest.php | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/db.class.php b/db.class.php index 6f24801..b0023b3 100644 --- a/db.class.php +++ b/db.class.php @@ -172,6 +172,15 @@ class DB return DB::insertOrReplace('REPLACE', $table, $data); } + public static function delete() { + $args = func_get_args(); + $table = self::formatTableName(array_shift($args)); + $where = array_shift($args); + $buildquery = "DELETE FROM $table WHERE $where"; + array_unshift($args, $buildquery); + call_user_func_array('DB::queryNull', $args); + } + public static function sqleval($text) { return new MeekroDBEval($text); } diff --git a/simpletest/BasicTest.php b/simpletest/BasicTest.php index bb4cc8e..3e83fdc 100644 --- a/simpletest/BasicTest.php +++ b/simpletest/BasicTest.php @@ -155,6 +155,24 @@ class BasicTest extends SimpleTest { $this->assert(DB::affectedRows() === 1); } + function test_4_2_delete() { + DB::insert('accounts', array( + 'username' => 'gonesoon', + 'password' => 'something', + 'age' => 61, + 'height' => 199.194 + )); + + $ct = DB::queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s AND height=%d", 'gonesoon', 199.194); + $this->assert(intval($ct) === 1); + + DB::delete('accounts', 'username=%s AND age=%i AND height=%d', 'gonesoon', '61', '199.194'); + $this->assert(DB::affectedRows() === 1); + + $ct = DB::queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s AND height=%d", 'gonesoon', '199.194'); + $this->assert(intval($ct) === 0); + } + function test_5_error_handler() { global $error_callback_worked;