Skip to content

Commit 8014e35

Browse files
committed
Add Connection::info(), conn::Info and conn::ServerType to get connection information
1 parent 068d594 commit 8014e35

File tree

3 files changed

+122
-3
lines changed

3 files changed

+122
-3
lines changed

ChangeLog.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## 0.6.3 (2024-XX-XX)
44

5+
New features:
6+
7+
* Add [`Connection::info()`], [`conn::Info`] and [`conn::ServerType`] to get connection information
8+
59
Fixed Issues:
610

711
* Fix panic when invalid argument Error is displayed
@@ -472,12 +476,15 @@ Incompatible changes:
472476
[`Collection::values()`]: https://www.jiubao.org/rust-oracle/oracle/sql_type/struct.Collection.html#method.values
473477
[`ColumnIndex`]: https://www.jiubao.org/rust-oracle/oracle/trait.ColumnIndex.html
474478
[`ColumnInfo::name()`]: https://www.jiubao.org/rust-oracle/oracle/struct.ColumnInfo.html#method.name
479+
[`conn::Info`]: https://www.jiubao.org/rust-oracle/oracle/conn/struct.Info.html
480+
[`conn::ServerType`]: https://www.jiubao.org/rust-oracle/oracle/conn/enum.ServerType.html
475481
[`Connection`]: https://www.jiubao.org/rust-oracle/oracle/struct.Connection.html
476482
[`Connection::connect()`]: https://www.jiubao.org/rust-oracle/oracle/struct.Connection.html#method.connect
477483
[`Connection::call_timeout()`]: https://www.jiubao.org/rust-oracle/oracle/struct.Connection.html#method.call_timeout
478484
[`Connection::close_with_mode()`]: https://www.jiubao.org/rust-oracle/oracle/struct.Connection.html#method.close_with_mode
479485
[`Connection::execute()`]: https://www.jiubao.org/rust-oracle/oracle/struct.Connection.html#method.execute
480486
[`Connection::execute_named()`]: https://www.jiubao.org/rust-oracle/oracle/struct.Connection.html#method.execute_named
487+
[`Connection::info()`]: https://www.jiubao.org/rust-oracle/oracle/struct.Connection.html#method.info
481488
[`Connection::is_new_connection()`]: https://www.jiubao.org/rust-oracle/oracle/struct.Connection.html#method.is_new_connection
482489
[`Connection::last_warning()`]: https://www.jiubao.org/rust-oracle/oracle/struct.Connection.html#method.last_warning
483490
[`Connection::object_type()`]: https://www.jiubao.org/rust-oracle/oracle/struct.Connection.html#method.object_type

src/conn.rs

+91-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// URL: https://github.com/kubo/rust-oracle
44
//
55
//-----------------------------------------------------------------------------
6-
// Copyright (c) 2017-2021 Kubo Takehiro <[email protected]>. All rights reserved.
6+
// Copyright (c) 2017-2024 Kubo Takehiro <[email protected]>. All rights reserved.
77
// This program is free software: you can modify it and/or redistribute it
88
// under the terms of:
99
//
@@ -17,8 +17,11 @@
1717
//!
1818
//! Some types at the top-level module will move here in future.
1919
use crate::binding::*;
20+
use crate::to_rust_str;
2021
#[cfg(doc)]
2122
use crate::Connection;
23+
use crate::Error;
24+
use crate::Result;
2225

