2323use Doctrine \DBAL \Platforms \AbstractPlatform ;
2424use FOD \DBALClickHouse \Driver \Exception \Exception ;
2525
26- use function array_map ;
27- use function array_replace ;
28- use function current ;
29- use function implode ;
30- use function is_array ;
31- use function is_bool ;
32- use function is_float ;
33- use function is_int ;
34- use function mb_stripos ;
35-
3626class ClickHouseStatement implements Statement
3727{
3828 protected Client $ client ;
@@ -47,9 +37,9 @@ class ClickHouseStatement implements Statement
4737
4838 public function __construct (Client $ client , string $ statement , AbstractPlatform $ platform )
4939 {
50- $ this ->client = $ client ;
40+ $ this ->client = $ client ;
5141 $ this ->statement = $ statement ;
52- $ this ->platform = $ platform ;
42+ $ this ->platform = $ platform ;
5343 }
5444
5545 /**
@@ -58,7 +48,7 @@ public function __construct(Client $client, string $statement, AbstractPlatform
5848 public function bindValue ($ param , $ value , $ type = ParameterType::STRING ): bool
5949 {
6050 $ this ->values [$ param ] = $ value ;
61- $ this ->types [$ param ] = $ type ;
51+ $ this ->types [$ param ] = $ type ;
6252
6353 return true ;
6454 }
@@ -69,7 +59,7 @@ public function bindValue($param, $value, $type = ParameterType::STRING): bool
6959 public function bindParam ($ param , &$ variable , $ type = ParameterType::STRING , $ length = null ): bool
7060 {
7161 $ this ->values [$ param ] = $ variable ;
72- $ this ->types [$ param ] = $ type ;
62+ $ this ->types [$ param ] = $ type ;
7363
7464 return true ;
7565 }
@@ -79,16 +69,16 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le
7969 */
8070 public function execute ($ params = null ): Result
8171 {
82- if (is_array ($ params )) {
83- $ this ->values = array_replace ($ this ->values , $ params );
72+ if (\ is_array ($ params )) {
73+ $ this ->values = \ array_replace ($ this ->values , $ params );
8474 }
8575
8676 $ statement = $ this ->statement ;
8777
8878 $ firstPlaceholder = array_key_first ($ this ->values );
8979
90- $ positionalPlaceholders = is_int ($ firstPlaceholder );
91- $ positionalPlaceholdersIsList = $ firstPlaceholder === 0 ;
80+ $ positionalPlaceholders = \ is_int ($ firstPlaceholder );
81+ $ positionalPlaceholdersIsList = 0 === $ firstPlaceholder ;
9282
9383 if ($ positionalPlaceholders ) {
9484 $ pieces = explode ('? ' , $ statement );
@@ -101,14 +91,14 @@ public function execute($params = null): Result
10191 }
10292 }
10393
104- $ statement = implode ('' , $ pieces );
94+ $ statement = \ implode ('' , $ pieces );
10595 } else {
10696 foreach (array_keys ($ this ->values ) as $ key ) {
107- $ namedPlaceholder = ": $ key " ;
108- $ namedPlaceholderOffset = mb_stripos ($ statement , $ namedPlaceholder );
97+ $ namedPlaceholder = ": $ key " ;
98+ $ namedPlaceholderOffset = \ mb_stripos ($ statement , $ namedPlaceholder );
10999 $ namedPlaceholderLength = mb_strlen ($ namedPlaceholder );
110100
111- if ($ namedPlaceholderOffset !== false ) {
101+ if (false !== $ namedPlaceholderOffset ) {
112102 $ statement = substr_replace (
113103 $ statement ,
114104 $ this ->resolveType ($ key ),
@@ -122,9 +112,10 @@ public function execute($params = null): Result
122112 try {
123113 return new ClickHouseResult (
124114 new \ArrayIterator (
125- mb_stripos ($ statement , 'select ' ) === 0 ||
126- mb_stripos ($ statement , 'show ' ) === 0 ||
127- mb_stripos ($ statement , 'describe ' ) === 0
115+ 0 === \mb_stripos ($ statement , 'select ' )
116+ || 1 === preg_match ('/with(.*)\)\s*select/ms ' , mb_strtolower ($ statement ))
117+ || 0 === \mb_stripos ($ statement , 'show ' )
118+ || 0 === \mb_stripos ($ statement , 'describe ' )
128119 ? $ this ->client ->select ($ statement )->rows ()
129120 : $ this ->client ->write ($ statement )->rows ()
130121 )
@@ -141,32 +132,32 @@ protected function resolveType(int|string $key): string
141132 {
142133 $ value = $ this ->values [$ key ];
143134
144- if ($ value === null ) {
135+ if (null === $ value ) {
145136 return 'NULL ' ;
146137 }
147138
148- if (is_array ($ value )) {
149- if (is_int (current ($ value )) || is_float (current ($ value ))) {
139+ if (\ is_array ($ value )) {
140+ if (\ is_int (\ current ($ value )) || \ is_float (\ current ($ value ))) {
150141 foreach ($ value as $ item ) {
151- if (!is_int ($ item ) && !is_float ($ item )) {
142+ if (!\ is_int ($ item ) && !\ is_float ($ item )) {
152143 throw new DBALException ('Array values must all be int/float or string, mixes not allowed ' );
153144 }
154145 }
155146 } else {
156- $ value = array_map (function (?string $ item ): string {
157- return $ item === null ? 'NULL ' : $ this ->platform ->quoteStringLiteral ($ item );
147+ $ value = \ array_map (function (?string $ item ): string {
148+ return null === $ item ? 'NULL ' : $ this ->platform ->quoteStringLiteral ($ item );
158149 }, $ value );
159150 }
160151
161- return '[ ' . implode (', ' , $ value ) . '] ' ;
152+ return '[ ' . \ implode (', ' , $ value ) . '] ' ;
162153 }
163154
164155 $ type = $ this ->types [$ key ] ?? null ;
165156
166- if ($ type === null ) {
167- if (is_int ($ value ) || is_float ($ value )) {
157+ if (null === $ type ) {
158+ if (\ is_int ($ value ) || \ is_float ($ value )) {
168159 $ type = ParameterType::INTEGER ;
169- } elseif (is_bool ($ value )) {
160+ } elseif (\ is_bool ($ value )) {
170161 $ type = ParameterType::BOOLEAN ;
171162 }
172163 }
0 commit comments