Skip to content

Commit

Permalink
Feature/improve prepared statements (#23)
Browse files Browse the repository at this point in the history
* set isParameterized when setParams is used

* will handle params passed from mock to addQuery

* set the proper value on isParameterized

* Add testLastInsertIdPreparedStatement test case

* Fix Pdo::lastInsertId method
QueryLog now receives queries issued with PdoStatement::execute
  • Loading branch information
michalmatoga authored and jimbojsb committed Feb 26, 2019
1 parent f6de3bd commit 6db566b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 21 deletions.
5 changes: 1 addition & 4 deletions src/Pseudo/Pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class Pdo extends \PDO
public function prepare($statement, $driver_options = null)
{
$result = $this->mockedQueries->getResult($statement);
$statement = new PdoStatement($result);
return $statement;
return new PdoStatement($result, $this->queryLog, $statement);
}

public function beginTransaction()
Expand Down Expand Up @@ -76,8 +75,6 @@ public function query($statement)
$statement->setResult($result);
return $statement;
}
} else {

}
}

Expand Down
31 changes: 26 additions & 5 deletions src/Pseudo/PdoStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,50 @@ class PdoStatement extends \PDOStatement
private $boundParams = [];
private $boundColumns = [];

public function __construct($result = null)
/**
* @var QueryLog
*/
private $queryLog;

/**
* @var string
*/
private $statement;

/**
* @param Querylog $queryLog
* @param string $statement
* @param Result|null $result
*/
public function __construct($result = null, QueryLog $queryLog = null, $statement = null)
{
if (!($result instanceof Result)) {
$result = new Result();
}
$this->result = $result;;
$this->result = $result;
if (!($queryLog instanceof QueryLog)) {
$queryLog = new QueryLog();
}
$this->queryLog = $queryLog;
$this->statement = $statement;
}

public function setResult(Result $result)
{
$this->result = $result;
}

/**
/**
* @param array|null $input_parameters
* @return bool
*/
public function execute($input_parameters = null)
{
$input_parameters = array_merge((array)$input_parameters, $this->boundParams);
try {
$this->result->setParams($input_parameters);
$this->result->setParams($input_parameters, !empty($this->boundParams));
$success = (bool) $this->result->getRows($input_parameters ?: []);
$this->queryLog->addQuery($this->statement);
return $success;
} catch (Exception $e) {
return false;
Expand Down Expand Up @@ -86,7 +107,7 @@ public function fetchColumn($column_number = 0)

public function fetchAll($fetch_style = \PDO::FETCH_BOTH, $fetch_argument = null, $ctor_args = 'array()')
{
$rows = $this->result->getRows();
$rows = $this->result->getRows() ?: [];
$returnArray = [];
foreach ($rows as $row) {
$returnArray[] = $this->proccessFetchedRow($row, $fetch_style);
Expand Down
22 changes: 16 additions & 6 deletions src/Pseudo/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,44 @@ class Result
private $rowOffset = 0;
private $params = null;

public function __construct($rows = null)
public function __construct($rows = null, $params = null)
{
if (is_array($rows)) {
$this->rows = $rows;
if($params) {
$this->rows[$this->stringifyParameterSet($params)] = $rows;
$this->isParameterized = true;
} else {
$this->rows = $rows;
}
}
}

public function addRow(array $row, $params = null)
{
if ($params) {
if ($this->isParameterized) {
if ($this->isParameterized && !empty($row)) {
$this->rows[$this->stringifyParameterSet($params)][] = $row;
} else if (!$this->isParameterized && !$this->rows) {
$this->rows[$this->stringifyParameterSet($params)][] = $row;
if(!empty($row)) {
$this->rows[$this->stringifyParameterSet($params)][] = $row;
}
$this->isParameterized = true;
}
} else {
if (!$this->isParameterized) {
if (!$this->isParameterized && !empty($row)) {
$this->rows[] = $row;
} else {
throw new Exception("Cannot mix parameterized and non-parameterized rowsets");
}
}
}

public function setParams($params)
public function setParams($params, $parameterize = null)
{
$this->params = $params;
if($parameterize) {
$this->isParameterized = true;
}
}

public function getRows(array $params = [])
Expand Down
6 changes: 3 additions & 3 deletions src/Pseudo/ResultCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public function count()
return count($this->queries);
}

public function addQuery($sql, $results)
public function addQuery($sql, $results, $params = null)
{
$query = new ParsedQuery($sql);

if (is_array($results)) {
$storedResults = new Result($results);
$storedResults = new Result($results, $params);
} else if ($results instanceof Result) {
$storedResults = $results;
} else {
Expand Down Expand Up @@ -45,4 +45,4 @@ public function getResult($query)
throw new Exception($message);
}
}
}
}
Empty file modified src/php-sql-parser.php
100755 → 100644
Empty file.
3 changes: 2 additions & 1 deletion tests/PdoStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ public function testExecute()

$r = new Pseudo\Result();
$r->addRow($row1, $params1);
$s = new Pseudo\PdoStatement($r);
$queryLog = new Pseudo\QueryLog();
$s = new Pseudo\PdoStatement($r, $queryLog, 'SELECT * FROM test');

$this->assertEquals(true, $s->execute($params1));
$this->assertEquals(false, $s->execute());
Expand Down
15 changes: 13 additions & 2 deletions tests/PdoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ public function testLastInsertId()
$this->assertEquals(1, $p->lastInsertId());
}

public function testLastInsertIdPreparedStatement()
{
$sql = "SELECT * FROM test WHERE foo='bar'";
$p = new Pseudo\Pdo();
$r = new Pseudo\Result();
$r->setInsertId(10);
$p->mock($sql, $r);
$statement = $p->prepare($sql);
$statement->execute();
$this->assertEquals(10, $p->lastInsertId());
}

public function testErrorInfo()
{
$sql = "SELECT 1";
Expand Down Expand Up @@ -165,7 +177,6 @@ public function testSave()

public function testDebuggingRawQueries()
{

$message = null;
$p = new Pseudo\Pdo();
try {
Expand All @@ -175,4 +186,4 @@ public function testDebuggingRawQueries()
}
$this->assertRegExp('/SELECT 123/', $message);
}
}
}

0 comments on commit 6db566b

Please sign in to comment.