Warning, /sdk/codevis/thirdparty/soci/docs/backends/postgresql.md is written in an unsupported language. File is not indexed.
0001 # PostgreSQL Backend Reference
0002
0003 SOCI backend for accessing PostgreSQL database.
0004
0005 ## Prerequisites
0006
0007 ### Supported Versions
0008
0009 The SOCI PostgreSQL backend is supported for use with PostgreSQL >= 7.3, although versions older than 8.0 will suffer from limited feature support. See below for details.
0010
0011 ### Tested Platforms
0012
0013 |PostgreSQL|OS|Compiler|
0014 |--- |--- |--- |
0015 |9.6|Windows Server 2016|MSVC++ 14.1|
0016 |9.4|Windows Server 2012 R2|MSVC++ 14.0|
0017 |9.4|Windows Server 2012 R2|MSVC++ 12.0|
0018 |9.4|Windows Server 2012 R2|MSVC++ 11.0|
0019 |9.4|Windows Server 2012 R2|Mingw-w64/GCC 4.8|
0020 |9.3|Ubuntu 12.04|g++ 4.6.3|
0021 |9.0|Mac OS X 10.6.6|g++ 4.2|
0022 |8.4|FreeBSD 8.2|g++ 4.1|
0023 |8.4|Debian 6|g++ 4.3|
0024 |8.4|RedHat 5|g++ 4.3|
0025 |10.03|macOS High Sierra 10.13.5|AppleClang 9.1.0.9020039|
0026
0027 ### Required Client Libraries
0028
0029 The SOCI PostgreSQL backend requires PostgreSQL's `libpq` client library.
0030
0031 Note that the SOCI library itself depends also on `libdl`, so the minimum set of libraries needed to compile a basic client program is:
0032
0033 ```console
0034 -lsoci_core -lsoci_postgresql -ldl -lpq
0035 ```
0036
0037 ### Connecting to the Database
0038
0039 To establish a connection to the PostgreSQL database, create a `session` object using the `postgresql` backend factory together with a connection string:
0040
0041 ```cpp
0042 session sql(postgresql, "dbname=mydatabase");
0043
0044 // or:
0045 session sql("postgresql", "dbname=mydatabase");
0046
0047 // or:
0048 session sql("postgresql://dbname=mydatabase");
0049 ```
0050
0051 The set of parameters used in the connection string for PostgreSQL is the same as accepted by the [PQconnectdb](http://www.postgresql.org/docs/8.3/interactive/libpq.html#LIBPQ-CONNECT) function from the `libpq` library.
0052
0053 In addition to standard PostgreSQL connection parameters, the following can be set:
0054
0055 * `singlerow` or `singlerows`
0056
0057 For example:
0058
0059 ```cpp
0060 session sql(postgresql, "dbname=mydatabase singlerows=true");
0061 ```
0062
0063 If the `singlerows` parameter is set to `true` or `yes`, then queries will be executed in the single-row mode, which prevents the client library from loading full query result sets into memory and instead fetches rows one by one, as they are requested by the statement's fetch() function. This mode can be of interest to those users who want to make their client applications more responsive (with more fine-grained operation) by avoiding potentially long blocking times when complete query results are loaded to client's memory.
0064 Note that in the single-row operation:
0065
0066 * bulk queries are not supported, and
0067 * in order to fulfill the expectations of the underlying client library, the complete rowset has to be exhausted before executing further queries on the same session.
0068
0069 Also please note that single rows mode requires PostgreSQL 9 or later, both at
0070 compile- and run-time. If you need to support earlier versions of PostgreSQL,
0071 you can define `SOCI_POSTGRESQL_NOSINGLEROWMODE` when building the library to
0072 disable it.
0073
0074 Once you have created a `session` object as shown above, you can use it to access the database, for example:
0075
0076 ```cpp
0077 int count;
0078 sql << "select count(*) from invoices", into(count);
0079 ```
0080
0081 (See the [connection](../connections.md) and [data binding](../binding.md) documentation for general information on using the `session` class.)
0082
0083 ## SOCI Feature Support
0084
0085 ### Dynamic Binding
0086
0087 The PostgreSQL backend supports the use of the SOCI `row` class, which facilitates retrieval of data whose type is not known at compile time.
0088
0089 When calling `row::get<T>()`, the type you should pass as `T` depends upon the underlying database type. For the PostgreSQL backend, this type mapping is:
0090
0091 |PostgreSQL Data Type|SOCI Data Type|`row::get<T>` specializations|
0092 |--- |--- |--- |
0093 |numeric, real, double|dt_double|double|
0094 |boolean, smallint, integer|dt_integer|int|
0095 |int8|dt_long_long|long long|
0096 |oid|dt_integer|unsigned long|
0097 |char, varchar, text, cstring, bpchar|dt_string|std::string|
0098 |abstime, reltime, date, time, timestamp, timestamptz, timetz|dt_date|std::tm|
0099
0100 (See the [dynamic resultset binding](../types.md#dynamic-binding) documentation for general information on using the `row` class.)
0101
0102 ### Binding by Name
0103
0104 In addition to [binding by position](../binding.md#binding-by-position), the PostgreSQL backend supports [binding by name](../binding.md#binding-by-name), via an overload of the `use()` function:
0105
0106 ```cpp
0107 int id = 7;
0108 sql << "select name from person where id = :id", use(id, "id")
0109 ```
0110
0111 ### Bulk Operations
0112
0113 The PostgreSQL backend has full support for SOCI's [bulk operations](../binding.md#bulk-operations) interface.
0114
0115 ### Transactions
0116
0117 [Transactions](../transactions.md) are also fully supported by the PostgreSQL backend.
0118
0119 ### blob Data Type
0120
0121 The PostgreSQL backend supports working with data stored in columns of type Blob, via SOCI's [blob](../lobs.md) class with the exception that trimming is not supported.
0122
0123 ### rowid Data Type
0124
0125 The concept of row identifier (OID in PostgreSQL) is supported via SOCI's [rowid](../api/client.md#class-rowid) class.
0126
0127 ### Nested Statements
0128
0129 Nested statements are not supported by PostgreSQL backend.
0130
0131 ### Stored Procedures
0132
0133 PostgreSQL stored procedures can be executed by using SOCI's [procedure](../procedures.md) class.
0134
0135 ## Native API Access
0136
0137 SOCI provides access to underlying database APIs via several `get_backend()` functions, as described in the [beyond SOCI](../beyond.md) documentation.
0138
0139 The PostgreSQL backend provides the following concrete classes for native API access:
0140
0141 |Accessor Function|Concrete Class|
0142 |--- |--- |
0143 |session_backend * session::get_backend()|postgresql_session_backend|
0144 |statement_backend * statement::get_backend()|postgresql_statement_backend|
0145 |blob_backend * blob::get_backend()|postgresql_blob_backend|
0146 |rowid_backend * rowid::get_backend()|postgresql_rowid_backend|
0147
0148 ## Backend-specific extensions
0149
0150 ### uuid Data Type
0151
0152 The PostgreSQL backend supports working with data stored in columns of type UUID via simple string operations. All string representations of UUID supported by PostgreSQL are accepted on input, the backend will return the standard
0153 format of UUID on output. See the test `test_uuid_column_type_support` for usage examples.
0154
0155 ## Configuration options
0156
0157 To support older PostgreSQL versions, the following configuration macros are recognized:
0158
0159 * `SOCI_POSTGRESQL_NOSINLGEROWMODE` - disable single mode retrieving query results row-by-row. It is necessary for PostgreSQL prior to version 9.