Skip to content

Commit be59ba3

Browse files
committed
Add documentation for h_build_where_clause
1 parent eddeae2 commit be59ba3

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,73 @@ char * h_escape_string(const struct _h_connection * conn, const char * unsafe);
283283
char * h_escape_string_with_quotes(const struct _h_connection * conn, const char * unsafe);
284284
```
285285

286+
### Build a more complicated where clause
287+
288+
When you need to run a query with a where clause using multiple parameters, such as `WHERE col1='a' AND (col2='b' OR col3=5) AND col4=42.3`, you can use the operator `raw`:
289+
290+
```JSON
291+
{
292+
"table": "table1",
293+
"columns": ["col1", "col2"]
294+
"where": {
295+
" ": {
296+
"operator", "raw",
297+
"value", "col1='a' AND (col2='b' or col3='c') AND col4=5"
298+
}
299+
}
300+
}
301+
```
302+
303+
In some cases, you may need to build the where clause with multiple variables. In hoel 1.4.27, the function `h_build_where_clause` was introduced to help that. Please note that this function is still in Beta.
304+
305+
```C
306+
/**
307+
* h_build_where_clause
308+
* Generates a where clause based on the pattern and the values given
309+
* @param conn the connection to the database
310+
* @param pattern the pattern to build the where clause
311+
* the pattern variables available are the following:
312+
* - %s: a string value to escape with quotes
313+
* - %S: a string value to escape without quotes
314+
* - %c: a string value not to escape with quotes
315+
* - %C: a string value not to escape without quotes
316+
* - %d: an integer value in json_int_t format
317+
* - %f: a double value
318+
* - %j: a json_t value, the value must be of the type JSON_INTEGER, JSON_REAL or JSON_STRING, string values will be escaped with quotes
319+
* - %%: the value '%'
320+
* @return a heap-allocated string
321+
* returned value must be h_free'd after use
322+
*/
323+
char * h_build_where_clause(const struct _h_connection * conn, const char * pattern, ...);
324+
```
325+
326+
Then, to build the where clause above using `h_build_where_clause`, you can use the following code:
327+
328+
```C
329+
const char col1[] = "a", col2[] = "b";
330+
json_int_t col3 = 5;
331+
double col4 = 42.3;
332+
char * where_clause = h_build_where_clause("col1=%s AND (col2='S' OR col3=%d) AND col4=%f", col1, col2, col3, col4);
333+
json_t * j_query = json_pack("{sss[ss]s{s{ssss}}}",
334+
"table", "table1",
335+
"columns",
336+
"col1",
337+
"col2",
338+
"where",
339+
" ",
340+
"operator", "raw",
341+
"value", where_clause);
342+
h_free(where_clause);
343+
// Execute j_query
344+
```
345+
346+
Note that if you use constant litteral for integer or double values, you should cast them first:
347+
348+
```C
349+
const char col1[] = "a", col2[] = "b";
350+
char * where_clause = h_build_where_clause("col1=%s AND (col2='S' OR col3=%d) AND col4=%f", col1, col2, (json_int_t)5, (double)42.3);
351+
```
352+
286353
### Execute a SQL query
287354

288355
To execute a SQL query, you can use the function `h_execute_query` which will run the query in the database specified by the parameter `conn`. If a `result` parameter is specified, the result of the query (if any) will be stored in the `result` structure.

0 commit comments

Comments
 (0)