Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
Transactions for MSSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius de Bruijn committed Jun 12, 2020
1 parent 5e2d0f1 commit 7e4e94b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ native-tls = { version = "0.2", optional = true }

mysql_async = { version = "0.23", optional = true }

tiberius = { version = "0.4", optional = true, features = ["rust_decimal", "sql-browser-tokio"] }
tiberius = { git = "https://github.com/prisma/tiberius", optional = true, features = ["rust_decimal", "sql-browser-tokio"], branch = "token-tx"}

log = { version = "0.4", features = ["release_max_level_trace"] }
tracing = { version = "0.1", optional = true }
Expand Down
23 changes: 21 additions & 2 deletions src/connector/mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ pub(crate) struct MssqlQueryParams {
connect_timeout: Option<Duration>,
}

/// A thing that can start a new transaction.
#[async_trait]
impl TransactionCapable for Mssql {
/// Starts a new transaction
async fn start_transaction(&self) -> crate::Result<Transaction<'_>> {
Transaction::new(self, "BEGIN TRAN").await
}
Expand Down Expand Up @@ -236,6 +234,10 @@ impl Queryable for Mssql {

Ok(version_string)
}

fn begin_statement(&self) -> &'static str {
"BEGIN TRAN"
}
}

impl MssqlUrl {
Expand Down Expand Up @@ -397,6 +399,23 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn transactions() -> crate::Result<()> {
let pool = pooled::Quaint::builder(&CONN_STR)?.build();
let connection = pool.check_out().await?;

let tx = connection.start_transaction().await?;
let res = tx.query_raw("SELECT 1", &[]).await?;

tx.commit().await?;

let row = res.get(0).unwrap();

assert_eq!(row[0].as_i64(), Some(1));

Ok(())
}

#[tokio::test]
async fn aliased_value() -> crate::Result<()> {
let connection = single::Quaint::new(&CONN_STR).await?;
Expand Down
7 changes: 6 additions & 1 deletion src/connector/queryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ pub trait Queryable: Send + Sync {
async fn server_reset_query(&self, _: &Transaction<'_>) -> crate::Result<()> {
Ok(())
}

/// Statement to begin a transaction
fn begin_statement(&self) -> &'static str {
"BEGIN"
}
}

/// A thing that can start a new transaction.
Expand All @@ -75,6 +80,6 @@ where
{
/// Starts a new transaction
async fn start_transaction(&self) -> crate::Result<Transaction<'_>> {
Transaction::new(self, "BEGIN").await
Transaction::new(self, self.begin_statement()).await
}
}
8 changes: 4 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/// Convert given set of tuples into `Values`.
///
/// ```rust
/// # use quaint::{values, ast::*, visitor::{Visitor, Sqlite}};
/// # use quaint::{col, values, ast::*, visitor::{Visitor, Sqlite}};
/// # fn main() -> Result<(), quaint::error::Error> {
///
/// let condition = Row::from(("id", "name"))
/// let condition = Row::from((col!("id"), col!("name")))
/// .in_selection(values!((1, "Musti"), (2, "Naukio")));
///
/// let query = Select::from_table("cats").so_that(conditions);
/// let query = Select::from_table("cats").so_that(condition);
/// let (sql, _) = Sqlite::build(query)?;
///
/// assert_eq!(
/// "SELECT * FROM `cats` WHERE (`id`, `name`) IN ((?, ?), (?, ?))",
/// "SELECT `cats`.* FROM `cats` WHERE (`id`,`name`) IN (VALUES (?,?),(?,?))",
/// sql
/// );
/// # Ok(())
Expand Down
4 changes: 4 additions & 0 deletions src/pooled/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ impl Queryable for PooledConnection {
async fn server_reset_query(&self, tx: &Transaction<'_>) -> crate::Result<()> {
self.inner.server_reset_query(tx).await
}

fn begin_statement(&self) -> &'static str {
self.inner.begin_statement()
}
}

#[doc(hidden)]
Expand Down
4 changes: 4 additions & 0 deletions src/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,8 @@ impl Queryable for Quaint {
async fn version(&self) -> crate::Result<Option<String>> {
self.inner.version().await
}

fn begin_statement(&self) -> &'static str {
self.inner.begin_statement()
}
}
2 changes: 1 addition & 1 deletion src/visitor/mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ mod tests {

#[test]
fn test_select_fields_from() {
let expected_sql = "SELECT [paw], [nose] FROM [cat].[musti]";
let expected_sql = "SELECT [paw], [nose] FROM [musti]";
let query = Select::from_table(("cat", "musti")).column("paw").column("nose");
let (sql, params) = Mssql::build(query).unwrap();

Expand Down

0 comments on commit 7e4e94b

Please sign in to comment.