88 * sql-queries to ClickHouse server and parsing answering data.
99 *
1010 * Main query-functions for use:
11- * - >queryTrue($sql, [post]) - Return false only if error, otherwise return true or data
1211 * - >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
1313 * - >queryValue($sql, [post]) - Send query and receive data in one string (false if error)
1414 * - >queryArray($sql) - for queries returning multi-rows data
1515 * - >queryKeyValues(see descr.) - for queries returning 2 columns key => value
@@ -85,9 +85,7 @@ class ClickHouseQuery extends ClickHouseAPI
8585 public $ extremes ;
8686
8787 /**
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.
9189 *
9290 * @var integer|null
9391 */
@@ -129,10 +127,8 @@ class ClickHouseQuery extends ClickHouseAPI
129127
130128 /**
131129 * 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
136132 *
137133 * queryTrue send POST-request by default for clear read_only-flag.
138134 *
@@ -149,8 +145,8 @@ public function queryTrue($sql, $post_data = [], $sess = null)
149145
150146 /**
151147 * 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
154150 *
155151 * queryFalse send POST-request by default for clear read_only-flag.
156152 *
@@ -166,15 +162,12 @@ public function queryFalse($sql, $post_data = [], $sess = null)
166162 }
167163
168164 /**
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)
174166 *
175167 * 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)
177169 * - 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
178171 *
179172 * @param string $sql SQL-request
180173 * @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)
226219 }
227220
228221 /**
229- * Return Array [keys => values]
222+ * For queries returning exactly 2 columns, which can mean key => value
230223 *
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.
234232 *
235233 * Similar than queryKeyValArr, but using JSONCompact for data transferring.
236234 *
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)
238236 * @param string|null $key_and_value_fields field names, example: 'id,name'
239237 * @param string|null $sess session_id
240238 * @return array|string Returns array if ok, or string with error description
@@ -253,20 +251,26 @@ public function queryKeyValues(
253251 if (!\is_array ($ data ) || !\count ($ data )) {
254252 return $ data ;
255253 }
256- if (count ($ data [0 ]) == 1 ) {
254+ if (\ count ($ data [0 ]) == 1 ) {
257255 return \array_column ($ data , 0 );
258256 }
259257 return \array_combine (\array_column ($ data , 0 ), \array_column ($ data , 1 ));
260258 }
261259
262260 /**
263- * Return Array [keys => values]
261+ * Return Array [keys => values], first column of results means keys.
264262 *
265263 * 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,
267265 * 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.
270274 *
271275 * @param string $tbl_or_sql Table name or full sql request
272276 * @param string|null $key_name_and_value_name fields like 'id, name'
@@ -524,6 +528,15 @@ public function queryInsertFile(
524528 /**
525529 * Inserting data into table from array
526530 *
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+ *
527540 * @param string $table_name Table name for inserting data
528541 * @param array|null $fields_names Array with names of inserting fields
529542 * @param array $fields_set_arr Array with inserting data
@@ -590,14 +603,14 @@ function($s) {
590603
591604
592605 /**
593- * Add quotes and slashes if need
606+ * Add quotes and slashes if need for ClickHouse-SQL parameter
594607 *
595608 * Examples:
596609 * - ("123") => 123 (No changes because is_numeric)
597610 * - ('"aaa"') => "aaa" (No changes because have begin-final quotes)
598611 * - ("'aaa'") => 'aaa' (No changes because have begin-final quotes)
599612 * - ("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 ] )
601614 *
602615 * @param string $str
603616 * @return string
@@ -610,7 +623,7 @@ public function quotePar($str, $quote = "'")
610623 (($ fc === '" ' || $ fc === "' " ) && ($ fc === $ lc )) ||
611624 (($ lc === ') ' && strpos ($ str , '( ' ) !== false )) ||
612625 (($ fc === '[ ' && $ lc === '] ' ))
613- ) ? $ str : $ quote . addcslashes ($ str , "' \t\n\r\0" ) . $ quote ;
626+ ) ? $ str : $ quote . addcslashes ($ str , "' \t\n\r\0` " ) . $ quote ;
614627 }
615628
616629 /**
@@ -677,7 +690,7 @@ public function queryTableSubstract(
677690
678691 $ bindings ['db ' ] = $ this ->quotePar ($ db );
679692 $ bindings ['table ' ] = $ this ->quotePar ($ table );
680- $ bindings ['dbtb ' ] = $ db . '. ' . $ table ;
693+ $ bindings ['dbtb ' ] = ( $ i ? $ db . '. ' : '' ) . $ table ;
681694
682695 $ sql = $ this ->bindPars ($ sql_pattern , $ bindings );
683696
@@ -742,7 +755,7 @@ public function queryTableSys($table, $sys = 'tables', $columns_del = ['n'])
742755 ], "No information about {dbtb} in {s} " , [], $ columns_del , null
743756 );
744757 if (\is_array ($ arr )) {
745- if (count ($ arr ['columns_arr ' ]) == 1 ) {
758+ if (\ count ($ arr ['columns_arr ' ]) == 1 ) {
746759 $ arr = $ arr ['columns_arr ' ][0 ];
747760 }
748761 }
0 commit comments