OpenVAS Manager  7.0.3~git
sql_pg.c File Reference
#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>
Include dependency graph for sql_pg.c:

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

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "md manage"

GLib log domain.

Definition at line 45 of file sql_pg.c.

Function Documentation

◆ iterator_column_count()

int iterator_column_count ( iterator_t iterator)

Get number of columns from an iterator.

Parameters
[in]iteratorIterator.
Returns
Number of columns.

Definition at line 706 of file sql_pg.c.

References iterator::done, sql_stmt::result, and iterator::stmt.

707 {
708  if (iterator->done) abort ();
709  assert (iterator->stmt->result);
710  return PQnfields (iterator->stmt->result);
711 }
gboolean done
End flag.
Definition: iterator.h:55
sql_stmt_t * stmt
SQL statement.
Definition: iterator.h:54
PGresult * result
Result set.
Definition: sql_pg.c:59

◆ iterator_column_name()

const char* iterator_column_name ( iterator_t iterator,
int  col 
)

Get a column name from an iterator.

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

Definition at line 691 of file sql_pg.c.

References iterator::done, sql_stmt::result, and iterator::stmt.

692 {
693  if (iterator->done) abort ();
694  assert (iterator->stmt->result);
695  return PQfname (iterator->stmt->result, col);
696 }
gboolean done
End flag.
Definition: iterator.h:55
sql_stmt_t * stmt
SQL statement.
Definition: iterator.h:54
PGresult * result
Result set.
Definition: sql_pg.c:59

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

Definition at line 675 of file sql_pg.c.

References iterator::done, sql_stmt::result, and iterator::stmt.

676 {
677  if (iterator->done) abort ();
678  assert (iterator->stmt->result);
679  return PQgetisnull (iterator->stmt->result, 0, col);
680 }
gboolean done
End flag.
Definition: iterator.h:55
sql_stmt_t * stmt
SQL statement.
Definition: iterator.h:54
PGresult * result
Result set.
Definition: sql_pg.c:59

◆ sql_begin_exclusive()

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().

599 {
600  sql ("BEGIN;");
601  sql ("SELECT pg_advisory_xact_lock (1);");
602 }
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
Here is the caller graph for this function:

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

Definition at line 610 of file sql_pg.c.

References sql_giveup(), sql_int(), and sql_rollback().

Referenced by sql_begin_immediate_giveup().

611 {
612  int ret;
613 
614  ret = sql_giveup ("BEGIN;");
615  if (ret)
616  return ret;
617  if (sql_int ("SELECT pg_try_advisory_xact_lock (1);"))
618  return 0;
619  sql_rollback ();
620  return 1;
621 }
int sql_int(char *sql,...)
Get a particular cell from a SQL query, as an int.
Definition: sql.c:438
int sql_giveup(char *sql,...)
Perform an SQL statement, giving up if database is busy or locked.
Definition: sql.c:257
void sql_rollback()
Roll a transaction back.
Definition: sql_pg.c:658
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sql_begin_immediate()

void sql_begin_immediate ( )

Begin an immediate transaction.

Definition at line 627 of file sql_pg.c.

References sql_begin_exclusive().

628 {
629  /* TODO This is just an exclusive lock. */
631 }
void sql_begin_exclusive()
Begin an exclusive transaction.
Definition: sql_pg.c:598
Here is the call graph for this function:

◆ sql_begin_immediate_giveup()

int sql_begin_immediate_giveup ( )

Begin an immediate transaction.

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

Definition at line 639 of file sql_pg.c.

References sql_begin_exclusive_giveup().

640 {
641  /* TODO This is just an exclusive lock. */
642  return sql_begin_exclusive_giveup ();
643 }
int sql_begin_exclusive_giveup()
Begin an exclusive transaction, giving up on failure.
Definition: sql_pg.c:610
Here is the call graph for this function:

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

Definition at line 751 of file sql_pg.c.

753 {
754  bind_param (stmt, position, value, value_size, 1);
755  return 0;
756 }

◆ sql_bind_double()

int sql_bind_double ( sql_stmt_t stmt,
int  position,
double *  value 
)

