PostgreSQL - Server Specific Guide

SQLAPI++ allows to work with a number of SQL database servers. It provides common mechanisms to access database, and as a general rule they work for any database server. But each server has some specific features which a developer has to know in order to leverage server's unique features and avoid potential errors.

This page collects all specific information that concerns working with PostreSQL server using SQLAPI++ Library. Full information about using SQLAPI++ see in How To and Documentation .

Available topics:

Connecting to a database

To connect to a database you should create a connection object and then connect it. A connection object is represented by SAConnection class. After the connection is created you need to connect it to PostreSQL server using SAConnection::Connect method

void Connect( const SAString &sDBString, const SAString &sUserID, const SAString &sPassword, SAClient_t eSAClient = SA_Client_NotSpecified);

with the following parameters:

*sDBString*.

A string describes PostgreSQL connection options, The format is "[[<server_name>]@][<database_name>][;<options>]":

  • "" or "@" - empty string or '@' character, connects to a local server.
  • "<database_name>" or "@ <database_name>" - connects to a database with the specified name on local server.
  • "<server_name>@" - connects to the specified server.
  • "<server_name>@<database_name>" - connects to a database with the specified name on the specified server.

<server_name> can be a cobination of hostname/address and port number (<server_name> = <host>[,<port>])) or unix_socket path.
<options> string used for PQsetdbLogin function pgoptions parameter (these are the server process parameters).
<database_name> string defines the database name for the connection but it can also define the client options like: "dbname=mydb connect_timeout=10".

*sUserID*.A string containing a user name to use when establishing the connection.
*sPassword*.A string containing a password to use when establishing the connection.
*eSAClient*.Optional. One of the following values from SAClient_t enum:
  • SA_PostgreSQL_Client PostgreSQL client.
  • SA_Client_NotSpecified Used by default if eSAClient parameter is omitted. You can use this default value only if you have SAConnection::setAPI method with SAPI object initialized with SA_PostgreSQL_Client constant before.

The SQLAPI++ Library requires libpq version 7.1.x or higher.

For more details see How To - Connecting to databases, SAConnection object, SAConnection::Connect.

Transaction isolation levels

SQL-92 defines four isolation levels, all of which are supported by SQLAPI++:

  • Read uncommitted (the lowest level where transactions are isolated just enough to ensure that physically corrupt data is not read)
  • Read committed
  • Repeatable read
  • Serializable (the highest level, where transactions are completely isolated from one another)

SQLAPI++ maps different isolation levels on PostgreSQL server in the following way:

SA_ReadUncommitted = 'READ UNCOMMITTED'
SA_ReadCommitted = 'READ COMMITTED'
SA_RepeatableRead = 'REPEATABLE READ'
SA_Serializable = 'SERIALIZABLE'

For more details see SAConnection::setIsolationLevel.

Working with Long or Lob(CLob, BLob) data

SQLAPI++ supports four types for working with Long or Lob(CLob, BLob) data:

Name C enum constant
LongBinary SA_dtLongBinary
LongChar SA_dtLongChar
BLob (Binary Large object) SA_dtBLob
CLob (Character Large object) SA_dtCLob

The table below shows how SQLAPI++ data types correspond with servers original data types:

SA_dtLongBinary <= > BYTEA
SA_dtLongChar <= > TEXT
SA_dtBLob <= > Large Object
SA_dtCLob <= > Large Object

Working with PostgreSQL Large Objects data (Oid field type) in PostgreSQL server has some particular features. Generally when you fetch Oid field data (which can point to any object, not necessary PostgreSQL Large Object) SQLAPI++ returns its value as a number (SQLAPI++ returns an object identifier). If you want to retrieve Oid field as PostgreSQL Large Object you should set command-related option OidTypeInterpretation to "LargeObject"value before the command execution. See SACommand::setOption for more detailes.

For more details see How To - Working with Long or Lob(CLob, BLob) data.

Returning output parameters

In PostgreSQL output parameters are available immediately after calling SACommand::Execute.

SQLAPI++ Library automatically creates SAParam object to represent function return value. You can refer to this SAParam object using SQLAPI++ predefined name "RETURN_VALUE". When PostgreSQL function has output parameters SQLAPI++ "RETURN_VALUE" parameter is not assigned.

For more details see SACommand::Execute, SAParam object, How To - Returning Output Parameters.

Cancelling queries

Using SACommand::Cancel method you can cancel the following types of processing on a statement:

  • A function running asynchronously on the statement.
  • A function running on the statement on another thread.

SQLAPI++ calls PQrequestCancel ** function to cancel a query. To get more details see **PQrequestCancel ** function description in **PostgreSQL documentation.

For more details see SACommand::Cancel.

Connection, command, parameter and field options

A server specific option can relate to a connection, command, parameter or field. We recommend you specify each option in an appropriate object, although it is possible to specify them in the parental object as well. In that case the option affects all the child objects.

An API related option must be specified in SAPI object. If an internal SAPI object used for the DBMS API initialization (implicit DBMS API initialization, see SAConnection::Connect method) the related DBMS specific options taken from initial connection object.

A connection related option may be specified in either SAPI object or SAConnection object. If it is specified in SAPI object it affects all the connections on that API.

A command related option may be specified in SAPI object, SAConnection object or SACommand object. If it is specified in a parent object it affects all the commands on that SAPI, SAConnection objects.

A parameter related option may be specified in SAPI object, SAConnection object, SACommand object or SAParam object. If it is specified in a parent object it affects all the parameters on that SAPI, SAConnection, SACommand objects.

A field related option may be specified in SAPI object, SAConnection object, SACommand object or SAField object. If it is specified in a parent object it affects all the fields on that on that SAPI, SAConnection, SACommand objects.

