Skip to content
This repository has been archived by the owner on Oct 8, 2023. It is now read-only.

Commit

Permalink
VII
Browse files Browse the repository at this point in the history
  • Loading branch information
mondrake committed Aug 31, 2023
1 parent 36ba3ac commit 9e79960
Showing 1 changed file with 58 additions and 62 deletions.
120 changes: 58 additions & 62 deletions src/Driver/Database/mysqli/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,28 @@ final class Parser
{
private const SPECIAL_CHARS = ':\?\'"`\\[\\-\\/';

private const BACKTICK_IDENTIFIER = '`[^`]*`';
private const BRACKET_IDENTIFIER = '(?<!\b(?i:ARRAY))\[(?:[^\]])*\]';
private const MULTICHAR = ':{2,}';
private const NAMED_PARAMETER = ':[a-zA-Z0-9_]+';
private const BACKTICK_IDENTIFIER = '`[^`]*`';
private const BRACKET_IDENTIFIER = '(?<!\b(?i:ARRAY))\[(?:[^\]])*\]';
private const MULTICHAR = ':{2,}';
private const NAMED_PARAMETER = ':[a-zA-Z0-9_]+';
private const POSITIONAL_PARAMETER = '(?<!\\?)\\?(?!\\?)';
private const ONE_LINE_COMMENT = '--[^\r\n]*';
private const MULTI_LINE_COMMENT = '/\*([^*]+|\*+[^/*])*\**\*/';
private const SPECIAL = '[' . self::SPECIAL_CHARS . ']';
private const OTHER = '[^' . self::SPECIAL_CHARS . ']+';
private const ONE_LINE_COMMENT = '--[^\r\n]*';
private const MULTI_LINE_COMMENT = '/\*([^*]+|\*+[^/*])*\**\*/';
private const SPECIAL = '[' . self::SPECIAL_CHARS . ']';
private const OTHER = '[^' . self::SPECIAL_CHARS . ']+';

private string $sqlPattern;

/** @var array<int,mixed>|array<string,mixed> */
private $originalParameters = [];
private array $originalParameters = [];

/** @var int */
private $originalParameterIndex = 0;
private int $originalParameterIndex = 0;

/** @var list<string> */
private $convertedSQL = [];
private array $convertedSQL = [];

/** @var list<mixed> */
private $convertedParameters = [];
private array $convertedParameters = [];

public function __construct()
{
Expand Down Expand Up @@ -71,16 +70,16 @@ public function parse(string $sql, array $args): void {

/** @var array<string,callable> $patterns */
$patterns = [
self::NAMED_PARAMETER => static function (string $sql): void {
self::NAMED_PARAMETER => function (string $sql): void {
$this->acceptNamedParameter($sql);
},
self::POSITIONAL_PARAMETER => static function (string $sql): void {
self::POSITIONAL_PARAMETER => function (string $sql): void {
$this->acceptPositionalParameter($sql);
},
$this->sqlPattern => static function (string $sql): void {
$this->sqlPattern => function (string $sql): void {
$this->acceptOther($sql);
},
self::SPECIAL => static function (string $sql): void {
self::SPECIAL => function (string $sql): void {
$this->acceptOther($sql);
},
];
Expand Down Expand Up @@ -115,60 +114,57 @@ private function getAnsiSQLStringLiteralPattern(string $delimiter): string {
* @todo
*/
private function acceptPositionalParameter(string $sql): void {
dump([__METHOD__, $sql]);
$index = $this->originalParameterIndex;
$index = $this->originalParameterIndex;

if (!array_key_exists($index, $this->originalParameters)) {
throw \RuntimeException('Missing Positional Parameter ' . $index);
}

$this->acceptParameter($index, $this->originalParameters[$index]);

$this->originalParameterIndex++;
if (!array_key_exists($index, $this->originalParameters)) {
throw \RuntimeException('Missing Positional Parameter ' . $index);
}

/**
* @todo
*/
private function acceptNamedParameter(string $sql): void {
dump([__METHOD__, $sql]);
$name = substr($sql, 1);
$this->acceptParameter($index, $this->originalParameters[$index]);

if (!array_key_exists($name, $this->originalParameters)) {
throw \RuntimeException('Missing Named Parameter ' . $name);
}
$this->originalParameterIndex++;
}

$this->acceptParameter($name, $this->originalParameters[$name]);
}
/**
* @todo
*/
private function acceptNamedParameter(string $sql): void {
$name = substr($sql, 1);

/**
* @todo
*/
private function acceptOther(string $sql): void {
dump([__METHOD__, $sql]);
$this->convertedSQL[] = $sql;
if (!array_key_exists($name, $this->originalParameters)) {
throw \RuntimeException('Missing Named Parameter ' . $name);
}

/**
* @todo
*/
public function getConvertedSQL(): string {
return implode('', $this->convertedSQL);
}
$this->acceptParameter($name, $this->originalParameters[$name]);
}

/**
* @todo
*/
public function getConvertedParameters(): array {
return $this->convertedParameters;
}
/**
* @todo
*/
private function acceptOther(string $sql): void {
$this->convertedSQL[] = $sql;
}

/**
* @todo
*/
private function acceptParameter($key, $value): void {
$this->convertedSQL[] = '?';
$this->convertedParameters[] = $value;
}
/**
* @todo
*/
public function getConvertedSQL(): string {
return implode('', $this->convertedSQL);
}

/**
* @todo
*/
public function getConvertedParameters(): array {
return $this->convertedParameters;
}

/**
* @todo
*/
private function acceptParameter($key, $value): void {
$this->convertedSQL[] = '?';
$this->convertedParameters[] = $value;
}

}

0 comments on commit 9e79960

Please sign in to comment.