Bind a double value to a statement.

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

Definition at line 787 of file sql_pg.c.

788 {
789  gchar *string;
790  string = g_strdup_printf ("%f", *value);
791  bind_param (stmt, position, string, strlen (string), 0);
792  g_free (string);
793  return 0;
794 }

◆ sql_bind_int64()

int sql_bind_int64 ( sql_stmt_t stmt,
int  position,
long long int *  value 
)

Bind an int64 value to a statement.

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

Definition at line 768 of file sql_pg.c.

769 {
770  int actual;
771  /* Caller is really binding an int4, because IDs in Postgres are int4s. */
772  actual = *value;
773  bind_param (stmt, position, &actual, sizeof (actual), 1);
774  return 0;
775 }

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

Definition at line 807 of file sql_pg.c.

809 {
810  bind_param (stmt,
811  position,
812  value,
813  value_size == -1 ? strlen (value) : value_size,
814  0);
815  return 0;
816 }

◆ sql_cancel_internal()

int sql_cancel_internal ( )

Cancels the current SQL statement.

Returns
0 on success, -1 on error.

Definition at line 961 of file sql_pg.c.

References conn, and log_errors.

962 {
963  PGcancel *cancel;
964  char errbuf[256] = "";
965 
966  cancel = PQgetCancel (conn);
967  if (cancel)
968  {
969  if (PQcancel (cancel, errbuf, 256))
970  {
971  log_errors = 0;
972  PQfreeCancel (cancel);
973  }
974  else
975  {
976  PQfreeCancel (cancel);
977  return -1;
978  }
979  }
980  else
981  {
982  return -1;
983  }
984 
985  return 0;
986 }
int log_errors
Whether to log errors.
Definition: sql.c:62
PGconn * conn
Handle on the database.
Definition: sql_pg.c:81

◆ sql_changes()

int sql_changes ( )

Return 0.

Returns
0.

Definition at line 385 of file sql_pg.c.

386 {
387  /* TODO PQcmdTuples needs a PQresult. Callers use for info only anyway. */
388  return 0;
389 }

◆ sql_close()

void sql_close ( )

Close the database.

Definition at line 363 of file sql_pg.c.

References conn.

364 {
365  PQfinish (conn);
366  conn = NULL;
367 }
PGconn * conn
Handle on the database.
Definition: sql_pg.c:81

◆ sql_close_fork()

void sql_close_fork ( )

Close the database in a forked process.

Definition at line 373 of file sql_pg.c.

References conn.

374 {
375  // FIX PQfinish?
376  conn = NULL;
377 }
PGconn * conn
Handle on the database.
Definition: sql_pg.c:81

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

Definition at line 868 of file sql_pg.c.

References sql_stmt::current_row, and sql_stmt::result.

869 {
870  if (PQgetisnull (stmt->result, stmt->current_row, position))
871  return 0.0;
872 
873  return atof (PQgetvalue (stmt->result, stmt->current_row, position));
874 }
int current_row
Row position in results.
Definition: sql_pg.c:60
PGresult * result
Result set.
Definition: sql_pg.c:59

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

Definition at line 906 of file sql_pg.c.

References sql_stmt::current_row, and sql_stmt::result.

907 {
908  char *cell;
909 
910  if (PQgetisnull (stmt->result, stmt->current_row, position))
911  return 0;
912 
913  cell = PQgetvalue (stmt->result, stmt->current_row, position);
914 
915  switch (PQftype (stmt->result, position))
916  {
917  case 16: /* BOOLOID */
918  return strcmp (cell, "f") ? 1 : 0;
919 
920  default:
921  return atoi (cell);
922  }
923 }
int current_row
Row position in results.
Definition: sql_pg.c:60
PGresult * result
Result set.
Definition: sql_pg.c:59

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

Definition at line 936 of file sql_pg.c.

References sql_stmt::current_row, and sql_stmt::result.

Referenced by sqli().

