Skip to content

Commit

Permalink
A new argument for StatementFactory::createFromAST(), this fixes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
sad-spirit committed Nov 15, 2023
1 parent 33e1efb commit 05c2ead
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/sad_spirit/pg_builder/StatementFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,21 @@ public function createFromString(string $sql): Statement
* Creates an object containing SQL statement string and parameter mappings from AST
*
* @param Statement $ast
* @param bool $forcePDOPrepareCompatibility Whether generated SQL should be compatible with \PDO::prepare()
* even if {@see $PDOCompatible} was not set or the query does not contain placeholders,
* {@see https://github.com/sad-spirit/pg-builder/issues/15 issue #15}
* @return NativeStatement
*/
public function createFromAST(Statement $ast): NativeStatement
public function createFromAST(Statement $ast, bool $forcePDOPrepareCompatibility = false): NativeStatement
{
$pw = new ParameterWalker($this->PDOCompatible);
$pw = new ParameterWalker($this->PDOCompatible || $forcePDOPrepareCompatibility);
$ast->dispatch($pw);

$builder = $this->getBuilder();
$builder->enablePDOPrepareCompatibility($this->PDOCompatible && [] !== $pw->getParameterTypes());
$builder->enablePDOPrepareCompatibility(
$this->PDOCompatible && [] !== $pw->getParameterTypes()
|| $forcePDOPrepareCompatibility
);

return new NativeStatement($ast->dispatch($builder), $pw->getParameterTypes(), $pw->getNamedParameterMap());
}
Expand Down
42 changes: 42 additions & 0 deletions tests/StatementFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,46 @@ public function testPDOPrepareCompatibility(): void

$this->assertStringsEqualIgnoringWhitespace($stmt2Source, $factory->createFromAST($stmt2)->getSql());
}

/**
* @noinspection SqlNoDataSourceInspection
* @noinspection SqlResolve
* @link https://github.com/sad-spirit/pg-builder/issues/15
*/
public function testForcePDOPrepareCompatibility(): void
{
$factories = [
new StatementFactory(),
new StatementFactory(null, null, true)
];

/** @var StatementFactory $factory */
foreach ($factories as $factory) {
$stmt = $factory->createFromString(<<<'BLAH'
select *
from foo
where bar = $$O'really? \ Yes, really$$
and baz ? :whatever
BLAH
);

$this->assertStringsEqualIgnoringWhitespace(
"select * from foo where bar = e'O\\'really? \\\\ Yes, really' and baz ?? :whatever",
$factory->createFromAST($stmt, true)->getSql()
);

$stmt2 = $factory->createFromString($stmt2Source = <<<'BLAH'
select *
from foo
where bar = $$O'really? \ Yes, really$$
and baz ? 'whatever'
BLAH
);

$this->assertStringsEqualIgnoringWhitespace(
"select * from foo where bar = e'O\\'really? \\\\ Yes, really' and baz ?? 'whatever'",
$factory->createFromAST($stmt2, true)->getSql()
);
}
}
}

0 comments on commit 05c2ead

Please sign in to comment.