Specific options for PostgreSQL:

Option name / ScopeDescription

LIBPQ.LIBS

API related. Should be specified before the API is initialized.

Forces SQLAPI++ Library to use specified LIBPQ library.

Valid values : Any valid LIBPQ library name list. Names separated by ';' on Windows or ':' on other OS.
Default value : Windows - "libpq.dll;pq.dll", Linux - "libpq.so:libpq.so.5:libpq.so.4:libpq.so.3".
Special value : "STATIC", forces using the linked PostgreSQL client API functions when the Library compiled with SA_STATIC_PQSQL build option.

APPNAME

Connection related. Should be specified before connection is made.

Specifies the client application name (PostgreSQL 9.0 and higher 'application_name' configuration parameter).

Valid values : client application name string.
Default value: none.

ClientEncoding

Connection related. Should be specified before connection is made.

Uses PQsetClientEncoding to set up PostgreSQL client encoding (for non-Unicode SQLAPI++ version only).

Valid values : See PostgreSQL documentation.

OidTypeInterpretation

Command related. Should be specified before describing parameters or command execution.

Forces SQLAPI++ Library to interpret fields of Oid type as an object identifier or a Large Object. After you set "LargeObject" value to this option, SQLAPI++ interprets Oid type fields data as BLob data. Otherwise, it reads them as object identifiers (as ordinary numbers).

Valid values : "LargeObject" and "Oid".
Default value : "Oid".

PreFetchRows

Command related. Should be specified before command execution.

Forces SQLAPI++ Library to fetch rows in bulk, rather than retrieving records one by one.

Valid values : String containing number of rows in the fetch buffer.
Default value: "1".

UseDynamicCursor or
Scrollable

Command related. Should be specified before describing parameters or command execution.

Forces SQLAPI++ to use scrollable dynamic server side cursor.

Valid values : "True", "1".
Default value : "false".

UseCursor

Command related. Should be specified before describing parameters or command execution.

Forces SQLAPI++ to use server side cursor within "select .." statement.

Valid values : "True", "1".
Default value : "false".

SetCursorName

Command related. Should be specified before describing parameters or command execution.

Allows to define the cursor name SQLAPI++ uses for the server side cursor within "select .." statement.

Valid values : See PostgreSQL documentation.
Default value : none.

SNAPSHOT_ID

Command related. Should be specified before describing parameters or command execution.

Allows to define the transaction snapshot that SQLAPI++ uses within the next transaction block.

Valid values : See PostgreSQL documentation.
Default value : none.

For more details see SAConnection::setOption, SACommand::setOption, SAField::setOption, SAParam::setOption.

Getting native PostreSQL API

You can call client specific API functions which are not directly supported by SQLAPI++ Library. SAConnection::NativeAPI method returns a pointer to the set of native API functions available for PostreSQL. To use the database API directly you have to downcast this IsaAPI pointer to the appropriate type and use its implementation-specific members. The following table shows what type cast you have to make and what additional header file you have to include to work with PostreSQL API.

Type castingAdditional include file

Cast the result to class pgAPI:

IsaAPI *pResult = con.NativeAPI();
pgAPI *p_pgAPI = (pgAPI *)pResult;
#include "pgAPI.h"

To get more information about DBMS API functions see this DBMS specific documentation.

For more details see SAConnection::NativeAPI.

Getting native PostgreSQL connection related handles

You have to use native API handles when you want to call specific PostgreSQL API functions which are not directly supported by the Library. API functions usually need to receive one or more active handles as a parameter(s). SAConnection::NativeHandles method returns a pointer to the set of native API connection related handles. To use API handles directly you have to downcast saConnectionHandles pointer to the appropriate type and use its implementation-specific members. The following table shows what type cast you have to make and what additional header file you have to include to work with specific PostgreSQL API.

Type casting

Cast the result to class pgConnectionHandles:

#include "pgAPI.h"
saConnectionHandles *pResult = con.NativeHandles();
pgConnectionHandles *p_pgCH = (pgConnectionHandles*)pResult;

To get more information about DBMS API functions and handles see this DBMS specific documentation.

For more details see SAConnection::NativeHandles.

Getting native PostgreSQL command related handles

You have to use native API handles when you want to call specific PostgreSQL API functions which are not directly supported by the Library. API functions usually need to receive one or more active handles as a parameter(s). SACommand::NativeHandles method returns a pointer to the set of native API command related handles. To use API handles directly you have to downcast saCommandHandles pointer to the appropriate type and use its implementation-specific members. The following table shows what type cast you have to make and what additional header file you have to include to work with specific PostgreSQL API.

Type casting

Cast the result to class pgCommandHandles :

#include "pgAPI.h"
saCommandHandles *pResult = cmd.NativeHandles();
pgCommandHandles  *p_pg2CH = (pgCommandHandles *)pResult;

To get more information about DBMS API functions and handles see this DBMS specific documentation.

For more details see SACommand::NativeHandles.

Error handling

When an error occurs inside SQLAPI++ Library it throws an exception of type SAException.

SAException::ErrPos method gets an error position in SQL statement. In PostgreSQL server SAException::ErrPos method returns -1 because PostgreSQL does not support this function.

For more details see How To - Error handling, SAException object.

Special header files - Compile time

The header files are in the include subdirectory of SQLAPI++ distributions:

  • #include "SQLAPI.h" - main header, should be used whenever SQLAPI++ is used.
  • #include "pgAPI.h" - PostgreSQL API, should be included if direct LIBPQ calls are required.

For more details see Documentation - Instructions for Compiling and Linking Applications with SQLAPI++

If you haven't found the answer to your questions or have some problems on using the Library, please, send e-mail to howto@sqlapi.com
SQLAPI++ 5.0.1, Mon Oct 21 2019