Rust-oracle
This is an Oracle database driver for Rust based on ODPI-C.
Don't use this until the version number reaches to 0.1.0. Methods for executing SQL statements are almost ready for 0.1.0. However methods for establishing connections will be changed. Especially Connector may or may not be removed.
Methods for querying rows were changed in 0.0.4. If you had written
programs using rust-oracle before 0.0.4, enable the restore-deleted
feature in Cargo.toml
. It restores deleted methods and disables
statement-type checking in execute methods.
Change Log
0.0.5
New features:
- Add query methods to
Connection
to fetch rows without usingStatement
.Connection.query()
Connection.query_named()
Connection.query_as()
Connection.query_as_named()
- Add query_row methods to
Statement
to fetch a first row without usingResultSet
.Statement.query_row()
Statement.query_row_named()
Statement.query_row_as()
Statement.query_row_as_named()
Incompatible changes:
- Merge
RowResultSet
struct intoRowValueResultSet
and rename it toResultSet
.
0.0.4
New features:
- Add query methods to
Statement
to fetch rows as iterator.Statement.query()
Statement.query_named()
Statement.query_as()
Statement.query_as_named()
- Add query_row methods to
Connection
to fetch a first row without usingStatement
.Connection.query_row()
Connection.query_row_named()
Connection.query_row_as()
Connection.query_row_as_named()
- Autocommit mode.
Incompatible changes:
- Execute methods fail for select statements. Use query methods instead.
Connection.execute()
Connection.execute_named()
Statement.execute()
Statement.execute_named()
- Renamed traits, methods and variants.
ColumnValues
→RowValue
Row.values()
→Row.get_as()
Row.columns()
→Row.sql_values()
Error::Overflow
→Error::OutOfRange
- Removed methods.
- Statement.column_count()
- Statement.column_names()
- Statement.column_info()
- Statement.fetch()
- SqlValue.clone()
Build-time Requirements
- Rust 1.19 or later
- C compiler. See
Compile-time Requirements
in this document.
Run-time Requirements
- Oracle client 11.2 or later. See ODPI-C installation document.
Usage
Put this in your Cargo.toml
:
[dependencies]
oracle = "0.0.5"
When you need to fetch or bind chrono
data types, enable chrono
feature:
[dependencies]
oracle = { version = "0.0.5", features = ["chrono"] }
If you had written programs using rust-oracle before 0.0.4, try
the restore-deleted
feature. It restores deleted methods and
disables statement-type checking in execute methods.
[dependencies]
oracle = { version = "0.0.5", features = ["restore-deleted"] }
Then put this in your crate root:
extern crate oracle;
Examples
Executes select statements and get rows:
// Connect to a database.
let conn = new.unwrap;
let sql = "select ename, sal, comm from emp where deptno = :1";
// Select a table with a bind variable.
println!;
let rows = conn.query.unwrap;
for row_result in rows
// Another way to fetch rows.
// The rows iterator returns Result<(String, i32, Option<i32>)>.
println!;
let rows = conn..unwrap;
for row_result in rows
Executes select statements and get the first rows:
// Connect to a database.
let conn = new.unwrap;
let sql = "select ename, sal, comm from emp where empno = :1";
// Print the first row.
let row = conn.query_row.unwrap;
let ename: String = row.get.unwrap;
let sal: i32 = row.get.unwrap;
let comm: = row.get.unwrap;
println!;
println!;
// When no rows are found, conn.query_row() returns `Err(oracle::Error::NoMoreData)`.
// Get the first row as a tupple
let row = conn..unwrap;
println!;
println!;
Executes non-select statements:
// Connect to a database.
let conn = new.unwrap;
conn.execute.unwrap;
// Execute a statement with positional parameters.
conn.execute.unwrap;
// Execute a statement with named parameters.
conn.execute_named.unwrap;
// Commit the transaction.
conn.commit.unwrap;
// Delete rows
conn.execute.unwrap;
// Rollback the transaction.
conn.rollback.unwrap;
Prints column information:
// Connect to a database.
let conn = new.unwrap;
let sql = "select ename, sal, comm from emp where 1 = 2";
let rows = conn.query.unwrap;
// Print column names
for info in rows.column_info
println!;
// Print column types
for info in rows.column_info
println!;
Prepared statement:
let conn = new.unwrap;
// Create a prepared statement
let mut stmt = conn.prepare.unwrap;
// Insert one row
stmt.execute.unwrap;
// Insert another row
stmt.execute.unwrap;
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:
// The territory is France.
set_var;
let conn = new.unwrap;
// 10.1 is converted to a string in Oracle and fetched as a string.
let result = conn..unwrap;
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..unwrap;
assert_eq!; // The decimal mark is always period(.).
Note that NLS_LANG must be set before first rust-oracle function execution if required.
TODO
- Connection pooling
- Read and write LOB as stream
- REF CURSOR, BOOLEAN
- Scrollable cursors
- Batch DML
- DML returning
License
Rust-oracle itself is under 2-clause BSD-style license.
ODPI-C bundled in rust-oracle is under the terms of: