From 6a9b77c8a09e95af9c14f7ccd2c1ca00b1770fd2 Mon Sep 17 00:00:00 2001 From: Stefan Andres Charsley Date: Thu, 29 May 2014 18:38:06 +1200 Subject: Fix for parameter types. --- idiorm.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/idiorm.php b/idiorm.php index 73072e6..0c36b6d 100644 --- a/idiorm.php +++ b/idiorm.php @@ -410,7 +410,20 @@ $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_numeric($parameters[i])) { + $type = PDO::PARAM_INT; + $parameters[$i] = +($parameters[$i]); + } + $statement->bindParams($i + 1, $parameters[$i], $type); + } + + $q = $statement->execute(); self::_log_query($query, $parameters, $connection_name, (microtime(true)-$time)); return $q; -- cgit v1.2.3 From c16a31f6b8938fdb1bcd9a3ef36bd2a79e72b97b Mon Sep 17 00:00:00 2001 From: Stefan Andres Charsley Date: Thu, 29 May 2014 18:43:01 +1200 Subject: Fix typos. --- idiorm.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/idiorm.php b/idiorm.php index 0c36b6d..7a86e99 100644 --- a/idiorm.php +++ b/idiorm.php @@ -414,9 +414,9 @@ $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_numeric($parameters[i])) { + if (is_null($parameters[$i])) $type = PDO::PARAM_NULL; + if (is_bool($parameters[$i])) $type = PDO::PARAM_BOOL; + if (is_numeric($parameters[$i])) { $type = PDO::PARAM_INT; $parameters[$i] = +($parameters[$i]); } -- cgit v1.2.3 From f66574f7f59059173b3cd5185dfc362762747b68 Mon Sep 17 00:00:00 2001 From: Stefan Andres Charsley Date: Thu, 29 May 2014 18:54:28 +1200 Subject: Fixed testing. --- test/bootstrap.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/test/bootstrap.php b/test/bootstrap.php index 9a40762..fe98ad1 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 -- cgit v1.2.3 From 0e42e9c0e143002c605b5750db5f3b655b3508c5 Mon Sep 17 00:00:00 2001 From: Stefan Andres Charsley Date: Thu, 29 May 2014 18:57:18 +1200 Subject: Fixed testing logic. --- test/bootstrap.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bootstrap.php b/test/bootstrap.php index fe98ad1..8790dc7 100644 --- a/test/bootstrap.php +++ b/test/bootstrap.php @@ -50,7 +50,7 @@ class MockPDOStatement extends PDOStatement { { // 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) + 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 -- cgit v1.2.3 From 41dd2cc40eb1e8d81ebb99eef98131a3a2c58661 Mon Sep 17 00:00:00 2001 From: Stefan Andres Charsley Date: Thu, 29 May 2014 19:16:29 +1200 Subject: Fixed type checks. Only make integers type int, all other number types get passed as strings. --- idiorm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/idiorm.php b/idiorm.php index 7a86e99..ef07d0b 100644 --- a/idiorm.php +++ b/idiorm.php @@ -416,7 +416,7 @@ $type = PDO::PARAM_STR; if (is_null($parameters[$i])) $type = PDO::PARAM_NULL; if (is_bool($parameters[$i])) $type = PDO::PARAM_BOOL; - if (is_numeric($parameters[$i])) { + if (is_int($parameters[$i])) { $type = PDO::PARAM_INT; $parameters[$i] = +($parameters[$i]); } -- cgit v1.2.3 From 93e050c209898f64e84112b3fffe77d100149b2e Mon Sep 17 00:00:00 2001 From: Stefan Andres Charsley Date: Thu, 29 May 2014 22:47:14 +1200 Subject: Removed useless code. --- idiorm.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/idiorm.php b/idiorm.php index ef07d0b..ae80abe 100644 --- a/idiorm.php +++ b/idiorm.php @@ -416,10 +416,7 @@ $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; - $parameters[$i] = +($parameters[$i]); - } + if (is_int($parameters[$i])) $type = PDO::PARAM_INT; $statement->bindParams($i + 1, $parameters[$i], $type); } -- cgit v1.2.3