bugfix: free any extra results so CALL commands don't fail
This commit is contained in:
48
db.class.php
48
db.class.php
@@ -312,14 +312,14 @@ class MeekroDB {
|
|||||||
$table = array_shift($args);
|
$table = array_shift($args);
|
||||||
$params = array_shift($args);
|
$params = array_shift($args);
|
||||||
$where = array_shift($args);
|
$where = array_shift($args);
|
||||||
$buildquery = "UPDATE " . self::formatTableName($table) . " SET ";
|
$buildquery = "UPDATE " . $this->formatTableName($table) . " SET ";
|
||||||
$keyval = array();
|
$keyval = array();
|
||||||
foreach ($params as $key => $value) {
|
foreach ($params as $key => $value) {
|
||||||
$value = $this->sanitize($value);
|
$value = $this->sanitize($value);
|
||||||
$keyval[] = "`" . $key . "`=" . $value;
|
$keyval[] = "`" . $key . "`=" . $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
$buildquery = "UPDATE " . self::formatTableName($table) . " SET " . implode(', ', $keyval) . " WHERE " . $where;
|
$buildquery = "UPDATE " . $this->formatTableName($table) . " SET " . implode(', ', $keyval) . " WHERE " . $where;
|
||||||
array_unshift($args, $buildquery);
|
array_unshift($args, $buildquery);
|
||||||
call_user_func_array(array($this, 'queryNull'), $args);
|
call_user_func_array(array($this, 'queryNull'), $args);
|
||||||
}
|
}
|
||||||
@@ -354,7 +354,7 @@ class MeekroDB {
|
|||||||
$values[] = '(' . implode(', ', $insert_values) . ')';
|
$values[] = '(' . implode(', ', $insert_values) . ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
$table = self::formatTableName($table);
|
$table = $this->formatTableName($table);
|
||||||
$keys_str = implode(', ', $this->wrapStr($keys, '`'));
|
$keys_str = implode(', ', $this->wrapStr($keys, '`'));
|
||||||
$values_str = implode(',', $values);
|
$values_str = implode(',', $values);
|
||||||
|
|
||||||
@@ -403,7 +403,7 @@ class MeekroDB {
|
|||||||
|
|
||||||
public function delete() {
|
public function delete() {
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
$table = self::formatTableName(array_shift($args));
|
$table = $this->formatTableName(array_shift($args));
|
||||||
$where = array_shift($args);
|
$where = array_shift($args);
|
||||||
$buildquery = "DELETE FROM $table WHERE $where";
|
$buildquery = "DELETE FROM $table WHERE $where";
|
||||||
array_unshift($args, $buildquery);
|
array_unshift($args, $buildquery);
|
||||||
@@ -579,32 +579,46 @@ class MeekroDB {
|
|||||||
|
|
||||||
$this->insert_id = $db->insert_id;
|
$this->insert_id = $db->insert_id;
|
||||||
$this->affected_rows = $db->affected_rows;
|
$this->affected_rows = $db->affected_rows;
|
||||||
|
|
||||||
if ($is_buffered) $this->num_rows = $result->num_rows;
|
// mysqli_result->num_rows won't initially show correct results for unbuffered data
|
||||||
|
if ($is_buffered && ($result instanceof MySQLi_Result)) $this->num_rows = $result->num_rows;
|
||||||
else $this->num_rows = null;
|
else $this->num_rows = null;
|
||||||
|
|
||||||
if ($is_null) {
|
if ($is_null) {
|
||||||
if ($result instanceof MySQLi_Result) $result->free();
|
if ($result instanceof MySQLi_Result) $this->freeResult($result);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function freeResult($result = null) {
|
||||||
|
if ($result instanceof MySQLi_Result) {
|
||||||
|
$result->free();
|
||||||
|
}
|
||||||
|
|
||||||
|
$db = $this->get();
|
||||||
|
while ($db->more_results()) {
|
||||||
|
$db->next_result();
|
||||||
|
if ($result = $db->use_result()) $result->free();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public function queryAllRows() {
|
public function queryAllRows() {
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
|
|
||||||
$rowlist = array();
|
$rowlist = array();
|
||||||
$this->num_rows = 0;
|
$this->num_rows = 0;
|
||||||
|
|
||||||
$result = call_user_func_array(array($this, 'queryUnbuf'), $args);
|
$result = call_user_func_array(array($this, 'queryBuf'), $args);
|
||||||
if ($result instanceof MySQLi_Result) {
|
if ($result instanceof MySQLi_Result) {
|
||||||
while ($row = $result->fetch_assoc()) {
|
while ($row = $result->fetch_assoc()) {
|
||||||
$rowlist[] = $row;
|
$rowlist[] = $row;
|
||||||
$this->num_rows++;
|
$this->num_rows++;
|
||||||
}
|
}
|
||||||
|
|
||||||
$result->free();
|
$this->freeResult($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $rowlist;
|
return $rowlist;
|
||||||
@@ -616,14 +630,14 @@ class MeekroDB {
|
|||||||
$rows = array();
|
$rows = array();
|
||||||
$this->num_rows = 0;
|
$this->num_rows = 0;
|
||||||
|
|
||||||
$result = call_user_func_array(array($this, 'queryUnbuf'), $args);
|
$result = call_user_func_array(array($this, 'queryBuf'), $args);
|
||||||
if ($result instanceof MySQLi_Result) {
|
if ($result instanceof MySQLi_Result) {
|
||||||
while ($row = $result->fetch_row()) {
|
while ($row = $result->fetch_row()) {
|
||||||
$rows[] = $row;
|
$rows[] = $row;
|
||||||
$this->num_rows++;
|
$this->num_rows++;
|
||||||
}
|
}
|
||||||
|
|
||||||
$result->free();
|
$this->freeResult($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $rows;
|
return $rows;
|
||||||
@@ -632,10 +646,10 @@ class MeekroDB {
|
|||||||
public function queryOneList() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstList'), $args); }
|
public function queryOneList() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstList'), $args); }
|
||||||
public function queryFirstList() {
|
public function queryFirstList() {
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
$result = call_user_func_array(array($this, 'queryUnbuf'), $args);
|
$result = call_user_func_array(array($this, 'queryBuf'), $args);
|
||||||
if ($result instanceof MySQLi_Result) {
|
if ($result instanceof MySQLi_Result) {
|
||||||
$row = $result->fetch_row();
|
$row = $result->fetch_row();
|
||||||
$result->free();
|
$this->freeResult($result);
|
||||||
} else {
|
} else {
|
||||||
$row = null;
|
$row = null;
|
||||||
}
|
}
|
||||||
@@ -646,11 +660,11 @@ class MeekroDB {
|
|||||||
public function queryOneRow() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstRow'), $args); }
|
public function queryOneRow() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstRow'), $args); }
|
||||||
public function queryFirstRow() {
|
public function queryFirstRow() {
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
$result = call_user_func_array(array($this, 'queryUnbuf'), $args);
|
$result = call_user_func_array(array($this, 'queryBuf'), $args);
|
||||||
|
|
||||||
if ($result instanceof MySQLi_Result) {
|
if ($result instanceof MySQLi_Result) {
|
||||||
$row = $result->fetch_assoc();
|
$row = $result->fetch_assoc();
|
||||||
$result->free();
|
$this->freeResult($result);
|
||||||
} else {
|
} else {
|
||||||
$row = null;
|
$row = null;
|
||||||
}
|
}
|
||||||
|
|||||||
17
simpletest/CallTest.php
Normal file
17
simpletest/CallTest.php
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
class CallTest extends SimpleTest {
|
||||||
|
function test_1_create_procedure() {
|
||||||
|
DB::query("DROP PROCEDURE IF EXISTS myProc");
|
||||||
|
DB::query("CREATE PROCEDURE myProc()
|
||||||
|
BEGIN
|
||||||
|
SELECT * FROM accounts;
|
||||||
|
END");
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_2_run_procedure() {
|
||||||
|
$r = DB::query("CALL myProc()");
|
||||||
|
$this->assert($r[0]['username'] === 'Abe');
|
||||||
|
$this->assert($r[2]['age'] === '914');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/php
|
#!/usr/bin/php
|
||||||
<?php
|
<?php
|
||||||
class SimpleTest {
|
class SimpleTest {
|
||||||
protected function assert($boolean) {
|
public function assert($boolean) {
|
||||||
if (! $boolean) $this->fail();
|
if (! $boolean) $this->fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,6 +33,7 @@ DB::$host = $set_host;
|
|||||||
DB::get(); //connect to mysql
|
DB::get(); //connect to mysql
|
||||||
|
|
||||||
require_once 'BasicTest.php';
|
require_once 'BasicTest.php';
|
||||||
|
require_once 'CallTest.php';
|
||||||
require_once 'ObjectTest.php';
|
require_once 'ObjectTest.php';
|
||||||
require_once 'WhereClauseTest.php';
|
require_once 'WhereClauseTest.php';
|
||||||
require_once 'ErrorTest.php';
|
require_once 'ErrorTest.php';
|
||||||
@@ -41,6 +42,7 @@ require_once 'HelperTest.php';
|
|||||||
|
|
||||||
$classes_to_test = array(
|
$classes_to_test = array(
|
||||||
'BasicTest',
|
'BasicTest',
|
||||||
|
'CallTest',
|
||||||
'WhereClauseTest',
|
'WhereClauseTest',
|
||||||
'ObjectTest',
|
'ObjectTest',
|
||||||
'ErrorTest',
|
'ErrorTest',
|
||||||
|
|||||||
Reference in New Issue
Block a user