Skip to content

Commit

Permalink
Quoter: Accept arrays of strings and quote parts as-is
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Feb 21, 2022
1 parent a28c024 commit ce0725c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
20 changes: 13 additions & 7 deletions src/Adapter/BaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,23 @@ public function setClientTimezone(Connection $db)
{
}

public function quoteIdentifier($identifier)
public function quoteIdentifier($identifiers)
{
if ($identifier === '*') {
return $identifier;
if (is_string($identifiers)) {
$identifiers = explode('.', $identifiers);
}

$identifier = str_replace($this->quoteCharacter[0], $this->escapeCharacter, $identifier);
foreach ($identifiers as $i => $identifier) {
if ($identifier === '*') {
continue;
}

$identifiers[$i] = $this->quoteCharacter[0]
. str_replace($this->quoteCharacter[0], $this->escapeCharacter, $identifier)
. $this->quoteCharacter[1];
}

return $this->quoteCharacter[0]
. str_replace('.', "{$this->quoteCharacter[0]}.{$this->quoteCharacter[1]}", $identifier)
. $this->quoteCharacter[1];
return implode('.', $identifiers);
}

protected function getTimezoneOffset()
Expand Down
10 changes: 7 additions & 3 deletions src/Contract/Quoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
interface Quoter
{
/**
* Quote a string so that it can be safely used as table or column name, even if it is a reserved name
* Quote an identifier so that it can be safely used as table or column name, even if it is a reserved name
*
* If a string is passed that contains dots, the parts separated by them are quoted individually.
* (e.g. `myschema.mytable` turns into `"myschema"."mytable"`) If an array is passed, the entries
* are quoted as-is. (e.g. `[myschema.my, table]` turns into `"myschema.my"."table"`)
*
* The quote character depends on the underlying database adapter that is being used.
*
* @param string $identifier
* @param string|string[] $identifiers
*
* @return string
*/
public function quoteIdentifier($identifier);
public function quoteIdentifier($identifiers);
}

0 comments on commit ce0725c

Please sign in to comment.