-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuotesIdentifiers.php
More file actions
68 lines (55 loc) · 2.1 KB
/
QuotesIdentifiers.php
File metadata and controls
68 lines (55 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace Utopia\Query;
use Utopia\Query\Exception\ValidationException;
trait QuotesIdentifiers
{
protected string $wrapChar = '`';
protected function quote(string $identifier): string
{
if ($identifier === '*') {
return '*';
}
if (\preg_match('/[\x00-\x1f\x7f]/', $identifier) === 1) {
throw new ValidationException('Identifier contains control character');
}
if (!\str_contains($identifier, '.')) {
return $this->wrapChar
. \str_replace($this->wrapChar, $this->wrapChar . $this->wrapChar, $identifier)
. $this->wrapChar;
}
$segments = \explode('.', $identifier);
$lastIndex = \count($segments) - 1;
$wrapped = [];
foreach ($segments as $index => $segment) {
if ($segment === '*' && $index === $lastIndex) {
$wrapped[] = '*';
continue;
}
$wrapped[] = $this->wrapChar
. \str_replace($this->wrapChar, $this->wrapChar . $this->wrapChar, $segment)
. $this->wrapChar;
}
return \implode('.', $wrapped);
}
/**
* Quote a single identifier without treating dots as qualifier separators.
*
* Use when the identifier is known to be atomic — e.g. a column name in a
* CREATE TABLE definition where the dot is a literal part of the name
* rather than a `schema.table.column` separator. The canonical case is
* ClickHouse's nested-array convention (`meta.key Array(String)`) where
* `meta.key` is a single top-level column whose name contains a dot.
*/
protected function quoteLiteral(string $identifier): string
{
if ($identifier === '*') {
return '*';
}
if (\preg_match('/[\x00-\x1f\x7f]/', $identifier) === 1) {
throw new ValidationException('Identifier contains control character');
}
return $this->wrapChar
. \str_replace($this->wrapChar, $this->wrapChar . $this->wrapChar, $identifier)
. $this->wrapChar;
}
}