bugfix: don't fail when inserting multiple rows at once, and one of the values is NULL

This commit is contained in:
Sergey Tsalkov
2012-09-19 14:58:09 -07:00
parent ed9c29fe49
commit 2ffb770f8a
2 changed files with 12 additions and 3 deletions

View File

@@ -355,7 +355,7 @@ class MeekroDB {
$insert_values = array(); $insert_values = array();
foreach ($keys as $key) { foreach ($keys as $key) {
if ($many && !isset($data[$key])) $this->nonSQLError('insert/replace many: each assoc array must have the same keys!'); if ($many && !array_key_exists($key, $data)) $this->nonSQLError('insert/replace many: each assoc array must have the same keys!');
$datum = $data[$key]; $datum = $data[$key];
$datum = $this->sanitize($datum); $datum = $this->sanitize($datum);
$insert_values[] = $datum; $insert_values[] = $datum;

View File

@@ -11,7 +11,7 @@ class BasicTest extends SimpleTest {
DB::query("CREATE TABLE `accounts` ( DB::query("CREATE TABLE `accounts` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 255 ) NOT NULL , `username` VARCHAR( 255 ) NOT NULL ,
`password` VARCHAR( 255 ) NOT NULL , `password` VARCHAR( 255 ) NULL ,
`age` INT NOT NULL DEFAULT '10', `age` INT NOT NULL DEFAULT '10',
`height` DOUBLE NOT NULL DEFAULT '10.0', `height` DOUBLE NOT NULL DEFAULT '10.0',
`favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi' `favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi'
@@ -195,9 +195,15 @@ class BasicTest extends SimpleTest {
'age' => 25, 'age' => 25,
'height' => 190.194 'height' => 190.194
); );
$ins[] = array(
'password' => NULL,
'username' => '3ofmany',
'age' => 15,
'height' => 111.951
);
DB::insert('accounts', $ins); DB::insert('accounts', $ins);
$this->assert(DB::affectedRows() === 2); $this->assert(DB::affectedRows() === 3);
$rows = DB::query("SELECT * FROM accounts WHERE height=%d ORDER BY age ASC", 190.194); $rows = DB::query("SELECT * FROM accounts WHERE height=%d ORDER BY age ASC", 190.194);
$this->assert(count($rows) === 2); $this->assert(count($rows) === 2);
@@ -207,6 +213,9 @@ class BasicTest extends SimpleTest {
$this->assert($rows[1]['password'] === 'somethingelse'); $this->assert($rows[1]['password'] === 'somethingelse');
$this->assert($rows[1]['username'] === '2ofmany'); $this->assert($rows[1]['username'] === '2ofmany');
$nullrow = DB::queryOneRow("SELECT * FROM accounts WHERE username=%s", '3ofmany');
$this->assert($nullrow['password'] === NULL);
$this->assert($nullrow['age'] === '15');
} }