From e8c19ca2f8ed6a0fd0012c6c97e78aac80fded30 Mon Sep 17 00:00:00 2001 From: Sergey Tsalkov Date: Fri, 10 Jul 2020 01:13:31 +0000 Subject: [PATCH] clean up update() and delete(), both will now be compatible with named and numbered arguments resolves #54 --- db.class.php | 25 +++++++++++++------------ simpletest/BasicTest.php | 20 +++++++++++++++++--- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/db.class.php b/db.class.php index 90963e1..c90c48a 100644 --- a/db.class.php +++ b/db.class.php @@ -308,14 +308,15 @@ class MeekroDB { $args = func_get_args(); $table = array_shift($args); $params = array_shift($args); - $where = array_shift($args); - - $query = str_replace('%', $this->param_char, "UPDATE %b SET %hc WHERE ") . $where; - - array_unshift($args, $params); - array_unshift($args, $table); - array_unshift($args, $query); - return call_user_func_array(array($this, 'query'), $args); + + $update_part = $this->parseQueryParams( + str_replace('%', $this->param_char, "UPDATE %b SET %hc"), + $table, $params + ); + + $where_part = call_user_func_array(array($this, 'parseQueryParams'), $args); + $query = $update_part . ' WHERE ' . $where_part; + return $this->query($query); } public function insertOrReplace($which, $table, $datas, $options=array()) { @@ -387,10 +388,10 @@ class MeekroDB { public function delete() { $args = func_get_args(); $table = $this->formatTableName(array_shift($args)); - $where = array_shift($args); - $buildquery = "DELETE FROM $table WHERE $where"; - array_unshift($args, $buildquery); - return call_user_func_array(array($this, 'query'), $args); + + $where = call_user_func_array(array($this, 'parseQueryParams'), $args); + $query = "DELETE FROM {$table} WHERE {$where}"; + return $this->query($query); } public function sqleval() { diff --git a/simpletest/BasicTest.php b/simpletest/BasicTest.php index 9fc1fcb..7ff3f6d 100644 --- a/simpletest/BasicTest.php +++ b/simpletest/BasicTest.php @@ -23,6 +23,11 @@ class BasicTest extends SimpleTest { `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `signature` VARCHAR( 255 ) NULL DEFAULT 'donewriting' ) ENGINE = InnoDB"); + + DB::query("CREATE TABLE `fake%s_table` ( + `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , + `name` VARCHAR( 255 ) NULL DEFAULT 'blah' + ) ENGINE = InnoDB"); $mysqli = DB::get(); DB::disconnect(); @@ -162,12 +167,12 @@ class BasicTest extends SimpleTest { $this->assert($columnlist[5] === 'height'); $tablelist = DB::tableList(); - $this->assert(count($tablelist) === 2); + $this->assert(count($tablelist) === 3); $this->assert($tablelist[0] === 'accounts'); $tablelist = null; $tablelist = DB::tableList(DB::$dbName); - $this->assert(count($tablelist) === 2); + $this->assert(count($tablelist) === 3); $this->assert($tablelist[0] === 'accounts'); } @@ -186,7 +191,7 @@ class BasicTest extends SimpleTest { $true = DB::update('accounts', array( 'password' => DB::sqleval("REPEAT('blah', %i)", 4), 'favorite_word' => null, - ), 'username=%s', 'newguy'); + ), 'username=%s_name', array('name' => 'newguy')); $row = null; $row = DB::queryOneRow("SELECT * FROM accounts WHERE username=%s", 'newguy'); @@ -414,7 +419,16 @@ class BasicTest extends SimpleTest { DB::update('profile',array('signature'=> "%li "),"id = %d",1); $signature = DB::queryFirstField("SELECT signature FROM profile WHERE id=%i", 1); $this->assert($signature === "%li "); + } + function test_902_faketable() { + DB::insert('fake%s_table', array('name' => 'karen')); + $count = DB::queryFirstField("SELECT COUNT(*) FROM %b", 'fake%s_table'); + $this->assert($count === '1'); + DB::update('fake%s_table', array('name' => 'haren%s'), 'name=%s_name', array('name' => 'karen')); + DB::delete('fake%s_table', 'name=%s0', 'haren%s'); + $count = DB::queryFirstField("SELECT COUNT(*) FROM %b", 'fake%s_table'); + $this->assert($count === '0'); }