clean up update() and delete(), both will now be compatible with named and numbered arguments

resolves #54
This commit is contained in:
Sergey Tsalkov
2020-07-10 01:13:31 +00:00
parent fa46824df1
commit e8c19ca2f8
2 changed files with 30 additions and 15 deletions

View File

@@ -308,14 +308,15 @@ class MeekroDB {
$args = func_get_args(); $args = func_get_args();
$table = array_shift($args); $table = array_shift($args);
$params = array_shift($args); $params = array_shift($args);
$where = array_shift($args);
$update_part = $this->parseQueryParams(
$query = str_replace('%', $this->param_char, "UPDATE %b SET %hc WHERE ") . $where; str_replace('%', $this->param_char, "UPDATE %b SET %hc"),
$table, $params
array_unshift($args, $params); );
array_unshift($args, $table);
array_unshift($args, $query); $where_part = call_user_func_array(array($this, 'parseQueryParams'), $args);
return call_user_func_array(array($this, 'query'), $args); $query = $update_part . ' WHERE ' . $where_part;
return $this->query($query);
} }
public function insertOrReplace($which, $table, $datas, $options=array()) { public function insertOrReplace($which, $table, $datas, $options=array()) {
@@ -387,10 +388,10 @@ class MeekroDB {
public function delete() { public function delete() {
$args = func_get_args(); $args = func_get_args();
$table = $this->formatTableName(array_shift($args)); $table = $this->formatTableName(array_shift($args));
$where = array_shift($args);
$buildquery = "DELETE FROM $table WHERE $where"; $where = call_user_func_array(array($this, 'parseQueryParams'), $args);
array_unshift($args, $buildquery); $query = "DELETE FROM {$table} WHERE {$where}";
return call_user_func_array(array($this, 'query'), $args); return $this->query($query);
} }
public function sqleval() { public function sqleval() {

View File

@@ -23,6 +23,11 @@ class BasicTest extends SimpleTest {
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`signature` VARCHAR( 255 ) NULL DEFAULT 'donewriting' `signature` VARCHAR( 255 ) NULL DEFAULT 'donewriting'
) ENGINE = InnoDB"); ) 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(); $mysqli = DB::get();
DB::disconnect(); DB::disconnect();
@@ -162,12 +167,12 @@ class BasicTest extends SimpleTest {
$this->assert($columnlist[5] === 'height'); $this->assert($columnlist[5] === 'height');
$tablelist = DB::tableList(); $tablelist = DB::tableList();
$this->assert(count($tablelist) === 2); $this->assert(count($tablelist) === 3);
$this->assert($tablelist[0] === 'accounts'); $this->assert($tablelist[0] === 'accounts');
$tablelist = null; $tablelist = null;
$tablelist = DB::tableList(DB::$dbName); $tablelist = DB::tableList(DB::$dbName);
$this->assert(count($tablelist) === 2); $this->assert(count($tablelist) === 3);
$this->assert($tablelist[0] === 'accounts'); $this->assert($tablelist[0] === 'accounts');
} }
@@ -186,7 +191,7 @@ class BasicTest extends SimpleTest {
$true = DB::update('accounts', array( $true = DB::update('accounts', array(
'password' => DB::sqleval("REPEAT('blah', %i)", 4), 'password' => DB::sqleval("REPEAT('blah', %i)", 4),
'favorite_word' => null, 'favorite_word' => null,
), 'username=%s', 'newguy'); ), 'username=%s_name', array('name' => 'newguy'));
$row = null; $row = null;
$row = DB::queryOneRow("SELECT * FROM accounts WHERE username=%s", 'newguy'); $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); DB::update('profile',array('signature'=> "%li "),"id = %d",1);
$signature = DB::queryFirstField("SELECT signature FROM profile WHERE id=%i", 1); $signature = DB::queryFirstField("SELECT signature FROM profile WHERE id=%i", 1);
$this->assert($signature === "%li "); $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');
} }