Skip to content

Commit

Permalink
Removed duplicated code for bound and input params
Browse files Browse the repository at this point in the history
  • Loading branch information
noahheck committed Oct 25, 2015
1 parent cc7797e commit 1d2f9ce
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 57 deletions.
85 changes: 28 additions & 57 deletions src/EPDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,77 +98,48 @@ public function interpolateQuery($inputParams = null)
{
$testQuery = $this->queryString;

/**
* If parameters were bound prior to execution, boundParams will be true
*/
if ($this->boundParams) {
// We ksort our bound parameters array to allow parameter binding to numbered ? markers and we need to
// replace them in the correct order
ksort($this->boundParams);
$params = ($this->boundParams) ? $this->boundParams : $inputParams;

foreach ($this->boundParams as $key => $array) {
/**
* UPDATE - Issue #3
* It is acceptable for bound parameters to be provided without the leading :, so if we are not matching
* a ?, we want to check for the presence of the leading : and add it if it is not there.
*/
if (is_numeric($key)) {
if ($params) {

$key = "\?";
ksort($params);

} else {
foreach ($params as $key => $value) {

$key = (preg_match("/^\:/", $key)) ? $key : ":" . $key;
$replValue = (is_array($value)) ? $value
: array(
'value' => $value
, 'datatype' => PDO::PARAM_STR
);

}
$value = $array;
$replValue = $this->prepareValue($replValue);

$testParam = "/" . $key . "(?!\w)/";
$replValue = $this->_prepareValue($value);
$testQuery = $this->replaceMarker($testQuery, $key, $replValue);

$testQuery = preg_replace($testParam, $replValue, $testQuery, 1);
}
}

/**
* Otherwise, if we have input parameters, we'll replace ? markers
* UPDATE - we can now accept $key => $value named parameters as well:
* $inputParams = array(
* ":username" => $username
* , ":password" => $password
* );
*/
if (is_array($inputParams) && $inputParams !== array()) {
ksort($inputParams);
foreach ($inputParams as $key => $replValue) {
/**
* UPDATE - Issue #3
* It is acceptable for bound parameters to be provided without the leading :, so if we are not matching
* a ?, we want to check for the presence of the leading : and add it if it is not there.
*/
if (is_numeric($key)) {

$key = "\?";

} else {

$key = (preg_match("/^\:/", $key)) ? $key : ":" . $key;

}
$this->fullQuery = $testQuery;

$testParam = "/" . $key . "(?!\w)/";
$replValue = $this->_prepareValue(array(
'value' => $replValue
, 'datatype' => PDO::PARAM_STR
));
return $testQuery;
}

$testQuery = preg_replace($testParam, $replValue, $testQuery, 1);
}
private function replaceMarker($queryString, $marker, $replValue)
{
/**
* UPDATE - Issue #3
* It is acceptable for bound parameters to be provided without the leading :, so if we are not matching
* a ?, we want to check for the presence of the leading : and add it if it is not there.
*/
if (is_numeric($marker)) {
$marker = "\?";
} else {
$marker = (preg_match("/^\:/", $marker)) ? $marker : ":" . $marker;
}

$this->fullQuery = $testQuery;
$testParam = "/" . $marker . "(?!\w)/";

return $testQuery;
return preg_replace($testParam, $replValue, $queryString, 1);
}

/**
Expand Down Expand Up @@ -196,7 +167,7 @@ public function execute($inputParams = null)
* @param str $value - the value to be prepared for injection as a value in the query string
* @return str $value - prepared $value
*/
private function _prepareValue($value)
private function prepareValue($value)
{
if ($this->_pdo) {

Expand Down
11 changes: 11 additions & 0 deletions tests/src/EPDOStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,15 @@ public function testValuesAreSuccessfullyInterpolatedIfNoPdoProvidedToEPDOStatem
$this->assertTrue(false == preg_match("/\:userId/", $result));
$this->assertTrue(false == preg_match("/\:user_status/", $result));
}

public function testQueryIsNotChangedIfNoParametersUsedInQuery()
{
$pdo = $this->getPdo();

$query = "SELECT * FROM test_table WHERE id = '123' AND userId = '456'";

$stmt = $pdo->prepare($query);

$this->assertEquals($query, $stmt->interpolateQuery());
}
}

0 comments on commit 1d2f9ce

Please sign in to comment.