summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNoah Heck <noah@noahheck.com>2015-10-25 10:20:03 -0600
committerNoah Heck <noah@noahheck.com>2015-10-25 10:20:03 -0600
commit1d2f9ce497207cdf71e14efa0faac29db8fcb742 (patch)
tree501d2cb50abd5011b6e418cfdede0be8fca2d830
parentcc7797e37077a36b8f7f1f51d86407611a33a772 (diff)
downloadE_PDOStatement-1d2f9ce497207cdf71e14efa0faac29db8fcb742.zip
E_PDOStatement-1d2f9ce497207cdf71e14efa0faac29db8fcb742.tar.gz
E_PDOStatement-1d2f9ce497207cdf71e14efa0faac29db8fcb742.tar.bz2
Removed duplicated code for bound and input params2.1.4
-rw-r--r--src/EPDOStatement.php85
-rw-r--r--tests/src/EPDOStatementTest.php11
2 files changed, 39 insertions, 57 deletions
diff --git a/src/EPDOStatement.php b/src/EPDOStatement.php
index 6876e0d..0f2fdd0 100644
--- a/src/EPDOStatement.php
+++ b/src/EPDOStatement.php
@@ -98,77 +98,48 @@ class EPDOStatement extends PDOStatement
{
$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);
}
/**
@@ -196,7 +167,7 @@ class EPDOStatement extends PDOStatement
* @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) {
diff --git a/tests/src/EPDOStatementTest.php b/tests/src/EPDOStatementTest.php
index cef8ef7..f204f8a 100644
--- a/tests/src/EPDOStatementTest.php
+++ b/tests/src/EPDOStatementTest.php
@@ -243,4 +243,15 @@ class EPDOStatementTest extends PHPUnit_Framework_TestCase
$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());
+ }
}