summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Holywell <[email protected]>2014-06-03 13:47:16 +0100
committerSimon Holywell <[email protected]>2014-06-03 13:47:16 +0100
commitf8f46c084cfbff704dd39f6d89de5308a3d30b0d (patch)
treec1e28e128d50b31347f51dfe967c8d707da4478c
parent4745ee73381c6a6bbb099cff745ac8781ce38ffc (diff)
parent93e050c209898f64e84112b3fffe77d100149b2e (diff)
Merge pull request #206 from charsleysa/develop
Fix for parameter types.
-rw-r--r--idiorm.php12
-rw-r--r--test/bootstrap.php18
2 files changed, 28 insertions, 2 deletions
diff --git a/idiorm.php b/idiorm.php
index 73072e6..ae80abe 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -410,7 +410,17 @@
$statement = self::get_db($connection_name)->prepare($query);
self::$_last_statement = $statement;
$time = microtime(true);
- $q = $statement->execute($parameters);
+
+ $count = count($parameters);
+ for ($i = 0; $i < $count; $i++) {
+ $type = PDO::PARAM_STR;
+ if (is_null($parameters[$i])) $type = PDO::PARAM_NULL;
+ if (is_bool($parameters[$i])) $type = PDO::PARAM_BOOL;
+ if (is_int($parameters[$i])) $type = PDO::PARAM_INT;
+ $statement->bindParams($i + 1, $parameters[$i], $type);
+ }
+
+ $q = $statement->execute();
self::_log_query($query, $parameters, $connection_name, (microtime(true)-$time));
return $q;
diff --git a/test/bootstrap.php b/test/bootstrap.php
index 9a40762..8790dc7 100644
--- a/test/bootstrap.php
+++ b/test/bootstrap.php
@@ -10,6 +10,7 @@ require_once dirname(__FILE__) . '/../idiorm.php';
class MockPDOStatement extends PDOStatement {
private $current_row = 0;
private $statement = NULL;
+ private $bindParams = array();
/**
* Store the statement that gets passed to the constructor
@@ -21,9 +22,10 @@ class MockPDOStatement extends PDOStatement {
/**
* Check that the array
*/
- public function execute($params) {
+ public function execute($params = NULL) {
$count = 0;
$m = array();
+ if (is_null($params)) $params = $this->bindParams;
if (preg_match_all('/"[^"\\\\]*(?:\\?)[^"\\\\]*"|\'[^\'\\\\]*(?:\\?)[^\'\\\\]*\'|(\\?)/', $this->statement, $m, PREG_SET_ORDER)) {
$count = count($m);
for ($v = 0; $v < $count; $v++) {
@@ -40,6 +42,20 @@ class MockPDOStatement extends PDOStatement {
}
}
}
+
+ /**
+ * Add data to arrays
+ */
+ public function bindParams($index, $value, $type)
+ {
+ // Do check on index, and type
+ if (!is_int($index)) throw new Exception('Incorrect parameter type. Expected $index to be an integer.');
+ if (!is_int($type) || ($type != PDO::PARAM_STR && $type != PDO::PARAM_NULL && $type != PDO::PARAM_BOOL && $type != PDO::PARAM_INT))
+ throw new Exception('Incorrect parameter type. Expected $type to be an integer.');
+
+ // Add param to array
+ $this->bindParams[$index - 1] = $value;
+ }
/**
* Return some dummy data