Mstdlib-1.24.0
|
Typedefs | |
typedef struct M_sql_report | M_sql_report_t |
typedef struct M_sql_report_state | M_sql_report_state_t |
typedef M_bool(* | M_sql_report_fetch_cb_t) (M_sql_stmt_t *stmt, void *arg) |
typedef M_sql_report_cberror_t(* | M_sql_report_cell_cb_t) (M_sql_stmt_t *stmt, void *arg, const char *name, size_t row, ssize_t col, M_buf_t *buf, M_bool *is_null) |
typedef struct M_sql_report_filter | M_sql_report_filter_t |
Functions | |
M_sql_report_t * | M_sql_report_create (M_uint32 flags) |
void | M_sql_report_destroy (M_sql_report_t *report) |
M_bool | M_sql_report_set_delims (M_sql_report_t *report, const unsigned char *field_delim, size_t field_delim_size, const unsigned char *row_delim, size_t row_delim_size, const unsigned char *field_encaps, size_t field_encaps_size, const unsigned char *field_escape, size_t field_escape_size) |
M_bool | M_sql_report_set_fetch_cb (M_sql_report_t *report, M_sql_report_fetch_cb_t fetch_cb) |
M_sql_report_cberror_t | M_sql_report_cell_cb_passthru (M_sql_stmt_t *stmt, void *arg, const char *name, size_t row, ssize_t col, M_buf_t *buf, M_bool *is_null) |
M_sql_report_cberror_t | M_sql_report_cell_cb_int2dec (M_sql_stmt_t *stmt, void *arg, const char *name, size_t row, ssize_t col, M_buf_t *buf, M_bool *is_null) |
M_sql_report_cberror_t | M_sql_report_cell_cb_int5dec (M_sql_stmt_t *stmt, void *arg, const char *name, size_t row, ssize_t col, M_buf_t *buf, M_bool *is_null) |
M_sql_report_cberror_t | M_sql_report_cell_cb_int5min2dec (M_sql_stmt_t *stmt, void *arg, const char *name, size_t row, ssize_t col, M_buf_t *buf, M_bool *is_null) |
M_sql_report_cberror_t | M_sql_report_cell_cb_boolyesno (M_sql_stmt_t *stmt, void *arg, const char *name, size_t row, ssize_t col, M_buf_t *buf, M_bool *is_null) |
M_bool | M_sql_report_add_column (M_sql_report_t *report, const char *name, M_sql_report_cell_cb_t cb, const char *sql_col_name, ssize_t sql_col_idx) |
M_bool | M_sql_report_hide_column (M_sql_report_t *report, const char *sql_col_name, ssize_t sql_col_idx) |
M_sql_report_filter_t * | M_sql_report_filter_create (M_sql_report_filter_type_t type) |
void | M_sql_report_filter_destroy (M_sql_report_filter_t *filter) |
M_bool | M_sql_report_filter_add_rule (M_sql_report_filter_t *filter, const char *column, M_sql_report_filter_rule_t rule, M_bool case_insensitive, const char *data) |
M_bool | M_sql_report_add_filter (M_sql_report_t *report, M_sql_report_filter_t *filter) |
M_sql_error_t | M_sql_report_process (const M_sql_report_t *report, M_sql_stmt_t *stmt, void *arg, char **out, size_t *out_len, char *error, size_t error_size) |
M_sql_error_t | M_sql_report_process_json (const M_sql_report_t *report, M_sql_stmt_t *stmt, void *arg, M_json_node_t *json, char *error, size_t error_size) |
M_sql_error_t | M_sql_report_process_partial (const M_sql_report_t *report, M_sql_stmt_t *stmt, size_t max_rows, void *arg, M_buf_t *buf, M_sql_report_state_t **state, char *error, size_t error_size) |
void | M_sql_report_state_cancel (M_sql_report_state_t *state) |
M_sql_error_t | M_sql_report_process_partial_json (const M_sql_report_t *report, M_sql_stmt_t *stmt, size_t max_rows, void *arg, M_json_node_t *json, M_sql_report_state_t **state, char *error, size_t error_size) |
SQL Report Generation
typedef struct M_sql_report M_sql_report_t |
Object holding the definition for report processing
typedef struct M_sql_report_state M_sql_report_state_t |
Object holding state data for M_sql_report_process_partial()
typedef M_bool(* M_sql_report_fetch_cb_t) (M_sql_stmt_t *stmt, void *arg) |
Prototype for fetch callback registered with M_sql_report_set_fetch_cb()
[in] | stmt | Statement handle object after fetch |
[in] | arg | Argument passed to M_sql_report_process() or M_sql_report_process_partial(). |
typedef M_sql_report_cberror_t(* M_sql_report_cell_cb_t) (M_sql_stmt_t *stmt, void *arg, const char *name, size_t row, ssize_t col, M_buf_t *buf, M_bool *is_null) |
Function callback prototype to use for cell formatting.
This function signature is used to process every column in a report, the output buffer is passed in by reference of a certain length. If the buffer is insufficient for the needs of the formatting function, the caller should allocate a new pointer instead and set it to the new buffer location. The report generation function will automatically free the callback's buffer.
[in] | stmt | Pointer to statement object being processed. |
[in] | arg | Custom user-supplied argument for registered callbacks. |
[in] | name | Assigned report name of column being processed (not necessarily the name of the SQL column returned) |
[in] | row | Row of result set currently being processed. |
[in] | col | Index of result set column being processed, or -1 if no specific column is referenced. |
[out] | buf | Pre-allocated M_buf_t buffer is provided to write column output. The provided buffer is empty, it is not pre-filled with column data as 'col' may be -1 or otherwise not a 1:1 mapping between input and output columns. It is up to the person implementing the callback to use the normal M_sql_stmt_result_*() functions to get the desired data. |
[out] | is_null | Output parameter that if set to M_TRUE, will ignore any contents in buf. It will also prevent quoting of the output cell so the output differentiates a blank cell (quoted) vs a NULL cell (unquoted). |
typedef struct M_sql_report_filter M_sql_report_filter_t |
Filter object created by M_sql_report_filter_create()
enum M_sql_report_flags_t |
Error conditions returned by M_sql_report_fetch_cb_t
Enumerator | |
---|---|
M_SQL_REPORT_ERROR | Error, abort report generation |
M_SQL_REPORT_SUCCESS | Success |
M_SQL_REPORT_SKIP_ROW | Do not output this row, but continue |
M_sql_report_t * M_sql_report_create | ( | M_uint32 | flags | ) |
Create a report object for processing SQL query results into a delimited data form.
Report processing is often used to turn SQL query results into delimited data like CSV.
Each column to be output must be defined, or set M_SQL_REPORT_FLAG_PASSTHRU_UNLISTED to pass through the data elements in their native form.
[in] | flags | Bitmap of M_sql_report_flags_t values to control behavior. |
void M_sql_report_destroy | ( | M_sql_report_t * | report | ) |
Destroy the report object.
[in] | report | Report object to be destroyed. |
M_bool M_sql_report_set_delims | ( | M_sql_report_t * | report, |
const unsigned char * | field_delim, | ||
size_t | field_delim_size, | ||
const unsigned char * | row_delim, | ||
size_t | row_delim_size, | ||
const unsigned char * | field_encaps, | ||
size_t | field_encaps_size, | ||
const unsigned char * | field_escape, | ||
size_t | field_escape_size | ||
) |
Set desired CSV delimiters, encapsulation, and escaping sequences to be used for the output data. Ignored for JSON.
If this function is not called, the defaults are used.
[in] | report | Initialized report object. |
[in] | field_delim | Delimiter to use between fields, default is a comma (,). NULL to not change. |
[in] | field_delim_size | Number of characters used in field delimiter. Max size 8. |
[in] | row_delim | Delimiter to use between rows, default is a new line (\r\n - CRLF). NULL to not change. |
[in] | row_delim_size | Number of characters used in row delimiter. Max size 8. |
[in] | field_encaps | Encapsulation character to use for field data that may contain the field_delim or row_delim, default is a double quote. NULL to not change. |
[in] | field_encaps_size | Number of characters used in field encapsulation. Max size 8. |
[in] | field_escape | Escape character to use if the field contains the encapsulation char, default is the same as the encapsulation, a double quote ("), as this is what is defined by RFC4180 (CSV) |
[in] | field_escape_size | Number of characters used in field escaping. Max size 8. |
M_bool M_sql_report_set_fetch_cb | ( | M_sql_report_t * | report, |
M_sql_report_fetch_cb_t | fetch_cb | ||
) |
Register a callback to be automatically called any time M_sql_stmt_fetch() is called successfully from within M_sql_report_process() or M_sql_report_process_partial().
This may be used if some bulk operation needs to process the data just fetched prior to processing the individual rows that were fetched.
[in] | report | Initialized report object |
[in] | fetch_cb | Callback to run every time M_sql_stmt_fetch() is successfully called. |
M_sql_report_cberror_t M_sql_report_cell_cb_passthru | ( | M_sql_stmt_t * | stmt, |
void * | arg, | ||
const char * | name, | ||
size_t | row, | ||
ssize_t | col, | ||
M_buf_t * | buf, | ||
M_bool * | is_null | ||
) |
Callback template for column passthru.
Any data on file will be passed-thru as-is, except for Binary data which will be automatically base64 encoded as the report output mandates string data only. If the cell is NULL, it will be output as blank.
M_sql_report_cberror_t M_sql_report_cell_cb_int2dec | ( | M_sql_stmt_t * | stmt, |
void * | arg, | ||
const char * | name, | ||
size_t | row, | ||
ssize_t | col, | ||
M_buf_t * | buf, | ||
M_bool * | is_null | ||
) |
Callback template for outputting an integer column stored with a 2-digit implied decimal point as an actual decimal with 2 decimal places. E.g.:
M_sql_report_cberror_t M_sql_report_cell_cb_int5dec | ( | M_sql_stmt_t * | stmt, |
void * | arg, | ||
const char * | name, | ||
size_t | row, | ||
ssize_t | col, | ||
M_buf_t * | buf, | ||
M_bool * | is_null | ||
) |
Callback template for outputting an integer column stored with a 5-digit implied decimal point as an actual decimal with 5 decimal places. E.g.:
M_sql_report_cberror_t M_sql_report_cell_cb_int5min2dec | ( | M_sql_stmt_t * | stmt, |
void * | arg, | ||
const char * | name, | ||
size_t | row, | ||
ssize_t | col, | ||
M_buf_t * | buf, | ||
M_bool * | is_null | ||
) |
Callback template for outputting an integer column stored with a 5-digit implied decimal point as an actual decimal with between 2 and 5 decimal places. E.g.:
M_sql_report_cberror_t M_sql_report_cell_cb_boolyesno | ( | M_sql_stmt_t * | stmt, |
void * | arg, | ||
const char * | name, | ||
size_t | row, | ||
ssize_t | col, | ||
M_buf_t * | buf, | ||
M_bool * | is_null | ||
) |
Callback template for outputting a boolean value column with yes or no.
If the cell is NULL, a blank column will be output instead of yes or no.
M_bool M_sql_report_add_column | ( | M_sql_report_t * | report, |
const char * | name, | ||
M_sql_report_cell_cb_t | cb, | ||
const char * | sql_col_name, | ||
ssize_t | sql_col_idx | ||
) |
Register column to be output in report.
If M_SQL_REPORT_FLAG_PASSTHRU_UNLISTED was used to initialize the report object, then if the sql_col_name or sql_col_idx matches a column (rather than being NULL and -1, respectively), then instead of adding a column, it overwrites its behavior ... either output column name, or the default callback can be changed from the default of passthrough.
[in] | report | Initialized report object. |
[in] | name | Name of column (used for headers in report) |
[in] | cb | Callback to use for formatting the column. |
[in] | sql_col_name | Optional, use NULL if not provieded. Retuned SQL column name returned with the data from the SQL server. This will be dereferenced and passed to the callback. |
[in] | sql_col_idx | Optional, use -1 if not provided. Returned SQL column index returned with the data from the SQL server. This will be passed to the callback. |
M_bool M_sql_report_hide_column | ( | M_sql_report_t * | report, |
const char * | sql_col_name, | ||
ssize_t | sql_col_idx | ||
) |
Hide a column from a report if M_SQL_REPORT_FLAG_PASSTHRU_UNLISTED was set.
When M_SQL_REPORT_FLAG_PASSTHRU_UNLISTED all columns in a report will be listed. This can be used to hide specific columns.
[in] | report | Initialized report object. |
[in] | sql_col_name | Conditional. Name of column to hide, or NULL. Must be set if sql_col_idx is -1. |
[in] | sql_col_idx | Conditional. Index of column to hide, or -1. Must be set if sql_col_name is NULL. |
M_sql_report_filter_t * M_sql_report_filter_create | ( | M_sql_report_filter_type_t | type | ) |
Create filter object
[in] | type | Type of filter to create |
void M_sql_report_filter_destroy | ( | M_sql_report_filter_t * | filter | ) |
Destroy filter object
[in] | filter | Initialized filter object by M_sql_report_filter_create() |
M_bool M_sql_report_filter_add_rule | ( | M_sql_report_filter_t * | filter, |
const char * | column, | ||
M_sql_report_filter_rule_t | rule, | ||
M_bool | case_insensitive, | ||
const char * | data | ||
) |
Add filter rule
[in] | filter | Initialized filter object from M_sql_report_filter_create() |
[in] | column | Name of column in report |
[in] | rule | Type of rule |
[in] | case_insensitive | For rules with data, whether the data should match case insensitive or not |
[in] | data | Data for matching |
M_bool M_sql_report_add_filter | ( | M_sql_report_t * | report, |
M_sql_report_filter_t * | filter | ||
) |
Attach a filter to a report. Only a single filter can be added to a report.
This function will take ownership of the filter object
[in] | report | Initialized report object |
[in] | filter | Initialized filter object with at least one rule |
M_sql_error_t M_sql_report_process | ( | const M_sql_report_t * | report, |
M_sql_stmt_t * | stmt, | ||
void * | arg, | ||
char ** | out, | ||
size_t * | out_len, | ||
char * | error, | ||
size_t | error_size | ||
) |
Process the results from the SQL statement based on the report template configured.
This function will call the registered report output generation functions to output each desired column of the report. If row fetching is used due to M_sql_stmt_set_max_fetch_rows(), this will automatically call M_sql_stmt_fetch() until all rows are returned.
No state is tracked in the report handle, it may be reused, and used concurrently if the implementor decides to cache the handle.
[in] | report | Initialized report object. |
[in] | stmt | Executed statement handle. |
[in] | arg | Custom user-supplied argument to be passed through to registered callbacks for column formatting. |
[out] | out | Formatted report data is returned in this variable and must be free'd by the caller. |
[out] | out_len | Optional. Returned length of output data. Should be equivalent to M_str_len(out), but on huge reports may be more efficient to already know the size. |
[out] | error | Buffer to hold error message. |
[in] | error_size | Size of error buffer passed in. |
M_sql_error_t M_sql_report_process_json | ( | const M_sql_report_t * | report, |
M_sql_stmt_t * | stmt, | ||
void * | arg, | ||
M_json_node_t * | json, | ||
char * | error, | ||
size_t | error_size | ||
) |
Process the results from the SQL statement based on the report template configured and append to the provided JSON array.
This function will call the registered report output generation functions to output each desired column of the report. If row fetching is used due to M_sql_stmt_set_max_fetch_rows(), this will automatically call M_sql_stmt_fetch() until all rows are returned.
No state is tracked in the report handle, it may be reused, and used concurrently if the implementor decides to cache the handle.
[in] | report | Initialized report object. |
[in] | stmt | Executed statement handle. |
[in] | arg | Custom user-supplied argument to be passed through to registered callbacks for column formatting. |
[in,out] | json | Passed in initialized json array node that each row will be appended to. |
[out] | error | Buffer to hold error message. |
[in] | error_size | Size of error buffer passed in. |
M_sql_error_t M_sql_report_process_partial | ( | const M_sql_report_t * | report, |
M_sql_stmt_t * | stmt, | ||
size_t | max_rows, | ||
void * | arg, | ||
M_buf_t * | buf, | ||
M_sql_report_state_t ** | state, | ||
char * | error, | ||
size_t | error_size | ||
) |
Process a chunk of report data rather than the whole report.
This function is useful if it is necessary to send a report in pieces either to a file or via a network connection, especially if the report may become extremely large and exceed the memory capabilities of the machine.
This function will be called in a loop until the return value is NOT M_SQL_ERROR_SUCCESS_ROW, it will fill in the user-supplied M_buf_t with the data. It is up to the user to clear the data from this buffer if the same buffer handle is passed in, or create a new handle, otherwise data will be appended.
[in] | report | Initialized report object. |
[in] | stmt | Executed statement handle. |
[in] | max_rows | Maximum number of rows to output per pass. Or 0 to output all. Typically it makes more sense to just call M_sql_report_process() if you want to use 0 for this value. |
[in] | arg | Custom user-supplied argument to be passed through to registered callbacks for column formatting. |
[in,out] | buf | User-supplied buffer to append report data to. |
[in,out] | state | Pointer to an M_sql_report_state_t * object, initialized to NULL on first pass. When there are more rows available, pass the same returned pointer back in. When the report generation is complete (last pass), this pointer will be automatically cleaned up. |
[out] | error | Buffer to hold error message. |
[in] | error_size | Size of error buffer passed in. |
void M_sql_report_state_cancel | ( | M_sql_report_state_t * | state | ) |
If for some reason a report must be aborted when using partial processing, this will clear up the memory associated with the state handle
[in] | state | M_sql_report_state_t * object populated from M_sql_report_process_partial() or M_sql_report_process_partial_json() |
M_sql_error_t M_sql_report_process_partial_json | ( | const M_sql_report_t * | report, |
M_sql_stmt_t * | stmt, | ||
size_t | max_rows, | ||
void * | arg, | ||
M_json_node_t * | json, | ||
M_sql_report_state_t ** | state, | ||
char * | error, | ||
size_t | error_size | ||
) |
Process a chunk of report data rather than the whole report and append to the provided JSON array.
This function is useful if it is necessary to send a report in pieces either to a file or via a network connection, especially if the report may become extremely large and exceed the memory capabilities of the machine.
This function will be called in a loop until the return value is NOT M_SQL_ERROR_SUCCESS_ROW, it will fill in the user-supplied M_buf_t with the data. It is up to the user to clear the data from this buffer if the same buffer handle is passed in, or create a new handle, otherwise data will be appended.
[in] | report | Initialized report object. |
[in] | stmt | Executed statement handle. |
[in] | max_rows | Maximum number of rows to output per pass. Or 0 to output all. Typically it makes more sense to just call M_sql_report_process() if you want to use 0 for this value. |
[in] | arg | Custom user-supplied argument to be passed through to registered callbacks for column formatting. |
[in,out] | json | Passed in initialized json array node that each row will be appended to. |
[in,out] | state | Pointer to an M_sql_report_state_t * object, initialized to NULL on first pass. When there are more rows available, pass the same returned pointer back in. When the report generation is complete (last pass), this pointer will be automatically cleaned up. |
[out] | error | Buffer to hold error message. |
[in] | error_size | Size of error buffer passed in. |