diff --git a/src/Medoo.php b/src/Medoo.php index 6d4bad89..d693fd67 100644 --- a/src/Medoo.php +++ b/src/Medoo.php @@ -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. * @@ -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. * @@ -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}."); @@ -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}.");