2326
/// The mode to use when closing connections to the database
2427
///
@@ -55,3 +58,90 @@ impl Purity {
5558
}
5659
}
5760
}
61+
62+
/// The type of server process associated with a connection
63+
///
64+
/// It is only available with Oracle Client libraries 23.4 or higher.
65+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
66+
pub enum ServerType {
67+
/// A dedicated server process is being used with the connection.
68+
Dedicated,
69+
/// A pooled server process (DRCP) is being used with the connection.
70+
Pooled,
71+
/// A shared server process is being used with the connection.
72+
Shared,
73+
/// The type of server process is unknown.
74+
Unknown,
75+
}
76+
77+
impl ServerType {
78+
pub(crate) fn from_dpi(server_type: u8) -> Result<ServerType> {
79+
match server_type as u32 {
80+
DPI_SERVER_TYPE_DEDICATED => Ok(ServerType::Dedicated),
81+
DPI_SERVER_TYPE_POOLED => Ok(ServerType::Pooled),
82+
DPI_SERVER_TYPE_SHARED => Ok(ServerType::Shared),
83+
DPI_SERVER_TYPE_UNKNOWN => Ok(ServerType::Unknown),
84+
_ => Err(Error::internal_error(format!(
85+
"Unknown dpiServerType {}",
86+
server_type
87+
))),
88+
}
89+
}
90+
}
91+
92+
/// Information about a connection
93+
///
94+
/// This is a return value of [`Connection::info()`].
95+
#[non_exhaustive]
96+
#[derive(Debug, Clone, PartialEq)]
97+
pub struct Info {
98+
/// The name of the Oracle Database Domain name associated with the connection
99+
///
100+
/// This is the same value returned by the SQL expression
101+
/// `SELECT VALUE FROM V$PARAMETER WHERE NAME = 'db_domain'`.
102+
pub db_domain: String,
103+
104+
/// The Oracle Database name associated with the connection
105+
pub db_name: String,
106+
107+
/// The Oracle Database instance name associated with the connection
108+
pub instance_name: String,
109+
110+
/// The Oracle Database service name associated with the connection
111+
///
112+
/// This is the same value returned by the SQL expression
113+
/// `SELECT SYS_CONTEXT('USERENV', 'SERVICE_NAME') FROM DUAL`.
114+
pub service_name: String,
115+
116+
/// The maximum length of identifiers (in bytes) supported by the
117+
/// database to which the connection has been established
118+
///
119+
/// See [Database Object Naming Rules](https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-75337742-67FD-4EC0-985F-741C93D918DA).
120+
pub max_identifier_length: u32,
121+
122+
/// The maximum number of cursors that can be opened
123+
///
124+
/// This is the same value returned by the SQL expression
125+
/// `SELECT VALUE FROM V$PARAMETER WHERE NAME = 'open_cursors'`.
126+
pub max_open_cursors: u32,
127+
128+
/// The type of server process used by the connection
129+
///
130+
/// This is only available with Oracle Client libraries 23.4 or higher.
131+
/// Otherwise, it is always `ServerType::Unknown`.
132+
pub server_type: ServerType,
133+
}
134+
135+
impl Info {
136+
pub(crate) fn from_dpi(info: &dpiConnInfo) -> Result<Info> {
137+
Ok(Info {
138+
db_domain: to_rust_str(info.dbDomain, info.dbDomainLength),
139+
db_name: to_rust_str(info.dbName, info.dbNameLength),
140+
instance_name: to_rust_str(info.instanceName, info.instanceNameLength),
141+
service_name: to_rust_str(info.serviceName, info.serviceNameLength),
142+
max_identifier_length: info.maxIdentifierLength,
143+
max_open_cursors: info.maxOpenCursors,
144+
server_type: ServerType::from_dpi(info.serverType)?,
145+
})
146+
}
147+
}

src/connection.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// URL: https://github.com/kubo/rust-oracle
44
//
55
//-----------------------------------------------------------------------------
6-
// Copyright (c) 2017-2021 Kubo Takehiro <[email protected]>. All rights reserved.
6+
// Copyright (c) 2017-2024 Kubo Takehiro <[email protected]>. All rights reserved.
77
// This program is free software: you can modify it and/or redistribute it
88
// under the terms of:
99
//
@@ -25,7 +25,7 @@ use std::time::Duration;
2525

2626
use crate::binding::*;
2727
use crate::chkerr;
28-
use crate::conn::{CloseMode, Purity};
28+
use crate::conn::{CloseMode, Info, Purity};
2929
use crate::error::DPI_ERR_NOT_CONNECTED;
3030
use crate::oci_attr::data_type::{AttrValue, DataType};
3131
use crate::oci_attr::handle::ConnHandle;
@@ -1740,6 +1740,28 @@ impl Connection {
17401740
self.conn.is_new_connection
17411741
}
17421742

1743+
/// Returns information about the connection
1744+
///
1745+
/// # Examples
1746+
///
1747+
/// ```
1748+
/// # use oracle::Error;
1749+
/// # use oracle::test_util;
1750+
/// # let conn = test_util::connect()?;
1751+
/// let info = conn.info()?;
1752+
/// let service_name = conn.query_row_as::<String>("SELECT SYS_CONTEXT('USERENV', 'SERVICE_NAME') FROM DUAL", &[])?;
1753+
/// assert_eq!(info.service_name, service_name);
1754+
/// # Ok::<(), Error>(())
1755+
/// ```
1756+
pub fn info(&self) -> Result<Info> {
1757+
let mut info = MaybeUninit::uninit();
1758+
chkerr!(
1759+
self.ctxt(),
1760+
dpiConn_getInfo(self.handle(), info.as_mut_ptr())
1761+
);
1762+
Info::from_dpi(unsafe { &info.assume_init() })
1763+
}
1764+
17431765
/// Gets an OCI handle attribute corresponding to the specified type parameter
17441766
/// See the [`oci_attr` module][crate::oci_attr] for details.
17451767
pub fn oci_attr<T>(&self) -> Result<<<T::DataType as DataType>::Type as ToOwned>::Owned>

0 commit comments

Comments
 (0)