Skip to content

Commit

Permalink
[fix] Fix column and table name with single character only #981 #983
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelaon committed Jun 26, 2021
1 parent c69c054 commit bc17bcc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/Medoo.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ protected function generate(string $statement, array $map): string
];

$statement = preg_replace(
'/(?!\'[^\s]+\s?)"([\p{L}_][\p{L}\p{N}@$#\-_]+)"(?!\s?[^\s]+\')/u',
'/(?!\'[^\s]+\s?)"([\p{L}_][\p{L}\p{N}@$#\-_]*)"(?!\s?[^\s]+\')/u',
$identifier[$this->type] ?? '"$1"',
$statement
);
Expand Down Expand Up @@ -659,7 +659,7 @@ protected function buildRaw($raw, array &$map): ?string
}

$query = preg_replace_callback(
'/(([`\']).*?)?((FROM|TABLE|INTO|UPDATE|JOIN)\s*)?\<(([\p{L}_][\p{L}\p{N}@$#\-_]+)(\.[\p{L}_][\p{L}\p{N}@$#\-_]+)?)\>([^,]*?\2)?/u',
'/(([`\']).*?)?((FROM|TABLE|INTO|UPDATE|JOIN)\s*)?\<(([\p{L}_][\p{L}\p{N}@$#\-_]*)(\.[\p{L}_][\p{L}\p{N}@$#\-_]*)?)\>([^,]*?\2)?/u',
function ($matches) {
if (!empty($matches[2]) && isset($matches[8])) {
return $matches[0];
Expand Down Expand Up @@ -708,7 +708,7 @@ public function quote(string $string): string
*/
public function tableQuote(string $table): string
{
if (preg_match('/^[\p{L}_][\p{L}\p{N}@$#\-_]+$/u', $table)) {
if (preg_match('/^[\p{L}_][\p{L}\p{N}@$#\-_]*$/u', $table)) {
return '"' . $this->prefix . $table . '"';
}

Expand All @@ -723,7 +723,7 @@ public function tableQuote(string $table): string
*/
public function columnQuote(string $column): string
{
if (preg_match('/^[\p{L}_][\p{L}\p{N}@$#\-_]+(\.?[\p{L}_][\p{L}\p{N}@$#\-_]+)?$/u', $column)) {
if (preg_match('/^[\p{L}_][\p{L}\p{N}@$#\-_]*(\.?[\p{L}_][\p{L}\p{N}@$#\-_]*)?$/u', $column)) {
return strpos($column, '.') !== false ?
'"' . $this->prefix . str_replace('.', '"."', $column) . '"' :
'"' . $column . '"';
Expand Down Expand Up @@ -1168,7 +1168,7 @@ protected function selectContext(
array $where = null,
$columnFn = null
): string {
preg_match('/(?<table>[\p{L}_][\p{L}\p{N}@$#\-_]+)\s*\((?<alias>[\p{L}_][\p{L}\p{N}@$#\-_]+)\)/u', $table, $tableMatch);
preg_match('/(?<table>[\p{L}_][\p{L}\p{N}@$#\-_]*)\s*\((?<alias>[\p{L}_][\p{L}\p{N}@$#\-_]*)\)/u', $table, $tableMatch);

if (isset($tableMatch['table'], $tableMatch['alias'])) {
$table = $this->tableQuote($tableMatch['table']);
Expand Down Expand Up @@ -1269,7 +1269,7 @@ protected function buildJoin(string $table, array $join, array &$map): string
];

foreach ($join as $subtable => $relation) {
preg_match('/(\[(?<join>\<\>?|\>\<?)\])?(?<table>[\p{L}_][\p{L}\p{N}@$#\-_]+)\s?(\((?<alias>[\p{L}_][\p{L}\p{N}@$#\-_]+)\))?/u', $subtable, $match);
preg_match('/(\[(?<join>\<\>?|\>\<?)\])?(?<table>[\p{L}_][\p{L}\p{N}@$#\-_]*)\s?(\((?<alias>[\p{L}_][\p{L}\p{N}@$#\-_]*)\))?/u', $subtable, $match);

if ($match['join'] === '' || $match['table'] === '') {
continue;
Expand Down Expand Up @@ -1336,7 +1336,7 @@ protected function columnMap($columns, array &$stack, bool $root): array

foreach ($columns as $key => $value) {
if (is_int($key)) {
preg_match('/([\p{L}_][\p{L}\p{N}@$#\-_]+\.)?(?<column>[\p{L}_][\p{L}\p{N}@$#\-_]+)(?:\s*\((?<alias>[\p{L}_][\p{L}\p{N}@$#\-_]+)\))?(?:\s*\[(?<type>(?:String|Bool|Int|Number|Object|JSON))\])?/u', $value, $keyMatch);
preg_match('/([\p{L}_][\p{L}\p{N}@$#\-_]*\.)?(?<column>[\p{L}_][\p{L}\p{N}@$#\-_]*)(?:\s*\((?<alias>[\p{L}_][\p{L}\p{N}@$#\-_]*)\))?(?:\s*\[(?<type>(?:String|Bool|Int|Number|Object|JSON))\])?/u', $value, $keyMatch);

$columnKey = !empty($keyMatch['alias']) ?
$keyMatch['alias'] :
Expand All @@ -1346,7 +1346,7 @@ protected function columnMap($columns, array &$stack, bool $root): array
[$columnKey, $keyMatch['type']] :
[$columnKey, 'String'];
} elseif ($this->isRaw($value)) {
preg_match('/([\p{L}_][\p{L}\p{N}@$#\-_]+\.)?(?<column>[\p{L}_][\p{L}\p{N}@$#\-_]+)(\s*\[(?<type>(String|Bool|Int|Number))\])?/u', $key, $keyMatch);
preg_match('/([\p{L}_][\p{L}\p{N}@$#\-_]*\.)?(?<column>[\p{L}_][\p{L}\p{N}@$#\-_]*)(\s*\[(?<type>(String|Bool|Int|Number))\])?/u', $key, $keyMatch);
$columnKey = $keyMatch['column'];

$stack[$key] = isset($keyMatch['type']) ?
Expand Down Expand Up @@ -1389,7 +1389,7 @@ protected function dataMap(

if (count($columnsKey) === 1 && is_array($columns[$columnsKey[0]])) {
$indexKey = array_keys($columns)[0];
$dataKey = preg_replace("/^[\p{L}_][\p{L}\p{N}@$#\-_]+\./u", '', $indexKey);
$dataKey = preg_replace("/^[\p{L}_][\p{L}\p{N}@$#\-_]*\./u", '', $indexKey);
$currentStack = [];

foreach ($data as $item) {
Expand Down Expand Up @@ -1523,7 +1523,7 @@ public function create(string $table, $columns, $options = null): ?PDOStatement

foreach ($columns as $name => $definition) {
if (is_int($name)) {
$stack[] = preg_replace('/\<([\p{L}_][\p{L}\p{N}@$#\-_]+)\>/u', '"$1"', $definition);
$stack[] = preg_replace('/\<([\p{L}_][\p{L}\p{N}@$#\-_]*)\>/u', '"$1"', $definition);
} elseif (is_array($definition)) {
$stack[] = $this->columnQuote($name) . ' ' . implode(' ', $definition);
} elseif (is_string($definition)) {
Expand Down Expand Up @@ -1778,7 +1778,7 @@ public function update(string $table, $data, $where = null): ?PDOStatement
continue;
}

preg_match('/(?<column>[\p{L}_][\p{L}\p{N}@$#\-_]+)(\[(?<operator>\+|\-|\*|\/)\])?/u', $key, $match);
preg_match('/(?<column>[\p{L}_][\p{L}\p{N}@$#\-_]*)(\[(?<operator>\+|\-|\*|\/)\])?/u', $key, $match);

if (isset($match['operator'])) {
if (is_numeric($value)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/MedooTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function expectedQuery($expected): string
];

return preg_replace(
'/(?!\'[^\s]+\s?)"((?![_\d])[\p{N}\p{L}\-_]+)"(?!\s?[^\s]+\')/u',
'/(?!\'[^\s]+\s?)"([\p{L}_][\p{L}\p{N}@$#\-_]*)"(?!\s?[^\s]+\')/u',
$identifier[$this->database->type] ?? '"$1"',
str_replace("\n", " ", $expected)
);
Expand Down
25 changes: 25 additions & 0 deletions tests/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,4 +687,29 @@ public function testSelectWithHyphenCharacter($type)
$this->database->queryString
);
}

/**
* @covers ::columnMap()
* @covers ::columnPush()
* @dataProvider typesProvider
*/
public function testSelectWithSingleCharacter($type)
{
$this->setType($type);

$this->database->select("a", [
"[>]e" => ["f"]
], [
"b (c)"
]);

$this->assertQuery(
<<<EOD
SELECT "b" AS "c"
FROM "a"
LEFT JOIN "e" USING ("f")
EOD,
$this->database->queryString
);
}
}

0 comments on commit bc17bcc

Please sign in to comment.