oracle-rs
A pure Rust driver for Oracle databases. No OCI or ODPI-C dependencies required.
Features
- Pure Rust - No Oracle client libraries required
- Async/await - Built on Tokio for modern async applications
- TLS/SSL - Secure connections with certificate and wallet support
- Connection Pooling - Via the companion
deadpool-oraclecrate - Statement Caching - LRU cache for prepared statements
- Comprehensive Type Support - Including LOBs, JSON, VECTORs, and more
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["rt-multi-thread", "macros"] }
Basic usage:
use ;
async
Connection Options
Basic Connection
use ;
let config = new;
let conn = connect_with_config.await?;
TLS/SSL Connection
use Config;
let config = new
.with_tls?; // Use system root certificates
let conn = connect_with_config.await?;
Oracle Wallet (ewallet.pem)
use Config;
let config = new
.with_wallet?;
let conn = connect_with_config.await?;
Database Resident Connection Pooling (DRCP)
use Config;
let config = new
.with_drcp;
Statement Caching
use Config;
let config = new
.with_statement_cache_size; // Cache up to 100 statements
Query Execution
SELECT Queries
// Simple query
let result = conn.query.await?;
// With bind parameters
let result = conn.query.await?;
// Access rows
for row in result.rows
DML Operations
// INSERT
let result = conn.execute.await?;
println!;
// UPDATE
let result = conn.execute.await?;
// DELETE
let result = conn.execute.await?;
Batch Operations
use BatchBuilder;
let batch = new
.add_row?
.add_row?
.add_row?;
let result = conn.execute_batch.await?;
Transactions
// Auto-commit is off by default
conn.execute.await?;
conn.execute.await?;
// Commit the transaction
conn.commit.await?;
// Or rollback on error
conn.rollback.await?;
// Savepoints
conn.savepoint.await?;
conn.execute.await?;
conn.rollback_to_savepoint.await?; // Undo the update
PL/SQL
Anonymous Blocks
conn.execute.await?;
OUT Parameters
use ;
let mut out_value = null;
conn.execute_with_binds.await?;
let tax: f64 = out_value.try_into?;
REF CURSOR
let mut cursor = null;
conn.execute_with_binds.await?;
// Fetch from the cursor
let result = conn.fetch_ref_cursor.await?;
for row in result.rows
Data Types
Supported Types
| Oracle Type | Rust Type |
|---|---|
| NUMBER | i8, i16, i32, i64, f32, f64, String |
| VARCHAR2, CHAR | String, &str |
| DATE | chrono::NaiveDateTime |
| TIMESTAMP | chrono::NaiveDateTime |
| TIMESTAMP WITH TIME ZONE | chrono::DateTime<FixedOffset> |
| INTERVAL DAY TO SECOND | chrono::Duration |
| RAW | Vec<u8>, &[u8] |
| CLOB, NCLOB | String (auto-fetched) or streaming |
| BLOB | Vec<u8> (auto-fetched) or streaming |
| BOOLEAN | bool |
| JSON | serde_json::Value |
| VECTOR | Vec<f32>, Vec<f64>, Vec<i8> |
| ROWID | String |
| BINARY_FLOAT | f32 |
| BINARY_DOUBLE | f64 |
Working with LOBs
// Small LOBs are auto-fetched as String/Vec<u8>
let result = conn.query.await?;
let content: String = result.rows.get?;
// Large LOB streaming
let lob = conn.get_lob.await?;
let mut buffer = vec!;
while let Some = lob.read.await?
Working with JSON
use json;
// Insert JSON
let data = json!;
conn.execute.await?;
// Query JSON
let result = conn.query.await?;
let data: Value = result.rows.get?;
Working with VECTORs (Oracle 23ai)
// Insert vector embeddings
let embedding: = vec!;
conn.execute.await?;
// Vector similarity search
let query_vector: = get_embedding;
let result = conn.query.await?;
Connection Pooling
Use the deadpool-oracle crate for connection pooling:
[]
= "0.1"
= "0.1"
use Config;
use PoolBuilder;
let config = new;
let pool = new
.max_size
.build?;
// Get a connection from the pool
let conn = pool.get.await?;
// Use the connection
let result = conn.query.await?;
// Connection is automatically returned to the pool when dropped
Scrollable Cursors
// Create a scrollable cursor
let cursor = conn.create_scrollable_cursor.await?;
// Navigate the result set
let first = cursor.fetch_first.await?; // First 10 rows
let last = cursor.fetch_last.await?; // Last 10 rows
let abs = cursor.fetch_absolute.await?; // 10 rows starting at position 100
let rel = cursor.fetch_relative.await?; // 10 rows, 5 positions back from current
What's Not Yet Implemented
The following features are planned but not yet available:
- LONG/LONG RAW - Legacy deprecated types
- XMLType - XML document type
- Associative Arrays - PL/SQL INDEX BY tables
- Advanced Queuing (AQ) - Oracle message queuing
- Change Notifications (CQN) - Push notifications on data changes
- Sharding - Distributed database routing
- XA Transactions - Two-phase commit
- SODA - Document/NoSQL API
- Application Continuity - Transparent failover
Minimum Oracle Version
Oracle Database 12c Release 1 (12.1) or later. Some features require newer versions:
- Native BOOLEAN: Oracle 23c (emulated on earlier versions)
- JSON type: Oracle 21c (use CLOB with JSON on earlier versions)
- VECTOR type: Oracle 23ai
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Author
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.