-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Made the OCI8Statement class final #3820
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
lib/Doctrine/DBAL/Driver/OCI8/ConvertPositionalToNamedPlaceholders.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Driver\OCI8; | ||
|
||
use const PREG_OFFSET_CAPTURE; | ||
use function count; | ||
use function implode; | ||
use function preg_match; | ||
use function preg_quote; | ||
use function substr; | ||
|
||
/** | ||
* Converts positional (?) into named placeholders (:param<num>). | ||
* | ||
* Oracle does not support positional parameters, hence this method converts all | ||
* positional parameters into artificially named parameters. Note that this conversion | ||
* is not perfect. All question marks (?) in the original statement are treated as | ||
* placeholders and converted to a named parameter. | ||
* | ||
* @internal This class is not covered by the backward compatibility promise | ||
*/ | ||
final class ConvertPositionalToNamedPlaceholders | ||
{ | ||
/** | ||
* @param string $statement The SQL statement to convert. | ||
* | ||
* @return mixed[] [0] => the statement value (string), [1] => the paramMap value (array). | ||
* | ||
* @throws OCI8Exception | ||
*/ | ||
public function __invoke(string $statement) : array | ||
{ | ||
$fragmentOffset = $tokenOffset = 0; | ||
$fragments = $paramMap = []; | ||
$currentLiteralDelimiter = null; | ||
|
||
do { | ||
if ($currentLiteralDelimiter === null) { | ||
$result = $this->findPlaceholderOrOpeningQuote( | ||
$statement, | ||
$tokenOffset, | ||
$fragmentOffset, | ||
$fragments, | ||
$currentLiteralDelimiter, | ||
$paramMap | ||
); | ||
} else { | ||
$result = $this->findClosingQuote($statement, $tokenOffset, $currentLiteralDelimiter); | ||
} | ||
} while ($result); | ||
|
||
if ($currentLiteralDelimiter) { | ||
throw NonTerminatedStringLiteral::new($tokenOffset - 1); | ||
} | ||
|
||
$fragments[] = substr($statement, $fragmentOffset); | ||
$statement = implode('', $fragments); | ||
|
||
return [$statement, $paramMap]; | ||
} | ||
|
||
/** | ||
* Finds next placeholder or opening quote. | ||
* | ||
* @param string $statement The SQL statement to parse | ||
* @param int $tokenOffset The offset to start searching from | ||
* @param int $fragmentOffset The offset to build the next fragment from | ||
* @param string[] $fragments Fragments of the original statement not containing placeholders | ||
* @param string|null $currentLiteralDelimiter The delimiter of the current string literal | ||
* or NULL if not currently in a literal | ||
* @param string[] $paramMap Mapping of the original parameter positions to their named replacements | ||
* | ||
* @return bool Whether the token was found | ||
*/ | ||
private function findPlaceholderOrOpeningQuote( | ||
string $statement, | ||
int &$tokenOffset, | ||
int &$fragmentOffset, | ||
array &$fragments, | ||
?string &$currentLiteralDelimiter, | ||
array &$paramMap | ||
) : bool { | ||
$token = $this->findToken($statement, $tokenOffset, '/[?\'"]/'); | ||
|
||
if (! $token) { | ||
return false; | ||
} | ||
|
||
if ($token === '?') { | ||
$position = count($paramMap) + 1; | ||
$param = ':param' . $position; | ||
$fragments[] = substr($statement, $fragmentOffset, $tokenOffset - $fragmentOffset); | ||
$fragments[] = $param; | ||
$paramMap[$position] = $param; | ||
$tokenOffset += 1; | ||
$fragmentOffset = $tokenOffset; | ||
|
||
return true; | ||
} | ||
|
||
$currentLiteralDelimiter = $token; | ||
++$tokenOffset; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Finds closing quote | ||
* | ||
* @param string $statement The SQL statement to parse | ||
* @param int $tokenOffset The offset to start searching from | ||
* @param string $currentLiteralDelimiter The delimiter of the current string literal | ||
* | ||
* @return bool Whether the token was found | ||
*/ | ||
private function findClosingQuote( | ||
string $statement, | ||
int &$tokenOffset, | ||
string &$currentLiteralDelimiter | ||
) : bool { | ||
$token = $this->findToken( | ||
$statement, | ||
$tokenOffset, | ||
'/' . preg_quote($currentLiteralDelimiter, '/') . '/' | ||
); | ||
|
||
if (! $token) { | ||
return false; | ||
} | ||
|
||
$currentLiteralDelimiter = null; | ||
++$tokenOffset; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Finds the token described by regex starting from the given offset. Updates the offset with the position | ||
* where the token was found. | ||
* | ||
* @param string $statement The SQL statement to parse | ||
* @param int $offset The offset to start searching from | ||
* @param string $regex The regex containing token pattern | ||
* | ||
* @return string|null Token or NULL if not found | ||
*/ | ||
private function findToken(string $statement, int &$offset, string $regex) : ?string | ||
{ | ||
if (preg_match($regex, $statement, $matches, PREG_OFFSET_CAPTURE, $offset)) { | ||
$offset = $matches[0][1]; | ||
|
||
return $matches[0][0]; | ||
} | ||
|
||
return null; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
lib/Doctrine/DBAL/Driver/OCI8/NonTerminatedStringLiteral.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Driver\OCI8; | ||
|
||
use function sprintf; | ||
|
||
final class NonTerminatedStringLiteral extends OCI8Exception | ||
{ | ||
public static function new(int $offset) : self | ||
{ | ||
return new self( | ||
sprintf( | ||
'The statement contains non-terminated string literal starting at offset %d.', | ||
$offset | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably start adding
@psalm-param list<T>
for theseThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add Psalm to the build pipeline first? Consumers using Psalm will benefit from those annotations regardless but we want to make sure those annotations are valid and kept up to date.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing - it's a big amount of work to get it to "like" our codebase though 😅