Skip to content

Commit

Permalink
No db triggers support
Browse files Browse the repository at this point in the history
Added an option 'no_db_trigger' on connection builder for disabled
the database triggers, like the ones when the client connect.
  • Loading branch information
fernandobatels committed Jan 1, 2024
1 parent 755275d commit 54a9db3
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 5 deletions.
1 change: 1 addition & 0 deletions rsfbclient-core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub trait FirebirdClientDbOps: Send {
&mut self,
config: &Self::AttachmentConfig,
dialect: Dialect,
no_db_triggers: bool,
) -> Result<Self::DbHandle, FbError>;

/// Disconnect from the database
Expand Down
8 changes: 7 additions & 1 deletion rsfbclient-native/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,16 @@ impl<T: LinkageMarker> FirebirdClientDbOps for NativeFbClient<T> {
&mut self,
config: &Self::AttachmentConfig,
dialect: Dialect,
no_db_triggers: bool,
) -> Result<NativeDbHandle, FbError> {
let (dpb, conn_string) = self.build_dpb(config, dialect);
let (mut dpb, conn_string) = self.build_dpb(config, dialect);
let mut handle = 0;

if no_db_triggers {
dpb.extend(&[ibase::isc_dpb_no_db_triggers as u8, 1 as u8]);

Check warning on line 123 in rsfbclient-native/src/connection.rs

View workflow job for this annotation

GitHub Actions / clippy

casting integer literal to `u8` is unnecessary

warning: casting integer literal to `u8` is unnecessary --> rsfbclient-native/src/connection.rs:123:63 | 123 | dpb.extend(&[ibase::isc_dpb_no_db_triggers as u8, 1 as u8]); | ^^^^^^^ help: try: `1_u8` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast = note: `#[warn(clippy::unnecessary_cast)]` on by default
dpb.extend(&[1 as u8]);

Check warning on line 124 in rsfbclient-native/src/connection.rs

View workflow job for this annotation

GitHub Actions / clippy

casting integer literal to `u8` is unnecessary

warning: casting integer literal to `u8` is unnecessary --> rsfbclient-native/src/connection.rs:124:26 | 124 | dpb.extend(&[1 as u8]); | ^^^^^^^ help: try: `1_u8` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
}

unsafe {
if self.ibase.isc_attach_database()(
&mut self.status[0],
Expand Down
6 changes: 5 additions & 1 deletion rsfbclient-rust/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl FirebirdClientDbOps for RustFbClient {
&mut self,
config: &Self::AttachmentConfig,
dialect: Dialect,
no_db_triggers: bool,
) -> Result<RustDbHandle, FbError> {
let host = config.host.as_str();
let port = config.port;
Expand All @@ -110,7 +111,8 @@ impl FirebirdClientDbOps for RustFbClient {
)?,
};

let attach_result = conn.attach_database(db_name, user, pass, role, dialect);
let attach_result =
conn.attach_database(db_name, user, pass, role, dialect, no_db_triggers);

// Put the connection back
self.conn.replace(conn);
Expand Down Expand Up @@ -420,6 +422,7 @@ impl FirebirdWireConnection {
pass: &str,
role_name: Option<&str>,
dialect: Dialect,
no_db_triggers: bool,
) -> Result<DbHandle, FbError> {
self.socket.write_all(&attach(
db_name,
Expand All @@ -429,6 +432,7 @@ impl FirebirdWireConnection {
self.charset.clone(),
role_name.clone(),

Check warning on line 433 in rsfbclient-rust/src/client.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `Option<&str>` which implements the `Copy` trait

warning: using `clone` on type `Option<&str>` which implements the `Copy` trait --> rsfbclient-rust/src/client.rs:433:13 | 433 | role_name.clone(), | ^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `role_name` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
dialect,
no_db_triggers,
))?;
self.socket.flush()?;

Expand Down
22 changes: 20 additions & 2 deletions rsfbclient-rust/src/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,18 @@ pub fn attach(
charset: Charset,
role_name: Option<&str>,
dialect: Dialect,
no_db_triggers: bool,
) -> Bytes {

Check warning on line 144 in rsfbclient-rust/src/wire.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> rsfbclient-rust/src/wire.rs:135:1 | 135 | / pub fn attach( 136 | | db_name: &str, 137 | | user: &str, 138 | | pass: &str, ... | 143 | | no_db_triggers: bool, 144 | | ) -> Bytes { | |__________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments = note: `#[warn(clippy::too_many_arguments)]` on by default
let dpb = build_dpb(user, pass, protocol, charset, None, role_name, dialect);
let dpb = build_dpb(
user,
pass,
protocol,
charset,
None,
role_name,
dialect,
no_db_triggers,
);

let mut attach = BytesMut::with_capacity(16 + db_name.len() + dpb.len());

Expand All @@ -166,7 +176,9 @@ pub fn create(
role_name: Option<&str>,
dialect: Dialect,
) -> Bytes {

Check warning on line 178 in rsfbclient-rust/src/wire.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> rsfbclient-rust/src/wire.rs:169:1 | 169 | / pub fn create( 170 | | db_name: &str, 171 | | user: &str, 172 | | pass: &str, ... | 177 | | dialect: Dialect, 178 | | ) -> Bytes { | |__________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
let dpb = build_dpb(user, pass, protocol, charset, page_size, role_name, dialect);
let dpb = build_dpb(
user, pass, protocol, charset, page_size, role_name, dialect, false,
);

let mut create = BytesMut::with_capacity(16 + db_name.len() + dpb.len());

Expand All @@ -189,6 +201,7 @@ fn build_dpb(
page_size: Option<u32>,
role_name: Option<&str>,
dialect: Dialect,
no_db_triggers: bool,
) -> Bytes {

Check warning on line 205 in rsfbclient-rust/src/wire.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> rsfbclient-rust/src/wire.rs:196:1 | 196 | / fn build_dpb( 197 | | user: &str, 198 | | pass: &str, 199 | | protocol: ProtocolVersion, ... | 204 | | no_db_triggers: bool, 205 | | ) -> Bytes { | |__________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
let mut dpb = BytesMut::with_capacity(64);

Expand All @@ -215,6 +228,11 @@ fn build_dpb(
dpb.extend(&[ibase::isc_dpb_sql_dialect as u8, 1 as u8]);

Check warning on line 228 in rsfbclient-rust/src/wire.rs

View workflow job for this annotation

GitHub Actions / clippy

casting integer literal to `u8` is unnecessary

warning: casting integer literal to `u8` is unnecessary --> rsfbclient-rust/src/wire.rs:228:52 | 228 | dpb.extend(&[ibase::isc_dpb_sql_dialect as u8, 1 as u8]); | ^^^^^^^ help: try: `1_u8` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
dpb.extend(&[dialect as u8]);

if no_db_triggers {
dpb.extend(&[ibase::isc_dpb_no_db_triggers as u8, 1 as u8]);

Check warning on line 232 in rsfbclient-rust/src/wire.rs

View workflow job for this annotation

GitHub Actions / clippy

casting integer literal to `u8` is unnecessary

warning: casting integer literal to `u8` is unnecessary --> rsfbclient-rust/src/wire.rs:232:59 | 232 | dpb.extend(&[ibase::isc_dpb_no_db_triggers as u8, 1 as u8]); | ^^^^^^^ help: try: `1_u8` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
dpb.extend(&[1 as u8]);

Check warning on line 233 in rsfbclient-rust/src/wire.rs

View workflow job for this annotation

GitHub Actions / clippy

casting integer literal to `u8` is unnecessary

warning: casting integer literal to `u8` is unnecessary --> rsfbclient-rust/src/wire.rs:233:22 | 233 | dpb.extend(&[1 as u8]); | ^^^^^^^ help: try: `1_u8` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
}

match protocol {
// Plaintext password
ProtocolVersion::V10 => {
Expand Down
6 changes: 6 additions & 0 deletions src/connection/builders/builder_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ where
self.conn_conf.transaction_conf = builder(&mut transaction_builder()).build();
self
}

/// Disabled the database triggers
pub fn no_db_triggers(&mut self) -> &mut Self {
self.conn_conf.no_db_triggers = true;
self
}
}

impl<A, B> NativeConnectionBuilder<A, B> {
Expand Down
6 changes: 6 additions & 0 deletions src/connection/builders/builder_pure_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ impl PureRustConnectionBuilder {
self
}

/// Disabled the database triggers
pub fn no_db_triggers(&mut self) -> &mut Self {
self.0.no_db_triggers = true;
self
}

/// Default transaction configuration
pub fn transaction(&mut self, conf: TransactionConfiguration) -> &mut Self {
self.0.transaction_conf = conf;
Expand Down
5 changes: 4 additions & 1 deletion src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub trait FirebirdClientFactory {
pub struct ConnectionConfiguration<A> {
attachment_conf: A,
dialect: Dialect,
no_db_triggers: bool,
stmt_cache_size: usize,
transaction_conf: TransactionConfiguration,
}
Expand All @@ -71,6 +72,7 @@ impl<A: Default> Default for ConnectionConfiguration<A> {
dialect: Dialect::D3,
stmt_cache_size: 20,
transaction_conf: TransactionConfiguration::default(),
no_db_triggers: false,
}
}
}
Expand Down Expand Up @@ -107,7 +109,8 @@ impl<C: FirebirdClient> Connection<C> {
mut cli: C,
conf: &ConnectionConfiguration<C::AttachmentConfig>,
) -> Result<Connection<C>, FbError> {
let handle = cli.attach_database(&conf.attachment_conf, conf.dialect)?;
let handle =
cli.attach_database(&conf.attachment_conf, conf.dialect, conf.no_db_triggers)?;
let stmt_cache = StmtCache::new(conf.stmt_cache_size);

Ok(Connection {
Expand Down
40 changes: 40 additions & 0 deletions src/tests/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,44 @@ mk_tests_default! {

Ok(())
}

#[test]
#[cfg(all(not(feature = "embedded_tests")))]
fn no_db_triggers() -> Result<(), FbError> {

let epoch = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_nanos();

let mut conn = cbuilder()
.connect()?;

conn.execute(&format!("create table log_conn{} (id int);", epoch), ())?;

conn.execute(&format!("create trigger trig_conexao{0} on connect as begin insert into log_conn{0} (id) values (5); end", epoch), ())?;

conn.close()?;

let mut conn2 = cbuilder()
.no_db_triggers()
.connect()?;

let resp: Option<(i32,)> = conn2.query_first(&format!("select * from log_conn{}", epoch), ())?;
assert_eq!(None, resp);

conn2.close()?;

let mut conn3 = cbuilder()
.connect()?;

let resp: Option<(i32,)> = conn3.query_first(&format!("select * from log_conn{}", epoch), ())?;
assert_eq!(Some((5,)), resp);

conn3.execute(&format!("drop trigger trig_conexao{};", epoch), ()).ok();
conn3.execute(&format!("drop table log_conn{};", epoch), ()).ok();
conn3.close()?;

Ok(())
}
}

0 comments on commit 54a9db3

Please sign in to comment.