insertUpdate() can now handle a second array for the update component, or nothing, so it'll
re-use the insert array for the update component
This commit is contained in:
27
db.class.php
27
db.class.php
@@ -170,7 +170,6 @@ class DB
|
||||
$keyval = array();
|
||||
foreach ($params as $key => $value) {
|
||||
$value = DB::sanitize($value);
|
||||
|
||||
$keyval[] = "`" . $key . "`=" . $value;
|
||||
}
|
||||
|
||||
@@ -217,8 +216,7 @@ class DB
|
||||
DB::queryNull("INSERT IGNORE INTO $table ($keys_str) VALUES $values_str");
|
||||
|
||||
} else if (isset($options['update']) && $options['update'] && strtolower($which) == 'insert') {
|
||||
$updatestr = call_user_func_array('DB::parseQueryParams', $options['update']);
|
||||
DB::queryNull("INSERT INTO $table ($keys_str) VALUES $values_str ON DUPLICATE KEY UPDATE $updatestr");
|
||||
DB::queryNull("INSERT INTO $table ($keys_str) VALUES $values_str ON DUPLICATE KEY UPDATE {$options['update']}");
|
||||
|
||||
} else {
|
||||
DB::queryNull("$which INTO $table ($keys_str) VALUES $values_str");
|
||||
@@ -233,7 +231,28 @@ class DB
|
||||
$args = func_get_args();
|
||||
$table = array_shift($args);
|
||||
$data = array_shift($args);
|
||||
return DB::insertOrReplace('INSERT', $table, $data, array('update' => $args));
|
||||
|
||||
if (! isset($args[0])) { // update will have all the data of the insert
|
||||
if (isset($data[0]) && is_array($data[0])) { //multiple insert rows specified -- failing!
|
||||
DB::nonSQLError("Badly formatted insertUpdate() query -- you didn't specify the update component!");
|
||||
}
|
||||
|
||||
$args[0] = $data;
|
||||
}
|
||||
|
||||
if (is_array($args[0])) {
|
||||
$keyval = array();
|
||||
foreach ($args[0] as $key => $value) {
|
||||
$value = DB::sanitize($value);
|
||||
$keyval[] = "`" . $key . "`=" . $value;
|
||||
}
|
||||
$updatestr = implode(', ', $keyval);
|
||||
|
||||
} else {
|
||||
$updatestr = call_user_func_array('DB::parseQueryParams', $args);
|
||||
}
|
||||
|
||||
return DB::insertOrReplace('INSERT', $table, $data, array('update' => $updatestr));
|
||||
}
|
||||
|
||||
public static function delete() {
|
||||
|
||||
@@ -248,7 +248,57 @@ class BasicTest extends SimpleTest {
|
||||
$this->assert(count($result) === 1);
|
||||
$this->assert($result[0]['height'] === '10.371');
|
||||
|
||||
DB::query("UPDATE accounts SET age=age-1 WHERE age=%i", 16);
|
||||
DB::insertUpdate('accounts', array(
|
||||
'id' => 2, //duplicate primary key
|
||||
'username' => 'blahblahdude',
|
||||
'age' => 233,
|
||||
'height' => 199.194
|
||||
));
|
||||
|
||||
$result = DB::query("SELECT * FROM accounts WHERE age = %i", 233);
|
||||
$this->assert(count($result) === 1);
|
||||
$this->assert($result[0]['height'] === '199.194');
|
||||
$this->assert($result[0]['username'] === 'blahblahdude');
|
||||
|
||||
DB::insertUpdate('accounts', array(
|
||||
'id' => 2, //duplicate primary key
|
||||
'username' => 'gonesoon',
|
||||
'password' => 'something',
|
||||
'age' => 61,
|
||||
'height' => 199.194
|
||||
), array(
|
||||
'age' => 74,
|
||||
));
|
||||
|
||||
$result = DB::query("SELECT * FROM accounts WHERE age = %i", 74);
|
||||
$this->assert(count($result) === 1);
|
||||
$this->assert($result[0]['height'] === '199.194');
|
||||
$this->assert($result[0]['username'] === 'blahblahdude');
|
||||
|
||||
$multiples[] = array(
|
||||
'id' => 3, //duplicate primary key
|
||||
'username' => 'gonesoon',
|
||||
'password' => 'something',
|
||||
'age' => 61,
|
||||
'height' => 199.194
|
||||
);
|
||||
$multiples[] = array(
|
||||
'id' => 1, //duplicate primary key
|
||||
'username' => 'gonesoon',
|
||||
'password' => 'something',
|
||||
'age' => 61,
|
||||
'height' => 199.194
|
||||
);
|
||||
|
||||
DB::insertUpdate('accounts', $multiples, array('age' => 914));
|
||||
$this->assert(DB::affectedRows() === 4);
|
||||
|
||||
$result = DB::query("SELECT * FROM accounts WHERE age=914 ORDER BY id ASC");
|
||||
$this->assert(count($result) === 2);
|
||||
$this->assert($result[0]['username'] === 'Abe');
|
||||
$this->assert($result[1]['username'] === 'Charlie\'s Friend');
|
||||
|
||||
DB::query("UPDATE accounts SET age=15, username='Bart' WHERE age=%i", 74);
|
||||
$this->assert(DB::affectedRows() === 1);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user