From a35cc192ad6bbf055fb774803f5d5e2de3008395 Mon Sep 17 00:00:00 2001 From: Omar Ramos Date: Mon, 12 Sep 2022 11:44:01 -0700 Subject: [PATCH 1/4] Added support for identifier delimiter modification. --- src/Medoo.php | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/Medoo.php b/src/Medoo.php index 6d4bad89..e8b699e0 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,24 @@ 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 void + */ + public function setIdentifierDelimiter(string $identifierDelimiter): void + { + if (strlen($identifierDelimiter) === 2) { + $this->identifierDelimiter = $identifierDelimiter; + } elseif (strlen($identifierDelimiter) === 1) { + $this->identifierDelimiter = $identifierDelimiter . $identifierDelimiter; + } + } + /** * Quote table name for use in a query. * @@ -711,7 +738,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 +754,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('.', '"."', $column) . $this->identifierDelimiter[1] : + $this->identifierDelimiter[0] . $column . $this->identifierDelimiter[1]; } throw new InvalidArgumentException("Incorrect column name: {$column}."); From 0a2ce5673ebbe0eb2ea507e98f362addd66387d7 Mon Sep 17 00:00:00 2001 From: Omar Ramos Date: Mon, 12 Sep 2022 12:22:12 -0700 Subject: [PATCH 2/4] Made new method chainable and corrected issue in columnQuote() --- src/Medoo.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Medoo.php b/src/Medoo.php index e8b699e0..7fba7b15 100644 --- a/src/Medoo.php +++ b/src/Medoo.php @@ -718,15 +718,17 @@ public function quote(string $string): string * like SQL Server where square brackets are used rather than a single character. * * @param string $identifierDelimiter - * @return void + * @return Medoo */ - public function setIdentifierDelimiter(string $identifierDelimiter): void + public function setIdentifierDelimiter(string $identifierDelimiter): self { if (strlen($identifierDelimiter) === 2) { $this->identifierDelimiter = $identifierDelimiter; } elseif (strlen($identifierDelimiter) === 1) { $this->identifierDelimiter = $identifierDelimiter . $identifierDelimiter; } + + return $this; } /** @@ -754,7 +756,7 @@ 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->identifierDelimiter[0] . $this->prefix . str_replace('.', '"."', $column) . $this->identifierDelimiter[1] : + $this->identifierDelimiter[0] . $this->prefix . str_replace('.', $this->identifierDelimiter[0] . '.' . $this->identifierDelimiter[1], $column) . $this->identifierDelimiter[1] : $this->identifierDelimiter[0] . $column . $this->identifierDelimiter[1]; } From 69462ae75166429758bc00f66ad0ad87043f46f1 Mon Sep 17 00:00:00 2001 From: Omar Ramos Date: Mon, 12 Sep 2022 12:23:12 -0700 Subject: [PATCH 3/4] Convert tabs to spaces --- src/Medoo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Medoo.php b/src/Medoo.php index 7fba7b15..eb56e3b1 100644 --- a/src/Medoo.php +++ b/src/Medoo.php @@ -728,7 +728,7 @@ public function setIdentifierDelimiter(string $identifierDelimiter): self $this->identifierDelimiter = $identifierDelimiter . $identifierDelimiter; } - return $this; + return $this; } /** From 01f561947b754a409be3ea5b22ed86a9cad2a494 Mon Sep 17 00:00:00 2001 From: Omar Ramos Date: Mon, 12 Sep 2022 13:18:50 -0700 Subject: [PATCH 4/4] Small logic correction in columnQuote() --- src/Medoo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Medoo.php b/src/Medoo.php index eb56e3b1..d693fd67 100644 --- a/src/Medoo.php +++ b/src/Medoo.php @@ -756,7 +756,7 @@ 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->identifierDelimiter[0] . $this->prefix . str_replace('.', $this->identifierDelimiter[0] . '.' . $this->identifierDelimiter[1], $column) . $this->identifierDelimiter[1] : + $this->identifierDelimiter[0] . $this->prefix . str_replace('.', $this->identifierDelimiter[1] . '.' . $this->identifierDelimiter[0], $column) . $this->identifierDelimiter[1] : $this->identifierDelimiter[0] . $column . $this->identifierDelimiter[1]; }