Veil
veil_query.c File Reference

Functions to simplify SPI-based queries. These are way more sophisticated than veil really needs but are nice and generic. More...

#include <stdio.h>
#include "postgres.h"
#include "catalog/pg_type.h"
#include "executor/spi.h"
#include "veil_version.h"
#include "access/xact.h"
#include "veil_funcs.h"

Go to the source code of this file.

Macros

#define FETCH_SIZE   20
 The number of records to fetch in one go from the query executor.
 

Typedefs

typedef bool() Fetch_fn(HeapTuple, TupleDesc, void *)
 A Fetch_fn is a function that processes records, one at a time, returned from a query.
 

Functions

int vl_spi_connect (bool *p_pushed)
 If already connected in this session, push the current connection, and get a new one. More...
 
int vl_spi_finish (bool pushed)
 Reciprocal function for vl_spi_connect()
 
static void prepare_query (const char *qry, int nargs, Oid *argtypes, Datum *args, bool read_only, void **saved_plan)
 Prepare a query for query(). More...
 
static int query (const char *qry, int nargs, Oid *argtypes, Datum *args, bool read_only, void **saved_plan, Fetch_fn process_row, void *fn_param)
 Prepare and execute a query. More...
 
static bool fetch_one_bool (HeapTuple tuple, TupleDesc tupdesc, void *p_result)
 Fetch_fn function for processing a single row of a single integer for query. More...
 
static bool fetch_one_str (HeapTuple tuple, TupleDesc tupdesc, void *p_result)
 Fetch_fn function for processing a single row of a single integer for query. More...
 
bool vl_bool_from_query (const char *qry, bool *result)
 Executes a query that returns a single bool value. More...
 
static bool str_from_oid_query (const char *qry, const Oid param, char *result)
 Executes a query by oid, that returns a single string value. More...
 
bool vl_db_exists (Oid db_id)
 Determine whether the given oid represents an existing database or not. More...
 
static bool exec_init_fn (HeapTuple tuple, TupleDesc tupdesc, void *p_param)
 Fetch_fn function for executing registered veil_init() functions for query. More...
 
int vl_call_init_fns (bool param)
 Identify any registered init_functions and execute them. More...
 

Detailed Description

Functions to simplify SPI-based queries. These are way more sophisticated than veil really needs but are nice and generic.

Author: Marc Munro
Copyright (c) 2005 - 2011 Marc Munro
License: BSD

Definition in file veil_query.c.

Function Documentation

◆ vl_spi_connect()

int vl_spi_connect ( bool *  p_pushed)

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

Definition at line 46 of file veil_query.c.

Referenced by ensure_init(), exec_init_fn(), veil_perform_reset(), and vl_call_init_fns().

◆ prepare_query()

static void prepare_query ( const char *  qry,
int  nargs,
Oid *  argtypes,
Datum *  args,
bool  read_only,
void **  saved_plan 
)
static

Prepare a query for query().

This creates and executes a plan. The caller must have established SPI_connect. It is assumed that no parameters to the query will be null.

Parameters
qryThe text of the SQL query to be performed.
nargsThe number of input parameters ($1, $2, etc) to the query
argtypesPointer to an array containing the OIDs of the data
argsActual parameters types of the parameters
read_onlyWhether the query should be read-only or not
saved_planAdress 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.

Definition at line 86 of file veil_query.c.

Referenced by query().

◆ query()

static int query ( const char *  qry,
int  nargs,
Oid *  argtypes,
Datum *  args,
bool  read_only,
void **  saved_plan,
Fetch_fn  process_row,
void *  fn_param 
)
static

Prepare and execute a query.

Query execution consists of a call to process_row for each returned record. Process_row can return a single value to the caller of this function through the fn_param parameter. It is the caller's responsibility to establish an SPI connection with SPI_connect. It is assumed that no parameters to the query, and no results will be null.

Parameters
qryThe text of the SQL query to be performed.
nargsThe number of input parameters ($1, $2, etc) to the query
argtypesPointer to an array containing the OIDs of the data
argsActual parameters types of the parameters
read_onlyWhether the query should be read-only or not
saved_planAdress 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.
process_rowThe Fetch_fn function to be called for each fetched row to process it. If this is null, we simply count the row, doing no processing on the tuples returned.
fn_paramAn optional parameter to the process_row function. This may be used to return a value to the caller.
Returns
The total number of records fetched and processed by process_row.

Definition at line 151 of file veil_query.c.

References prepare_query().

Referenced by str_from_oid_query(), vl_bool_from_query(), and vl_call_init_fns().

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

Parameters
tupleThe row to be processed
tupdescDescriptor for the types of the fields in the tuple.
p_resultPointer to an int4 variable into which the value returned from the query will be placed.
Returns
false. This causes query to terminate after processing a single row.

Definition at line 194 of file veil_query.c.

Referenced by vl_bool_from_query().

◆ fetch_one_str()

static bool fetch_one_str ( HeapTuple  tuple,
TupleDesc  tupdesc,
void *  p_result 
)
static

Fetch_fn function for processing a single row of a single integer for query.

Parameters
tupleThe row to be processed
tupdescDescriptor for the types of the fields in the tuple.
p_resultPointer to an int4 variable into which the value returned from the query will be placed.
Returns
false. This causes query to terminate after processing a single row.

Definition at line 214 of file veil_query.c.

Referenced by str_from_oid_query().

◆ vl_bool_from_query()

bool vl_bool_from_query ( const char *  qry,
bool *  result 
)

Executes a query that returns a single bool value.

Parameters
qryThe text of the query to be performed.
resultVariable into which the result of the query will be placed.
Returns
true if the query returned a record, false otherwise.

Definition at line 232 of file veil_query.c.

References fetch_one_bool(), and query().

Referenced by ensure_init(), exec_init_fn(), and veil_perform_reset().

◆ str_from_oid_query()

static bool str_from_oid_query ( const char *  qry,
const Oid  param,
char *  result 
)
static

Executes a query by oid, that returns a single string value.

Parameters
qryThe text of the query to be performed.
paramThe oid of the row to be fetched.
resultVariable into which the result of the query will be placed.
Returns
true if the query returned a record, false otherwise.

Definition at line 253 of file veil_query.c.

References fetch_one_str(), and query().

Referenced by vl_db_exists().

◆ vl_db_exists()

bool vl_db_exists ( Oid  db_id)

Determine whether the given oid represents an existing database or not.

Parameters
db_idOid of the database in which we are interested.
Returns
True if the database exists.

Definition at line 275 of file veil_query.c.

References str_from_oid_query().

Referenced by get_shmem_context().

◆ exec_init_fn()

static bool exec_init_fn ( HeapTuple  tuple,
TupleDesc  tupdesc,
void *  p_param 
)
static

Fetch_fn function for executing registered veil_init() functions for query.

Parameters
tupleThe row to be processed
tupdescDescriptor for the types of the fields in the tuple.
p_paramPointer to a boolean value which is the value of the argument to the init function being called.
Returns
true. This allows query to process further rows.

Definition at line 294 of file veil_query.c.

References vl_bool_from_query(), vl_spi_connect(), and vl_spi_finish().

Referenced by vl_call_init_fns().

◆ vl_call_init_fns()

int vl_call_init_fns ( bool  param)

Identify any registered init_functions and execute them.

Parameters
paramThe boolean parameter to be passed to each init_function.
Returns
The number of init_functions executed.

Definition at line 336 of file veil_query.c.

References exec_init_fn(), query(), vl_spi_connect(), and vl_spi_finish().

Referenced by veil_init().