OpenVAS Manager
7.0.3~git
|
#include "sql.h"
#include <assert.h>
#include <endian.h>
#include <errno.h>
#include <arpa/inet.h>
#include <glib.h>
#include <inttypes.h>
#include <netinet/in.h>
#include <postgresql/libpq-fe.h>
#include <stdlib.h>
#include <string.h>
#include <openvas/base/array.h>
Go to the source code of this file.
Data Structures | |
struct | sql_stmt |
Macros | |
#define | G_LOG_DOMAIN "md manage" |
GLib log domain. More... | |
Functions | |
int | sql_x (char *, va_list args, sql_stmt_t **) |
Get a particular cell from a SQL query. More... | |
int | sql_is_sqlite3 () |
Get whether backend is SQLite3. More... | |
const char * | sql_schema () |
Get main schema name. More... | |
const char * | sql_greatest () |
Get keyword for "greatest" SQL function. More... | |
const char * | sql_select_limit (int max) |
Setup a LIMIT argument. More... | |
const char * | sql_ilike_op () |
Get case insensitive LIKE operator. More... | |
const char * | sql_regexp_op () |
Get regular expression operator. More... | |
int | sql_is_open () |
Check whether the database is open. More... | |
const char * | sql_database () |
Return name of current database. More... | |
const char * | sql_default_database () |
Return name of default database. More... | |
int | sql_open (const char *database) |
Open the database. More... | |
void | sql_close () |
Close the database. More... | |
void | sql_close_fork () |
Close the database in a forked process. More... | |
int | sql_changes () |
Return 0. More... | |
resource_t | sql_last_insert_id () |
Get the ID of the last inserted row. More... | |
void | sqli (resource_t *resource, char *sql,...) |
Perform an SQL statement, retrying if database is busy or locked. More... | |
int | sql_prepare_internal (int retry, int log, const char *sql, va_list args, sql_stmt_t **stmt) |
Prepare a statement. More... | |
int | sql_exec_internal (int retry, sql_stmt_t *stmt) |
Execute a prepared statement. More... | |
int | sql_explain_internal (const char *sql, va_list args) |
Write debug messages with the query plan for an SQL query to the log. More... | |
void | sql_begin_exclusive () |
Begin an exclusive transaction. More... | |
int | sql_begin_exclusive_giveup () |
Begin an exclusive transaction, giving up on failure. More... | |
void | sql_begin_immediate () |
Begin an immediate transaction. More... | |
int | sql_begin_immediate_giveup () |
Begin an immediate transaction. More... | |
void | sql_commit () |
Commit a transaction. More... | |
void | sql_rollback () |
Roll a transaction back. More... | |
int | iterator_null (iterator_t *iterator, int col) |
Get whether a column is NULL. More... | |
const char * | iterator_column_name (iterator_t *iterator, int col) |
Get a column name from an iterator. More... | |
int | iterator_column_count (iterator_t *iterator) |
Get number of columns from an iterator. More... | |
int | sql_bind_blob (sql_stmt_t *stmt, int position, const void *value, int value_size) |
Bind a blob to a statement. More... | |
int | sql_bind_int64 (sql_stmt_t *stmt, int position, long long int *value) |
Bind an int64 value to a statement. More... | |
int | sql_bind_double (sql_stmt_t *stmt, int position, double *value) |
Bind a double value to a statement. More... | |
int | sql_bind_text (sql_stmt_t *stmt, int position, const gchar *value, gsize value_size) |
Bind a text value to a statement. More... | |
void | sql_finalize (sql_stmt_t *stmt) |
Free a prepared statement. More... | |
int | sql_reset (sql_stmt_t *stmt) |
Reset a prepared statement. More... | |
double | sql_column_double (sql_stmt_t *stmt, int position) |
Return a column as a double from a prepared statement. More... | |
const char * | sql_column_text (sql_stmt_t *stmt, int position) |
Return a column as text from a prepared statement. More... | |
int | sql_column_int (sql_stmt_t *stmt, int position) |
Return a column as an integer from a prepared statement. More... | |
long long int | sql_column_int64 (sql_stmt_t *stmt, int position) |
Return a column as an int64 from a prepared statement. More... | |
int | sql_cancel_internal () |
Cancels the current SQL statement. More... | |
Variables | |
int | log_errors |
Whether to log errors. More... | |
PGconn * | conn = NULL |
Handle on the database. More... | |
int iterator_column_count | ( | iterator_t * | iterator | ) |
Get number of columns from an iterator.
[in] | iterator | Iterator. |
Definition at line 706 of file sql_pg.c.
References iterator::done, sql_stmt::result, and iterator::stmt.
const char* iterator_column_name | ( | iterator_t * | iterator, |
int | col | ||
) |
Get a column name from an iterator.
[in] | iterator | Iterator. |
[in] | col | Column offset. |
Definition at line 691 of file sql_pg.c.
References iterator::done, sql_stmt::result, and iterator::stmt.
int iterator_null | ( | iterator_t * | iterator, |
int | col | ||
) |
Get whether a column is NULL.
[in] | iterator | Iterator. |
[in] | col | Column offset. |
Definition at line 675 of file sql_pg.c.
References iterator::done, sql_stmt::result, and iterator::stmt.
void sql_begin_exclusive | ( | ) |
Begin an exclusive transaction.
Definition at line 598 of file sql_pg.c.
References sql_stmt::sql.
Referenced by sql_begin_immediate().
int sql_begin_exclusive_giveup | ( | ) |
Begin an exclusive transaction, giving up on failure.
Definition at line 610 of file sql_pg.c.
References sql_giveup(), sql_int(), and sql_rollback().
Referenced by sql_begin_immediate_giveup().
void sql_begin_immediate | ( | ) |
Begin an immediate transaction.
Definition at line 627 of file sql_pg.c.
References sql_begin_exclusive().
int sql_begin_immediate_giveup | ( | ) |
Begin an immediate transaction.
Definition at line 639 of file sql_pg.c.
References sql_begin_exclusive_giveup().
int sql_bind_blob | ( | sql_stmt_t * | stmt, |
int | position, | ||
const void * | value, | ||
int | value_size | ||
) |
int sql_bind_double | ( | sql_stmt_t * | stmt, |
int | position, | ||
double * | value | ||
) |
Bind a double value to a statement.
[in] | stmt | Statement. |
[in] | position | Position in statement. |
[in] | value | Value. |
int sql_bind_int64 | ( | sql_stmt_t * | stmt, |
int | position, | ||
long long int * | value | ||
) |
Bind an int64 value to a statement.
[in] | stmt | Statement. |
[in] | position | Position in statement. |
[in] | value | Value. |
int sql_bind_text | ( | sql_stmt_t * | stmt, |
int | position, | ||
const gchar * | value, | ||
gsize | value_size | ||
) |
Bind a text value to a statement.
[in] | stmt | Statement. |
[in] | position | Position in statement. |
[in] | value | Value. |
[in] | value_size | Value size, or -1 to use strlen of value. |
int sql_cancel_internal | ( | ) |
Cancels the current SQL statement.
Definition at line 961 of file sql_pg.c.
References conn, and log_errors.
int sql_changes | ( | ) |
void sql_close | ( | ) |
void sql_close_fork | ( | ) |
double sql_column_double | ( | sql_stmt_t * | stmt, |
int | position | ||
) |
Return a column as a double from a prepared statement.
It's up to the caller to ensure that there is a row available.
[in] | stmt | Statement. |
[in] | position | Column position. |
Definition at line 868 of file sql_pg.c.
References sql_stmt::current_row, and sql_stmt::result.
int sql_column_int | ( | sql_stmt_t * | stmt, |
int | position | ||
) |
Return a column as an integer from a prepared statement.
It's up to the caller to ensure that there is a row available.
[in] | stmt | Statement. |
[in] | position | Column position. |
Definition at line 906 of file sql_pg.c.
References sql_stmt::current_row, and sql_stmt::result.
long long int sql_column_int64 | ( | sql_stmt_t * | stmt, |
int | position | ||
) |
Return a column as an int64 from a prepared statement.
It's up to the caller to ensure that there is a row available.
[in] | stmt | Statement. |
[in] | position | Column position. |
Definition at line 936 of file sql_pg.c.
References sql_stmt::current_row, and sql_stmt::result.
Referenced by sqli().
const char* sql_column_text | ( | sql_stmt_t * | stmt, |
int | position | ||
) |
Return a column as text from a prepared statement.
It's up to the caller to ensure that there is a row available.
[in] | stmt | Statement. |
[in] | position | Column position. |
Definition at line 887 of file sql_pg.c.
References sql_stmt::current_row, and sql_stmt::result.
void sql_commit | ( | ) |
const char* sql_database | ( | ) |
const char* sql_default_database | ( | ) |
int sql_exec_internal | ( | int | retry, |
sql_stmt_t * | stmt | ||
) |
Execute a prepared statement.
[in] | retry | Whether to keep retrying while database is busy or locked. |
[in] | stmt | Statement. |
Definition at line 482 of file sql_pg.c.
References conn, sql_stmt::current_row, sql_stmt::executed, log_errors, sql_stmt::param_formats, sql_stmt::param_lengths, sql_stmt::param_values, sql_stmt::result, and sql_stmt::sql.
Referenced by sql_explain_internal().
int sql_explain_internal | ( | const char * | sql, |
va_list | args | ||
) |
Write debug messages with the query plan for an SQL query to the log.
[in] | sql | Format string for the SQL query. |
[in] | args | Format string arguments in a va_list. |
Definition at line 551 of file sql_pg.c.
References sql_stmt::current_row, log_errors, sql_stmt::result, sql_exec_internal(), sql_finalize(), and sql_prepare_internal().
void sql_finalize | ( | sql_stmt_t * | stmt | ) |
Free a prepared statement.
[in] | stmt | Statement. |
Definition at line 824 of file sql_pg.c.
References sql_stmt::param_formats, sql_stmt::param_lengths, sql_stmt::param_values, sql_stmt::result, and sql_stmt::sql.
Referenced by sql_explain_internal(), and sqli().
const char* sql_greatest | ( | ) |
const char* sql_ilike_op | ( | ) |
int sql_is_open | ( | ) |
int sql_is_sqlite3 | ( | ) |
resource_t sql_last_insert_id | ( | ) |
int sql_open | ( | const char * | database | ) |
Open the database.
[in] | database | Database, or NULL for default. |
Definition at line 250 of file sql_pg.c.
int sql_prepare_internal | ( | int | retry, |
int | log, | ||
const char * | sql, | ||
va_list | args, | ||
sql_stmt_t ** | stmt | ||
) |
Prepare a statement.
[in] | retry | Whether to keep retrying while database is busy or locked. |
[in] | log | Whether to keep retrying while database is busy or locked. |
[in] | sql | Format string for SQL statement. |
[in] | args | Arguments for format string. |
[out] | stmt | Statement return. |
Definition at line 458 of file sql_pg.c.
Referenced by sql_explain_internal().
const char* sql_regexp_op | ( | ) |
int sql_reset | ( | sql_stmt_t * | stmt | ) |
Reset a prepared statement.
[in] | stmt | Statement. |
Definition at line 842 of file sql_pg.c.
References sql_stmt::param_formats, sql_stmt::param_lengths, sql_stmt::param_values, sql_stmt::result, and sql_stmt::sql.
void sql_rollback | ( | ) |
Roll a transaction back.
Definition at line 658 of file sql_pg.c.
References sql_stmt::sql.
Referenced by sql_begin_exclusive_giveup().
const char* sql_schema | ( | ) |
const char* sql_select_limit | ( | int | max | ) |
Setup a LIMIT argument.
[in] | max | Max. |
int sql_x | ( | char * | sql, |
va_list | args, | ||
sql_stmt_t ** | stmt_return | ||
) |
Get a particular cell from a SQL query.
Do logging as usual.
[in] | sql | Format string for SQL query. |
[in] | args | Arguments for format string. |
[out] | stmt_return | Return from statement. |
Definition at line 385 of file sql.c.
Referenced by sql_double(), sql_int(), sql_int64(), sql_string(), and sqli().
void sqli | ( | resource_t * | resource, |
char * | sql, | ||
... | |||
) |
Perform an SQL statement, retrying if database is busy or locked.
[out] | resource | Last inserted resource. |
[in] | sql | Format string for SQL statement. |
[in] | ... | Arguments for format string. |
Definition at line 408 of file sql_pg.c.
References sql_column_int64(), sql_finalize(), sql_x(), and sql_stmt::stmt.
PGconn* conn = NULL |
Handle on the database.
Definition at line 81 of file sql_pg.c.
Referenced by sql_cancel_internal(), sql_close(), sql_close_fork(), sql_database(), sql_exec_internal(), and sql_is_open().
int log_errors |
Whether to log errors.
Used to turn off logging when cancelling statements on exit. Defined in sql.c.
Used to turn off logging when cancelling statements on exit.
Definition at line 62 of file sql.c.
Referenced by next(), sql_cancel_internal(), sql_exec_internal(), sql_explain_internal(), sql_quiet(), and sqlv().