Oracle DB

Oracle Database is a multi-model database management system produced and marketed by Oracle Corporation.

diesel_oci

diesel-oci diesel-oci-crates.io diesel-oci-github diesel-oci-lib.rs cat-database

A oci database adapter for diesel

// COMING SOON

oracle

oracle oracle-crates.io oracle-github oracle-lib.rs

Oracle bindings for Rust. This crate provides a safe and ergonomic interface to Oracle databases.

use std::env;

use dotenvy::dotenv;
use oracle::Connection;
use oracle::Error;

// Rust bindings to ODPI-C

// In `.env`:
// ORACLE_DB_USERNAME=your_username
// ORACLE_DB_PASSWORD=your_password
// ORACLE_DB_URL=your_db_host:port/your_service_name

#[tokio::main]
async fn main() -> Result<(), Error> {
    // Load environment variables from a .env file (for secure handling of
    // credentials)
    dotenv().ok();

    // Fetch the connection details from environment variables
    let username =
        env::var("ORACLE_DB_USERNAME").expect("ORACLE_DB_USERNAME not set");
    let password =
        env::var("ORACLE_DB_PASSWORD").expect("ORACLE_DB_PASSWORD not set");
    let db_url = env::var("ORACLE_DB_URL").expect("ORACLE_DB_URL not set");

    // Connect to the Oracle database
    let conn = Connection::connect(&username, &password, &db_url)?;

    // Query the database (example: retrieve the database name;
    // user_tables is a common system table in Oracle)
    let sql = "SELECT * FROM user_tables WHERE ROWNUM <= 5";
    let mut stmt = conn.statement(sql).build()?;

    // Execute the query and fetch results
    let rows = stmt.query(&[])?;

    // Iterate over the rows and print the results
    for row in rows {
        let row = row?;
        // Print each column's value
        // (assuming the result has one column, adjust as needed)
        println!("Table: {}", row.get::<_, String>(0)?);
    }

    conn.close()?;
    Ok(())
}

sibyl

sibyl-website sibyl sibyl-crates.io sibyl-github sibyl-lib.rs cat-database

sibyl offers an OCI-based (synchronous or asynchronous) interface between Rust applications and Oracle databases.


// // Sibyl is an OCI-based interface between Rust applications and Oracle
// // databases. Sibyl supports both blocking (threads) and nonblocking (async)
// // API.

// // To use Sibyl, you need to download the appropriate Instant Client packages
// // for your Linux distribution and architecture (usually 64-bit) from the
// // official Oracle website:
// // https://www.oracle.com/database/technologies/instant-client/downloads.html
// // Install the alien package: This tool is used to convert RPM packages
// // (which Oracle provides) to Debian packages suitable for Ubuntu.
// // sudo apt update
// // sudo apt install alien libaio1
// // Extract the downloaded ZIP files: Choose a suitable directory (e.g.,
// /opt/oracle) and extract the downloaded ZIP files there.
// // sudo mkdir -p /opt/oracle
// // sudo unzip instantclient-basic-linux.x64-<version>.zip -d /opt/oracle
// // sudo unzip instantclient-sqlplus-linux.x64-<version>.zip -d
// // /opt/oracle
// // Convert RPM packages to DEB packages (if applicable):
// // If you downloaded RPM packages, convert them to DEB using the `alien`
// // command.
// // sudo alien -i oracle-instantclient-<version>-basic-<version>.rpm
// // sudo alien -i oracle-instantclient-<version>-sqlplus-<version>.rpm
// // Install the DEB packages (if applicable):
// // sudo dpkg -i oracle-instantclient*.deb
// // Update the library path:
// // sudo sh -c 'echo /opt/oracle/instantclient_<version> >
// // /etc/ld.so.conf.d/oracle-instantclient.conf' // sudo ldconfig
// // Set environment variables (optional):
// // export PATH=$PATH:/opt/oracle/instantclient_<version> (for `sqlplus`)
// // export LD_LIBRARY_PATH=/opt/oracle/instantclient_<version>:
// // $LD_LIBRARY_PATH
// // Try connecting to your Oracle database using sqlplus:
// // sqlplus <username>/<password>@<hostname>:<port>/<service_name>

// // Define the environment variables:
// // DBNAME = "db"
// // DBUSER = "user"
// // DBPASS = "passwd"

// // In your Cargo.toml, add:
// // sibyl = { version = "0.6.18", features = [ "blocking" ] }

// use sibyl as oracle;
// fn main()  -> anyhow::Result<()> {
//     // Create an Oracle environment
//     let oracle = oracle::env()?;
//     // Get database credentials from environment variables.
//     let dbname = std::env::var("DBNAME").expect("database name");
//     let dbuser = std::env::var("DBUSER").expect("user name");
//     let dbpass = std::env::var("DBPASS").expect("password");

// // Establishes a connection to the database using the provided
// // credentials
// let session = oracle.connect(&dbname, &dbuser, &dbpass)?;

//     // Prepare the SQL statement
//     let stmt = session.prepare("SELECT first_name, last_name
//                                 FROM hr.employees
//                                 WHERE department_id = :department_id")?;

//     // Executes the prepared statement with the department ID set to 10 and
//     // retrieves a Rows iterator
//     let rows = stmt.query(&10)?;

//     while let Some(row) = rows.next()? {
//         let first_name: Option<&str> = row.get(0)?;
//         let last_name: Option<&str> = row.get(1)?;

//         if let (Some(first), Some(last)) = (first_name, last_name) {
//             println!("{} {}", first, last);
//         }
//     }

//     Ok(())
// }