Struct sql_query_builder::Transaction

source ·
pub struct Transaction { /* private fields */ }
Expand description

Builder to contruct a Transaction block

Implementations§

source§

impl Transaction

source

pub fn as_string(&self) -> String

Gets the current state of the Transaction and returns it as string

§Example
let transaction_query = sql::Transaction::new()
  .start_transaction("")
  .commit("")
  .as_string();

Output

START TRANSACTION;
COMMIT;
source

pub fn commit(self, arg: &str) -> Self

The commit command, this method will be always added at the end of the transaction and all consecutive call will override the previous value

§Example
let transaction_query = sql::Transaction::new()
  .commit("WORK")
  .commit("TRANSACTION")
  .as_string();

Output

COMMIT TRANSACTION;
source

pub fn debug(self) -> Self

Prints the current state of the Transaction to the standard output in a more ease to read version. This method is useful to debug complex queries or just print the generated SQL while you type

§Example
let insert_foo = sql::Insert::new()
  .insert_into("users (login, name)")
  .values("('foo', 'Foo')");

let transaction = sql::Transaction::new()
  .start_transaction("isolation level serializable")
  .insert(insert_foo)
  .commit("")
  .debug();

Prints to the standard output

-- ------------------------------------------------------------------------------
START TRANSACTION isolation level serializable;
INSERT INTO users (login, name)
VALUES ('foo', 'Foo');
COMMIT;
-- ------------------------------------------------------------------------------
source

pub fn create_table(self, create_table: CreateTable) -> Self

The create table command, access the CreateTable for more info

§Example
let users_table = sql::CreateTable::new()
  .create_table("users")
  .column("login varchar(40) not null");

let query = sql::Transaction::new()
  .start_transaction("")
  .create_table(users_table)
  .commit("")
  .debug()
  .as_string();

Prints to the standard output

-- ------------------------------------------------------------------------------
START TRANSACTION;
CREATE TABLE users (
  login varchar(40) not null
);
COMMIT;
-- ------------------------------------------------------------------------------
source

pub fn delete(self, delete: Delete) -> Self

The delete command, access the Delete for more info

§Example
let delete_foo = sql::Delete::new()
  .delete_from("users")
  .where_clause("login = 'foo'");

let query = sql::Transaction::new()
  .start_transaction("")
  .delete(delete_foo)
  .commit("")
  .debug()
  .as_string();

Prints to the standard output

-- ------------------------------------------------------------------------------
START TRANSACTION;
DELETE FROM users
WHERE login = 'foo';
COMMIT;
-- ------------------------------------------------------------------------------
source

pub fn insert(self, insert: Insert) -> Self

The insert command, access the Insert for more info

§Example
let insert_foo = sql::Insert::new()
  .insert_into("users (login, name)")
  .values("('foo', 'Foo')");

let query = sql::Transaction::new()
  .start_transaction("")
  .insert(insert_foo)
  .commit("")
  .debug()
  .as_string();

Prints to the standard output

-- ------------------------------------------------------------------------------
START TRANSACTION;
INSERT INTO users (login, name)
VALUES ('foo', 'Foo');
COMMIT;
-- ------------------------------------------------------------------------------
source

pub fn new() -> Self

Creates instance to be used with Transaction commands

source

pub fn print(self) -> Self

Prints the current state of the Transaction to the standard output similar to debug method, the difference is that this method prints in one line.

source

pub fn raw(self, raw_sql: &str) -> Self

Adds at the beginning a raw SQL query.

§Example
let raw_query = "\
  start transaction; \
  set transaction isolation level read committed;\
";
let transaction_query = sql::Transaction::new()
  .raw(raw_query)
  .commit("")
  .as_string();

";

Output

start transaction;
set transaction isolation level read committed;
COMMIT;
source

pub fn release_savepoint(self, name: &str) -> Self

The release savepoint command

§Example
let transaction_query = sql::Transaction::new()
  .release_savepoint("saved_foo")
  .as_string();

Output

RELEASE_SAVEPOINT saved_foo;
source

pub fn rollback(self, arg: &str) -> Self

The rollback command, this method can be used to add a rollback to savepoint my_savepoint command.

§Example
let transaction_query = sql::Transaction::new()
  .rollback("")
  .as_string();

Output

ROLLBACK;
§Example
let transaction_query = sql::Transaction::new()
  .rollback("TO SAVEPOINT my_savepoint")
  .as_string();

Output

ROLLBACK TO SAVEPOINT my_savepoint;
source

pub fn savepoint(self, name: &str) -> Self

The savepoint command

§Example
let transaction_query = sql::Transaction::new()
  .savepoint("my_savepoint")
  .as_string();

Output

SAVEPOINT my_savepoint;
source

pub fn select(self, select: Select) -> Self

The select command, access the Select for more info

§Example
let select_foo = sql::Select::new()
  .select("login, name")
  .from("users")
  .where_clause("id = $1");

let query = sql::Transaction::new()
  .start_transaction("")
  .select(select_foo)
  .commit("")
  .debug()
  .as_string();

Prints to the standard output

-- ------------------------------------------------------------------------------
START TRANSACTION;
SELECT login, name FROM users WHERE id = $1;
COMMIT;
-- ------------------------------------------------------------------------------
source

pub fn update(self, update: Update) -> Self

The update command, access the Update for more info

§Example
let update_foo = sql::Update::new()
  .update("users")
  .set("name = 'Foooo'")
  .where_clause("id = $1");

let query = sql::Transaction::new()
  .start_transaction("")
  .update(update_foo)
  .commit("")
  .debug()
  .as_string();

Prints to the standard output

-- ------------------------------------------------------------------------------
START TRANSACTION;
UPDATE users SET name = 'Foooo' WHERE id = $1;
COMMIT;
-- ------------------------------------------------------------------------------
source§

impl Transaction

source

pub fn begin(self, mode: &str) -> Self

Available on crate features postgresql and sqlite only.

The begin command, this method will be always added at the beginning of the transation and all consecutive call will override the previous value.

§Example
let transaction_query = sql::Transaction::new()
  .begin("transaction")
  .commit("")
  .as_string();

Output

BEGIN transaction;
COMMIT;
source

pub fn end(self, mode: &str) -> Self

Available on crate features postgresql and sqlite only.

The end command, this method will be always added at the end of the transation and all consecutive call will override the previous value.

§Example
let transaction_query = sql::Transaction::new()
  .begin("")
  .end("")
  .as_string();

Output

BEGIN;
END;

Trait Implementations§

source§

impl Debug for Transaction

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Transaction

source§

fn default() -> Transaction

Returns the “default value” for a type. Read more
source§

impl Display for Transaction

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.