Greenbone Vulnerability Manager  22.4.0~dev1
Macros | Functions | Variables
sql.c File Reference

Generic SQL interface. More...

#include "sql.h"
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

Macros

#define G_LOG_DOMAIN   "md manage"
 GLib log domain.
 
#define DEADLOCK_SLEEP   1000
 amount of ms sql should wait before retrying when a deadlock occurred
 
#define DEADLOCK_THRESHOLD   25
 defines the amount of retries after a deadlock is considered a warning
 

Functions

int sql_prepare_internal (int, int, const char *, va_list, sql_stmt_t **)
 Prepare a statement. More...
 
int sql_exec_internal (int, sql_stmt_t *)
 Execute a statement. More...
 
void sql_finalize (sql_stmt_t *)
 Free a statement. More...
 
double sql_column_double (sql_stmt_t *, int)
 Return a column as a double from a statement. More...
 
const char * sql_column_text (sql_stmt_t *, int)
 Return a column as text from a statement. More...
 
int sql_column_int (sql_stmt_t *, int)
 Return a column as an integer from a statement. More...
 
long long int sql_column_int64 (sql_stmt_t *, int)
 Return a column as an int64 from a statement. More...
 
gchar ** sql_column_array (sql_stmt_t *, int)
 Return a column as text from a statement. More...
 
gchar * sql_nquote (const char *string, size_t length)
 Quotes a string of a known length to be passed to sql statements. More...
 
gchar * sql_quote (const char *string)
 Quotes a string to be passed to sql statements. More...
 
gchar * sql_ascii_escape_and_quote (const char *string)
 Quotes a string for use in SQL statements, also ASCII escaping it if it is not valid UTF-8. More...
 
gchar * sql_insert (const char *string)
 Get the SQL insert expression for a string. More...
 
int sqlv (int retry, char *sql, va_list args)
 Perform an SQL statement. More...
 
void sql (char *sql,...)
 Perform an SQL statement, retrying if database is busy or locked. More...
 
int sql_error (char *sql,...)
 Perform an SQL statement, retrying if database is busy or locked. More...
 
int sql_giveup (char *sql,...)
 Perform an SQL statement, giving up if database is busy or locked. More...
 
int sql_x (char *sql, va_list args, sql_stmt_t **stmt_return)
 Get a particular cell from a SQL query. More...
 
double sql_double (char *sql,...)
 Get the first value from a SQL query, as a double. More...
 
int sql_int (char *sql,...)
 Get a particular cell from a SQL query, as an int. More...
 
char * sql_string (char *sql,...)
 Get a particular cell from a SQL query, as an string. More...
 
int sql_int64 (long long int *ret, char *sql,...)
 Get a particular cell from a SQL query, as an int64. More...
 
long long int sql_int64_0 (char *sql,...)
 Get a first column of first row from a SQL query, as an int64. More...
 
void init_iterator (iterator_t *iterator, const char *sql,...)
 Initialise an iterator. More...
 
double iterator_double (iterator_t *iterator, int col)
 Get a double column from an iterator. More...
 
int iterator_int (iterator_t *iterator, int col)
 Get a int column from an iterator. More...
 
long long int iterator_int64 (iterator_t *iterator, int col)
 Get an integer column from an iterator. More...
 
const char * iterator_string (iterator_t *iterator, int col)
 Get a string column from an iterator. More...
 
gchar ** iterator_array (iterator_t *iterator, int col)
 Get a string column from an iterator. More...
 
void cleanup_iterator (iterator_t *iterator)
 Cleanup an iterator. More...
 
gboolean next (iterator_t *iterator)
 Increment an iterator. More...
 

Variables

int log_errors = 1
 Whether to log errors. More...
 

Detailed Description

Generic SQL interface.

This is a small generic interface for SQL database access.

To add support for a specific database, like Postgres, a few functions (for example, sql_prepare_internal and sql_exec_internal) need to be implemented for that database.

Function Documentation

◆ cleanup_iterator()

void cleanup_iterator ( iterator_t iterator)

Cleanup an iterator.

Parameters
[in]iteratorIterator.

◆ init_iterator()

void init_iterator ( iterator_t iterator,
const char *  sql,
  ... 
)

Initialise an iterator.

Parameters
[in]iteratorIterator.
[in]sqlFormat string for SQL.

