1010use function addcslashes ;
1111use function array_map ;
1212use function array_slice ;
13- use function count ;
1413use function explode ;
1514use function implode ;
1615use function is_string ;
1716use function preg_match ;
18- use function preg_replace ;
1917use function preg_replace_callback ;
2018use function str_contains ;
2119use function str_replace ;
@@ -117,22 +115,21 @@ public function ensureNameQuoted(string $name): string
117115 {
118116 $ name = str_replace (["' " , '" ' , '` ' , '[ ' , '] ' ], '' , $ name );
119117
120- if ($ name && ! preg_match ( ' /^{{.*}}$/ ' , $ name )) {
121- return ' {{ ' . $ name . ' }} ' ;
118+ if (empty ( $ name) || str_starts_with ( $ name , ' {{ ' )) {
119+ return $ name ;
122120 }
123121
124- return $ name ;
122+ return ' {{ ' . $ name . ' }} ' ;
125123 }
126124
127125 public function ensureColumnName (string $ name ): string
128126 {
129- if (strrpos ($ name , '. ' ) !== false ) {
130- $ parts = explode ('. ' , $ name );
131- $ name = $ parts [count ($ parts ) - 1 ];
127+ if (($ pos = strrpos ($ name , '. ' )) !== false ) {
128+ $ name = substr ($ name , $ pos + 1 );
132129 }
133130
134131 /** @var string */
135- return preg_replace ('|^\[\[([\w\-. ]+)]]$| ' , '\1 ' , $ name );
132+ return preg_replace ('|^\[\[([\w\- ]+)]]$| ' , '\1 ' , $ name );
136133 }
137134
138135 public function quoteColumnName (string $ name ): string
@@ -157,13 +154,17 @@ public function quoteColumnName(string $name): string
157154
158155 public function quoteSimpleColumnName (string $ name ): string
159156 {
157+ if ($ name === '' || $ name === '* ' ) {
158+ return $ name ;
159+ }
160+
160161 if (is_string ($ this ->columnQuoteCharacter )) {
161162 $ startingCharacter = $ endingCharacter = $ this ->columnQuoteCharacter ;
162163 } else {
163164 [$ startingCharacter , $ endingCharacter ] = $ this ->columnQuoteCharacter ;
164165 }
165166
166- return $ name === ' * ' || str_starts_with ( $ name , $ startingCharacter)
167+ return $ name[ 0 ] === $ startingCharacter
167168 ? $ name
168169 : $ startingCharacter . $ name . $ endingCharacter ;
169170 }
@@ -176,7 +177,7 @@ public function quoteSimpleTableName(string $name): string
176177 [$ startingCharacter , $ endingCharacter ] = $ this ->tableQuoteCharacter ;
177178 }
178179
179- return str_starts_with ($ name, $ startingCharacter)
180+ return ($ name[ 0 ] ?? '' ) === $ startingCharacter
180181 ? $ name
181182 : $ startingCharacter . $ name . $ endingCharacter ;
182183 }
@@ -185,25 +186,21 @@ public function quoteSql(string $sql): string
185186 {
186187 /** @var string */
187188 return preg_replace_callback (
188- '/( {{(%?[\w\-. ]+)%?}}| \\[ \\[([\w\-. ]+)]]) / ' ,
189+ '/{{(%?[\w\-. ]+)%?}}| \\[ \\[([\w\-. ]+)]]/ ' ,
189190 function ($ matches ) {
190- if (isset ($ matches [3 ])) {
191- return $ this ->quoteColumnName ($ matches [3 ]);
191+ if (isset ($ matches [2 ])) {
192+ return $ this ->quoteSimpleColumnName ($ matches [2 ]);
192193 }
193194
194- return str_replace ('% ' , $ this ->tablePrefix , $ this ->quoteTableName ($ matches [2 ]));
195+ return str_replace ('% ' , $ this ->tablePrefix , $ this ->quoteSimpleTableName ($ matches [1 ]));
195196 },
196197 $ sql
197198 );
198199 }
199200
200201 public function quoteTableName (string $ name ): string
201202 {
202- if (str_starts_with ($ name , '( ' )) {
203- return $ name ;
204- }
205-
206- if (str_contains ($ name , '{{ ' )) {
203+ if ($ name [0 ] === '( ' || str_contains ($ name , '{{ ' )) {
207204 return $ name ;
208205 }
209206
@@ -213,8 +210,8 @@ public function quoteTableName(string $name): string
213210
214211 $ parts = $ this ->getTableNameParts ($ name );
215212
216- foreach ($ parts as $ i => $ part ) {
217- $ parts [ $ i ] = $ this ->quoteSimpleTableName ($ part );
213+ foreach ($ parts as & $ part ) {
214+ $ part = $ this ->quoteSimpleTableName ($ part );
218215 }
219216
220217 return implode ('. ' , $ parts );
@@ -232,26 +229,14 @@ public function setTablePrefix(string $value): void
232229
233230 public function unquoteSimpleColumnName (string $ name ): string
234231 {
235- if (is_string ($ this ->columnQuoteCharacter )) {
236- $ startingCharacter = $ this ->columnQuoteCharacter ;
237- } else {
238- $ startingCharacter = $ this ->columnQuoteCharacter [0 ];
239- }
240-
241- return !str_starts_with ($ name , $ startingCharacter )
232+ return ($ name [0 ] ?? '' ) !== $ this ->columnQuoteCharacter [0 ]
242233 ? $ name
243234 : substr ($ name , 1 , -1 );
244235 }
245236
246237 public function unquoteSimpleTableName (string $ name ): string
247238 {
248- if (is_string ($ this ->tableQuoteCharacter )) {
249- $ startingCharacter = $ this ->tableQuoteCharacter ;
250- } else {
251- $ startingCharacter = $ this ->tableQuoteCharacter [0 ];
252- }
253-
254- return !str_starts_with ($ name , $ startingCharacter )
239+ return ($ name [0 ] ?? '' ) !== $ this ->tableQuoteCharacter [0 ]
255240 ? $ name
256241 : substr ($ name , 1 , -1 );
257242 }
0 commit comments