Skip to content

Commit

Permalink
Dialect fixed
Browse files Browse the repository at this point in the history
Utilization of dialect options on database creation and attachament
fixed. Basically, the dialect param was only in use on stmt commands.

Issue #150
  • Loading branch information
fernandobatels committed Dec 19, 2023
1 parent 0a034c0 commit 1cad824
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 11 deletions.
2 changes: 2 additions & 0 deletions examples/createdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn main() -> Result<(), FbError> {
.user("SYSDBA")
.pass("masterkey")
.page_size(8 * 1024) // Optional
.dialect(rsfbclient::Dialect::D1)
.create_database()?;

#[cfg(feature = "dynamic_loading")]
Expand All @@ -38,6 +39,7 @@ fn main() -> Result<(), FbError> {
.user("SYSDBA")
.pass("masterkey")
.page_size(16 * 1024) // Optional
.dialect(rsfbclient::Dialect::D3)
.create_database()?;

conn.close()?;
Expand Down
5 changes: 4 additions & 1 deletion rsfbclient-core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub trait FirebirdClientDbOps: Send {
fn attach_database(
&mut self,
config: &Self::AttachmentConfig,
dialect: Dialect,
) -> Result<Self::DbHandle, FbError>;

/// Disconnect from the database
Expand All @@ -50,6 +51,7 @@ pub trait FirebirdClientDbOps: Send {
&mut self,
config: &Self::AttachmentConfig,
page_size: Option<u32>,
dialect: Dialect,
) -> Result<Self::DbHandle, FbError>;
}

Expand Down Expand Up @@ -145,10 +147,11 @@ pub trait FirebirdClientDbEvents: FirebirdClientDbOps {
) -> Result<(), FbError>;
}

#[derive(Debug, Eq, PartialEq, Copy, Clone)]
#[derive(Debug, Eq, PartialEq, Copy, Clone, Default)]
#[repr(u8)]
/// Firebird sql dialect
pub enum Dialect {
#[default]
D1 = 1,
D2 = 2,
D3 = 3,
Expand Down
15 changes: 12 additions & 3 deletions rsfbclient-native/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ impl<T: LinkageMarker> FirebirdClientDbOps for NativeFbClient<T> {
fn attach_database(
&mut self,
config: &Self::AttachmentConfig,
dialect: Dialect,
) -> Result<NativeDbHandle, FbError> {
let (dpb, conn_string) = self.build_dpb(config);
let (dpb, conn_string) = self.build_dpb(config, dialect);
let mut handle = 0;

unsafe {
Expand Down Expand Up @@ -162,8 +163,9 @@ impl<T: LinkageMarker> FirebirdClientDbOps for NativeFbClient<T> {
&mut self,
config: &Self::AttachmentConfig,
page_size: Option<u32>,
dialect: Dialect,
) -> Result<NativeDbHandle, FbError> {
let (mut dpb, conn_string) = self.build_dpb(config);
let (mut dpb, conn_string) = self.build_dpb(config, dialect);
let mut handle = 0;

if let Some(ps) = page_size {
Expand Down Expand Up @@ -668,7 +670,11 @@ impl<T: LinkageMarker> NativeFbClient<T> {
/// Build the dpb and the connection string
///
/// Used by attach database operations
fn build_dpb(&mut self, config: &NativeFbAttachmentConfig) -> (Vec<u8>, String) {
fn build_dpb(
&mut self,
config: &NativeFbAttachmentConfig,
dialect: Dialect,
) -> (Vec<u8>, String) {
let user = &config.user;
let mut password = None;
let db_name = &config.db_name;
Expand Down Expand Up @@ -709,6 +715,9 @@ impl<T: LinkageMarker> NativeFbClient<T> {
dpb.extend(role.bytes());
}

dpb.extend(&[ibase::isc_dpb_sql_dialect as u8, 1 as u8]);
dpb.extend(&[dialect as u8]);

dpb
};

Expand Down
10 changes: 8 additions & 2 deletions rsfbclient-rust/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl FirebirdClientDbOps for RustFbClient {
fn attach_database(
&mut self,
config: &Self::AttachmentConfig,
dialect: Dialect,
) -> Result<RustDbHandle, FbError> {
let host = config.host.as_str();
let port = config.port;
Expand All @@ -109,7 +110,7 @@ impl FirebirdClientDbOps for RustFbClient {
)?,
};

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

// Put the connection back
self.conn.replace(conn);
Expand All @@ -135,6 +136,7 @@ impl FirebirdClientDbOps for RustFbClient {
&mut self,
config: &Self::AttachmentConfig,
page_size: Option<u32>,
dialect: Dialect,
) -> Result<RustDbHandle, FbError> {
let host = config.host.as_str();
let port = config.port;
Expand All @@ -159,7 +161,7 @@ impl FirebirdClientDbOps for RustFbClient {
)?,
};

let attach_result = conn.create_database(db_name, user, pass, page_size, role);
let attach_result = conn.create_database(db_name, user, pass, page_size, role, dialect);

// Put the connection back
self.conn.replace(conn);
Expand Down Expand Up @@ -391,6 +393,7 @@ impl FirebirdWireConnection {
pass: &str,
page_size: Option<u32>,
role_name: Option<&str>,
dialect: Dialect,
) -> Result<DbHandle, FbError> {
self.socket.write_all(&create(
db_name,
Expand All @@ -400,6 +403,7 @@ impl FirebirdWireConnection {
self.charset.clone(),
page_size,
role_name.clone(),
dialect,
))?;
self.socket.flush()?;

Expand All @@ -415,6 +419,7 @@ impl FirebirdWireConnection {
user: &str,
pass: &str,
role_name: Option<&str>,
dialect: Dialect,
) -> Result<DbHandle, FbError> {
self.socket.write_all(&attach(
db_name,
Expand All @@ -423,6 +428,7 @@ impl FirebirdWireConnection {
self.version,
self.charset.clone(),
role_name.clone(),
dialect,
))?;
self.socket.flush()?;

Expand Down
12 changes: 9 additions & 3 deletions rsfbclient-rust/src/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
util::*,
xsqlda::{XSqlVar, XSQLDA_DESCRIBE_VARS},
};
use rsfbclient_core::{ibase, Charset, Column, FbError, FreeStmtOp, SqlType, TrOp};
use rsfbclient_core::{ibase, Charset, Column, Dialect, FbError, FreeStmtOp, SqlType, TrOp};

/// Buffer length to use in the connection
pub const BUFFER_LENGTH: u32 = 1024;
Expand Down Expand Up @@ -139,8 +139,9 @@ pub fn attach(
protocol: ProtocolVersion,
charset: Charset,
role_name: Option<&str>,
dialect: Dialect,
) -> Bytes {
let dpb = build_dpb(user, pass, protocol, charset, None, role_name);
let dpb = build_dpb(user, pass, protocol, charset, None, role_name, dialect);

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

Expand All @@ -163,8 +164,9 @@ pub fn create(
charset: Charset,
page_size: Option<u32>,
role_name: Option<&str>,
dialect: Dialect,
) -> Bytes {
let dpb = build_dpb(user, pass, protocol, charset, page_size, role_name);
let dpb = build_dpb(user, pass, protocol, charset, page_size, role_name, dialect);

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

Expand All @@ -186,6 +188,7 @@ fn build_dpb(
charset: Charset,
page_size: Option<u32>,
role_name: Option<&str>,
dialect: Dialect,
) -> Bytes {
let mut dpb = BytesMut::with_capacity(64);

Expand All @@ -209,6 +212,9 @@ fn build_dpb(
dpb.extend(role.bytes());
}

dpb.extend(&[ibase::isc_dpb_sql_dialect as u8, 1 as u8]);
dpb.extend(&[dialect as u8]);

match protocol {
// Plaintext password
ProtocolVersion::V10 => {
Expand Down
4 changes: 2 additions & 2 deletions src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<C: FirebirdClient> Connection<C> {
mut cli: C,
conf: &ConnectionConfiguration<C::AttachmentConfig>,
) -> Result<Connection<C>, FbError> {
let handle = cli.attach_database(&conf.attachment_conf)?;
let handle = cli.attach_database(&conf.attachment_conf, conf.dialect)?;
let stmt_cache = StmtCache::new(conf.stmt_cache_size);

Ok(Connection {
Expand All @@ -127,7 +127,7 @@ impl<C: FirebirdClient> Connection<C> {
conf: &ConnectionConfiguration<C::AttachmentConfig>,
page_size: Option<u32>,
) -> Result<Connection<C>, FbError> {
let handle = cli.create_database(&conf.attachment_conf, page_size)?;
let handle = cli.create_database(&conf.attachment_conf, page_size, conf.dialect)?;
let stmt_cache = StmtCache::new(conf.stmt_cache_size);

Ok(Connection {
Expand Down
34 changes: 34 additions & 0 deletions src/tests/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,38 @@ mk_tests_default! {

Ok(())
}

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

let rs = builder_native()
.with_dyn_link()
.with_remote()
.db_name("test_create_db5.fdb")
.user("SYSDBA")
.host("localhost")
.dialect(rsfbclient::Dialect::D2)
.create_database();

assert!(rs.is_err());
assert!(rs.err().unwrap().to_string().contains("Database dialect 2 is not a valid dialec"));

Ok(())
}

#[test]
#[cfg(all(feature = "pure_rust", not(feature = "native_client")))]
fn pure_rust_create_with_invalid_dialect() -> Result<(), FbError> {
let rs = builder_pure_rust()
.db_name("test_create_db55.fdb")
.user("SYSDBA")
.dialect(rsfbclient::Dialect::D2)
.create_database();

assert!(rs.is_err());
assert!(rs.err().unwrap().to_string().contains("Database dialect 2 is not a valid dialec"));

Ok(())
}
}

0 comments on commit 1cad824

Please sign in to comment.