◆ iterator_array()

gchar** iterator_array ( iterator_t iterator,
int  col 
)

Get a string column from an iterator.

Note that sql_column_array gets the array as text and parses that text into an array, but it does not consider escaping so it probably will not work with strings that can contain commas, '{'s or '}'s.

Parameters
[in]iteratorIterator.
[in]colColumn offset.
Returns
Value of given column.

◆ iterator_double()

double iterator_double ( iterator_t iterator,
int  col 
)

Get a double column from an iterator.

Parameters
[in]iteratorIterator.
[in]colColumn offset.
Returns
Value of given column.

◆ iterator_int()

int iterator_int ( iterator_t iterator,
int  col 
)

Get a int column from an iterator.

Parameters
[in]iteratorIterator.
[in]colColumn offset.
Returns
Value of given column.

◆ iterator_int64()

long long int iterator_int64 ( iterator_t iterator,
int  col 
)

Get an integer column from an iterator.

Parameters
[in]iteratorIterator.
[in]colColumn offset.
Returns
Value of given column.

◆ iterator_string()

const char* iterator_string ( iterator_t iterator,
int  col 
)

Get a string column from an iterator.

Parameters
[in]iteratorIterator.
[in]colColumn offset.
Returns
Value of given column.

◆ next()

gboolean next ( iterator_t iterator)

Increment an iterator.

Parameters
[in]iteratorIterator.
Returns
TRUE if there was a next item, else FALSE.

◆ sql()

void sql ( char *  sql,
  ... 
)

Perform an SQL statement, retrying if database is busy or locked.

Parameters
[in]sqlFormat string for SQL statement.
[in]...Arguments for format string.

◆ sql_ascii_escape_and_quote()

gchar* sql_ascii_escape_and_quote ( const char *  string)

Quotes a string for use in SQL statements, also ASCII escaping it if it is not valid UTF-8.

Parameters
[in]stringString to quote, has to be \0 terminated.
Returns
Freshly allocated, quoted string. Free with g_free.

◆ sql_column_array()

gchar** sql_column_array ( sql_stmt_t stmt,
int  position 
)

Return a column as text from a statement.

It's up to the caller to ensure that there is a row available.

Parameters
[in]stmtStatement.
[in]positionColumn position.
Returns
Column value. NULL if column is NULL.

◆ sql_column_double()

double sql_column_double ( sql_stmt_t stmt,
int  position 
)

Return a column as a double from a statement.

It's up to the caller to ensure that there is a row available.

Parameters
[in]stmtStatement.
[in]positionColumn position.
Returns
0 success, -1 error.

◆ sql_column_int()

int sql_column_int ( sql_stmt_t stmt,
int  position 
)

Return a column as an integer from a statement.

It's up to the caller to ensure that there is a row available.

Parameters
[in]stmtStatement.
[in]positionColumn position.
Returns
Column value. 0 if column is NULL or false. 1 if column true.

◆ sql_column_int64()

long long int sql_column_int64 ( sql_stmt_t stmt,
int  position 
)

Return a column as an int64 from a statement.

It's up to the caller to ensure that there is a row available.

Parameters
[in]stmtStatement.
[in]positionColumn position.
Returns
Column value. 0 if column is NULL or false. 1 if column true.

◆ sql_column_text()

const char* sql_column_text ( sql_stmt_t stmt,
int  position 
)

Return a column as text from a statement.

It's up to the caller to ensure that there is a row available.

Parameters
[in]stmtStatement.
[in]positionColumn position.
Returns
Column value. NULL if column is NULL.

◆ sql_double()

double sql_double ( char *  sql,
  ... 
)

Get the first value from a SQL query, as a double.

Warning
Aborts on invalid queries.
Aborts when the query returns fewer rows than row. The caller must ensure that the query will return sufficient rows.
Parameters
[in]sqlFormat string for SQL query.
[in]...Arguments for format string.
Returns
Result of the query as an integer.

◆ sql_error()

int sql_error ( char *  sql,
  ... 
)

Perform an SQL statement, retrying if database is busy or locked.

Return on error, instead of aborting.

Parameters
[in]sqlFormat string for SQL statement.
[in]...Arguments for format string.
Returns
0 success, 2 reserved (lock unavailable), 3 unique constraint violation, -1 error.

◆ sql_exec_internal()

