Skip to content

Commit

Permalink
feat: indexed/named parameter helper (#23)
Browse files Browse the repository at this point in the history
* feat: indexed/named parameter helper
closes #15

* remove obsolete property setting
  • Loading branch information
g105b authored Mar 24, 2021
1 parent 894354a commit 0405dc8
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/Condition/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -58,4 +66,20 @@ public function getCondition(

return $condition;
}
}

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;
}
}
8 changes: 8 additions & 0 deletions src/Condition/MixedIndexedAndNamedParametersException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace Gt\SqlBuilder\Condition;

use Gt\SqlBuilder\SqlBuilderException;

class MixedIndexedAndNamedParametersException extends SqlBuilderException {

}
12 changes: 12 additions & 0 deletions src/Query/SqlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Gt\SqlBuilder\Condition\AndCondition;
use Gt\SqlBuilder\Condition\Condition;
use Gt\SqlBuilder\Condition\MixedIndexedAndNamedParametersException;

abstract class SqlQuery {
const PRE_QUERY_COMMENT = "/* preQuery */";
Expand Down Expand Up @@ -134,13 +135,24 @@ protected function processWhereClause(
$query = $name;
$query .= "\n\t";

$shortParameterSyntax = null;
$conditionalQuery = "";
foreach($parts as $i => $part) {
if(is_string($part)) {
$part = new AndCondition($part);
}

/** @var Condition $part */
if($partShortParameterSyntax = $part->getShortParameterSyntax()) {
if($shortParameterSyntax) {
if($shortParameterSyntax !== $partShortParameterSyntax) {
throw new MixedIndexedAndNamedParametersException();
}
}
else {
$shortParameterSyntax = $partShortParameterSyntax;
}
}

if(strlen($conditionalQuery) > 0) {
$conditionalQuery .= "\n";
Expand Down
8 changes: 8 additions & 0 deletions src/SqlBuilderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace Gt\SqlBuilder;

use RuntimeException;

class SqlBuilderException extends RuntimeException {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace Gt\SqlBuilder\Test\Helper\Query;

class SelectExampleExtendWhereIndexedNamedMixedParameters extends SelectExample {
public function where():array {
return array_merge(parent::where(), [
"test = 123",
":id",
"?name",
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace Gt\SqlBuilder\Test\Helper\Query;

class SelectExampleExtendWhereIndexedParameters extends SelectExample {
public function where():array {
return array_merge(parent::where(), [
"test = 123",
"?id",
"?name",
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace Gt\SqlBuilder\Test\Helper\Query;

class SelectExampleExtendWhereNamedParameters extends SelectExample {
public function where():array {
return array_merge(parent::where(), [
"test = 123",
":id",
":name",
]);
}
}
24 changes: 23 additions & 1 deletion test/phpunit/Query/SqlQueryTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php
namespace Gt\SqlBuilder\Test\Query;

use Gt\SqlBuilder\Condition\MixedIndexedAndNamedParametersException;
use Gt\SqlBuilder\Query\SqlQuery;
use Gt\SqlBuilder\Test\Helper\Query\SelectExampleExtendWhereIndexedNamedMixedParameters;
use Gt\SqlBuilder\Test\Helper\Query\SelectExampleExtendWhereIndexedParameters;
use Gt\SqlBuilder\Test\Helper\Query\SelectExampleExtendWhereNamedParameters;
use Gt\SqlBuilder\Test\QueryTestCase;
use PHPUnit\Framework\MockObject\MockObject;

Expand All @@ -12,4 +16,22 @@ public function testDefaults() {
self::assertEquals("", $sut->preQuery());
self::assertEquals("", $sut->postQuery());
}
}

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();
}
}

0 comments on commit 0405dc8

Please sign in to comment.