Mstdlib-1.24.0
m_sql_stmt.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2017 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_SQL_STMT_H__
25#define __M_SQL_STMT_H__
26
27/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
28
29#include <mstdlib/base/m_defs.h>
30#include <mstdlib/base/m_types.h>
31#include <mstdlib/sql/m_sql.h>
32
33/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
34
35__BEGIN_DECLS
36
37/*! \addtogroup m_sql_stmt SQL Statement Management
38 * \ingroup m_sql
39 *
40 * SQL Statement Management
41 *
42 * @{
43 */
44struct M_sql_stmt;
45/*! Data type for prepared SQL statements */
46typedef struct M_sql_stmt M_sql_stmt_t;
47
48/*! Create a prepared statement object for executing queries.
49 *
50 * The statement object holds both request data as well as response data from the server.
51 *
52 * Use the \link m_sql_stmt_bind M_sql_stmt_bind_*() \endlink series of functions to bind data to the statement handle
53 * matching the number of bound parameters referenced in the query. When binding parameters,
54 * they must be bound in order of appearance in the query.
55 *
56 * \return Initialized #M_sql_stmt_t object
57 */
59
60/*! Destroy a prepared statement object
61 *
62 * \param[in] stmt Initialized #M_sql_stmt_t object
63 */
65
66
67/*! Prepare statement for execution.
68 *
69 * This process will perform basic preprocessing and transformation on the statement query.
70 * Parameters for the query may be bound either before or after this call. A placeholder
71 * of a question mark (?) will be used for each bound parameter in the statement.
72 *
73 * No inline text is allowed in a prepared statement, callers must ensure they bind any
74 * text values.
75 *
76 * Only a single query per statement execution is allowed.
77 *
78 * \param[in] stmt Initialized #M_sql_stmt_t object
79 * \param[in] query Query to be prepared
80 * \return #M_SQL_ERROR_SUCCESS on sucess, or one of the #M_sql_error_t values on failure.
81 */
82M_API M_sql_error_t M_sql_stmt_prepare(M_sql_stmt_t *stmt, const char *query);
83
84/*! Prepare statement for execution from an #M_buf_t object that will be automatically free'd.
85 *
86 * This process will perform basic preprocessing and transformation on the statement query.
87 * Parameters for the query may be bound either before or after this call. A placeholder
88 * of a question mark (?) will be used for each bound parameter in the statement.
89 *
90 * No inline text is allowed in a prepared statement, callers must ensure they bind any
91 * text values.
92 *
93 * Only a single query per statement execution is allowed.
94 *
95 * \param[in] stmt Initialized #M_sql_stmt_t object
96 * \param[in] query Query to be prepared held in an #M_buf_t object. The #M_buf_t passed
97 * to this function will be automatically destroyed so must not be used
98 * after a call to this function.
99 * \return #M_SQL_ERROR_SUCCESS on sucess, or one of the #M_sql_error_t values on failure.
100 */
102
103
104/*! Create a "grouped" SQL statement for optimizing server round-trips and commits
105 * for "like" INSERT statements.
106 *
107 * When multiple threads are performing similar actions, such as during transaction
108 * processing, it is very likely that those multiple threads might need to perform
109 * essentially the same insert action on the same table with the same number of
110 * bound parameters. Instead of sending these insertions individually, it is more
111 * efficient to group them together which could result in a single round trip and
112 * transaction instead of dozens or even hundreds.
113 *
114 * This implementation will generate a hashtable entry in the pool with the query
115 * as the key and the statement handle as the value. If the entry already exists,
116 * it will use the existing statement handle and simply prepare it to take a
117 * new row then once M_sql_stmt_execute() is called, it wait on a thread conditional
118 * rather than trying to directly execute the statement, which will wake when a
119 * result is ready. If the entry is not already in the hashtable, it will add
120 * it, then on M_sql_stmt_execute() it will temporarily M_thread_yield() in order
121 * to allow other threads to obtain a lock and append additional rows, then finally
122 * execute and trigger the other threads waiting on the conditional that a result
123 * is ready.
124 *
125 * All threads must still call M_sql_stmt_destroy() as it becomes reference counted
126 * when this function is used. All normal M_sql_stmt_*() functions, except
127 * M_sql_stmt_prepare() and M_sql_stmt_prepare_buf() may be called. It should be
128 * advised that M_sql_stmt_result_affected_rows() may not return an expected count
129 * since it would reflect the overall count of grouped rows. Also, if an error
130 * such as #M_SQL_ERROR_QUERY_CONSTRAINT is returned, the error maybe for another
131 * row, so it is advisable to simply re-run the query without using
132 * M_sql_stmt_groupinsert_prepare() so you know if the record being inserted is
133 * the culprit or not.
134 *
135 * \note At a minimum, one of the M_sql_stmt_bind_*() functions should be called,
136 * as well as M_sql_stmt_execute() and M_sql_stmt_destroy().
137 *
138 * \warning If an error is triggered, such as #M_SQL_ERROR_QUERY_CONSTRAINT, the
139 * caller must re-try the transaction using normal M_sql_stmt_create()
140 * and M_sql_stmt_prepare() rather than M_sql_stmt_groupinsert_prepare()
141 * to recover.
142 *
143 * \param[in] pool Initialized connection pool.
144 * \param[in] query Query string to execute
145 * \return #M_sql_stmt_t handle to bind parameters and execute.
146 */
148
149
150/*! Create a "grouped" SQL statement for optimizing server round-trips and commits
151 * for "like" INSERT statements using an M_buf_t as the query string.
152 *
153 * See M_sql_stmt_groupinsert_prepare() for additional information.
154 * \param[in] pool Initialized connection pool.
155 * \param[in] query Query string to execute
156 * \return #M_sql_stmt_t handle to bind parameters and execute.
157 */
159
160
161/*! Execute a single query against the database and auto-commit if appropriate.
162 *
163 * Must call M_sql_stmt_prepare() or M_sql_stmt_prepare_buf() prior to execution.
164 * Must also bind any parameters using \link m_sql_stmt_bind M_sql_stmt_bind_*() \endlink
165 * series of functions.
166 *
167 * If executing as part of a transaction, use M_sql_trans_execute() instead.
168 *
169 * \param[in] pool Initialized #M_sql_connpool_t object
170 * \param[in] stmt Initialized and prepared #M_sql_stmt_t object
171 * \return #M_SQL_ERROR_SUCCESS on success, or one of the #M_sql_error_t values on failure.
172 */
174
175
176/*! Set the maximum number of rows to fetch/cache in the statement handle.
177 *
178 * By default, all available rows are cached, if this is called, only
179 * up to this number of rows will be cached client-side. The M_sql_stmt_fetch()
180 * function must be called until there are no remaining rows server-side.
181 *
182 * It is recommended that users use partial fetching for extremely large result
183 * sets (either by number of rows, or for extremely large rows).
184 *
185 * \warning If a user chooses not to call this function, and the dataset is very
186 * large (especially if it contains BLOBs), then the user risks running
187 * out of memory. However, if the user sets this value too low for small
188 * row sizes, it could significantly increase the query time on some
189 * servers (like Oracle).
190 *
191 * \param[in] stmt Initialized, but not yet executed #M_sql_stmt_t object.
192 * \param[in] num Number of rows to cache at a time.
193 * \return M_TRUE on success, M_FALSE on failure (misuse, already executed).
194 */
195M_API M_bool M_sql_stmt_set_max_fetch_rows(M_sql_stmt_t *stmt, size_t num);
196
197
198/*! Enforce the selection of the master pool, not the read-only pool for this
199 * statement.
200 *
201 * Queries will, by default, be routed to the read-only pool. In some instances,
202 * this may not be desirable if it is known that the query must be as fresh
203 * as possible and thus route to the read/write pool.
204 *
205 * Another work around is simply to wrap the read request in a transaction,
206 * but if not performing other tasks, that may be overkill and this function
207 * simplifies that logic.
208 *
209 * \param[in] stmt Initialized and not yet executed #M_sql_stmt_t object
210 * \return M_TRUE on success, M_FALSE on failure (misuse, already executed).
211 */
213
214
215/*! Retrieve whether there are still remaining rows on the server yet to be
216 * fetched by the client.
217 *
218 * If there are remaining rows, the client must call M_sql_stmt_fetch() to
219 * cache the next result set.
220 *
221 * \param[in] stmt Initialized and executed #M_sql_stmt_t object.
222 * \return M_TRUE if there are remaining rows on the server that can be fetched,
223 * M_FALSE otherwise.
224 */
226
227
228/*! Fetch additional rows from the server.
229 *
230 * Any existing cached rows will be cleared.
231 *
232 * \param[in] stmt Initialized and executed M_sql_stmt_t object.
233 * \return #M_SQL_ERROR_SUCCESS_ROW on success when there may be additional
234 * remaining rows, or #M_SQL_ERROR_SUCCESS if there
235 * are no remaining rows (if #M_SQL_ERROR_SUCCESS is returned, it is
236 * guaranteed no additional rows can be fetched using M_sql_stmt_fetch()).
237 * However, there may still be additional rows in the buffer that
238 * need to be processed, please check with M_sql_stmt_result_num_rows().
239 * Otherwise one of the #M_sql_error_t error conditions will be returned.
240 */
242
243
244/*! Retrieve the last recorded error.
245 *
246 * \param[in] stmt Initialized #M_sql_stmt_t object.
247 * \return last recorded #M_sql_error_t for statement handle.
248 */
250
251/*! Retrieve the last recorded error message in string form.
252 *
253 * \param[in] stmt Initialized #M_sql_stmt_t object.
254 * \return last recorded error string, or NULL if none
255 */
257
258/*! @} */
259
260
261/*! \addtogroup m_sql_stmt_bind SQL Statement Request Parameter Binding
262 * \ingroup m_sql_stmt
263 *
264 * SQL Statement Parameter Binding
265 *
266 * @{
267 */
268
269/*! Clear all bound parameters from a prepared statement object.
270 *
271 * \param[in] stmt Initialized #M_sql_stmt_t object
272 */
274
275/*! Clear current row bound parameters for prepared statement object.
276 * This can be used if the caller wants to cancel the current row parameters
277 * due to an internal condition.
278 *
279 * \param[in] stmt Initialized #M_sql_stmt_t object
280 */
282
283/*! Increment to next row for statement binding.
284 *
285 * Callers can bind multiple rows for insert statements to reduce server round
286 * trips during bulk data insertion. This function creates a new row and resets
287 * the current column index for binding a new row worth of columns.
288 *
289 * All rows must contain the same number of columns consisting of the same data
290 * types (with the exception that NULL may be used) or it is considered a failure.
291 *
292 * TODO: It may not be possible to determine which row caused a failure, such as a key
293 * conflict. Figure this out.
294 *
295 * \param[in] stmt Initialized #M_sql_stmt_t object
296 */
298
299/*! Bind M_bool as next column to prepared statement handle
300 *
301 * \param[in] stmt Initialized #M_sql_stmt_t object
302 * \param[in] val Boolean value to bind.
303 * \return #M_SQL_ERROR_SUCCESS on success, or one of the #M_sql_error_t values on failure.
304 */
306
307/*! Bind M_bool NULL column to prepared statement handle
308 *
309 * Due to quirks with ODBC, you must know the data type of the bound parameter when
310 * binding NULL values.
311 *
312 * \param[in] stmt Initialized #M_sql_stmt_t object
313 * \return #M_SQL_ERROR_SUCCESS on success, or one of the #M_sql_error_t values on failure.
314 */
316
317/*! Bind M_int16 as next column to prepared statement handle
318 *
319 * \param[in] stmt Initialized #M_sql_stmt_t object
320 * \param[in] val 16bit signed integer value to bind.
321 * \return #M_SQL_ERROR_SUCCESS on success, or one of the #M_sql_error_t values on failure.
322 */
324
325/*! Bind M_int16 NULL column to prepared statement handle
326 *
327 * Due to quirks with ODBC, you must know the data type of the bound parameter when
328 * binding NULL values.
329 *
330 * \param[in] stmt Initialized #M_sql_stmt_t object
331 * \return #M_SQL_ERROR_SUCCESS on success, or one of the #M_sql_error_t values on failure.
332 */
334
335/*! Bind M_int32 as next column to prepared statement handle
336 *
337 * \param[in] stmt Initialized #M_sql_stmt_t object
338 * \param[in] val 32bit signed integer value to bind.
339 * \return #M_SQL_ERROR_SUCCESS on success, or one of the #M_sql_error_t values on failure.
340 */
342
343/*! Bind M_int32 NULL column to prepared statement handle
344 *
345 * Due to quirks with ODBC, you must know the data type of the bound parameter when
346 * binding NULL values.
347 *
348 * \param[in] stmt Initialized #M_sql_stmt_t object
349 * \return #M_SQL_ERROR_SUCCESS on success, or one of the #M_sql_error_t values on failure.
350 */
352
353/*! Bind M_int64 as next column to prepared statement handle
354 *
355 * \param[in] stmt Initialized #M_sql_stmt_t object
356 * \param[in] val 64bit signed integer value to bind.
357 * \return #M_SQL_ERROR_SUCCESS on success, or one of the #M_sql_error_t values on failure.
358 */
360
361/*! Bind M_int64 NULL column to prepared statement handle
362 *
363 * Due to quirks with ODBC, you must know the data type of the bound parameter when
364 * binding NULL values.
365 *
366 * \param[in] stmt Initialized #M_sql_stmt_t object
367 * \return #M_SQL_ERROR_SUCCESS on success, or one of the #M_sql_error_t values on failure.
368 */
370
371/*! Bind a const string/text as next column to prepared statement handle
372 *
373 * \param[in] stmt Initialized #M_sql_stmt_t object
374 * \param[in] text Constant string, that is guaranteed to be available until the statement is executed, to bind to
375 * statement. Pass NULL for a NULL value.
376 * \param[in] max_len Maximum length of text/string value to use, use 0 for no maximum.
377 * \return #M_SQL_ERROR_SUCCESS on success, or one of the #M_sql_error_t values on failure.
378 */
379M_API M_sql_error_t M_sql_stmt_bind_text_const(M_sql_stmt_t *stmt, const char *text, size_t max_len);
380
381/*! Bind string/text as next column to prepared statement handle that will be automatically freed upon statement destruction.
382 *
383 * \param[in] stmt Initialized #M_sql_stmt_t object
384 * \param[in] text Pointer to text/string to bind to statement. Must not be free'd by caller as destruction of
385 * the prepared statement handle will automatically free the value.
386 * \param[in] max_len Maximum length of text/string value to use, use 0 for no maximum.
387 * \return #M_SQL_ERROR_SUCCESS on success, or one of the #M_sql_error_t values on failure.
388 */
389M_API M_sql_error_t M_sql_stmt_bind_text_own(M_sql_stmt_t *stmt, char *text, size_t max_len);
390
391/*! Bind string/text as next column to prepared statement handle that will be duplicated internally as the caller cannot
392 * guarantee the pointer will survive after execution of this bind call.
393 *
394 * \param[in] stmt Initialized #M_sql_stmt_t object
395 * \param[in] text Pointer to text/string that will be duplicated and bound to the statement handle.
396 * \param[in] max_len Maximum length of text/string value to use, use 0 for no maximum.
397 * \return #M_SQL_ERROR_SUCCESS on success, or one of the #M_sql_error_t values on failure.
398 */
399M_API M_sql_error_t M_sql_stmt_bind_text_dup(M_sql_stmt_t *stmt, const char *text, size_t max_len);
400
401/*! Bind a const binary buffer as next column to prepared statement handle
402 *
403 * \param[in] stmt Initialized #M_sql_stmt_t object
404 * \param[in] bin Constant binary data, that is guaranteed to be available until the statement is executed, to bind to
405 * statement. Pass NULL for a NULL value.
406 * \param[in] bin_len Length of binary value to use. Only values up to 64k are allowed.
407 * \return #M_SQL_ERROR_SUCCESS on success, or one of the #M_sql_error_t values on failure.
408 */
409M_API M_sql_error_t M_sql_stmt_bind_binary_const(M_sql_stmt_t *stmt, const M_uint8 *bin, size_t bin_len);
410
411/*! Bind binary buffer as next column to prepared statement handle that will be automatically freed upon statement destruction.
412 *
413 * \param[in] stmt Initialized #M_sql_stmt_t object
414 * \param[in] bin Pointer to binary data to bind to statement. Must not be free'd by caller as destruction of
415 * the prepared statement handle will automatically free the value.
416 * \param[in] bin_len Length of binary value to use. Only values up to 64k are allowed.
417 * \return #M_SQL_ERROR_SUCCESS on success, or one of the #M_sql_error_t values on failure.
418 */
419M_API M_sql_error_t M_sql_stmt_bind_binary_own(M_sql_stmt_t *stmt, M_uint8 *bin, size_t bin_len);
420
421/*! Bind binary data as next column to prepared statement handle that will be duplicated internally as the caller cannot
422 * guarantee the pointer will survive after execution of this bind call.
423 *
424 * \param[in] stmt Initialized #M_sql_stmt_t object
425 * \param[in] bin Pointer to binary data that will be duplicated and bound to the statement handle.
426 * \param[in] bin_len Length of binary value to use. Only values up to 64k are allowed.
427 * \return #M_SQL_ERROR_SUCCESS on success, or one of the #M_sql_error_t values on failure.
428 */
429M_API M_sql_error_t M_sql_stmt_bind_binary_dup(M_sql_stmt_t *stmt, const M_uint8 *bin, size_t bin_len);
430
431/*! @} */
432
433
434/*! \addtogroup m_sql_stmt_result SQL Statement Results
435 * \ingroup m_sql_stmt
436 *
437 * SQL Statement Result Processing
438 *
439 * @{
440 */
441
442/*! Retrieve the number of rows affected by the executed statement.
443 *
444 * Does not apply to SELECT statements, used often on UPDATE and DELETE
445 * statements to reflect how many rows were affected.
446 *
447 * NOTE: On update, if updating a row in the database, and the passed in
448 * fields being updated are the same as the database already contains, depending
449 * on the backend database driver, that row may or may not be included.
450 * Developers should not rely on this behavior.
451 *
452 * \param[in] stmt Initialized #M_sql_stmt_t object.
453 * \return affected rows.
454 */
456
457
458/*! Retrieve the number of cached rows in statement handle.
459 *
460 * There may be additional rows yet to be fetched if not retrieving all rows
461 * at once. This function will return only the number of cached rows client-side,
462 * each time M_sql_stmt_fetch() is called, previous cached rows are cleared. This
463 * result is NOT cumulative.
464 *
465 * \param[in] stmt Initialized #M_sql_stmt_t object.
466 * \return row count
467 */
469
470
471/*! Retrieve the number of total rows that have been fetched so far.
472 *
473 * This number may be greater than or equal to M_sql_stmt_result_num_rows()
474 * as each call to M_sql_stmt_fetch() will clear the current cached rows
475 * (and count), but this value will continue to increment.
476 *
477 * \param[in] stmt Initialized #M_sql_stmt_t object.
478 * \return total row count fetched so far.
479 */
481
482
483/*! Retrieve the number of columns returned from the server in response to a query.
484 *
485 * \param[in] stmt Initialized #M_sql_stmt_t object.
486 * \return column count.
487 */
489
490/*! Retrieve the name of a returned column.
491 *
492 * \param[in] stmt Initialized and executed #M_sql_stmt_t object.
493 * \param[in] col Index of column.
494 * \return column name or NULL on failure.
495 */
496M_API const char *M_sql_stmt_result_col_name(M_sql_stmt_t *stmt, size_t col);
497
498
499/*! Retrieve the data type of the returned column.
500 *
501 * \param[in] stmt Initialized and executed #M_sql_stmt_t object.
502 * \param[in] col Index of column.
503 * \param[out] type_size Optional, pass NULL if not desired. For TEXT and BINARY types, the column
504 * definition may indicate a possible size (or maximum size). If the value is 0,
505 * it means the column width is bounded by the maximums of the SQL server.
506 * \return Column type for referenced column.
507 */
508M_API M_sql_data_type_t M_sql_stmt_result_col_type(M_sql_stmt_t *stmt, size_t col, size_t *type_size);
509
510
511/*! Retrieve the data type of the returned column.
512 *
513 * \param[in] stmt Initialized and executed #M_sql_stmt_t object.
514 * \param[in] col Name of column.
515 * \param[out] type_size Optional, pass NULL if not desired. For TEXT and BINARY types, the column
516 * definition may indicate a possible size (or maximum size). If the value is 0,
517 * it means the column width is bounded by the maximums of the SQL server.
518 * \return Column type for referenced column or M_SQL_DATA_TYPE_UNKNOWN if column not found.
519 */
520M_API M_sql_data_type_t M_sql_stmt_result_col_type_byname(M_sql_stmt_t *stmt, const char *col, size_t *type_size);
521
522
523/*! Retrieve the column index by name
524 *
525 * \param[in] stmt Initialized and executed #M_sql_stmt_t object.
526 * \param[in] col Column name, case insensitive.
527 * \param[out] idx Index of column.
528 * \return M_TRUE on success, M_FALSE if column not found.
529 */
530M_API M_bool M_sql_stmt_result_col_idx(M_sql_stmt_t *stmt, const char *col, size_t *idx);
531
532
533/*! Retrieve if a cell is NULL.
534 *
535 * \param[in] stmt Initialized #M_sql_stmt_t object.
536 * \param[in] row Row index to retrieve
537 * \param[in] col Column index to retrieve
538 * \param[out] is_null Return whether column is NULL or not.
539 * \return #M_SQL_ERROR_SUCCESS on success, or one of #M_sql_error_t errors on failure.
540 */
541M_API M_sql_error_t M_sql_stmt_result_isnull(M_sql_stmt_t *stmt, size_t row, size_t col, M_bool *is_null);
542
543/*! Retrieve a textual cell from the resultset.
544 *
545 * All cell types may be retrieved in their textual form.
546 *
547 * \param[in] stmt Initialized #M_sql_stmt_t object.
548 * \param[in] row Row index to retrieve
549 * \param[in] col Column index to retrieve
550 * \param[out] text Output constant pointer to cell data. May be NULL if a NULL
551 * column.
552 * \return #M_SQL_ERROR_SUCCESS on success, or one of #M_sql_error_t errors on failure.
553 */
554M_API M_sql_error_t M_sql_stmt_result_text(M_sql_stmt_t *stmt, size_t row, size_t col, const char **text);
555
556/*! Retrieve a bool value from the resultset.
557 *
558 * \param[in] stmt Initialized #M_sql_stmt_t object.
559 * \param[in] row Row index to retrieve
560 * \param[in] col Column index to retrieve
561 * \param[out] val Output boolean value.
562 * \return #M_SQL_ERROR_SUCCESS on success, or one of #M_sql_error_t errors on failure.
563 */
564M_API M_sql_error_t M_sql_stmt_result_bool(M_sql_stmt_t *stmt, size_t row, size_t col, M_bool *val);
565
566/*! Retrieve a signed 16bit Integer cell from the resultset.
567 *
568 * \param[in] stmt Initialized #M_sql_stmt_t object.
569 * \param[in] row Row index to retrieve
570 * \param[in] col Column index to retrieve
571 * \param[out] val Output integer data. If NULL, outputs 0.
572 * \return #M_SQL_ERROR_SUCCESS on success, or one of #M_sql_error_t errors on failure.
573 */
574M_API M_sql_error_t M_sql_stmt_result_int16(M_sql_stmt_t *stmt, size_t row, size_t col, M_int16 *val);
575
576
577/*! Retrieve a signed 32bit Integer cell from the resultset.
578 *
579 * \param[in] stmt Initialized #M_sql_stmt_t object.
580 * \param[in] row Row index to retrieve
581 * \param[in] col Column index to retrieve
582 * \param[out] val Output integer data. If NULL, outputs 0.
583 * \return #M_SQL_ERROR_SUCCESS on success, or one of #M_sql_error_t errors on failure.
584 */
585M_API M_sql_error_t M_sql_stmt_result_int32(M_sql_stmt_t *stmt, size_t row, size_t col, M_int32 *val);
586
587/*! Retrieve a signed 64bit Integer cell from the resultset.
588 *
589 * \param[in] stmt Initialized #M_sql_stmt_t object.
590 * \param[in] row Row index to retrieve
591 * \param[in] col Column index to retrieve
592 * \param[out] val Output integer data. If NULL, outputs 0.
593 * \return #M_SQL_ERROR_SUCCESS on success, or one of #M_sql_error_t errors on failure.
594 */
595M_API M_sql_error_t M_sql_stmt_result_int64(M_sql_stmt_t *stmt, size_t row, size_t col, M_int64 *val);
596
597/*! Retrieve a binary cell from the resultset.
598 *
599 * \param[in] stmt Initialized #M_sql_stmt_t object.
600 * \param[in] row Row index to retrieve
601 * \param[in] col Column index to retrieve
602 * \param[out] bin Output constant pointer to cell data. May be NULL if a NULL
603 * column.
604 * \param[out] bin_size Size of binary data returned.
605 * \return #M_SQL_ERROR_SUCCESS on success, or one of #M_sql_error_t errors on failure.
606 */
607M_API M_sql_error_t M_sql_stmt_result_binary(M_sql_stmt_t *stmt, size_t row, size_t col, const M_uint8 **bin, size_t *bin_size);
608
609
610/*! Retrieve if a cell is NULL, directly, ignoring errors.
611 *
612 * \param[in] stmt Initialized #M_sql_stmt_t object.
613 * \param[in] row Row index to retrieve
614 * \param[in] col Column index to retrieve
615 * \return M_TRUE on usage failure, or if NULL cell, otherwise M_FALSE
616 */
617M_API M_bool M_sql_stmt_result_isnull_direct(M_sql_stmt_t *stmt, size_t row, size_t col);
618
619/*! Retrieve a textual cell from the resultset, directly, ignoring errors.
620 *
621 * All cell types may be retrieved in their textual form.
622 *
623 * \param[in] stmt Initialized #M_sql_stmt_t object.
624 * \param[in] row Row index to retrieve
625 * \param[in] col Column index to retrieve
626 * \return String result on success, or NUL on failure.
627 */
628M_API const char *M_sql_stmt_result_text_direct(M_sql_stmt_t *stmt, size_t row, size_t col);
629
630/*! Retrieve a bool value from the resultset, directly, ignoring errors.
631 *
632 * \param[in] stmt Initialized #M_sql_stmt_t object.
633 * \param[in] row Row index to retrieve
634 * \param[in] col Column index to retrieve
635 * \return Return M_TRUE if data is a boolean value resulting in truth, or M_FALSE for all other cases
636 */
637M_API M_bool M_sql_stmt_result_bool_direct(M_sql_stmt_t *stmt, size_t row, size_t col);
638
639/*! Retrieve a signed 16bit Integer cell from the resultset, directly, ignoring errors.
640 *
641 * \param[in] stmt Initialized #M_sql_stmt_t object.
642 * \param[in] row Row index to retrieve
643 * \param[in] col Column index to retrieve
644 * \return Return integer value represented in cell, or possibly 0 on error
645 */
646M_API M_int16 M_sql_stmt_result_int16_direct(M_sql_stmt_t *stmt, size_t row, size_t col);
647
648
649/*! Retrieve a signed 32bit Integer cell from the resultset, directly, ignoring errors.
650 *
651 * \param[in] stmt Initialized #M_sql_stmt_t object.
652 * \param[in] row Row index to retrieve
653 * \param[in] col Column index to retrieve
654 * \return Return integer value represented in cell, or possibly 0 on error
655 */
656M_API M_int32 M_sql_stmt_result_int32_direct(M_sql_stmt_t *stmt, size_t row, size_t col);
657
658/*! Retrieve a signed 64bit Integer cell from the resultset, directly, ignoring errors.
659 *
660 * \param[in] stmt Initialized #M_sql_stmt_t object.
661 * \param[in] row Row index to retrieve
662 * \param[in] col Column index to retrieve
663 * \return Return integer value represented in cell, or possibly 0 on error
664 */
665M_API M_int64 M_sql_stmt_result_int64_direct(M_sql_stmt_t *stmt, size_t row, size_t col);
666
667/*! Retrieve a binary cell from the resultset, directly, ignoring errors.
668 *
669 * \param[in] stmt Initialized #M_sql_stmt_t object.
670 * \param[in] row Row index to retrieve
671 * \param[in] col Column index to retrieve
672 * \param[out] bin_size Size of binary data returned.
673 * \return NULL on error (not right data type), or error
674 */
675M_API const M_uint8 *M_sql_stmt_result_binary_direct(M_sql_stmt_t *stmt, size_t row, size_t col, size_t *bin_size);
676
677
678
679/*! Retrieve if a cell is NULL (by column name).
680 *
681 * \param[in] stmt Initialized #M_sql_stmt_t object.
682 * \param[in] row Row index to retrieve
683 * \param[in] col Column nameto retrieve
684 * \param[out] is_null Return whether column is NULL or not.
685 * \return #M_SQL_ERROR_SUCCESS on success, or one of #M_sql_error_t errors on failure.
686 */
687M_API M_sql_error_t M_sql_stmt_result_isnull_byname(M_sql_stmt_t *stmt, size_t row, const char *col, M_bool *is_null);
688
689/*! Retrieve a textual cell from the resultset (by column name).
690 *
691 * All cell types may be retrieved in their textual form.
692 *
693 * \param[in] stmt Initialized #M_sql_stmt_t object.
694 * \param[in] row Row index to retrieve
695 * \param[in] col Column name to retrieve
696 * \param[out] text Output constant pointer to cell data. May be NULL if a NULL
697 * column.
698 * \return #M_SQL_ERROR_SUCCESS on success, or one of #M_sql_error_t errors on failure.
699 */
700M_API M_sql_error_t M_sql_stmt_result_text_byname(M_sql_stmt_t *stmt, size_t row, const char *col, const char **text);
701
702/*! Retrieve a bool value from the resultset (by column name).
703 *
704 * \param[in] stmt Initialized #M_sql_stmt_t object.
705 * \param[in] row Row index to retrieve
706 * \param[in] col Column name to retrieve
707 * \param[out] val Output boolean value.
708 * \return #M_SQL_ERROR_SUCCESS on success, or one of #M_sql_error_t errors on failure.
709 */
710M_API M_sql_error_t M_sql_stmt_result_bool_byname(M_sql_stmt_t *stmt, size_t row, const char *col, M_bool *val);
711
712/*! Retrieve a signed 16bit Integer cell from the resultset (by column name).
713 *
714 * \param[in] stmt Initialized #M_sql_stmt_t object.
715 * \param[in] row Row index to retrieve
716 * \param[in] col Column name to retrieve
717 * \param[out] val Output integer data.
718 * \return #M_SQL_ERROR_SUCCESS on success, or one of #M_sql_error_t errors on failure.
719 */
720M_API M_sql_error_t M_sql_stmt_result_int16_byname(M_sql_stmt_t *stmt, size_t row, const char *col, M_int16 *val);
721
722
723/*! Retrieve a signed 32bit Integer cell from the resultset (by column name).
724 *
725 * \param[in] stmt Initialized #M_sql_stmt_t object.
726 * \param[in] row Row index to retrieve
727 * \param[in] col Column name to retrieve
728 * \param[out] val Output integer data.
729 * \return #M_SQL_ERROR_SUCCESS on success, or one of #M_sql_error_t errors on failure.
730 */
731M_API M_sql_error_t M_sql_stmt_result_int32_byname(M_sql_stmt_t *stmt, size_t row, const char *col, M_int32 *val);
732
733/*! Retrieve a signed 64bit Integer cell from the resultset (by column name).
734 *
735 * \param[in] stmt Initialized #M_sql_stmt_t object.
736 * \param[in] row Row index to retrieve
737 * \param[in] col Column name to retrieve
738 * \param[out] val Output integer data.
739 * \return #M_SQL_ERROR_SUCCESS on success, or one of #M_sql_error_t errors on failure.
740 */
741M_API M_sql_error_t M_sql_stmt_result_int64_byname(M_sql_stmt_t *stmt, size_t row, const char *col, M_int64 *val);
742
743/*! Retrieve a binary cell from the resultset (by column name).
744 *
745 * \param[in] stmt Initialized #M_sql_stmt_t object.
746 * \param[in] row Row index to retrieve
747 * \param[in] col Column index to retrieve
748 * \param[out] bin Output constant pointer to cell data. May be NULL if a NULL
749 * column.
750 * \param[out] bin_size Size of binary data returned, maximum is 64k.
751 * \return #M_SQL_ERROR_SUCCESS on success, or one of #M_sql_error_t errors on failure.
752 */
753M_API M_sql_error_t M_sql_stmt_result_binary_byname(M_sql_stmt_t *stmt, size_t row, const char *col, const M_uint8 **bin, size_t *bin_size);
754
755/*! Retrieve if a cell is NULL, directly, ignoring errors (by column name).
756 *
757 * \param[in] stmt Initialized #M_sql_stmt_t object.
758 * \param[in] row Row index to retrieve
759 * \param[in] col Column name to retrieve
760 * \return M_TRUE on usage failure, or if NULL cell, otherwise M_FALSE
761 */
762M_API M_bool M_sql_stmt_result_isnull_byname_direct(M_sql_stmt_t *stmt, size_t row, const char *col);
763
764
765/*! Retrieve a textual cell from the resultset, directly, ignoring errors (by column name).
766 *
767 * All cell types may be retrieved in their textual form.
768 *
769 * \param[in] stmt Initialized #M_sql_stmt_t object.
770 * \param[in] row Row index to retrieve
771 * \param[in] col Column name to retrieve
772 * \return String result on success, or NUL on failure.
773 */
774M_API const char *M_sql_stmt_result_text_byname_direct(M_sql_stmt_t *stmt, size_t row, const char *col);
775
776/*! Retrieve a bool value from the resultset, directly, ignoring errors (by column name).
777 *
778 * \param[in] stmt Initialized #M_sql_stmt_t object.
779 * \param[in] row Row index to retrieve
780 * \param[in] col Column name to retrieve
781 * \return Return M_TRUE if data is a boolean value resulting in truth, or M_FALSE for all other cases
782 */
783M_API M_bool M_sql_stmt_result_bool_byname_direct(M_sql_stmt_t *stmt, size_t row, const char *col);
784
785/*! Retrieve a signed 16bit Integer cell from the resultset, directly, ignoring errors (by column name).
786 *
787 * \param[in] stmt Initialized #M_sql_stmt_t object.
788 * \param[in] row Row index to retrieve
789 * \param[in] col Column name to retrieve
790 * \return Return integer value represented in cell, or possibly 0 on error
791 */
792M_API M_int16 M_sql_stmt_result_int16_byname_direct(M_sql_stmt_t *stmt, size_t row, const char *col);
793
794
795/*! Retrieve a signed 32bit Integer cell from the resultset, directly, ignoring errors (by column name).
796 *
797 * \param[in] stmt Initialized #M_sql_stmt_t object.
798 * \param[in] row Row index to retrieve
799 * \param[in] col Column name to retrieve
800 * \return Return integer value represented in cell, or possibly 0 on error
801 */
802M_API M_int32 M_sql_stmt_result_int32_byname_direct(M_sql_stmt_t *stmt, size_t row, const char *col);
803
804/*! Retrieve a signed 64bit Integer cell from the resultset, directly, ignoring errors (by column name).
805 *
806 * \param[in] stmt Initialized #M_sql_stmt_t object.
807 * \param[in] row Row index to retrieve
808 * \param[in] col Column name to retrieve
809 * \return Return integer value represented in cell, or possibly 0 on error
810 */
811M_API M_int64 M_sql_stmt_result_int64_byname_direct(M_sql_stmt_t *stmt, size_t row, const char *col);
812
813/*! Retrieve a binary cell from the resultset, directly, ignoring errors (by column name).
814 *
815 * \param[in] stmt Initialized #M_sql_stmt_t object.
816 * \param[in] row Row index to retrieve
817 * \param[in] col Column name to retrieve
818 * \param[out] bin_size Size of binary data returned.
819 * \return NULL on error (not right data type), or error
820 */
821M_API const M_uint8 *M_sql_stmt_result_binary_byname_direct(M_sql_stmt_t *stmt, size_t row, const char *col, size_t *bin_size);
822
823
824/*! @} */
825
826__END_DECLS
827
828#endif /* __M_SQL_STMT_H__ */
struct M_buf M_buf_t
Definition: m_buf.h:77
struct M_sql_connpool M_sql_connpool_t
Definition: m_sql.h:335
M_sql_error_t
Definition: m_sql.h:190
M_sql_data_type_t
Definition: m_sql.h:256
void M_sql_stmt_bind_clear(M_sql_stmt_t *stmt)
void M_sql_stmt_bind_new_row(M_sql_stmt_t *stmt)
M_sql_error_t M_sql_stmt_bind_int16_null(M_sql_stmt_t *stmt)
M_sql_error_t M_sql_stmt_bind_int16(M_sql_stmt_t *stmt, M_int16 val)
M_sql_error_t M_sql_stmt_bind_text_own(M_sql_stmt_t *stmt, char *text, size_t max_len)
M_sql_error_t M_sql_stmt_bind_int32(M_sql_stmt_t *stmt, M_int32 val)
M_sql_error_t M_sql_stmt_bind_text_dup(M_sql_stmt_t *stmt, const char *text, size_t max_len)
M_sql_error_t M_sql_stmt_bind_int32_null(M_sql_stmt_t *stmt)
M_sql_error_t M_sql_stmt_bind_binary_const(M_sql_stmt_t *stmt, const M_uint8 *bin, size_t bin_len)
M_sql_error_t M_sql_stmt_bind_binary_own(M_sql_stmt_t *stmt, M_uint8 *bin, size_t bin_len)
M_sql_error_t M_sql_stmt_bind_int64(M_sql_stmt_t *stmt, M_int64 val)
M_sql_error_t M_sql_stmt_bind_int64_null(M_sql_stmt_t *stmt)
void M_sql_stmt_bind_clear_row(M_sql_stmt_t *stmt)
M_sql_error_t M_sql_stmt_bind_bool(M_sql_stmt_t *stmt, M_bool val)
M_sql_error_t M_sql_stmt_bind_bool_null(M_sql_stmt_t *stmt)
M_sql_error_t M_sql_stmt_bind_text_const(M_sql_stmt_t *stmt, const char *text, size_t max_len)
M_sql_error_t M_sql_stmt_bind_binary_dup(M_sql_stmt_t *stmt, const M_uint8 *bin, size_t bin_len)
const M_uint8 * M_sql_stmt_result_binary_byname_direct(M_sql_stmt_t *stmt, size_t row, const char *col, size_t *bin_size)
const char * M_sql_stmt_result_col_name(M_sql_stmt_t *stmt, size_t col)
M_sql_error_t M_sql_stmt_result_bool(M_sql_stmt_t *stmt, size_t row, size_t col, M_bool *val)
M_bool M_sql_stmt_result_isnull_byname_direct(M_sql_stmt_t *stmt, size_t row, const char *col)
M_sql_data_type_t M_sql_stmt_result_col_type_byname(M_sql_stmt_t *stmt, const char *col, size_t *type_size)
M_sql_error_t M_sql_stmt_result_text_byname(M_sql_stmt_t *stmt, size_t row, const char *col, const char **text)
M_sql_error_t M_sql_stmt_result_text(M_sql_stmt_t *stmt, size_t row, size_t col, const char **text)
M_sql_error_t M_sql_stmt_result_binary_byname(M_sql_stmt_t *stmt, size_t row, const char *col, const M_uint8 **bin, size_t *bin_size)
M_int64 M_sql_stmt_result_int64_byname_direct(M_sql_stmt_t *stmt, size_t row, const char *col)
M_int64 M_sql_stmt_result_int64_direct(M_sql_stmt_t *stmt, size_t row, size_t col)
M_bool M_sql_stmt_result_bool_direct(M_sql_stmt_t *stmt, size_t row, size_t col)
M_int16 M_sql_stmt_result_int16_byname_direct(M_sql_stmt_t *stmt, size_t row, const char *col)
M_bool M_sql_stmt_result_isnull_direct(M_sql_stmt_t *stmt, size_t row, size_t col)
size_t M_sql_stmt_result_num_rows(M_sql_stmt_t *stmt)
M_sql_error_t M_sql_stmt_result_int32_byname(M_sql_stmt_t *stmt, size_t row, const char *col, M_int32 *val)
M_int32 M_sql_stmt_result_int32_byname_direct(M_sql_stmt_t *stmt, size_t row, const char *col)
size_t M_sql_stmt_result_total_rows(M_sql_stmt_t *stmt)
M_sql_error_t M_sql_stmt_result_int32(M_sql_stmt_t *stmt, size_t row, size_t col, M_int32 *val)
M_sql_error_t M_sql_stmt_result_bool_byname(M_sql_stmt_t *stmt, size_t row, const char *col, M_bool *val)
size_t M_sql_stmt_result_num_cols(M_sql_stmt_t *stmt)
const M_uint8 * M_sql_stmt_result_binary_direct(M_sql_stmt_t *stmt, size_t row, size_t col, size_t *bin_size)
M_sql_error_t M_sql_stmt_result_binary(M_sql_stmt_t *stmt, size_t row, size_t col, const M_uint8 **bin, size_t *bin_size)
M_sql_error_t M_sql_stmt_result_int16_byname(M_sql_stmt_t *stmt, size_t row, const char *col, M_int16 *val)
M_sql_error_t M_sql_stmt_result_isnull(M_sql_stmt_t *stmt, size_t row, size_t col, M_bool *is_null)
M_bool M_sql_stmt_result_col_idx(M_sql_stmt_t *stmt, const char *col, size_t *idx)
M_sql_error_t M_sql_stmt_result_int64(M_sql_stmt_t *stmt, size_t row, size_t col, M_int64 *val)
const char * M_sql_stmt_result_text_direct(M_sql_stmt_t *stmt, size_t row, size_t col)
M_bool M_sql_stmt_result_bool_byname_direct(M_sql_stmt_t *stmt, size_t row, const char *col)
M_sql_error_t M_sql_stmt_result_int16(M_sql_stmt_t *stmt, size_t row, size_t col, M_int16 *val)
M_sql_data_type_t M_sql_stmt_result_col_type(M_sql_stmt_t *stmt, size_t col, size_t *type_size)
size_t M_sql_stmt_result_affected_rows(M_sql_stmt_t *stmt)
M_int16 M_sql_stmt_result_int16_direct(M_sql_stmt_t *stmt, size_t row, size_t col)
M_int32 M_sql_stmt_result_int32_direct(M_sql_stmt_t *stmt, size_t row, size_t col)
const char * M_sql_stmt_result_text_byname_direct(M_sql_stmt_t *stmt, size_t row, const char *col)
M_sql_error_t M_sql_stmt_result_isnull_byname(M_sql_stmt_t *stmt, size_t row, const char *col, M_bool *is_null)
M_sql_error_t M_sql_stmt_result_int64_byname(M_sql_stmt_t *stmt, size_t row, const char *col, M_int64 *val)
M_sql_error_t M_sql_stmt_prepare(M_sql_stmt_t *stmt, const char *query)
M_bool M_sql_stmt_set_max_fetch_rows(M_sql_stmt_t *stmt, size_t num)
M_sql_error_t M_sql_stmt_execute(M_sql_connpool_t *pool, M_sql_stmt_t *stmt)
M_sql_stmt_t * M_sql_stmt_groupinsert_prepare(M_sql_connpool_t *pool, const char *query)
M_sql_error_t M_sql_stmt_fetch(M_sql_stmt_t *stmt)
struct M_sql_stmt M_sql_stmt_t
Definition: m_sql_stmt.h:46
const char * M_sql_stmt_get_error_string(M_sql_stmt_t *stmt)
M_sql_error_t M_sql_stmt_prepare_buf(M_sql_stmt_t *stmt, M_buf_t *query)
M_sql_stmt_t * M_sql_stmt_groupinsert_prepare_buf(M_sql_connpool_t *pool, M_buf_t *query)
M_bool M_sql_stmt_has_remaining_rows(M_sql_stmt_t *stmt)
M_sql_error_t M_sql_stmt_get_error(M_sql_stmt_t *stmt)
void M_sql_stmt_destroy(M_sql_stmt_t *stmt)
M_bool M_sql_stmt_set_master_only(M_sql_stmt_t *stmt)
M_sql_stmt_t * M_sql_stmt_create(void)