Rust-oracle
This is an Oracle database driver for Rust based on ODPI-C.
Change Log
See ChangeLog.md.
Compile-time Requirements
- C compiler. See
Compile-time Requirements
.
Run-time Requirements
- Oracle client 11.2 or later. See ODPI-C installation document.
Supported Rust Versions
The oracle crate supports at least 6 rust minor versions including the stable release at the time when the crate was released. The MSRV (minimum supported rust version) may be changed when a patch version is incremented though it will not be changed frequently. The current MSRV is 1.60.0.
Usage
Put this in your Cargo.toml
:
[dependencies]
oracle = "0.6.3"
Optional Features
The following features can be enabled from Cargo.toml:
Feature | Description | available version |
---|---|---|
chrono |
Implements ToSql and FromSql for chrono data types. |
any |
stmt_without_lifetime |
Removes conn lifetime from Statement . This is available to avoid lifetime conflicts. |
0.5.7 only |
aq_unstable |
Enables Oracle Advanced Queuing support. This is unstable. It may be changed incompatibly by minor version upgrades. | since 0.5.5 |
Examples
Executes select statements and get rows:
use ;
// Connect to a database.
let conn = connect?;
let sql = "select ename, sal, comm from emp where deptno = :1";
// Select a table with a bind variable.
println!;
let rows = conn.query?;
for row_result in rows
// Another way to fetch rows.
// The rows iterator returns Result<(String, i32, Option<i32>)>.
println!;
let rows = conn.?;
for row_result in rows
# Ok::
Executes select statements and get the first rows:
use Connection;
// Connect to a database.
let conn = connect?;
let sql = "select ename, sal, comm from emp where empno = :1";
// Print the first row.
let row = conn.query_row?;
let ename: String = row.get?;
let sal: i32 = row.get?;
let comm: = row.get?;
println!;
println!;
// When no rows are found, conn.query_row() returns `Err(Error::NoDataFound)`.
// Get the first row as a tupple
let row = conn.?;
println!;
println!;
# Ok::
Executes non-select statements:
use Connection;
// Connect to a database.
let conn = connect?;
conn.execute?;
// Execute a statement with positional parameters.
conn.execute?;
// Execute a statement with named parameters.
conn.execute_named?;
// Commit the transaction.
conn.commit?;
// Delete rows
conn.execute?;
// Rollback the transaction.
conn.rollback?;
# Ok::
Prints column information:
use Connection;
// Connect to a database.
let conn = connect?;
let sql = "select ename, sal, comm from emp where 1 = 2";
let rows = conn.query?;
// Print column names
for info in rows.column_info
println!;
// Print column types
for info in rows.column_info
println!;
# Ok::
Prepared statement:
use Connection;
let conn = connect?;
// Create a prepared statement
let mut stmt = conn.statement.build?;
// Insert one row
stmt.execute?;
// Insert another row
stmt.execute?;
# Ok::
This is more efficient than two conn.execute()
.
An SQL statement is executed in the DBMS as follows:
- step 1. Parse the SQL statement and create an execution plan.
- step 2. Execute the plan with bind parameters.
When a prepared statement is used, step 1 is called only once.
NLS_LANG parameter
NLS_LANG consists of three components: language, territory and charset. However the charset component is ignored and UTF-8(AL32UTF8) is used as charset because rust characters are UTF-8.
The territory component specifies numeric format, date format and so on. However it affects only conversion in Oracle. See the following example:
use Connection;
// The territory is France.
set_var;
let conn = connect?;
// 10.1 is converted to a string in Oracle and fetched as a string.
let result = conn.?;
assert_eq!; // The decimal mark depends on the territory.
// 10.1 is fetched as a number and converted to a string in rust-oracle
let result = conn.?;
assert_eq!; // The decimal mark is always period(.).
# Ok::
Note that NLS_LANG must be set before first rust-oracle function execution if required.
TODO
- BFILEs (External LOBs) (Note: Reading contents of BFILEs as
Vec<u8>
is supported.) - Scrollable cursors
- Better Oracle object type support
- XML data type
- JSON data type
Related Projects
Other crates for connecting to Oracle:
- Sibyl: an OCI-based interface supporting both blocking (threads) and nonblocking (async) API
Oracle-related crates:
- bb8-oracle: bb8 connection pool support for oracle
- diesel-oci: A Oracle SQL database backend implementation for Diesel
- include-oracle-sql: an extension of include-sql using Sibyl for database access
- r2d2-oracle: Oracle support for the r2d2 connection pool
License
Rust-oracle is under the terms of:
- the Universal Permissive License v 1.0 or at your option, any later version; and/or
- the Apache License v 2.0.
Some of doc comments were copied from ODPI-C documentation verbatim. Oracle and/or its affiliates hold the copyright of the parts.