8
8
* sql-queries to ClickHouse server and parsing answering data.
9
9
*
10
10
* Main query-functions for use:
11
- * - >queryTrue($sql, [post]) - Return false only if error, otherwise return true or data
12
11
* - >queryFalse($sql, [post])- Return false only if NOT error, otherwise string with error.
12
+ * - >queryTrue($sql, [post]) - Return false only if error, otherwise return true or data
13
13
* - >queryValue($sql, [post]) - Send query and receive data in one string (false if error)
14
14
* - >queryArray($sql) - for queries returning multi-rows data
15
15
* - >queryKeyValues(see descr.) - for queries returning 2 columns key => value
@@ -85,9 +85,7 @@ class ClickHouseQuery extends ClickHouseAPI
85
85
public $ extremes ;
86
86
87
87
/**
88
- * Stored [rows]-section from received data, contains rows count.
89
- *
90
- * (not need because count(array) is same)
88
+ * Stored [rows]-section from received data, contains data-rows count.
91
89
*
92
90
* @var integer|null
93
91
*/
@@ -129,10 +127,8 @@ class ClickHouseQuery extends ClickHouseAPI
129
127
130
128
/**
131
129
* For queries that involve either no return value or one string value.
132
- *
133
- * Return true or non-empty string if ok
134
- *
135
- * Return false if error
130
+ * - Return true (or non-empty string) if ok
131
+ * - Return false only if error
136
132
*
137
133
* queryTrue send POST-request by default for clear read_only-flag.
138
134
*
@@ -149,8 +145,8 @@ public function queryTrue($sql, $post_data = [], $sess = null)
149
145
150
146
/**
151
147
* For queries that involve either no return value or any string data.
152
- *
153
- * Return false if ok (no error) or string with error description
148
+ * - Return false only if no errors.
149
+ * - In case of an error, return string with error description
154
150
*
155
151
* queryFalse send POST-request by default for clear read_only-flag.
156
152
*
@@ -166,15 +162,12 @@ public function queryFalse($sql, $post_data = [], $sess = null)
166
162
}
167
163
168
164
/**
169
- * For queries that involve either no return value or string.
170
- *
171
- * Send POST-request if have post_data, send GET-request if no post_data
172
- *
173
- * Return string with results if ok, or false if error.
165
+ * Sends query and returns response data in one string (return false if error)
174
166
*
175
167
* Nuances:
176
- * - Error describe available in $this->last_error_str (or empty string if no error)
168
+ * - Error description available in $this->last_error_str (contains empty string if no error)
177
169
* - To results are applied to the trim function (for removing \n from end)
170
+ * - Function send POST-request if have post_data, send GET-request if no post_data
178
171
*
179
172
* @param string $sql SQL-request
180
173
* @param array|string|null $post_data Post-data or Null for Get-request
@@ -226,15 +219,20 @@ public function queryStrings($sql, $with_names_types = false, $sess = null)
226
219
}
227
220
228
221
/**
229
- * Return Array [keys => values]
222
+ * For queries returning exactly 2 columns, which can mean key => value
230
223
*
231
- * Request data from table by 2 specified field-names
232
- *
233
- * Results of first column interpreted as keys of array, second column as values.
224
+ * Data of first column interpreted as array keys, the second column as values.
225
+ * - If the result contains only 1 column, it returns as an array with numeric keys.
226
+ * - If return contains more of 2 columns, extra data ignored, only 2 columns used.
227
+ * - First column as keys must be unique
228
+ * - if second parameter is null, first parameter must contain full sql-request
229
+ * - if second parameter not null, first parameter means table name (that after FROM)
230
+ * - Second parameter means field names (that between SELECT and FROM)
231
+ * - Returns array only if not error. If error returns string with error description.
234
232
*
235
233
* Similar than queryKeyValArr, but using JSONCompact for data transferring.
236
234
*
237
- * @param string $tbl_or_sql Table name (or SQL-request if next parameter is null)
235
+ * @param string $tbl_or_sql Table name (or SQL-request if next parameter is null)
238
236
* @param string|null $key_and_value_fields field names, example: 'id,name'
239
237
* @param string|null $sess session_id
240
238
* @return array|string Returns array if ok, or string with error description
@@ -253,20 +251,26 @@ public function queryKeyValues(
253
251
if (!\is_array ($ data ) || !\count ($ data )) {
254
252
return $ data ;
255
253
}
256
- if (count ($ data [0 ]) == 1 ) {
254
+ if (\ count ($ data [0 ]) == 1 ) {
257
255
return \array_column ($ data , 0 );
258
256
}
259
257
return \array_combine (\array_column ($ data , 0 ), \array_column ($ data , 1 ));
260
258
}
261
259
262
260
/**
263
- * Return Array [keys => values]
261
+ * Return Array [keys => values], first column of results means keys.
264
262
*
265
263
* Similar than queryKeyValues, but using TabSeparated for data transferring.
266
- * Provides the best performance on powerful servers,
264
+ * Returned data not unescaped. Provides the best performance on powerful servers,
267
265
* but, on weak processors it runs slower than queryKeyValues
268
- * - Returned data not unescaped!
269
- * If unescape is important, it's best to use queryKeyValues instead.
266
+ * - If unescape is important, it's best to use queryKeyValues instead.
267
+ * - If the result contains only 1 column, it returns as an array with numeric keys.
268
+ * - If the results contain 2 columns or more, first column as keys must be unique
269
+ * - If return contains more of 2 columns, values returned in tab-separated format.
270
+ * - if second parameter is null, first parameter must contain full sql-request
271
+ * - if second parameter not null, first parameter means table name (that after FROM)
272
+ * - Second parameter means field names (that between SELECT and FROM), may be '*'
273
+ * - Returns array only if not error. If error returns string with error description.
270
274
*
271
275
* @param string $tbl_or_sql Table name or full sql request
272
276
* @param string|null $key_name_and_value_name fields like 'id, name'
@@ -524,6 +528,15 @@ public function queryInsertFile(
524
528
/**
525
529
* Inserting data into table from array
526
530
*
531
+ * Array of inserting data may be in following variants of format:
532
+ * For insert one row:
533
+ * - [value1, value2, ...] - array without keys. Keys must define in $field_names
534
+ * - [field1=>value1, field2=>value2 ...] - array with keys.
535
+ * For insert many rows:
536
+ * - [[row1value1, row1value2, ...] , [row2value1, row2value2, ...] ...]
537
+ * - [[row1field1=>row1value1, row1field2=>row1value2, ...] , ...]
538
+ * When inserting many rows, it is more efficient to pass an array without keys.
539
+ *
527
540
* @param string $table_name Table name for inserting data
528
541
* @param array|null $fields_names Array with names of inserting fields
529
542
* @param array $fields_set_arr Array with inserting data
@@ -590,14 +603,14 @@ function($s) {
590
603
591
604
592
605
/**
593
- * Add quotes and slashes if need
606
+ * Add quotes and slashes if need for ClickHouse-SQL parameter
594
607
*
595
608
* Examples:
596
609
* - ("123") => 123 (No changes because is_numeric)
597
610
* - ('"aaa"') => "aaa" (No changes because have begin-final quotes)
598
611
* - ("'aaa'") => 'aaa' (No changes because have begin-final quotes)
599
612
* - ("fn(x)") => fn(x) (No changes because have final ")" and "(" within)
600
- * - ("aaa") => 'aaa' (add $quote-quotes and slashes for [ ' \t \n \r ] )
613
+ * - ("aaa") => 'aaa' (add $quote-quotes and slashes for [ ` ' \t \n \r ] )
601
614
*
602
615
* @param string $str
603
616
* @return string
@@ -610,7 +623,7 @@ public function quotePar($str, $quote = "'")
610
623
(($ fc === '" ' || $ fc === "' " ) && ($ fc === $ lc )) ||
611
624
(($ lc === ') ' && strpos ($ str , '( ' ) !== false )) ||
612
625
(($ fc === '[ ' && $ lc === '] ' ))
613
- ) ? $ str : $ quote . addcslashes ($ str , "' \t\n\r\0" ) . $ quote ;
626
+ ) ? $ str : $ quote . addcslashes ($ str , "' \t\n\r\0` " ) . $ quote ;
614
627
}
615
628
616
629
/**
@@ -677,7 +690,7 @@ public function queryTableSubstract(
677
690
678
691
$ bindings ['db ' ] = $ this ->quotePar ($ db );
679
692
$ bindings ['table ' ] = $ this ->quotePar ($ table );
680
- $ bindings ['dbtb ' ] = $ db . '. ' . $ table ;
693
+ $ bindings ['dbtb ' ] = ( $ i ? $ db . '. ' : '' ) . $ table ;
681
694
682
695
$ sql = $ this ->bindPars ($ sql_pattern , $ bindings );
683
696
@@ -742,7 +755,7 @@ public function queryTableSys($table, $sys = 'tables', $columns_del = ['n'])
742
755
], "No information about {dbtb} in {s} " , [], $ columns_del , null
743
756
);
744
757
if (\is_array ($ arr )) {
745
- if (count ($ arr ['columns_arr ' ]) == 1 ) {
758
+ if (\ count ($ arr ['columns_arr ' ]) == 1 ) {
746
759
$ arr = $ arr ['columns_arr ' ][0 ];
747
760
}
748
761
}
0 commit comments