Warning, /sdk/codevis/thirdparty/soci/docs/backends/oracle.md is written in an unsupported language. File is not indexed.
0001 # Oracle Backend Reference 0002 0003 SOCI backend for accessing Oracle database. 0004 0005 ## Prerequisites 0006 0007 ### Supported Versions 0008 0009 The SOCI Oracle backend is currently supported for use with Oracle 10 or later. 0010 Older versions of Oracle may work as well, but they have not been tested by the SOCI team. 0011 0012 ### Tested Platforms 0013 0014 |Oracle|OS|Compiler| 0015 |--- |--- |--- | 0016 |10.2.0 (XE)|RedHat 5|g++ 4.3| 0017 |11.2.0 (XE)|Ubuntu 12.04|g++ 4.6.3| 0018 |12.2.0.1|macOS High Sierra 10.13.5|AppleClang 9.1.0.9020039| 0019 0020 ### Required Client Libraries 0021 0022 The SOCI Oracle backend requires Oracle's `libclntsh` client library. Depending on the particular system, the `libnnz10` library might be needed as well. 0023 0024 Note that the SOCI library itself depends also on `libdl`, so the minimum set of libraries needed to compile a basic client program is: 0025 0026 ```console 0027 -lsoci_core -lsoci_oracle -ldl -lclntsh -lnnz10 0028 ``` 0029 0030 ### Connecting to the Database 0031 0032 To establish a connection to an Oracle database, create a `session` object using the oracle backend factory together with a connection string: 0033 0034 ```cpp 0035 session sql(oracle, "service=orcl user=scott password=tiger"); 0036 0037 // or: 0038 session sql("oracle", "service=orcl user=scott password=tiger"); 0039 0040 // or: 0041 session sql("oracle://service=orcl user=scott password=tiger"); 0042 0043 // or: 0044 session sql(oracle, "service=//your_host:1521/your_sid user=scott password=tiger"); 0045 ``` 0046 0047 The set of parameters used in the connection string for Oracle is: 0048 0049 * `service` 0050 * `user` 0051 * `password` 0052 * `mode` (optional; valid values are `sysdba`, `sysoper` and `default`) 0053 * `charset` and `ncharset` (optional; valid values are `utf8`, `utf16`, `we8mswin1252` and `win1252`) 0054 0055 If both `user` and `password` are provided, the session will authenticate using the database credentials, whereas if none of them is set, then external Oracle credentials will be used - this allows integration with so called Oracle wallet authentication. 0056 0057 Once you have created a `session` object as shown above, you can use it to access the database, for example: 0058 0059 ```cpp 0060 int count; 0061 sql << "select count(*) from user_tables", into(count); 0062 ``` 0063 0064 (See the [connection](../connections.md) and [data binding](../binding.md) documentation for general information on using the `session` class.) 0065 0066 ## SOCI Feature Support 0067 0068 ### Dynamic Binding 0069 0070 The Oracle backend supports the use of the SOCI `row` class, which facilitates retrieval of data which type is not known at compile time. 0071 0072 When calling `row::get<T>()`, the type you should pass as `T` depends upon the underlying database type. For the Oracle backend, this type mapping is: 0073 0074 |Oracle Data Type|SOCI Data Type|`row::get<T>` specializations| 0075 |--- |--- |--- | 0076 |number (where scale > 0)|dt_double|double| 0077 |number(where scale = 0 and precision ≤ `std::numeric_limits<int>::digits10`)|dt_integer|int| 0078 |number|dt_long_long|long long| 0079 |char, varchar, varchar2|dt_string|std::string| 0080 |date|dt_date|std::tm| 0081 0082 (See the [dynamic resultset binding](../types.md#dynamic-binding) documentation for general information on using the `row` class.) 0083 0084 ### Binding by Name 0085 0086 In addition to [binding by position](../binding.md#binding-by-position), the Oracle backend supports [binding by name](../binding.md#binding-by-name), via an overload of the `use()` function: 0087 0088 ```cpp 0089 int id = 7; 0090 sql << "select name from person where id = :id", use(id, "id") 0091 ``` 0092 0093 SOCI's use of ':' to indicate a value to be bound within a SQL string is 0094 consistent with the underlying Oracle client library syntax. 0095 0096 ### Bulk Operations 0097 0098 The Oracle backend has full support for SOCI's [bulk operations](../binding.md#bulk-operations) interface. 0099 0100 ### Transactions 0101 0102 [Transactions](../statements.md#transactions) are also fully supported by 0103 the Oracle backend, although transactions with non-default isolation levels 0104 have to be managed by explicit SQL statements. 0105 0106 ### blob Data Type 0107 0108 The Oracle backend supports working with data stored in columns of type Blob, via SOCI's [blob](../lobs.md) class. 0109 0110 ### rowid Data Type 0111 0112 Oracle rowid's are accessible via SOCI's [rowid](../api/client.md#class-rowid) class. 0113 0114 ### Nested Statements 0115 0116 The Oracle backend supports selecting into objects of type `statement`, so that you may work with nested sql statements and PL/SQL cursors: 0117 0118 ```cpp 0119 statement stInner(sql); 0120 statement stOuter = (sql.prepare << 0121 "select cursor(select name from person order by id)" 0122 " from person where id = 1", 0123 into(stInner)); 0124 stInner.exchange(into(name)); 0125 stOuter.execute(); 0126 stOuter.fetch(); 0127 0128 while (stInner.fetch()) 0129 { 0130 std::cout << name << '\n'; 0131 } 0132 ``` 0133 0134 ### Stored Procedures 0135 0136 Oracle stored procedures can be executed by using SOCI's [procedure](../procedures.md) class. 0137 0138 ## Native API Access 0139 0140 SOCI provides access to underlying datbabase APIs via several `get_backend()` functions, as described in the [Beyond SOCI](../beyond.md) documentation. 0141 0142 The Oracle backend provides the following concrete classes for navite API access: 0143 0144 |Accessor Function|Concrete Class| 0145 |--- |--- | 0146 |session_backend * session::get_backend()|oracle_session_backend| 0147 |statement_backend * statement::get_backend()|oracle_statement_backend| 0148 |blob_backend * blob::get_backend()|oracle_blob_backend| 0149 |rowid_backend * rowid::get_backend()|oracle_rowid_backend| 0150 0151 ## Backend-specific extensions 0152 0153 ### oracle_soci_error 0154 0155 The Oracle backend can throw instances of class `oracle_soci_error`, which is publicly derived from `soci_error` and has an additional public `err_num_` member containing the Oracle error code: 0156 0157 ```cpp 0158 int main() 0159 { 0160 try 0161 { 0162 // regular code 0163 } 0164 catch (oracle_soci_error const & e) 0165 { 0166 cerr << "Oracle error: " << e.err_num_ 0167 << " " << e.what() << endl; 0168 } 0169 catch (exception const & e) 0170 { 0171 cerr << "Some other error: "<< e.what() << endl; 0172 } 0173 } 0174 ```