Functions to simplify SPI-based queries.
More...
#include <stdio.h>
#include "postgres.h"
#include "catalog/pg_type.h"
#include "executor/spi.h"
#include "access/xact.h"
#include "veil2.h"
Go to the source code of this file.
|
void | veil2_spi_connect (bool *p_pushed, const char *msg) |
|
void | veil2_spi_finish (bool pushed, const char *msg) |
|
static void | prepare_query (const char *qry, int nargs, Oid *argtypes, Datum *args, const char *nulls, bool read_only, void **saved_plan) |
|
int | veil2_query_wn (const char *qry, int nargs, Oid *argtypes, Datum *args, const char *nulls, bool read_only, void **saved_plan, Fetch_fn process_row, void *fn_param) |
|
int | veil2_query (const char *qry, int nargs, Oid *argtypes, Datum *args, bool read_only, void **saved_plan, Fetch_fn process_row, void *fn_param) |
|
static bool | fetch_one_bool (HeapTuple tuple, TupleDesc tupdesc, void *p_result) |
|
bool | veil2_bool_from_query (const char *qry, int nargs, Oid *argtypes, Datum *args, void **saved_plan, bool *result) |
|
Functions to simplify SPI-based queries.
Author: Marc Munro
Copyright (c) 2020 Marc Munro
License: GPL V3
Definition in file query.c.
◆ fetch_one_bool()
static bool fetch_one_bool |
( |
HeapTuple |
tuple, |
|
|
TupleDesc |
tupdesc, |
|
|
void * |
p_result |
|
) |
| |
|
static |
Fetch_fn function for processing a single row of a single integer for veil2_query.
- Parameters
-
tuple | The row to be processed |
tupdesc | Descriptor for the types of the fields in the tuple. |
p_result | Pointer to an int4 variable into which the value returned from the query will be placed. |
- Returns
- false. This causes veil2_query to terminate after processing a single row.
Definition at line 242 of file query.c.
◆ prepare_query()
static void prepare_query |
( |
const char * |
qry, |
|
|
int |
nargs, |
|
|
Oid * |
argtypes, |
|
|
Datum * |
args, |
|
|
const char * |
nulls, |
|
|
bool |
read_only, |
|
|
void ** |
saved_plan |
|
) |
| |
|
static |
Prepare a query for veil2_query(). This creates and executes a plan. The caller must have established an SPI connection. It is assumed that no parameters to the query will be null.
- Parameters
-
qry | The text of the SQL query to be performed. |
nargs | The number of input parameters ($1, $2, etc) to the query |
argtypes | Pointer to an array containing the OIDs of the data |
args | Actual parameters types of the parameters |
nulls | String identifying which args are null. Null args contain 'n' in the appropriate character position, otherwise there will be a space. If no args may be null, a NULL value can be used instead of a string. |
read_only | Whether the query should be read-only or not |
saved_plan | Adress of void pointer into which the query plan will be saved. Passing the same void pointer on a subsequent call will cause the saved query plan to be re-used. This may be NULL, in which case the query plan will not be saved. |
Definition at line 101 of file query.c.
◆ veil2_bool_from_query()
bool veil2_bool_from_query |
( |
const char * |
qry, |
|
|
int |
nargs, |
|
|
Oid * |
argtypes, |
|
|
Datum * |
args, |
|
|
void ** |
saved_plan, |
|
|
bool * |
result |
|
) |
| |
Executes a query that returns a single bool value.
- Parameters
-
qry | The text of the query to be performed. |
nargs | The number of input parameters ($1, $2, etc) to the query |
argtypes | Pointer to an array containing the OIDs of the data |
args | Actual parameters |
saved_plan | Adress of void pointer into which the query plan will be saved. Passing the same void pointer on a subsequent call will cause the saved query plan to be re-used. This may be NULL, in which case the query plan will not be saved. |
result | Variable into which the result of the query will be placed. |
- Returns
- true if the query returned a record, false otherwise.
Definition at line 267 of file query.c.
◆ veil2_query()
int veil2_query |
( |
const char * |
qry, |
|
|
int |
nargs, |
|
|
Oid * |
argtypes, |
|
|
Datum * |
args, |
|
|
bool |
read_only, |
|
|
void ** |
saved_plan, |
|
|
Fetch_fn |
process_row, |
|
|
void * |
fn_param |
|
) |
| |
Execute a query (with all args being non-null) and process the results.
- Parameters
-
qry | The text of the SQL query to be performed. |
nargs | The number of input parameters ($1, $2, etc) to the query |
argtypes | Pointer to an array containing the OIDs of the data |
args | Actual parameters types of the parameters |
read_only | Whether the query should be read-only or not. |
saved_plan | Adress of void pointer into which the query plan will be saved. Passing the same void pointer on a subsequent call will cause the saved query plan to be re-used. This may be NULL, in which case the query plan will not be saved. |
process_row | A Fetch_fn() to process each tuple retruned by the query. |
fn_param | A parameter to pass to process_row. |
- Returns
- The number of rows processed.
Definition at line 218 of file query.c.
◆ veil2_query_wn()
int veil2_query_wn |
( |
const char * |
qry, |
|
|
int |
nargs, |
|
|
Oid * |
argtypes, |
|
|
Datum * |
args, |
|
|
const char * |
nulls, |
|
|
bool |
read_only, |
|
|
void ** |
saved_plan, |
|
|
Fetch_fn |
process_row, |
|
|
void * |
fn_param |
|
) |
| |
Execute a query with nulls (ie allowing null arguments) and process the results.
- Parameters
-
qry | The text of the SQL query to be performed. |
nargs | The number of input parameters ($1, $2, etc) to the query |
argtypes | Pointer to an array containing the OIDs of the data |
args | Actual parameters types of the parameters |
nulls | String identifying which args are null. Null args contain 'n' in the appropriate character position, otherwise there will be a space. If no args may be null, a NULL value can be used instead of a string. |
read_only | Whether the query should be read-only or not. |
saved_plan | Adress of void pointer into which the query plan will be saved. Passing the same void pointer on a subsequent call will cause the saved query plan to be re-used. This may be NULL, in which case the query plan will not be saved. |
process_row | A Fetch_fn() to process each tuple retruned by the query. |
fn_param | A parameter to pass to process_row. |
- Returns
- The number of rows processed.
Definition at line 165 of file query.c.
◆ veil2_spi_connect()
void veil2_spi_connect |
( |
bool * |
p_pushed, |
|
|
const char * |
msg |
|
) |
| |
If already connected in this session, push the current connection, and get a new one. We are already connected, if:
- are within a query
- and the current transaction id matches the saved transaction id
- Parameters
-
p_pushed | Pointer to a boolean into which we will record whether we have saved a presiously active SPI connection. This allows recursive queries, which is probably overkill for our needs, but since the overhead is low... |
msg | An error message string to be issued in the event of a failure. |
- Returns
- integer giving an SPI error code or success.
Definition at line 38 of file query.c.
◆ veil2_spi_finish()
void veil2_spi_finish |
( |
bool |
pushed, |
|
|
const char * |
msg |
|
) |
| |
Reciprocal function for veil2_spi_connect()
- Parameters
-
pushed | Boolean as set up by veil2_spi_connect(). This tells us whether to revert to a previously active SPI connection. |
msg | An error message string to be issued in the event of a failure. |
- Returns
- integer giving an SPI error code or success.
Definition at line 66 of file query.c.