Skip to content

Commit

Permalink
Merge pull request #700 from pangjunrong/feature/oracle-tnsnames-conn
Browse files Browse the repository at this point in the history
feat: Added Option for Oracle Connector to use Alias w/ Params
  • Loading branch information
wangxiaoying authored Oct 29, 2024
2 parents 124a3a6 + d08f6ce commit bd57460
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
17 changes: 13 additions & 4 deletions connectorx/src/sources/oracle/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod errors;
mod typesystem;

use std::collections::HashMap;

pub use self::errors::OracleSourceError;
pub use self::typesystem::OracleTypeSystem;
use crate::constants::{DB_BUFFER_SIZE, ORACLE_ARRAY_SIZE};
Expand Down Expand Up @@ -55,12 +57,19 @@ pub fn connect_oracle(conn: &Url) -> Connector {
let user = decode(conn.username())?.into_owned();
let password = decode(conn.password().unwrap_or(""))?.into_owned();
let host = decode(conn.host_str().unwrap_or("localhost"))?.into_owned();
let port = conn.port().unwrap_or(1521);
let path = decode(conn.path())?.into_owned();

let conn_str = format!("//{}:{}{}", host, port, path);
let params: HashMap<String, String> = conn.query_pairs().into_owned().collect();

let conn_str = if params.get("alias").map_or(false, |v| v == "true") {
host.clone()
} else {
let port = conn.port().unwrap_or(1521);
let path = decode(conn.path())?.into_owned();
format!("//{}:{}{}", host, port, path)
};

let mut connector = oracle::Connector::new(user.as_str(), password.as_str(), conn_str.as_str());
if user.is_empty() && password.is_empty() && host == "localhost" {
if user.is_empty() && password.is_empty() {
debug!("No username or password provided, assuming system auth.");
connector.external_auth(true);
}
Expand Down
12 changes: 12 additions & 0 deletions docs/databases/oracle.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Oracle

### System Authentication
```{hint}
If you want to use system authentication to access Oracle, username & password should not present in the connection string.
```

### Oracle Connection
```py
Expand All @@ -9,6 +13,14 @@ query = 'SELECT * FROM table' # query string
cx.read_sql(conn, query) # read data from Oracle
```

### Oracle TNS Alias (DNS) Connection
```py
import connectorx as cx
conn = 'oracle://username:password@alias_name?alias=true' # connection token
query = 'SELECT * FROM table' # query string
cx.read_sql(conn, query) # read data from Oracle
```

### Oracle-Pandas Type Mapping
| Oracle Type | Pandas Type | Comment |
|:-------------------------:|:---------------------------:|:----------------------------------:|
Expand Down

0 comments on commit bd57460

Please sign in to comment.