1 Commits
v1.4 ... v1.5

Author SHA1 Message Date
Sergey Tsalkov
4faebb957c add DB::sqleval() -- can be used with insert() and update() to inject raw sql stuff like NOW() 2011-04-08 13:56:28 -04:00
2 changed files with 49 additions and 3 deletions

View File

@@ -133,7 +133,14 @@ class DB
$buildquery = "UPDATE " . self::formatTableName($table) . " SET ";
$keyval = array();
foreach ($params as $key => $value) {
$keyval[] = "`" . $key . "`=" . (is_int($value) ? $value : "'" . DB::escape($value) . "'");
if (is_object($value) && ($value instanceof MeekroDBEval)) {
$value = $value->text;
} else {
if (is_array($value)) $value = serialize($value);
$value = (is_int($value) ? $value : "'" . DB::escape($value) . "'");
}
$keyval[] = "`" . $key . "`=" . $value;
}
$buildquery = "UPDATE " . self::formatTableName($table) . " SET " . implode(', ', $keyval) . " WHERE " . $where;
@@ -146,8 +153,12 @@ class DB
$keys_str = implode(', ', DB::wrapStr(array_keys($data), '`'));
foreach ($data as &$datum) {
if (is_array($datum)) $datum = serialize($datum);
$datum = "'" . DB::escape($datum) . "'";
if (is_object($datum) && ($datum instanceof MeekroDBEval)) {
$datum = $datum->text;
} else {
if (is_array($datum)) $datum = serialize($datum);
$datum = (is_int($datum) ? $datum : "'" . DB::escape($datum) . "'");
}
}
$values_str = implode(', ', array_values($data));
@@ -164,6 +175,10 @@ class DB
return DB::insertOrReplace('REPLACE', $table, $data);
}
public static function sqleval($text) {
return new MeekroDBEval($text);
}
public static function columnList($table) {
return DB::queryOneColumn('Field', "SHOW COLUMNS FROM $table");
}
@@ -602,4 +617,12 @@ function meekrodb_debugmode_handler($params) {
}
}
class MeekroDBEval {
public $text = '';
function __construct($text) {
$this->text = $text;
}
}
?>

View File

@@ -118,6 +118,29 @@ class BasicTest extends SimpleTest {
$this->assert(count($results) === 2);
}
function test_4_1_query() {
DB::insert('accounts', array(
'username' => 'newguy',
'password' => DB::sqleval("REPEAT('blah', 3)"),
'age' => 172,
'height' => 111.15
));
$row = DB::queryOneRow("SELECT * FROM accounts WHERE password=%s", 'blahblahblah');
$this->assert($row['username'] === 'newguy');
DB::update('accounts', array(
'password' => DB::sqleval("REPEAT('blah', 4)"),
), 'username=%s', 'newguy');
$row = null;
$row = DB::queryOneRow("SELECT * FROM accounts WHERE username=%s", 'newguy');
$this->assert($row['password'] === 'blahblahblahblah');
DB::query("DELETE FROM accounts WHERE password=%s", 'blahblahblahblah');
$this->assert(DB::affectedRows() === 1);
}
function test_5_error_handler() {
global $error_callback_worked;