Greenbone Vulnerability Manager  8.0.1~git
Data Structures | Macros | Functions | Variables
sql_pg.c File Reference

Generic SQL interface: PostgreSQL backend. More...

#include "sql.h"
#include <arpa/inet.h>
#include <assert.h>
#include <endian.h>
#include <errno.h>
#include <glib.h>
#include <gvm/base/array.h>
#include <inttypes.h>
#include <netinet/in.h>
#include <postgresql/libpq-fe.h>
#include <stdlib.h>
#include <string.h>

Data Structures

struct  sql_stmt
 An SQL statement. More...
 

Macros

#define G_LOG_DOMAIN   "md manage"
 GLib log domain.
 

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...
 
static void sql_stmt_param_add (sql_stmt_t *stmt, const char *param_value, int param_size, int param_format)
 Add param to statement. More...
 
static void sql_stmt_init (sql_stmt_t *stmt)
 Init statement, preserving SQL. 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...
 
static void log_notice (void *arg, const char *message)
 Log a NOTICE message. More...
 
const char * sql_database ()
 Return name of current database. More...
 
const char * sql_default_database ()
 Return name of default database. More...
 
void sql_recursive_triggers_off ()
 Turn off recursive triggers. More...
 
int sql_open (const char *database)
 Open the database. More...
 
void sql_close ()
 Close the database.
 
void sql_close_fork ()
 Close the database in a forked process.
 
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.
 
int sql_begin_exclusive_giveup ()
 Begin an exclusive transaction, giving up on failure. More...
 
void sql_begin_immediate ()
 Begin an immediate transaction.
 
int sql_begin_immediate_giveup ()
 Begin an immediate transaction. More...
 
void sql_commit ()
 Commit a transaction.
 
void sql_rollback ()
 Roll a transaction back.
 
int iterator_null (iterator_t *iterator, int col)
 Get whether a column is NULL. More...
 
static void bind_param (sql_stmt_t *stmt, int position, const void *param_value, int param_size, int param_format)
 Bind a param to a statement. 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_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...
 
static PGconn * conn = NULL
 Handle on the database.
 

Detailed Description

Generic SQL interface: PostgreSQL backend.

PostreSQL backend of the SQL interface.

Function Documentation

◆ bind_param()

static void bind_param ( sql_stmt_t stmt,
int  position,
const void *  param_value,
int  param_size,
int  param_format 
)
static

Bind a param to a statement.

Parameters
[in]stmtStatement.
[in]positionPosition in statement.
[in]param_valueParam value.
[in]param_sizeParam size.
[in]param_format0 text, 1 binary.

◆ iterator_null()

int iterator_null ( iterator_t iterator,
int  col 
)

Get whether a column is NULL.

Parameters
[in]iteratorIterator.
[in]colColumn offset.
Returns
1 if NULL, else 0.

◆ log_notice()

static void log_notice ( void *  arg,
const char *  message 
)
static

Log a NOTICE message.

Parameters
[in]argDummy arg.
[in]messageArg.
Returns
0 success, -1 error.

◆ sql_begin_exclusive_giveup()

int sql_begin_exclusive_giveup ( )

Begin an exclusive transaction, giving up on failure.

Returns
0 got lock, 1 gave up, -1 error.

◆ sql_begin_immediate_giveup()

int sql_begin_immediate_giveup ( )

Begin an immediate transaction.

Returns
0 got lock, 1 gave up, -1 error.

◆ sql_bind_blob()

int sql_bind_blob ( sql_stmt_t stmt,
int  position,
const void *  value,
int  value_size 
)

Bind a blob to a statement.

Parameters
[in]stmtStatement.
[in]positionPosition in statement.
[in]valueBlob.
[in]value_sizeBlob size.
Returns
0 success, -1 error.

◆ sql_bind_text()

int sql_bind_text ( sql_stmt_t stmt,
int  position,
const gchar *  value,
gsize  value_size 
)

Bind a text value to a statement.

Parameters
[in]stmtStatement.
[in]positionPosition in statement.
[in]valueValue.
[in]value_sizeValue size, or -1 to use strlen of value.
Returns
0 success, -1 error.

◆ sql_cancel_internal()

int sql_cancel_internal ( )

Cancels the current SQL statement.

Returns
0 on success, -1 on error.

◆ sql_changes()

int sql_changes ( )

Return 0.

Returns
0.

◆ sql_column_double()

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.

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 prepared 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 prepared 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 prepared 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_database()

const char* sql_database ( )

Return name of current database.

Returns
Name of database.

◆ sql_default_database()

const char* sql_default_database ( )

Return name of default database.

Returns
Name.

◆ sql_exec_internal()

int sql_exec_internal ( int  retry,
sql_stmt_t stmt 
)

Execute a prepared 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.

◆ 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.

Parameters
[in]sqlFormat string for the SQL query.
[in]argsFormat string arguments in a va_list.
Returns
0 success, -1 error.

◆ sql_finalize()

void sql_finalize ( sql_stmt_t stmt)

Free a prepared statement.

Parameters
[in]stmtStatement.

◆ sql_greatest()

const char* sql_greatest ( )

Get keyword for "greatest" SQL function.

Returns
Keyword.

◆ sql_ilike_op()

const char* sql_ilike_op ( )

Get case insensitive LIKE operator.

Returns
Like operator.

◆ sql_is_open()

int sql_is_open ( )

Check whether the database is open.

Returns
1 if open, else 0.

◆ sql_is_sqlite3()

int sql_is_sqlite3 ( )

Get whether backend is SQLite3.

Returns
0.

◆ sql_last_insert_id()

resource_t sql_last_insert_id ( )

Get the ID of the last inserted row.

Returns
Resource.

◆ sql_open()

int sql_open ( const char *  database)

Open the database.

Parameters
[in]databaseDatabase, or NULL for default.
Returns
0 success, -1 error.

◆ 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_recursive_triggers_off()

void sql_recursive_triggers_off ( )

Turn off recursive triggers.

Ignored when DB is Postgres.

◆ sql_regexp_op()

const char* sql_regexp_op ( )

Get regular expression operator.

Returns
Regexp operator.

◆ sql_reset()

int sql_reset ( sql_stmt_t stmt)

Reset a prepared statement.

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

◆ sql_schema()

const char* sql_schema ( )

Get main schema name.

Returns
Schema name.

◆ sql_select_limit()

const char* sql_select_limit ( int  max)

Setup a LIMIT argument.

Parameters
[in]maxMax.
Returns
Argument for LIMIT as a static string.

◆ sql_stmt_init()

static void sql_stmt_init ( sql_stmt_t stmt)
static

Init statement, preserving SQL.

Parameters
[in]stmtStatement.

◆ sql_stmt_param_add()

static void sql_stmt_param_add ( sql_stmt_t stmt,
const char *  param_value,
int  param_size,
int  param_format 
)
static

Add param to statement.

Parameters
[in]stmtStatement.
[in]param_valueValue.
[in]param_sizeSize.
[in]param_format0 text, 1 binary.

◆ sql_x()

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.

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.

◆ sqli()

void sqli ( resource_t resource,
char *  sql,
  ... 
)

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

Parameters
[out]resourceLast inserted resource.
[in]sqlFormat string for SQL statement.
[in]...Arguments for format string.

Variable Documentation

◆ log_errors

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.