Skip to content

Commit

Permalink
Merge pull request #206 from charsleysa/develop
Browse files Browse the repository at this point in the history
Fix for parameter types.
  • Loading branch information
treffynnon committed Jun 3, 2014
2 parents 4745ee7 + 93e050c commit f8f46c0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
12 changes: 11 additions & 1 deletion idiorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,17 @@ protected static function _execute($query, $parameters = array(), $connection_na
$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;
Expand Down
18 changes: 17 additions & 1 deletion test/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class MockPDOStatement extends PDOStatement {
private $current_row = 0;
private $statement = NULL;
private $bindParams = array();

/**
* Store the statement that gets passed to the constructor
Expand All @@ -21,9 +22,10 @@ public function __construct($statement) {
/**
* 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++) {
Expand All @@ -40,6 +42,20 @@ public function execute($params) {
}
}
}

/**
* 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
Expand Down

0 comments on commit f8f46c0

Please sign in to comment.