use error::Result;
use odpi::structs::{
ODPIAppContext, ODPICommonCreateParams, ODPIConnCreateParams, ODPIPoolCreateParams,
ODPISubscrCreateParams,
};
use odpi::{enums, externs, flags};
use pool::Pool;
use std::convert::TryFrom;
use std::ffi::{CStr, CString};
use util::{ODPIStr, PrivateTryFromUsize};
#[derive(Builder, Clone, Debug)]
pub struct AppContext {
#[builder(default)]
inner: ODPIAppContext,
}
impl AppContext {
pub fn new(namespace: &str, name: &str, value: &str) -> Result<Self> {
let namespace_s: ODPIStr = TryFrom::try_from(namespace)?;
let name_s: ODPIStr = TryFrom::try_from(name)?;
let value_s: ODPIStr = TryFrom::try_from(value)?;
let ctxt = ODPIAppContext {
namespace_name: namespace_s.ptr(),
namespace_name_length: namespace_s.len(),
name: name_s.ptr(),
name_length: name_s.len(),
value: value_s.ptr(),
value_length: value_s.len(),
};
Ok(Self { inner: ctxt })
}
pub fn get_namespace_name(&self) -> String {
let namespace_name_s =
ODPIStr::new(self.inner.namespace_name, self.inner.namespace_name_length);
namespace_name_s.into()
}
pub fn get_name(&self) -> String {
let name_s = ODPIStr::new(self.inner.name, self.inner.name_length);
name_s.into()
}
pub fn get_value(&self) -> String {
let value_s = ODPIStr::new(self.inner.value, self.inner.value_length);
value_s.into()
}
}
impl From<ODPIAppContext> for AppContext {
fn from(inner: ODPIAppContext) -> Self {
Self { inner }
}
}
pub struct CommonCreate {
inner: ODPICommonCreateParams,
encoding: Option<CString>,
nchar_encoding: Option<CString>,
}
impl CommonCreate {
#[doc(hidden)]
pub fn inner(&self) -> ODPICommonCreateParams {
self.inner
}
pub fn get_create_mode(&self) -> flags::ODPICreateMode {
self.inner.create_mode
}
pub fn set_create_mode(&mut self, create_mode: flags::ODPICreateMode) -> &mut Self {
self.inner.create_mode = create_mode;
self
}
pub fn get_encoding(&self) -> String {
let encoding_cstr = unsafe { CStr::from_ptr(self.inner.encoding) };
encoding_cstr.to_string_lossy().into_owned()
}
pub fn set_encoding(&mut self, encoding: &str) -> Result<&mut Self> {
let enc_cstr = CString::new(encoding)?;
let enc_ptr = enc_cstr.as_ptr();
self.encoding = Some(enc_cstr);
self.inner.encoding = enc_ptr;
Ok(self)
}
pub fn get_nchar_encoding(&self) -> String {
let encoding_cstr = unsafe { CStr::from_ptr(self.inner.nchar_encoding) };
encoding_cstr.to_string_lossy().into_owned()
}
pub fn set_nchar_encoding(&mut self, nchar_encoding: &str) -> Result<&mut Self> {
let nchar_enc_cstr = CString::new(nchar_encoding)?;
let nchar_enc_ptr = nchar_enc_cstr.as_ptr();
self.nchar_encoding = Some(nchar_enc_cstr);
self.inner.encoding = nchar_enc_ptr;
self.inner.nchar_encoding = nchar_enc_ptr;
Ok(self)
}
pub fn get_edition(&self) -> String {
let edition_s = ODPIStr::new(self.inner.edition, self.inner.edition_length);
edition_s.into()
}
pub fn set_edition(&mut self, edition: &str) -> Result<&mut Self> {
let edition_s: ODPIStr = TryFrom::try_from(edition)?;
self.inner.edition = edition_s.ptr();
self.inner.edition_length = edition_s.len();
Ok(self)
}
pub fn get_driver_name(&self) -> String {
let driver_name_s = ODPIStr::new(self.inner.driver_name, self.inner.driver_name_length);
driver_name_s.into()
}
pub fn set_driver_name(&mut self, driver_name: &str) -> Result<&mut Self> {
let driver_name_s: ODPIStr = TryFrom::try_from(driver_name)?;
self.inner.driver_name = driver_name_s.ptr();
self.inner.driver_name_length = driver_name_s.len();
Ok(self)
}
}
impl From<ODPICommonCreateParams> for CommonCreate {
fn from(inner: ODPICommonCreateParams) -> Self {
Self {
inner,
encoding: None,
nchar_encoding: None,
}
}
}
#[derive(Debug, Default)]
pub struct ConnCreate {
conn: ODPIConnCreateParams,
}
impl ConnCreate {
#[doc(hidden)]
pub fn new(conn: ODPIConnCreateParams) -> Self {
Self { conn }
}
#[doc(hidden)]
pub fn inner(&self) -> ODPIConnCreateParams {
self.conn
}
pub fn get_auth_mode(&self) -> flags::ODPIAuthMode {
self.conn.auth_mode
}
pub fn set_auth_mode(&mut self, auth_mode: flags::ODPIAuthMode) -> &mut Self {
self.conn.auth_mode = auth_mode;
self
}
pub fn get_connection_class(&self) -> String {
let connection_class_s = ODPIStr::new(
self.conn.connection_class,
self.conn.connection_class_length,
);
connection_class_s.into()
}
pub fn set_connection_class(&mut self, connection_class: &str) -> Result<&mut Self> {
let connection_class_s: ODPIStr = TryFrom::try_from(connection_class)?;
self.conn.connection_class = connection_class_s.ptr();
self.conn.connection_class_length = connection_class_s.len();
Ok(self)
}
pub fn get_purity(&self) -> enums::ODPIPurity {
self.conn.purity
}
pub fn set_purity(&mut self, purity: enums::ODPIPurity) -> &mut Self {
self.conn.purity = purity;
self
}
pub fn get_new_password(&self) -> String {
let new_password_s = ODPIStr::new(self.conn.new_password, self.conn.new_password_length);
new_password_s.into()
}
pub fn set_new_password(&mut self, new_password: &str) -> Result<&mut Self> {
let new_password_s: ODPIStr = TryFrom::try_from(new_password)?;
self.conn.new_password = new_password_s.ptr();
self.conn.new_password_length = new_password_s.len();
Ok(self)
}
#[cfg_attr(feature = "cargo-clippy", allow(cast_possible_wrap))]
pub fn get_app_context(&self) -> Result<Vec<AppContext>> {
let len: isize = self.conn.num_app_context as isize;
let head_ptr = self.conn.app_context;
let mut app_contexts = Vec::new();
for i in 0..len {
app_contexts.push(unsafe { *head_ptr.offset(i) }.into());
}
Ok(app_contexts)
}
pub fn set_app_context(&mut self, app_contexts: &[AppContext]) -> Result<&mut Self> {
let len: u32 = u32::private_try_from(app_contexts.len())?;
let mut oac_vec: Vec<ODPIAppContext> = Vec::new();
for ac in app_contexts {
oac_vec.push(ac.inner);
}
let ac_ptr = app_contexts.as_ptr();
self.conn.app_context = ac_ptr as *mut ODPIAppContext;
self.conn.num_app_context = len;
Ok(self)
}
pub fn get_num_app_context(&self) -> u32 {
self.conn.num_app_context
}
pub fn get_external_auth(&self) -> i32 {
self.conn.external_auth
}
pub fn set_external_auth(&mut self, external_auth: i32) -> &mut Self {
self.conn.external_auth = external_auth;
self
}
pub fn get_external_handle(&self) -> *mut ::std::os::raw::c_void {
self.conn.external_handle
}
pub fn set_external_handle(
&mut self,
external_handle: *mut ::std::os::raw::c_void,
) -> &mut Self {
self.conn.external_handle = external_handle;
self
}
pub fn get_pool(&self) -> Result<Pool> {
Ok(TryFrom::try_from(self.conn.pool)?)
}
pub fn set_pool(&mut self, pool: &Pool) -> &mut Self {
self.conn.pool = pool.inner();
self
}
pub fn get_tag(&self) -> String {
let tag_s = ODPIStr::new(self.conn.tag, self.conn.tag_length);
tag_s.into()
}
pub fn set_tag(&mut self, tag: &str) -> Result<&mut Self> {
let tag_s: ODPIStr = TryFrom::try_from(tag)?;
self.conn.tag = tag_s.ptr();
self.conn.tag_length = tag_s.len();
Ok(self)
}
pub fn get_match_any_tag(&self) -> bool {
self.conn.match_any_tag == 1
}
pub fn set_match_any_tag(&mut self, match_any_tag: bool) -> &mut Self {
self.conn.match_any_tag = if match_any_tag { 1 } else { 0 };
self
}
pub fn get_out_tag(&self) -> String {
if self.conn.out_tag.is_null() {
"".to_string()
} else {
let res = ODPIStr::new(self.conn.out_tag, self.conn.out_tag_length);
res.into()
}
}
pub fn get_out_tag_found(&self) -> bool {
self.conn.out_tag_found != 0
}
}
pub struct PoolCreate {
pool: ODPIPoolCreateParams,
}
impl PoolCreate {
#[doc(hidden)]
pub fn new(pool: ODPIPoolCreateParams) -> Self {
Self { pool }
}
#[doc(hidden)]
pub fn inner(&self) -> ODPIPoolCreateParams {
self.pool
}
pub fn get_min_sessions(&self) -> u32 {
self.pool.min_sessions
}
pub fn set_min_sessions(&mut self, min_sessions: u32) -> &mut Self {
self.pool.min_sessions = min_sessions;
self
}
pub fn get_max_sessions(&self) -> u32 {
self.pool.max_sessions
}
pub fn set_max_sessions(&mut self, max_sessions: u32) -> &mut Self {
self.pool.max_sessions = max_sessions;
self
}
pub fn get_session_increment(&self) -> u32 {
self.pool.session_increment
}
pub fn set_session_increment(&mut self, session_increment: u32) -> &mut Self {
self.pool.session_increment = session_increment;
self
}
pub fn get_ping_interval(&self) -> i32 {
self.pool.ping_interval
}
pub fn set_ping_interval(&mut self, ping_interval: i32) -> &mut Self {
self.pool.ping_interval = ping_interval;
self
}
pub fn get_ping_timeout(&self) -> i32 {
self.pool.ping_timeout
}
pub fn set_ping_timeout(&mut self, ping_timeout: i32) -> &mut Self {
self.pool.ping_timeout = ping_timeout;
self
}
pub fn get_homogeneous(&self) -> bool {
self.pool.homogeneous == 1
}
pub fn set_homogeneous(&mut self, homogeneous: bool) -> &mut Self {
self.pool.homogeneous = if homogeneous { 1 } else { 0 };
self
}
pub fn get_external_auth(&self) -> bool {
self.pool.external_auth == 1
}
pub fn set_external_auth(&mut self, external_auth: bool) -> &mut Self {
self.pool.external_auth = if external_auth { 1 } else { 0 };
self
}
pub fn get_get_mode(&self) -> enums::ODPIPoolGetMode {
self.pool.get_mode
}
pub fn set_get_mode(&mut self, get_mode: enums::ODPIPoolGetMode) -> &mut Self {
self.pool.get_mode = get_mode;
self
}
pub fn get_out_pool_name(&self) -> String {
if self.pool.out_pool_name.is_null() {
"".to_string()
} else {
let res = ODPIStr::new(self.pool.out_pool_name, self.pool.out_pool_name_length);
res.into()
}
}
}
pub struct SubscrCreate {
subscr: ODPISubscrCreateParams,
}
impl SubscrCreate {
#[doc(hidden)]
pub fn new(subscr: ODPISubscrCreateParams) -> Self {
Self { subscr }
}
#[doc(hidden)]
pub fn inner(&self) -> ODPISubscrCreateParams {
self.subscr
}
pub fn get_subscr_namespace(&self) -> enums::ODPISubscrNamespace {
self.subscr.subscr_namespace
}
pub fn set_subscr_namespace(
&mut self,
subscr_namespace: enums::ODPISubscrNamespace,
) -> &mut Self {
self.subscr.subscr_namespace = subscr_namespace;
self
}
pub fn get_protocol(&self) -> enums::ODPISubscrProtocol {
self.subscr.protocol
}
pub fn set_protocol(&mut self, protocol: enums::ODPISubscrProtocol) -> &mut Self {
self.subscr.protocol = protocol;
self
}
pub fn get_qos(&self) -> flags::ODPISubscrQOS {
self.subscr.qos
}
pub fn set_qos(&mut self, qos: flags::ODPISubscrQOS) -> &mut Self {
self.subscr.qos = qos;
self
}
pub fn get_operations(&self) -> flags::ODPIOpCode {
self.subscr.operations
}
pub fn set_operations(&mut self, operations: flags::ODPIOpCode) -> &mut Self {
self.subscr.operations = operations;
self
}
pub fn get_port_number(&self) -> u32 {
self.subscr.port_number
}
pub fn set_port_number(&mut self, port_number: u32) -> &mut Self {
self.subscr.port_number = port_number;
self
}
pub fn get_timeout(&self) -> u32 {
self.subscr.timeout
}
pub fn set_timeout(&mut self, timeout: u32) -> &mut Self {
self.subscr.timeout = timeout;
self
}
pub fn get_name(&self) -> String {
if self.subscr.name.is_null() {
"".to_string()
} else {
let res = ODPIStr::new(self.subscr.name, self.subscr.name_length);
res.into()
}
}
pub fn set_name(&mut self, name: &str) -> Result<&mut Self> {
let name_s: ODPIStr = TryFrom::try_from(name)?;
self.subscr.name = name_s.ptr();
self.subscr.name_length = name_s.len();
Ok(self)
}
pub fn get_callback(&self) -> externs::ODPISubscrCallback {
self.subscr.callback
}
pub fn set_callback(&mut self, callback: externs::ODPISubscrCallback) -> &mut Self {
self.subscr.callback = callback;
self
}
pub fn get_callback_context(&self) -> *mut ::std::os::raw::c_void {
self.subscr.callback_context
}
pub fn set_callback_context(
&mut self,
callback_context: *mut ::std::os::raw::c_void,
) -> &mut Self {
self.subscr.callback_context = callback_context;
self
}
pub fn get_recipient_name(&self) -> String {
if self.subscr.recipient_name.is_null() {
"".to_string()
} else {
let res = ODPIStr::new(
self.subscr.recipient_name,
self.subscr.recipient_name_length,
);
res.into()
}
}
pub fn set_recipient_name(&mut self, recipient_name: &str) -> Result<&mut Self> {
let recipient_name_s: ODPIStr = TryFrom::try_from(recipient_name)?;
self.subscr.recipient_name = recipient_name_s.ptr();
self.subscr.recipient_name_length = recipient_name_s.len();
Ok(self)
}
}