int sql_exec_internal ( int  retry,
sql_stmt_t stmt 
)

Execute a statement.

Parameters
[in]retryWhether to keep retrying while database is busy or locked.
[in]stmtStatement.
Returns
0 complete, 1 row available in results, -1 error, -2 gave up, -3 lock unavailable, -4 unique constraint violation.

◆ sql_finalize()

void sql_finalize ( sql_stmt_t stmt)

Free a statement.

Parameters
[in]stmtStatement.

◆ sql_giveup()

int sql_giveup ( char *  sql,
  ... 
)

Perform an SQL statement, giving up if database is busy or locked.

Parameters
[in]sqlFormat string for SQL statement.
[in]...Arguments for format string.
Returns
0 success, 1 gave up, 2 reserved (lock unavailable), 3 unique constraint violation, -1 error.

◆ sql_insert()

gchar* sql_insert ( const char *  string)

Get the SQL insert expression for a string.

Parameters
[in]stringThe string, which may be NULL.
Returns
Freshly allocated expression suitable for an INSERT statement, including SQL quotation marks.

◆ sql_int()

int sql_int ( char *  sql,
  ... 
)

Get a particular cell from a SQL query, as an int.

Warning
Aborts on invalid queries.
Aborts when the query returns fewer rows than row. The caller must ensure that the query will return sufficient rows.
Parameters
[in]sqlFormat string for SQL query.
[in]...Arguments for format string.
Returns
Result of the query as an integer.

◆ sql_int64()

int sql_int64 ( long long int *  ret,
char *  sql,
  ... 
)

Get a particular cell from a SQL query, as an int64.

Parameters
[in]retReturn value.
[in]sqlFormat string for SQL query.
[in]...Arguments for format string.
Returns
0 success, 1 too few rows, -1 error.

◆ sql_int64_0()

long long int sql_int64_0 ( char *  sql,
  ... 
)

Get a first column of first row from a SQL query, as an int64.

Return 0 on error.

Parameters
[in]sqlFormat string for SQL query.
[in]...Arguments for format string.
Returns
Column value. 0 if no row.

◆ sql_nquote()

gchar* sql_nquote ( const char *  string,
size_t  length 
)

Quotes a string of a known length to be passed to sql statements.

Parameters
[in]stringString to quote.
[in]lengthSize of string.
Returns
Freshly allocated, quoted string. Free with g_free.

◆ sql_prepare_internal()

int sql_prepare_internal ( int  retry,
int  log,
const char *  sql,
va_list  args,
sql_stmt_t **  stmt 
)

Prepare a statement.

Parameters
[in]retryWhether to keep retrying while database is busy or locked.
[in]logWhether to keep retrying while database is busy or locked.
[in]sqlFormat string for SQL statement.
[in]argsArguments for format string.
[out]stmtStatement return.
Returns
0 success, 1 gave up, -1 error.

◆ sql_quote()

gchar* sql_quote ( const char *  string)

Quotes a string to be passed to sql statements.

Parameters
[in]stringString to quote, has to be \0 terminated.
Returns
Freshly allocated, quoted string. Free with g_free.

◆ sql_string()

char* sql_string ( char *  sql,
  ... 
)

Get a particular cell from a SQL query, as an string.

Parameters
[in]sqlFormat string for SQL query.
[in]...Arguments for format string.
Returns
Freshly allocated string containing the result, NULL otherwise. NULL means that either the selected value was NULL or there were no rows in the result.

◆ sql_x()

int sql_x ( char *  sql,
va_list  args,
sql_stmt_t **  stmt_return 
)

Get a particular cell from a SQL query.

Parameters
[in]sqlFormat string for SQL query.
[in]argsArguments for format string.
[out]stmt_returnReturn from statement.
Returns
0 success, 1 too few rows, -1 error.

◆ sqlv()

int sqlv ( int  retry,
char *  sql,
va_list  args 
)

Perform an SQL statement.

Parameters
[in]retryWhether to keep retrying while database is busy or locked.
[in]sqlFormat string for SQL statement.
[in]argsArguments for format string.
Returns
0 success, 1 gave up (even when retry given), 2 reserved (lock unavailable), 3 unique constraint violation, -1 error.

Variable Documentation

◆ log_errors

int log_errors = 1

Whether to log errors.

Used to turn off logging when cancelling statements on exit.