Last active
November 3, 2023 12:18
-
-
Save germanviscuso/ff97b37410c21d052f01e271c781f41f to your computer and use it in GitHub Desktop.
Code for rust-oracle to ADB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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