- Notifications
You must be signed in to change notification settings - Fork46
kubo/rust-oracle
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This is anOracle database driver forRust based onODPI-C.
SeeChangeLog.md.
- C compiler. See
Compile-time Requirements
.
- Oracle client 11.2 or later. SeeODPI-C installation document.
The oracle crate supportsat least 6 rust minor versions including the stablerelease at the time when the crate was released. The MSRV (minimum supportedrust version) may be changed when a patch version is incremented though it willnot be changed frequently. The current MSRV is 1.68.0.
oracle crate version | MSRV |
---|---|
0.7.0 ~ | 1.68.0 |
0.6.0 ~ 0.6.3 | 1.60.0 |
0.5.5 ~ 0.5.7 | 1.54.0 |
Put this in yourCargo.toml
:
[dependencies]oracle = "0.6.3"
The following features can be enabled from Cargo.toml:
Feature | Description | available version |
---|---|---|
chrono | ImplementsToSql andFromSql forchrono data types. | any |
stmt_without_lifetime | Removesconn lifetime fromStatement . This is available to avoid lifetime conflicts. | 0.5.7 only |
aq_unstable | EnablesOracle Advanced Queuing support. This is unstable. It may be changed incompatibly by minor version upgrades. | since 0.5.5 |
Executes select statements and get rows:
use oracle::{Connection,Error};// Connect to a database.let conn =Connection::connect("scott","tiger","//localhost/XE")?;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])?;for row_resultin rows{let row = row_result?;// get a column value by position (0-based)let ename:String = row.get(0)?;// get a column by name (case-insensitive)let sal:i32 = row.get("sal")?;// Use `Option<...>` to get a nullable column.// Otherwise, `Err(Error::NullValue)` is returned// for null values.let comm:Option<i32> = row.get(2)?;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<i32>)>.println!("---------------|---------------|---------------|");let rows = conn.query_as::<(String,i32,Option<i32>)>(sql,&[&10])?;for row_resultin rows{let(ename, sal, comm) = row_result?;println!(" {:14}| {:>10} | {:>10} |", ename, sal, comm.map_or("".to_string(), |v| v.to_string()));}#Ok::<(), oracle::Error>(())
Executes select statements and get the first rows:
use oracle::Connection;// Connect to a database.let conn =Connection::connect("scott","tiger","//localhost/XE")?;let sql ="select ename, sal, comm from emp where empno = :1";// Print the first row.let row = conn.query_row(sql,&[&7369])?;let ename:String = row.get("empno")?;let sal:i32 = row.get("sal")?;let comm:Option<i32> = row.get("comm")?;println!("---------------|---------------|---------------|");println!(" {:14}| {:>10} | {:>10} |", ename, sal, comm.map_or("".to_string(), |v| v.to_string()));// When no rows are found, conn.query_row() returns `Err(Error::NoDataFound)`.// Get the first row as a tupplelet row = conn.query_row_as::<(String,i32,Option<i32>)>(sql,&[&7566])?;println!("---------------|---------------|---------------|");println!(" {:14}| {:>10} | {:>10} |", row.0, row.1, row.2.map_or("".to_string(), |v| v.to_string()));#Ok::<(), oracle::Error>(())
Executes non-select statements:
use oracle::Connection;// Connect to a database.let conn =Connection::connect("scott","tiger","//localhost/XE")?;conn.execute("create table person (id number(38), name varchar2(40))",&[])?;// Execute a statement with positional parameters.conn.execute("insert into person values (:1, :2)",&[&1,// first parameter&"John"// second parameter])?;// Execute a statement with named parameters.conn.execute_named("insert into person values (:id, :name)",&[("id",&2),// 'id' parameter("name",&"Smith"),// 'name' parameter])?;// Commit the transaction.conn.commit()?;// Delete rowsconn.execute("delete from person",&[])?;// Rollback the transaction.conn.rollback()?;#Ok::<(), oracle::Error>(())
Prints column information:
use oracle::Connection;// Connect to a database.let conn =Connection::connect("scott","tiger","//localhost/XE")?;let sql ="select ename, sal, comm from emp where 1 = 2";let rows = conn.query(sql,&[])?;// Print column namesfor infoin rows.column_info(){print!(" {:14}|", info.name());}println!("");// Print column typesfor infoin rows.column_info(){print!(" {:14}|", info.oracle_type().to_string());}println!("");#Ok::<(), oracle::Error>(())
Prepared statement:
use oracle::Connection;let conn =Connection::connect("scott","tiger","//localhost/XE")?;// Create a prepared statementletmut stmt = conn.statement("insert into person values (:1, :2)").build()?;// Insert one rowstmt.execute(&[&1,&"John"])?;// Insert another rowstmt.execute(&[&2,&"Smith"])?;#Ok::<(), oracle::Error>(())
This is more efficient than twoconn.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 consists of three components: language, territory andcharset. However the charset component is ignored and UTF-8(AL32UTF8) is usedas 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 oracle::Connection;// The territory is France.std::env::set_var("NLS_LANG","french_france.AL32UTF8");let conn =Connection::connect("scott","tiger","")?;// 10.1 is converted to a string in Oracle and fetched as a string.let result = conn.query_row_as::<String>("select to_char(10.1) from dual",&[])?;assert_eq!(result,"10,1");// The decimal mark depends on the territory.// 10.1 is fetched as a number and converted to a string in rust-oraclelet result = conn.query_row_as::<String>("select 10.1 from dual",&[])?;assert_eq!(result,"10.1");// The decimal mark is always period(.).#Ok::<(), oracle::Error>(())
Note that NLS_LANG must be set before first rust-oracle function execution ifrequired.
- 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
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 forDiesel
- include-oracle-sql: an extension ofinclude-sql usingSibyl for database access
- r2d2-oracle: Oracle support for ther2d2 connection pool
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.
About
Oracle driver for Rust
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.