Struct sql_query_builder::Transaction
source · pub struct Transaction { /* private fields */ }
Expand description
Builder to contruct a Transaction block
Implementations§
source§impl Transaction
impl Transaction
sourcepub fn as_string(&self) -> String
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;
sourcepub fn commit(self, arg: &str) -> Self
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;
sourcepub fn debug(self) -> Self
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;
-- ------------------------------------------------------------------------------
sourcepub fn create_table(self, create_table: CreateTable) -> Self
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;
-- ------------------------------------------------------------------------------
sourcepub fn delete(self, delete: Delete) -> Self
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;
-- ------------------------------------------------------------------------------
sourcepub fn insert(self, insert: Insert) -> Self
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;
-- ------------------------------------------------------------------------------
sourcepub fn print(self) -> Self
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.
sourcepub fn raw(self, raw_sql: &str) -> Self
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;
sourcepub fn release_savepoint(self, name: &str) -> Self
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;
sourcepub fn rollback(self, arg: &str) -> Self
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;
sourcepub fn savepoint(self, name: &str) -> Self
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;
sourcepub fn select(self, select: Select) -> Self
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;
-- ------------------------------------------------------------------------------
sourcepub fn update(self, update: Update) -> Self
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
impl Transaction
sourcepub fn begin(self, mode: &str) -> Self
Available on crate features postgresql
and sqlite
only.
pub fn begin(self, mode: &str) -> Self
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;
sourcepub fn end(self, mode: &str) -> Self
Available on crate features postgresql
and sqlite
only.
pub fn end(self, mode: &str) -> Self
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;