Warning, /sdk/codevis/thirdparty/soci/docs/backends/odbc.md is written in an unsupported language. File is not indexed.
0001 # ODBC Backend Reference 0002 0003 SOCI backend for accessing variety of databases via ODBC API. 0004 0005 ## Prerequisites 0006 0007 ### Supported Versions 0008 0009 The SOCI ODBC backend is supported for use with ODBC 3. 0010 0011 ### Tested Platforms 0012 0013 |ODBC|OS|Compiler| 0014 |--- |--- |--- | 0015 |3|Linux (Ubuntu 12.04)|g++ 4.6.3| 0016 |3|Linux (Ubuntu 12.04)|clang 3.2| 0017 |3.8|Windows 8|Visual Studio 2012| 0018 |3|Windows 7|Visual Studio 2010| 0019 |3|Windows XP|Visual Studio 2005 (express)| 0020 |3|Windows XP|Visual C++ 8.0 Professional| 0021 |3|Windows XP|g++ 3.3.4 (Cygwin)| 0022 |3 (unixodbc 2.3.6)|macOS High Sierra 10.13.5|AppleClang 9.1.0.9020039| 0023 0024 ### Required Client Libraries 0025 0026 The SOCI ODBC backend requires the ODBC client library. 0027 0028 ## Connecting to the Database 0029 0030 To establish a connection to the ODBC database, create a Session object using the `ODBC` backend factory together with a connection string: 0031 0032 ```cpp 0033 backend_factory const& backEnd = odbc; 0034 session sql(backEnd, "filedsn=c:\\my.dsn"); 0035 ``` 0036 0037 or simply: 0038 0039 ```cpp 0040 session sql(odbc, "filedsn=c:\\my.dsn"); 0041 ``` 0042 0043 The set of parameters used in the connection string for ODBC is the same as accepted by the [SQLDriverConnect](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbcsql/od_odbc_d_4x4k.asp) function from the ODBC library. 0044 0045 Once you have created a `session` object as shown above, you can use it to access the database, for example: 0046 0047 ```cpp 0048 int count; 0049 sql << "select count(*) from invoices", into(count); 0050 ``` 0051 0052 (See the [connection](../connections.md) and [data binding](../binding.md) documentation for general information on using the `session` class.) 0053 0054 ## SOCI Feature Support 0055 0056 ### Dynamic Binding 0057 0058 The ODBC backend supports the use of the SOCI `row` class, which facilitates retrieval of data whose type is not known at compile time. 0059 0060 When calling `row::get<T>()`, the type you should pass as T depends upon the underlying database type. 0061 For the ODBC backend, this type mapping is: 0062 0063 |ODBC Data Type|SOCI Data Type|`row::get<T>` specializations| 0064 |--- |--- |--- | 0065 |SQL_DOUBLE, SQL_DECIMAL, SQL_REAL, SQL_FLOAT, SQL_NUMERIC|dt_double|double| 0066 |SQL_TINYINT, SQL_SMALLINT, SQL_INTEGER, SQL_BIGINT|dt_integer|int| 0067 |SQL_CHAR, SQL_VARCHAR|dt_string|std::string| 0068 |SQL_TYPE_DATE, SQL_TYPE_TIME, SQL_TYPE_TIMESTAMP|dt_date|std::tm| 0069 0070 Not all ODBC drivers support all datatypes. 0071 0072 (See the [dynamic resultset binding](../types.md#dynamic-binding) documentation for general information on using the `row` class.) 0073 0074 ### Binding by Name 0075 0076 In addition to [binding by position](../binding.md#binding-by-position), the ODBC backend supports [binding by name](../binding.md#binding-by-name), via an overload of the `use()` function: 0077 0078 ```cpp 0079 int id = 7; 0080 sql << "select name from person where id = :id", use(id, "id") 0081 ``` 0082 0083 Apart from the portable "colon-name" syntax above, which is achieved by rewriting the query string, the backend also supports the ODBC ? syntax: 0084 0085 ```cpp 0086 int i = 7; 0087 int j = 8; 0088 sql << "insert into t(x, y) values(?, ?)", use(i), use(j); 0089 ``` 0090 0091 ### Bulk Operations 0092 0093 The ODBC backend has support for SOCI's [bulk operations](../binding.md#bulk-operations) interface. Not all ODBC drivers support bulk operations, the following is a list of some tested backends: 0094 0095 |ODBC Driver|Bulk Read|Bulk Insert| 0096 |--- |--- |--- | 0097 |MS SQL Server 2005|YES|YES| 0098 |MS Access 2003|YES|NO| 0099 |PostgresQL 8.1|YES|YES| 0100 |MySQL 4.1|NO|NO| 0101 0102 ### Transactions 0103 0104 [Transactions](../transactions.md) are also fully supported by the ODBC backend, provided that they are supported by the underlying database. 0105 0106 ### BLOB Data Type 0107 0108 Not currently supported. 0109 0110 ### RowID Data Type 0111 0112 Not currently supported. 0113 0114 ### Nested Statements 0115 0116 Not currently supported. 0117 0118 ### Stored Procedures 0119 0120 Not currently supported. 0121 0122 ## Native API Access 0123 0124 SOCI provides access to underlying datbabase APIs via several getBackEnd() functions, as described in the [beyond SOCI](../beyond.md) documentation. 0125 0126 The ODBC backend provides the following concrete classes for navite API access: 0127 0128 |Accessor Function|Concrete Class| 0129 |--- |--- | 0130 |session_backend* session::get_backend()|odbc_session_backend| 0131 |statement_backend* statement::get_backend()|odbc_statement_backend| 0132 |rowid_backend* rowid::get_backend()|odbc_rowid_backend| 0133 0134 ## Backend-specific extensions 0135 0136 ### odbc_soci_error 0137 0138 The ODBC backend can throw instances of class `odbc_soci_error`, which is publicly derived from `soci_error` and has additional public members containing the ODBC error code, the Native database error code, and the message returned from ODBC: 0139 0140 ```cpp 0141 int main() 0142 { 0143 try 0144 { 0145 // regular code 0146 } 0147 catch (soci::odbc_soci_error const& e) 0148 { 0149 cerr << "ODBC Error Code: " << e.odbc_error_code() << endl 0150 << "Native Error Code: " << e.native_error_code() << endl 0151 << "SOCI Message: " << e.what() << std::endl 0152 << "ODBC Message: " << e.odbc_error_message() << endl; 0153 } 0154 catch (exception const &e) 0155 { 0156 cerr << "Some other error: " << e.what() << endl; 0157 } 0158 } 0159 ``` 0160 0161 ### get_connection_string() 0162 0163 The `odbc_session_backend` class provides `std::string get_connection_string() const` method 0164 that returns fully expanded connection string as returned by the `SQLDriverConnect` function. 0165 0166 ## Configuration options 0167 0168 This backend supports `odbc_option_driver_complete` option which can be passed to it via `connection_parameters` class. The value of this option is passed to `SQLDriverConnect()` function as "driver completion" parameter and so must be one of `SQL_DRIVER_XXX` values, in the string form. The default value of this option is `SQL_DRIVER_PROMPT` meaning that the driver will query the user for the user name and/or the password if they are not stored together with the connection. If this is undesirable for some reason, you can use `SQL_DRIVER_NOPROMPT` value for this option to suppress showing the message box: 0169 0170 ```cpp 0171 connection_parameters parameters("odbc", "DSN=mydb"); 0172 parameters.set_option(odbc_option_driver_complete, "0" /* SQL_DRIVER_NOPROMPT */); 0173 session sql(parameters); 0174 ```