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.
New features:
Connection
to fetch rows without using Statement
.
Connection.query()
Connection.query_named()
Connection.query_as()
Connection.query_as_named()
Statement
to fetch a first row without using ResultSet
.
Statement.query_row()
Statement.query_row_named()
Statement.query_row_as()
Statement.query_row_as_named()
Incompatible changes:
RowResultSet
struct into RowValueResultSet
and rename it to ResultSet
.New features:
Statement
to fetch rows as iterator.
Statement.query()
Statement.query_named()
Statement.query_as()
Statement.query_as_named()
Connection
to fetch a first row without using Statement
.
Connection.query_row()
Connection.query_row_named()
Connection.query_row_as()
Connection.query_row_as_named()
Incompatible changes:
Connection.execute()
Connection.execute_named()
Statement.execute()
Statement.execute_named()
ColumnValues
→ RowValue
Row.values()
→ Row.get_as()
Row.columns()
→ Row.sql_values()
Error::Overflow
→ Error::OutOfRange
Compile-time Requirements
in this document.Put this in your Cargo.toml
:
text
[dependencies]
oracle = "0.0.5"
When you need to fetch or bind chrono
data types, enable chrono
feature:
text
[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.
text
[dependencies]
oracle = { version = "0.0.5", features = ["restore-deleted"] }
Then put this in your crate root:
rust
extern crate oracle;
Executes select statements and get rows:
```rust // Connect to a database. let conn = oracle::Connection::new("scott", "tiger", "//localhost/XE").unwrap();
let sql = "select ename, sal, comm from emp where deptno = :1";
// Select a table with a bind variable.
println!("---------------|---------------|---------------|");
let rows = conn.query(sql, &[&30]).unwrap();
for rowresult in rows {
let row = rowresult.unwrap();
// get a column value by position (0-based)
let ename: String = row.get(0).unwrap();
// get a column by name (case-insensitive)
let sal: i32 = row.get("sal").unwrap();
// Use Option<...>
to get a nullable column.
// Otherwise, Err(oracle::Error::NullValue)
is returned
// for null values.
let comm: Option
println!(" {:14}| {:>10} | {:>10} |",
ename,
sal,
comm.map_or("".to_string(), |v| v.to_string()));
}
// Another way to fetch rows.
// The rows iterator returns Result<(String, i32, Option
Executes select statements and get the first rows:
```rust // Connect to a database. let conn = oracle::Connection::new("scott", "tiger", "//localhost/XE").unwrap();
let sql = "select ename, sal, comm from emp where empno = :1";
// Print the first row.
let row = conn.queryrow(sql, &[&7369]).unwrap();
let ename: String = row.get("empno").unwrap();
let sal: i32 = row.get("sal").unwrap();
let comm: OptionErr(oracle::Error::NoMoreData)
.
// Get the first row as a tupple
let row = conn.queryrowas::<(String, i32, Option
Executes non-select statements:
```rust // Connect to a database. let conn = oracle::Connection::new("scott", "tiger", "//localhost/XE").unwrap();
conn.execute("create table person (id number(38), name varchar2(40))", &[]).unwrap();
// Execute a statement with positional parameters. conn.execute("insert into person values (:1, :2)", &[&1, // first parameter &"John" // second parameter ]).unwrap();
// Execute a statement with named parameters. conn.execute_named("insert into person values (:id, :name)", &[("id", &2), // 'id' parameter ("name", &"Smith"), // 'name' parameter ]).unwrap();
// Commit the transaction. conn.commit().unwrap();
// Delete rows conn.execute("delete from person", &[]).unwrap();
// Rollback the transaction. conn.rollback().unwrap(); ```
Prints column information:
```rust // Connect to a database. let conn = oracle::Connection::new("scott", "tiger", "//localhost/XE").unwrap();
let sql = "select ename, sal, comm from emp where 1 = 2"; let rows = conn.query(sql, &[]).unwrap();
// Print column names for info in rows.column_info() { print!(" {:14}|", info.name()); } println!("");
// Print column types for info in rows.columninfo() { print!(" {:14}|", info.oracletype().to_string()); } println!(""); ```
Prepared statement:
```rust let conn = oracle::Connection::new("scott", "tiger", "//localhost/XE").unwrap();
// Create a prepared statement let mut stmt = conn.prepare("insert into person values (:1, :2)").unwrap(); // Insert one row stmt.execute(&[&1, &"John"]).unwrap(); // Insert another row stmt.execute(&[&2, &"Smith"]).unwrap(); ```
This is more efficient than two conn.execute()
.
An SQL statement is executed in the DBMS as follows:
When a prepared statement is used, step 1 is called only once.
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:
```rust // The territory is France. std::env::setvar("NLSLANG", "french_france.AL32UTF8"); let conn = oracle::Connection::new("scott", "tiger", "").unwrap();
// 10.1 is converted to a string in Oracle and fetched as a string.
let result = conn.queryrowas::
// 10.1 is fetched as a number and converted to a string in rust-oracle
let result = conn.queryrowas::
Note that NLS_LANG must be set before first rust-oracle function execution if required.
Rust-oracle itself is under 2-clause BSD-style license.
ODPI-C bundled in rust-oracle is under the terms of: