Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions src/Medoo.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ class Medoo
*/
protected $prefix;

/**
* The identifier delimiter used for quoting table and column names.
*
* Defaults to the SQL-92 standard delimiter.
*
* @var string
*/
protected $identifierDelimiter = '""';

/**
* The PDO statement object.
*
Expand Down Expand Up @@ -702,6 +711,26 @@ public function quote(string $string): string
return "'" . preg_replace('/\'/', '\'\'', $string) . "'";
}

/**
* Sets the identifier delimiter for use when quoting tables or columns.
*
* Ensures it is set to a two-character string for usage with databases
* like SQL Server where square brackets are used rather than a single character.
*
* @param string $identifierDelimiter
* @return Medoo
*/
public function setIdentifierDelimiter(string $identifierDelimiter): self
{
if (strlen($identifierDelimiter) === 2) {
$this->identifierDelimiter = $identifierDelimiter;
} elseif (strlen($identifierDelimiter) === 1) {
$this->identifierDelimiter = $identifierDelimiter . $identifierDelimiter;
}

return $this;
}

/**
* Quote table name for use in a query.
*
Expand All @@ -711,7 +740,7 @@ public function quote(string $string): string
public function tableQuote(string $table): string
{
if (preg_match('/^[\p{L}_][\p{L}\p{N}@$#\-_]*$/u', $table)) {
return '"' . $this->prefix . $table . '"';
return $this->identifierDelimiter[0] . $this->prefix . $table . $this->identifierDelimiter[1];
}

throw new InvalidArgumentException("Incorrect table name: {$table}.");
Expand All @@ -727,8 +756,8 @@ public function columnQuote(string $column): string
{
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 . '"';
$this->identifierDelimiter[0] . $this->prefix . str_replace('.', $this->identifierDelimiter[1] . '.' . $this->identifierDelimiter[0], $column) . $this->identifierDelimiter[1] :
$this->identifierDelimiter[0] . $column . $this->identifierDelimiter[1];
}

throw new InvalidArgumentException("Incorrect column name: {$column}.");
Expand Down