937 {
938  char *cell;
939 
940  if (PQgetisnull (stmt->result, stmt->current_row, position))
941  return 0;
942 
943  cell = PQgetvalue (stmt->result, stmt->current_row, position);
944 
945  switch (PQftype (stmt->result, position))
946  {
947  case 16: /* BOOLOID */
948  return strcmp (cell, "f") ? 1 : 0;
949 
950  default:
951  return atol (cell);
952  }
953 }
int current_row
Row position in results.
Definition: sql_pg.c:60
PGresult * result
Result set.
Definition: sql_pg.c:59
Here is the caller graph for this function:

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

Definition at line 887 of file sql_pg.c.

References sql_stmt::current_row, and sql_stmt::result.

888 {
889  if (PQgetisnull (stmt->result, stmt->current_row, position))
890  return NULL;
891 
892  return (const char*) PQgetvalue (stmt->result, stmt->current_row, position);
893 }
int current_row
Row position in results.
Definition: sql_pg.c:60
PGresult * result
Result set.
Definition: sql_pg.c:59

◆ sql_commit()

void sql_commit ( )

Commit a transaction.

Definition at line 649 of file sql_pg.c.

References sql_stmt::sql.

650 {
651  sql ("COMMIT;");
652 }
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199

◆ sql_database()

const char* sql_database ( )

Return name of current database.

Returns
Name of database.

Definition at line 226 of file sql_pg.c.

References conn.

227 {
228  return PQdb (conn);
229 }
PGconn * conn
Handle on the database.
Definition: sql_pg.c:81

◆ sql_default_database()

const char* sql_default_database ( )

Return name of default database.

Returns
Name.

Definition at line 237 of file sql_pg.c.

238 {
239  return "tasks";
240 }

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

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().

