insert(), replace(), and update() now handle the value NULL correctly
This commit is contained in:
14
db.class.php
14
db.class.php
@@ -133,8 +133,12 @@ class DB
|
|||||||
if (is_object($value) && ($value instanceof MeekroDBEval)) {
|
if (is_object($value) && ($value instanceof MeekroDBEval)) {
|
||||||
$value = $value->text;
|
$value = $value->text;
|
||||||
} else {
|
} else {
|
||||||
if (is_array($value)) $value = serialize($value);
|
if (is_array($value) || is_object($value)) $value = serialize($value);
|
||||||
$value = (is_int($value) ? $value : "'" . DB::escape($value) . "'");
|
|
||||||
|
if (is_string($value)) $value = "'" . DB::escape($value) . "'";
|
||||||
|
else if (is_null($value)) $value = 'NULL';
|
||||||
|
else if (is_bool($value)) $value = ($value ? 1 : 0);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$keyval[] = "`" . $key . "`=" . $value;
|
$keyval[] = "`" . $key . "`=" . $value;
|
||||||
@@ -171,7 +175,11 @@ class DB
|
|||||||
if (is_object($datum) && ($datum instanceof MeekroDBEval)) {
|
if (is_object($datum) && ($datum instanceof MeekroDBEval)) {
|
||||||
$datum = $datum->text;
|
$datum = $datum->text;
|
||||||
} else {
|
} else {
|
||||||
$datum = (is_int($datum) ? $datum : "'" . DB::escape($datum) . "'");
|
if (is_array($datum) || is_object($datum)) $datum = serialize($datum);
|
||||||
|
|
||||||
|
if (is_string($datum)) $datum = "'" . DB::escape($datum) . "'";
|
||||||
|
else if (is_null($datum)) $datum = 'NULL';
|
||||||
|
else if (is_bool($datum)) $datum = ($datum ? 1 : 0);
|
||||||
}
|
}
|
||||||
$insert_values[] = $datum;
|
$insert_values[] = $datum;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,8 @@ class BasicTest extends SimpleTest {
|
|||||||
`username` VARCHAR( 255 ) NOT NULL ,
|
`username` VARCHAR( 255 ) NOT NULL ,
|
||||||
`password` VARCHAR( 255 ) NOT NULL ,
|
`password` VARCHAR( 255 ) NOT 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'
|
||||||
) ENGINE = InnoDB");
|
) ENGINE = InnoDB");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +79,8 @@ class BasicTest extends SimpleTest {
|
|||||||
'username' => 'Charlie\'s Friend',
|
'username' => 'Charlie\'s Friend',
|
||||||
'password' => 'goodbye',
|
'password' => 'goodbye',
|
||||||
'age' => 30,
|
'age' => 30,
|
||||||
'height' => 155.23
|
'height' => 155.23,
|
||||||
|
'favorite_word' => null,
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assert(DB::insertId() === 3);
|
$this->assert(DB::insertId() === 3);
|
||||||
@@ -86,6 +88,9 @@ class BasicTest extends SimpleTest {
|
|||||||
$counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
|
$counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
|
||||||
$this->assert($counter === strval(3));
|
$this->assert($counter === strval(3));
|
||||||
|
|
||||||
|
$password = DB::queryFirstField("SELECT password FROM accounts WHERE favorite_word IS NULL");
|
||||||
|
$this->assert($password === 'goodbye');
|
||||||
|
|
||||||
DB::$param_char = '###';
|
DB::$param_char = '###';
|
||||||
$bart = DB::queryFirstRow("SELECT * FROM accounts WHERE age IN ###li AND height IN ###ld AND username IN ###ls",
|
$bart = DB::queryFirstRow("SELECT * FROM accounts WHERE age IN ###li AND height IN ###ld AND username IN ###ls",
|
||||||
array(15, 25), array(10.371, 150.123), array('Bart', 'Barts'));
|
array(15, 25), array(10.371, 150.123), array('Bart', 'Barts'));
|
||||||
@@ -120,7 +125,7 @@ class BasicTest extends SimpleTest {
|
|||||||
$this->assert(count($results) === 2);
|
$this->assert(count($results) === 2);
|
||||||
|
|
||||||
$columnlist = DB::columnList('accounts');
|
$columnlist = DB::columnList('accounts');
|
||||||
$this->assert(count($columnlist) === 5);
|
$this->assert(count($columnlist) === 6);
|
||||||
$this->assert($columnlist[0] === 'id');
|
$this->assert($columnlist[0] === 'id');
|
||||||
$this->assert($columnlist[4] === 'height');
|
$this->assert($columnlist[4] === 'height');
|
||||||
|
|
||||||
@@ -148,11 +153,13 @@ class BasicTest extends SimpleTest {
|
|||||||
|
|
||||||
DB::update('accounts', array(
|
DB::update('accounts', array(
|
||||||
'password' => DB::sqleval("REPEAT('blah', %i)", 4),
|
'password' => DB::sqleval("REPEAT('blah', %i)", 4),
|
||||||
|
'favorite_word' => null,
|
||||||
), 'username=%s', 'newguy');
|
), 'username=%s', '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');
|
||||||
$this->assert($row['password'] === 'blahblahblahblah');
|
$this->assert($row['password'] === 'blahblahblahblah');
|
||||||
|
$this->assert($row['favorite_word'] === null);
|
||||||
|
|
||||||
DB::query("DELETE FROM accounts WHERE password=%s", 'blahblahblahblah');
|
DB::query("DELETE FROM accounts WHERE password=%s", 'blahblahblahblah');
|
||||||
$this->assert(DB::affectedRows() === 1);
|
$this->assert(DB::affectedRows() === 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user