diff --git a/src/Condition/Condition.php b/src/Condition/Condition.php index 128eb2c..04aaceb 100644 --- a/src/Condition/Condition.php +++ b/src/Condition/Condition.php @@ -38,6 +38,14 @@ public function getCondition( $brackets = true; continue; } + elseif(is_string($part)) { + if($part[0] === "?") { + $part = substr($part, 1) . " = ?"; + } + elseif($part[0] === ":") { + $part = substr($part, 1) . " = $part"; + } + } if($i > 0) { if($subLogic) { @@ -58,4 +66,20 @@ public function getCondition( return $condition; } -} \ No newline at end of file + + public function getShortParameterSyntax():?string { + foreach($this->parts as $i => $part) { + if(!is_string($part)) { + continue; + } + + $char1 = $part[0]; + if($char1 === "?" + || $char1 === ":") { + return $char1; + } + } + + return null; + } +} diff --git a/src/Condition/MixedIndexedAndNamedParametersException.php b/src/Condition/MixedIndexedAndNamedParametersException.php new file mode 100644 index 0000000..f15fcf3 --- /dev/null +++ b/src/Condition/MixedIndexedAndNamedParametersException.php @@ -0,0 +1,8 @@ + $part) { if(is_string($part)) { @@ -141,6 +143,16 @@ protected function processWhereClause( } /** @var Condition $part */ + if($partShortParameterSyntax = $part->getShortParameterSyntax()) { + if($shortParameterSyntax) { + if($shortParameterSyntax !== $partShortParameterSyntax) { + throw new MixedIndexedAndNamedParametersException(); + } + } + else { + $shortParameterSyntax = $partShortParameterSyntax; + } + } if(strlen($conditionalQuery) > 0) { $conditionalQuery .= "\n"; diff --git a/src/SqlBuilderException.php b/src/SqlBuilderException.php new file mode 100644 index 0000000..42fc841 --- /dev/null +++ b/src/SqlBuilderException.php @@ -0,0 +1,8 @@ +preQuery()); self::assertEquals("", $sut->postQuery()); } -} \ No newline at end of file + + public function testIndexedParameterSyntax() { + $sut = new SelectExampleExtendWhereIndexedParameters(); + $sql = self::normalise($sut); + self::assertEquals("select id, name, dateOfBirth from student where deletedAt is null and test = 123 and id = ? and name = ?", $sql); + } + + public function testNamedParameterSyntax() { + $sut = new SelectExampleExtendWhereNamedParameters(); + $sql = self::normalise($sut); + self::assertEquals("select id, name, dateOfBirth from student where deletedAt is null and test = 123 and id = :id and name = :name", $sql); + } + + public function testIndexedNamedParameterMixedSyntax() { + $sut = new SelectExampleExtendWhereIndexedNamedMixedParameters(); + self::expectException(MixedIndexedAndNamedParametersException::class); + $sut->__toString(); + } +}