483 {
484  PGresult *result;
485 
486  assert (stmt->sql);
487 
488  if (stmt->executed == 0)
489  {
490  // FIX retry?
491 
492  result = PQexecParams (conn,
493  stmt->sql,
494  stmt->param_values->len,
495  NULL, /* Default param types. */
496  (const char* const*) stmt->param_values->pdata,
497  (const int*) stmt->param_lengths->data,
498  (const int*) stmt->param_formats->data,
499  0); /* Results as text. */
500  if (PQresultStatus (result) != PGRES_TUPLES_OK
501  && PQresultStatus (result) != PGRES_COMMAND_OK)
502  {
503  char *sqlstate;
504 
505  sqlstate = PQresultErrorField (result, PG_DIAG_SQLSTATE);
506  g_debug ("%s: sqlstate: %s\n", __FUNCTION__, sqlstate);
507  if (sqlstate && (strcmp (sqlstate, "57014") == 0)) /* query_canceled */
508  {
509  log_errors = 0;
510  g_debug ("%s: canceled SQL: %s\n", __FUNCTION__, stmt->sql);
511  }
512 
513  if (log_errors)
514  {
515  g_warning ("%s: PQexec failed: %s (%i)\n",
516  __FUNCTION__,
517  PQresultErrorMessage (result),
518  PQresultStatus (result));
519  g_warning ("%s: SQL: %s\n", __FUNCTION__, stmt->sql);
520  }
521 #if 0
522  // FIX ?
523  PQclear (result);
524  PQfinish (conn);
525 #endif
526  return -1;
527  }
528 
529  stmt->result = result;
530  stmt->executed = 1;
531  }
532 
533  if (stmt->current_row < (PQntuples (stmt->result) - 1))
534  {
535  stmt->current_row++;
536  return 1;
537  }
538 
539  return 0;
540 }
int executed
Whether statement has been executed.
Definition: sql_pg.c:61
array_t * param_values
Parameter values.
Definition: sql_pg.c:62
int log_errors
Whether to log errors.
Definition: sql.c:62
GArray * param_lengths
Parameter lengths (int&#39;s).
Definition: sql_pg.c:63
gchar * sql
SQL statement.
Definition: sql_pg.c:58
int current_row
Row position in results.
Definition: sql_pg.c:60
GArray * param_formats
Parameter formats (int&#39;s).
Definition: sql_pg.c:64
PGconn * conn
Handle on the database.
Definition: sql_pg.c:81
PGresult * result
Result set.
Definition: sql_pg.c:59
Here is the caller graph for this function:

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

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().

552 {
553  char *explain_sql;
554  sql_stmt_t *explain_stmt;
555  int explain_ret;
556 
557  explain_sql = g_strconcat ("EXPLAIN ", sql, NULL);
558  if (sql_prepare_internal (1, 1, explain_sql, args, &explain_stmt))
559  {
560  if (log_errors)
561  g_warning ("%s : Failed to prepare EXPLAIN statement", __FUNCTION__);
562  g_free (explain_sql);
563  return -1;
564  }
565 
566  while (1)
567  {
568  explain_ret = sql_exec_internal (1, explain_stmt);
569  if (explain_ret == 1)
570  g_debug ("%s : %s",
571  __FUNCTION__,
572  PQgetvalue (explain_stmt->result,
573  explain_stmt->current_row,
574  0));
575  else if (explain_ret == 0)
576  break;
577  else
578  {
579  g_warning ("%s : Failed to get EXPLAIN row", __FUNCTION__);
580  sql_finalize (explain_stmt);
581  g_free (explain_sql);
582  return -1;
583  }
584  }
585 
586  sql_finalize (explain_stmt);
587  g_free (explain_sql);
588  return 0;
589 }
int sql_prepare_internal(int retry, int log, const char *sql, va_list args, sql_stmt_t **stmt)
Prepare a statement.
Definition: sql_pg.c:458
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
int log_errors
Whether to log errors.
Definition: sql.c:62
void sql_finalize(sql_stmt_t *stmt)
Free a prepared statement.
Definition: sql_pg.c:824
int sql_exec_internal(int retry, sql_stmt_t *stmt)
Execute a prepared statement.
Definition: sql_pg.c:482
int current_row
Row position in results.
Definition: sql_pg.c:60
PGresult * result
Result set.
Definition: sql_pg.c:59
Here is the call graph for this function:

◆ sql_finalize()

void sql_finalize ( sql_stmt_t stmt)

Free a prepared statement.

Parameters
[in]stmtStatement.

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().

825 {
826  PQclear (stmt->result);
827  g_free (stmt->sql);
828  array_free (stmt->param_values);
829  g_array_free (stmt->param_lengths, TRUE);
830  g_array_free (stmt->param_formats, TRUE);
831  g_free (stmt);
832 }
array_t * param_values
Parameter values.
Definition: sql_pg.c:62
GArray * param_lengths
Parameter lengths (int&#39;s).
Definition: sql_pg.c:63
gchar * sql
SQL statement.
Definition: sql_pg.c:58
GArray * param_formats
Parameter formats (int&#39;s).
Definition: sql_pg.c:64
PGresult * result
Result set.
Definition: sql_pg.c:59
Here is the caller graph for this function:

◆ sql_greatest()

const char* sql_greatest ( )

Get keyword for "greatest" SQL function.

Returns
Keyword.

Definition at line 114 of file sql_pg.c.

115 {
116  return "greatest";
117 }

◆ sql_ilike_op()

const char* sql_ilike_op ( )

Get case insensitive LIKE operator.

Returns
Like operator.

Definition at line 179 of file sql_pg.c.

180 {
181  return "ILIKE";
182 }

◆ sql_is_open()

int sql_is_open ( )

Check whether the database is open.

Returns
1 if open, else 0.

Definition at line 201 of file sql_pg.c.

References conn.

202 {
203  return conn ? 1 : 0;
204 }
PGconn * conn
Handle on the database.
Definition: sql_pg.c:81

◆ sql_is_sqlite3()

int sql_is_sqlite3 ( )

Get whether backend is SQLite3.

Returns
0.

Definition at line 92 of file sql_pg.c.

93 {
94  return 0;
95 }

◆ sql_last_insert_id()

resource_t sql_last_insert_id ( )

Get the ID of the last inserted row.

Definition at line 395 of file sql_pg.c.

References sql_int().

396 {
397  return sql_int ("SELECT LASTVAL ();");
398 }
int sql_int(char *sql,...)
Get a particular cell from a SQL query, as an int.
Definition: sql.c:438
Here is the call graph for this function:

◆ sql_open()

int sql_open ( const char *  database)

Open the database.

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

Definition at line 250 of file sql_pg.c.

251 {
252  gchar *conn_info;
253  PostgresPollingStatusType poll_status;
254  int socket;
255 
256  conn_info = g_strdup_printf ("dbname='%s' application_name='%s'",
257  database
258  ? database
260  "gvmd");
261  conn = PQconnectStart (conn_info);
262  g_free (conn_info);
263  if (conn == NULL)
264  {
265  g_warning ("%s: PQconnectStart failed to allocate conn\n",
266  __FUNCTION__);
267  return -1;
268  }
269  if (PQstatus (conn) == CONNECTION_BAD)
270  {
271  g_warning ("%s: PQconnectStart to '%s' failed: %s\n",
272  __FUNCTION__,
273  database ? database : sql_default_database (),
274  PQerrorMessage (conn));
275  goto fail;
276  }
277 
278  socket = PQsocket (conn);
279  if (socket == 0)
280  {
281  g_warning ("%s: PQsocket 0\n", __FUNCTION__);
282  goto fail;
283  }
284 
285  poll_status = PGRES_POLLING_WRITING;
286 
287  g_debug ("%s: polling\n", __FUNCTION__);
288 
289  while (1)
290  {
291  if (poll_status == PGRES_POLLING_READING)
292  {
293  fd_set readfds, writefds;
294  int ret;
295 
296  FD_ZERO (&readfds);
297  FD_ZERO (&writefds);
298  FD_SET (socket, &readfds);
299  ret = select (socket + 1, &readfds, &writefds, NULL, NULL);
300  if (ret == 0)
301  continue;
302  if (ret < 0)
303  {
304  g_warning ("%s: write select failed: %s\n",
305  __FUNCTION__, strerror (errno));
306  goto fail;
307  }
308  /* Poll again. */
309  }
310  else if (poll_status == PGRES_POLLING_WRITING)
311  {
312  fd_set readfds, writefds;
313  int ret;
314 
315  FD_ZERO (&readfds);
316  FD_ZERO (&writefds);
317  FD_SET (socket, &writefds);
318  ret = select (socket + 1, &readfds, &writefds, NULL, NULL);
319  if (ret == 0)
320  continue;
321  if (ret < 0)
322  {
323  g_warning ("%s: read select failed: %s\n",
324  __FUNCTION__, strerror (errno));
325  goto fail;
326  }
327  /* Poll again. */
328  }
329  else if (poll_status == PGRES_POLLING_FAILED)
330  {
331  g_warning ("%s: PQconnectPoll failed\n",
332  __FUNCTION__);
333  goto fail;
334  }
335  else if (poll_status == PGRES_POLLING_OK)
336  /* Connection is ready, exit loop. */
337  break;
338 
339  poll_status = PQconnectPoll (conn);
340  }
341 
342  PQsetNoticeProcessor (conn, log_notice, NULL);
343 
344  g_debug ("%s: db: %s\n", __FUNCTION__, PQdb (conn));
345  g_debug ("%s: user: %s\n", __FUNCTION__, PQuser (conn));
346  g_debug ("%s: host: %s\n", __FUNCTION__, PQhost (conn));
347  g_debug ("%s: port: %s\n", __FUNCTION__, PQport (conn));
348  g_debug ("%s: socket: %i\n", __FUNCTION__, PQsocket (conn));
349  g_debug ("%s: postgres version: %i\n", __FUNCTION__, PQserverVersion (conn));
350 
351  return 0;
352 
353  fail:
354  PQfinish (conn);
355  conn = NULL;
356  return -1;
357 }
const char * sql_default_database()
Return name of default database.
Definition: sql_pg.c:237
PGconn * conn
Handle on the database.
Definition: sql_pg.c:81

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

Definition at line 458 of file sql_pg.c.

Referenced by sql_explain_internal().

460 {
461  assert (stmt);
462 
463  *stmt = (sql_stmt_t*) g_malloc (sizeof (sql_stmt_t));
464  sql_stmt_init (*stmt);
465  (*stmt)->sql = g_strdup_vprintf (sql, args);
466 
467  if (log)
468  g_debug (" sql: %s\n", (*stmt)->sql);
469 
470  return 0;
471 }
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
Here is the caller graph for this function:

◆ sql_regexp_op()

const char* sql_regexp_op ( )

Get regular expression operator.

Returns
Regexp operator.

Definition at line 190 of file sql_pg.c.

191 {
192  return "?~#";
193 }

◆ sql_reset()

int sql_reset ( sql_stmt_t stmt)

Reset a prepared statement.

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

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.

843 {
844  gchar *sql;
845 
846  PQclear (stmt->result);
847  array_free (stmt->param_values);
848  g_array_free (stmt->param_lengths, TRUE);
849  g_array_free (stmt->param_formats, TRUE);
850 
851  sql = stmt->sql;
852  sql_stmt_init (stmt);
853  stmt->sql = sql;
854  return 0;
855 }
array_t * param_values
Parameter values.
Definition: sql_pg.c:62
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
GArray * param_lengths
Parameter lengths (int&#39;s).
Definition: sql_pg.c:63
gchar * sql
SQL statement.
Definition: sql_pg.c:58
GArray * param_formats
Parameter formats (int&#39;s).
Definition: sql_pg.c:64
PGresult * result
Result set.
Definition: sql_pg.c:59

◆ sql_rollback()

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().

659 {
660  sql ("ROLLBACK;");
661 }
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
Here is the caller graph for this function:

◆ sql_schema()

const char* sql_schema ( )

Get main schema name.

Returns
Schema name.

Definition at line 103 of file sql_pg.c.

104 {
105  return "public";
106 }

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

Definition at line 127 of file sql_pg.c.

128 {
129  static char string[20];
130  if (max < 0)
131  return "ALL";
132  if (snprintf (string, 19, "%i", max) < 0)
133  {
134  g_warning ("%s: snprintf failed\n", __FUNCTION__);
135  abort ();
136  }
137  string[19] = '\0';
138  return string;
139 }

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

Definition at line 385 of file sql.c.

Referenced by sql_double(), sql_int(), sql_int64(), sql_string(), and sqli().

386 {
387  return sql_x_internal (1, sql, args, stmt_return);
388 }
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
Here is the caller graph for this function:

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

Definition at line 408 of file sql_pg.c.

References sql_column_int64(), sql_finalize(), sql_x(), and sql_stmt::stmt.

409 {
410  gchar *new_sql;
411  sql_stmt_t* stmt;
412  int sql_x_ret;
413  va_list args;
414 
415  assert (sql && strlen (sql) && (sql[strlen (sql) - 1] != ';'));
416 
417  /* Append RETURNING clause to SQL. */
418 
419  new_sql = g_strdup_printf ("%s RETURNING id;", sql);
420 
421  /* Run statement, returning integer. */
422 
423  va_start (args, sql);
424  sql_x_ret = sql_x (new_sql, args, &stmt);
425  va_end (args);
426  g_free (new_sql);
427  switch (sql_x_ret)
428  {
429  case 0:
430  break;
431  case 1:
432  sql_finalize (stmt);
433  abort ();
434  default:
435  assert (0);
436  /* Fall through. */
437  case -1:
438  sql_finalize (stmt);
439  abort ();
440  }
441  if (resource)
442  *resource = sql_column_int64 (stmt, 0);
443  sql_finalize (stmt);
444 }
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
void sql_finalize(sql_stmt_t *stmt)
Free a prepared statement.
Definition: sql_pg.c:824
long long int sql_column_int64(sql_stmt_t *stmt, int position)
Return a column as an int64 from a prepared statement.
Definition: sql_pg.c:936
int sql_x(char *, va_list args, sql_stmt_t **)
Get a particular cell from a SQL query.
Definition: sql.c:385
Here is the call graph for this function:

Variable Documentation

◆ conn

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().

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

Definition at line 62 of file sql.c.

Referenced by next(), sql_cancel_internal(), sql_exec_internal(), sql_explain_internal(), sql_quiet(), and sqlv().