23
23
use Doctrine \DBAL \Platforms \AbstractPlatform ;
24
24
use FOD \DBALClickHouse \Driver \Exception \Exception ;
25
25
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
-
36
26
class ClickHouseStatement implements Statement
37
27
{
38
28
protected Client $ client ;
@@ -47,9 +37,9 @@ class ClickHouseStatement implements Statement
47
37
48
38
public function __construct (Client $ client , string $ statement , AbstractPlatform $ platform )
49
39
{
50
- $ this ->client = $ client ;
40
+ $ this ->client = $ client ;
51
41
$ this ->statement = $ statement ;
52
- $ this ->platform = $ platform ;
42
+ $ this ->platform = $ platform ;
53
43
}
54
44
55
45
/**
@@ -58,7 +48,7 @@ public function __construct(Client $client, string $statement, AbstractPlatform
58
48
public function bindValue ($ param , $ value , $ type = ParameterType::STRING ): bool
59
49
{
60
50
$ this ->values [$ param ] = $ value ;
61
- $ this ->types [$ param ] = $ type ;
51
+ $ this ->types [$ param ] = $ type ;
62
52
63
53
return true ;
64
54
}
@@ -69,7 +59,7 @@ public function bindValue($param, $value, $type = ParameterType::STRING): bool
69
59
public function bindParam ($ param , &$ variable , $ type = ParameterType::STRING , $ length = null ): bool
70
60
{
71
61
$ this ->values [$ param ] = $ variable ;
72
- $ this ->types [$ param ] = $ type ;
62
+ $ this ->types [$ param ] = $ type ;
73
63
74
64
return true ;
75
65
}
@@ -79,16 +69,16 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le
79
69
*/
80
70
public function execute ($ params = null ): Result
81
71
{
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 );
84
74
}
85
75
86
76
$ statement = $ this ->statement ;
87
77
88
78
$ firstPlaceholder = array_key_first ($ this ->values );
89
79
90
- $ positionalPlaceholders = is_int ($ firstPlaceholder );
91
- $ positionalPlaceholdersIsList = $ firstPlaceholder === 0 ;
80
+ $ positionalPlaceholders = \ is_int ($ firstPlaceholder );
81
+ $ positionalPlaceholdersIsList = 0 === $ firstPlaceholder ;
92
82
93
83
if ($ positionalPlaceholders ) {
94
84
$ pieces = explode ('? ' , $ statement );
@@ -101,14 +91,14 @@ public function execute($params = null): Result
101
91
}
102
92
}
103
93
104
- $ statement = implode ('' , $ pieces );
94
+ $ statement = \ implode ('' , $ pieces );
105
95
} else {
106
96
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 );
109
99
$ namedPlaceholderLength = mb_strlen ($ namedPlaceholder );
110
100
111
- if ($ namedPlaceholderOffset !== false ) {
101
+ if (false !== $ namedPlaceholderOffset ) {
112
102
$ statement = substr_replace (
113
103
$ statement ,
114
104
$ this ->resolveType ($ key ),
@@ -122,9 +112,10 @@ public function execute($params = null): Result
122
112
try {
123
113
return new ClickHouseResult (
124
114
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 ' )
128
119
? $ this ->client ->select ($ statement )->rows ()
129
120
: $ this ->client ->write ($ statement )->rows ()
130
121
)
@@ -141,32 +132,32 @@ protected function resolveType(int|string $key): string
141
132
{
142
133
$ value = $ this ->values [$ key ];
143
134
144
- if ($ value === null ) {
135
+ if (null === $ value ) {
145
136
return 'NULL ' ;
146
137
}
147
138
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 ))) {
150
141
foreach ($ value as $ item ) {
151
- if (!is_int ($ item ) && !is_float ($ item )) {
142
+ if (!\ is_int ($ item ) && !\ is_float ($ item )) {
152
143
throw new DBALException ('Array values must all be int/float or string, mixes not allowed ' );
153
144
}
154
145
}
155
146
} 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 );
158
149
}, $ value );
159
150
}
160
151
161
- return '[ ' . implode (', ' , $ value ) . '] ' ;
152
+ return '[ ' . \ implode (', ' , $ value ) . '] ' ;
162
153
}
163
154
164
155
$ type = $ this ->types [$ key ] ?? null ;
165
156
166
- if ($ type === null ) {
167
- if (is_int ($ value ) || is_float ($ value )) {
157
+ if (null === $ type ) {
158
+ if (\ is_int ($ value ) || \ is_float ($ value )) {
168
159
$ type = ParameterType::INTEGER ;
169
- } elseif (is_bool ($ value )) {
160
+ } elseif (\ is_bool ($ value )) {
170
161
$ type = ParameterType::BOOLEAN ;
171
162
}
172
163
}
0 commit comments