Skip to content

Instantly share code, notes, and snippets.

@germanviscuso
Last active November 3, 2023 12:18
Show Gist options
  • Save germanviscuso/ff97b37410c21d052f01e271c781f41f to your computer and use it in GitHub Desktop.
Save germanviscuso/ff97b37410c21d052f01e271c781f41f to your computer and use it in GitHub Desktop.
Code for rust-oracle to ADB
[package]
name = "adb_rust"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
oracle = "0.5.7"
[package]
name = "adb_rust_o"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
oracle = "0.5.7"
tokio = { version = "1", features = ["full"] }
[package]
name = "adb_rust_o"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
oracle = "0.5.7"
#!/bin/sh
# This example script sets up Oracle environment variables.
# Set TNS_ADMIN
export TNS_ADMIN='/Users/gviscuso/.oci/Wallet_IQRYYGS7ID28DBNW'
# Set Oracle user
export ORACLE_USER='ADMIN'
# Set Oracle password
# Note: If your password contains special characters, they need to be escaped or quoted.
export ORACLE_PASSWORD='Webinar1234!'
# Set Oracle TNS name
export ORACLE_TNSNAME='iqryygs7id28dbnw_high'
use oracle::{Result, Version};
use std::env;
fn main() -> Result<()> {
// Ensure that the TNS_ADMIN environment variable is set
let _tns_admin = env::var("TNS_ADMIN").expect("TNS_ADMIN environment variable is not set. Please configure it to point to your wallet.");
// Retrieve connection parameters from environment variables
let user = env::var("ORACLE_USER").expect("Expected ORACLE_USER environment variable not found.");
let password = env::var("ORACLE_PASSWORD").expect("Expected ORACLE_PASSWORD environment variable not found.");
let tnsname = env::var("ORACLE_TNSNAME").expect("Expected ORACLE_TNSNAME environment variable not found.");
// Establish connection using the retrieved environment variables
let conn = oracle::Connection::connect(user, password, tnsname)?; // TNS profile is in tnsnames.ora
let client_ver = Version::client()?;
println!("Oracle Client Version: {}", client_ver);
let (server_ver, banner) = conn.server_version()?;
println!("Oracle Server Version: {}", server_ver);
println!("--- Server Version Banner ---");
println!("{}", banner);
println!("-----------------------------");
Ok(())
}
use oracle;
use std::env;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Ensure that the TNS_ADMIN environment variable is set
let _tns_admin = env::var("TNS_ADMIN").expect("TNS_ADMIN environment variable is not set. Please configure it to point to your wallet.");
// Retrieve connection parameters from environment variables
let user = env::var("ORACLE_USER").expect("Expected ORACLE_USER environment variable not found.");
let password = env::var("ORACLE_PASSWORD").expect("Expected ORACLE_PASSWORD environment variable not found.");
let tnsname = env::var("ORACLE_TNSNAME").expect("Expected ORACLE_TNSNAME environment variable not found.");
// Establish connection using the retrieved environment variables
let conn = oracle::Connection::connect(user, password, tnsname)?; // TNS profile is in tnsnames.ora
let sql = "
SELECT
NAME,
ROUND(SUM(ACTUAL_PRICE), 0) AS SALES
FROM CUSTSALES C, GENRE G
WHERE C.GENRE_ID = G.GENRE_ID
GROUP BY NAME
ORDER BY SALES DESC
FETCH FIRST :TOP_SALES ROWS ONLY
";
let mut stmt = conn.statement(sql).build()?;
let rows = stmt.query(&[&5])?; // &[] if query requires no arguments
// Get the column names
for info in rows.column_info() {
print!("{} ", info.name())
}
println!("");
// Display the resultset
for row_result in rows {
let row = row_result?;
let name: String = row.get(0)?;
let sales: f64 = row.get(1)?;
println!("{:10}: {:>5}", name, sales);
}
conn.close()?;
Ok(())
}
use oracle;
use std::env;
use tokio::task;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
task::spawn_blocking(|| {
// Ensure that the TNS_ADMIN environment variable is set
let _tns_admin = env::var("TNS_ADMIN").expect("TNS_ADMIN environment variable is not set. Please configure it to point to your wallet.");
// Retrieve connection parameters from environment variables
let user = env::var("ORACLE_USER").expect("Expected ORACLE_USER environment variable not found.");
let password = env::var("ORACLE_PASSWORD").expect("Expected ORACLE_PASSWORD environment variable not found.");
let tnsname = env::var("ORACLE_TNSNAME").expect("Expected ORACLE_TNSNAME environment variable not found.");
// Establish connection using the retrieved environment variables
let conn = oracle::Connection::connect(user, password, tnsname)?; // TNS profile is in tnsnames.ora
let sql = "
SELECT
NAME,
ROUND(SUM(ACTUAL_PRICE), 0) AS SALES
FROM CUSTSALES C, GENRE G
WHERE C.GENRE_ID = G.GENRE_ID
GROUP BY NAME
ORDER BY SALES DESC
FETCH FIRST :TOP_SALES ROWS ONLY
";
let mut stmt = conn.statement(sql).build()?;
let rows = stmt.query(&[&5])?;
// Get the column names
for info in rows.column_info() {
print!("{} ", info.name())
}
println!("");
for row_result in rows {
let row = row_result?;
let name: String = row.get(0)?;
let sales: f64 = row.get(1)?;
println!("{:10}: {:>5}", name, sales);
}
conn.close()?;
Ok::<(), oracle::Error>(())
}).await??;
Ok(())
}
use oracle::{Connection, Result};
use std::env;
fn main() -> Result<()> {
// Ensure that the TNS_ADMIN environment variable is set
let _tns_admin = env::var("TNS_ADMIN").expect("TNS_ADMIN environment variable is not set. Please configure it to point to your wallet.");
// Retrieve connection parameters from environment variables
let user = env::var("ORACLE_USER").expect("Expected ORACLE_USER environment variable not found.");
let password = env::var("ORACLE_PASSWORD").expect("Expected ORACLE_PASSWORD environment variable not found.");
let tnsname = env::var("ORACLE_TNSNAME").expect("Expected ORACLE_TNSNAME environment variable not found.");
// Establish connection using the retrieved environment variables
let conn = oracle::Connection::connect(user, password, tnsname)?; // TNS profile is in tnsnames.ora
let plsql = "
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE T';
EXCEPTION WHEN OTHERS
THEN NULL;
END;";
let table_t = "
CREATE TABLE T (
ID NUMBER NOT NULL PRIMARY KEY,
ADDR VARCHAR(64),
PRICE NUMBER(6,2)
)";
// Unconditionally drop table T
conn.execute(plsql, &[])?;
// Create table T
conn.execute(table_t, &[])?;
// Data to insert
let data = [
(1, "Seasame Street", 42.69),
(2, "500 Oracle Parkway", 10.00),
(3, "3rd Ave", 1049.27),
];
let inssql = "INSERT INTO T(ID, ADDR, PRICE) VALUES (:ID, :ADDR, :PRICE)";
// Prepare the statement
let mut insstmt = conn.statement(inssql).build()?;
// insert rows using positional parameters
for d in &data {
insstmt.execute(&[&d.0, &d.1, &d.2])?;
}
conn.commit()?;
// Display the resultset
println!("\nRows in table T after the inserts");
display_rows( &conn )?;
// Update some rows
conn.execute("UPDATE T SET ADDR = 'Where the streets have no name'", &[])?;
conn.commit()?;
println!("\nRows in table T after the updates");
display_rows( &conn )?;
// Delete a row
conn.execute("DELETE FROM T WHERE ID = 3", &[])?;
conn.commit()?;
println!("\nRows in table T after the delete");
display_rows( &conn )?;
conn.commit()?;
conn.close()?;
Ok(())
}
fn display_rows (conn: &Connection) -> Result<()> {
let sql = "SELECT * FROM T";
let mut stmt = conn.statement(sql).build()?;
let rows = stmt.query(&[])?;
for row_result in rows {
for (idx, val) in row_result?.sql_values().iter().enumerate() {
if idx != 0 {
print!(",");
}
print!("{}", val);
}
println!();
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment