successful insert/replace/update/delete will return true

This commit is contained in:
Sergey Tsalkov
2013-03-19 11:24:53 -07:00
parent 6cb757797b
commit ce11e65070
2 changed files with 20 additions and 14 deletions

View File

@@ -319,7 +319,7 @@ class MeekroDB {
$buildquery = "UPDATE " . $this->formatTableName($table) . " SET " . implode(', ', $keyval) . " WHERE " . $where;
array_unshift($args, $buildquery);
call_user_func_array(array($this, 'query'), $args);
return call_user_func_array(array($this, 'query'), $args);
}
public function insertOrReplace($which, $table, $datas, $options=array()) {
@@ -357,13 +357,13 @@ class MeekroDB {
$values_str = implode(',', $values);
if (isset($options['ignore']) && $options['ignore'] && strtolower($which) == 'insert') {
$this->query("INSERT IGNORE INTO $table ($keys_str) VALUES $values_str");
return $this->query("INSERT IGNORE INTO $table ($keys_str) VALUES $values_str");
} else if (isset($options['update']) && $options['update'] && strtolower($which) == 'insert') {
$this->query("INSERT INTO $table ($keys_str) VALUES $values_str ON DUPLICATE KEY UPDATE {$options['update']}");
return $this->query("INSERT INTO $table ($keys_str) VALUES $values_str ON DUPLICATE KEY UPDATE {$options['update']}");
} else {
$this->query("$which INTO $table ($keys_str) VALUES $values_str");
return $this->query("$which INTO $table ($keys_str) VALUES $values_str");
}
}
@@ -405,7 +405,7 @@ class MeekroDB {
$where = array_shift($args);
$buildquery = "DELETE FROM $table WHERE $where";
array_unshift($args, $buildquery);
call_user_func_array(array($this, 'query'), $args);
return call_user_func_array(array($this, 'query'), $args);
}
public function sqleval() {
@@ -608,10 +608,9 @@ class MeekroDB {
if ($is_buffered && ($result instanceof MySQLi_Result)) $this->num_rows = $result->num_rows;
else $this->num_rows = null;
if ($row_type == 'raw') return $result;
if ($row_type == 'raw' || !($result instanceof MySQLi_Result)) return $result;
$return = array();
if (!($result instanceof MySQLi_Result)) return $return;
if ($full_names) {
$infos = array();

View File

@@ -45,11 +45,12 @@ class BasicTest extends SimpleTest {
}
function test_2_insert_row() {
DB::insert('accounts', array(
$true = DB::insert('accounts', array(
'username' => 'Abe',
'password' => 'hello'
));
$this->assert($true === true);
$this->assert(DB::affectedRows() === 1);
$counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
@@ -155,13 +156,14 @@ class BasicTest extends SimpleTest {
$this->assert($row['username'] === 'newguy');
$this->assert($row['age'] === '172');
DB::update('accounts', array(
$true = DB::update('accounts', array(
'password' => DB::sqleval("REPEAT('blah', %i)", 4),
'favorite_word' => null,
), 'username=%s', 'newguy');
$row = null;
$row = DB::queryOneRow("SELECT * FROM accounts WHERE username=%s", 'newguy');
$this->assert($true === true);
$this->assert($row['password'] === 'blahblahblahblah');
$this->assert($row['favorite_word'] === null);
@@ -172,7 +174,8 @@ class BasicTest extends SimpleTest {
$this->assert($row[0]['username'] === 'newguy');
$this->assert($row[0]['age'] === '172');
DB::query("DELETE FROM accounts WHERE password=%s", 'blahblahblahblah');
$true = DB::query("DELETE FROM accounts WHERE password=%s", 'blahblahblahblah');
$this->assert($true === true);
$this->assert(DB::affectedRows() === 1);
}
@@ -265,7 +268,7 @@ class BasicTest extends SimpleTest {
}
function test_7_insert_update() {
DB::insertUpdate('accounts', array(
$true = DB::insertUpdate('accounts', array(
'id' => 2, //duplicate primary key
'username' => 'gonesoon',
'password' => 'something',
@@ -273,6 +276,7 @@ class BasicTest extends SimpleTest {
'height' => 199.194
), 'age = age + %i', 1);
$this->assert($true === true);
$this->assert(DB::affectedRows() === 2); // a quirk of MySQL, even though only 1 row was updated
$result = DB::query("SELECT * FROM accounts WHERE age = %i", 16);
@@ -329,7 +333,8 @@ class BasicTest extends SimpleTest {
$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);
$true = DB::query("UPDATE accounts SET age=15, username='Bart' WHERE age=%i", 74);
$this->assert($true === true);
$this->assert(DB::affectedRows() === 1);
}
@@ -339,14 +344,15 @@ class BasicTest extends SimpleTest {
'password' => 'dookoo',
);
DB::query("INSERT into accounts %lb VALUES %ls", array_keys($data), array_values($data));
$true = DB::query("INSERT into accounts %lb VALUES %ls", array_keys($data), array_values($data));
$result = DB::query("SELECT * FROM accounts WHERE username=%s", 'vookoo');
$this->assert($true === true);
$this->assert(count($result) === 1);
$this->assert($result[0]['password'] === 'dookoo');
}
function test_9_fullcolumns() {
DB::insert('profile', array(
$true = DB::insert('profile', array(
'id' => 1,
'signature' => 'u_suck'
));
@@ -355,6 +361,7 @@ class BasicTest extends SimpleTest {
$r = DB::queryFullColumns("SELECT accounts.*, profile.*, 1+1 FROM accounts
INNER JOIN profile ON accounts.profile_id=profile.id");
$this->assert($true === true);
$this->assert(count($r) === 1);
$this->assert($r[0]['accounts.id'] === '2');
$this->assert($r[0]['profile.id'] === '1');