Skip to content

Commit e8c19ca

Browse files
committed
clean up update() and delete(), both will now be compatible with named and numbered arguments
resolves #54
1 parent fa46824 commit e8c19ca

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

db.class.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,15 @@ public function update() {
308308
$args = func_get_args();
309309
$table = array_shift($args);
310310
$params = array_shift($args);
311-
$where = array_shift($args);
312-
313-
$query = str_replace('%', $this->param_char, "UPDATE %b SET %hc WHERE ") . $where;
314-
315-
array_unshift($args, $params);
316-
array_unshift($args, $table);
317-
array_unshift($args, $query);
318-
return call_user_func_array(array($this, 'query'), $args);
311+
312+
$update_part = $this->parseQueryParams(
313+
str_replace('%', $this->param_char, "UPDATE %b SET %hc"),
314+
$table, $params
315+
);
316+
317+
$where_part = call_user_func_array(array($this, 'parseQueryParams'), $args);
318+
$query = $update_part . ' WHERE ' . $where_part;
319+
return $this->query($query);
319320
}
320321

321322
public function insertOrReplace($which, $table, $datas, $options=array()) {
@@ -387,10 +388,10 @@ public function insertUpdate() {
387388
public function delete() {
388389
$args = func_get_args();
389390
$table = $this->formatTableName(array_shift($args));
390-
$where = array_shift($args);
391-
$buildquery = "DELETE FROM $table WHERE $where";
392-
array_unshift($args, $buildquery);
393-
return call_user_func_array(array($this, 'query'), $args);
391+
392+
$where = call_user_func_array(array($this, 'parseQueryParams'), $args);
393+
$query = "DELETE FROM {$table} WHERE {$where}";
394+
return $this->query($query);
394395
}
395396

396397
public function sqleval() {

simpletest/BasicTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ function test_1_create_table() {
2323
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
2424
`signature` VARCHAR( 255 ) NULL DEFAULT 'donewriting'
2525
) ENGINE = InnoDB");
26+
27+
DB::query("CREATE TABLE `fake%s_table` (
28+
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
29+
`name` VARCHAR( 255 ) NULL DEFAULT 'blah'
30+
) ENGINE = InnoDB");
2631

2732
$mysqli = DB::get();
2833
DB::disconnect();
@@ -162,12 +167,12 @@ function test_4_query() {
162167
$this->assert($columnlist[5] === 'height');
163168

164169
$tablelist = DB::tableList();
165-
$this->assert(count($tablelist) === 2);
170+
$this->assert(count($tablelist) === 3);
166171
$this->assert($tablelist[0] === 'accounts');
167172

168173
$tablelist = null;
169174
$tablelist = DB::tableList(DB::$dbName);
170-
$this->assert(count($tablelist) === 2);
175+
$this->assert(count($tablelist) === 3);
171176
$this->assert($tablelist[0] === 'accounts');
172177
}
173178

@@ -186,7 +191,7 @@ function test_4_1_query() {
186191
$true = DB::update('accounts', array(
187192
'password' => DB::sqleval("REPEAT('blah', %i)", 4),
188193
'favorite_word' => null,
189-
), 'username=%s', 'newguy');
194+
), 'username=%s_name', array('name' => 'newguy'));
190195

191196
$row = null;
192197
$row = DB::queryOneRow("SELECT * FROM accounts WHERE username=%s", 'newguy');
@@ -414,7 +419,16 @@ function test_901_updatewithspecialchar() {
414419
DB::update('profile',array('signature'=> "%li "),"id = %d",1);
415420
$signature = DB::queryFirstField("SELECT signature FROM profile WHERE id=%i", 1);
416421
$this->assert($signature === "%li ");
422+
}
417423

424+
function test_902_faketable() {
425+
DB::insert('fake%s_table', array('name' => 'karen'));
426+
$count = DB::queryFirstField("SELECT COUNT(*) FROM %b", 'fake%s_table');
427+
$this->assert($count === '1');
428+
DB::update('fake%s_table', array('name' => 'haren%s'), 'name=%s_name', array('name' => 'karen'));
429+
DB::delete('fake%s_table', 'name=%s0', 'haren%s');
430+
$count = DB::queryFirstField("SELECT COUNT(*) FROM %b", 'fake%s_table');
431+
$this->assert($count === '0');
418432
}
419433

420434

0 commit comments

Comments
 (0)