Struct sql_query_builder::Insert
source · pub struct Insert<'a> { /* private fields */ }Expand description
Builder to contruct a Insert command
Implementations§
source§impl<'a> Insert<'a>
impl<'a> Insert<'a>
sourcepub fn as_string(&self) -> String
pub fn as_string(&self) -> String
Gets the current state of the Insert and returns it as string
Examples
use sql_query_builder as sql;
let query = sql::Insert::new()
.insert_into("users (login)")
.values("('foo')")
.as_string();Output
INSERT INTO users (login) VALUES ('foo')
sourcepub fn debug(self) -> Self
pub fn debug(self) -> Self
Prints the current state of the Insert into console output in a more ease to read version. This method is useful to debug complex queries or just to print the generated SQL while you type
Examples
use sql_query_builder as sql;
let insert_query = sql::Insert::new()
.insert_into("users (login, name)")
.values("('foo', 'Foo')")
.debug()
.values("('bar', 'Bar')")
.as_string();Output
INSERT INTO users (login, name)
VALUES ('foo', 'Foo')
sourcepub fn insert_into(self, table_name: &'a str) -> Self
pub fn insert_into(self, table_name: &'a str) -> Self
The insert into clause. This method overrides the previous value
Examples
use sql_query_builder as sql;
let insert = sql::Insert::new()
.insert_into("users (login, name)");
let insert = sql::Insert::new()
.insert_into("address (state, country)")
.insert_into("users (login, name)");sourcepub fn on_conflict(self, conflict: &'a str) -> Self
pub fn on_conflict(self, conflict: &'a str) -> Self
The on conflict clause. This method overrides the previous value
sourcepub fn overriding(self, option: &'a str) -> Self
pub fn overriding(self, option: &'a str) -> Self
The overriding clause. This method overrides the previous value
sourcepub fn print(self) -> Self
pub fn print(self) -> Self
Prints the current state of the Insert into console output similar to debug method, the difference is that this method prints in one line.
sourcepub fn select(self, select: Select<'a>) -> Self
pub fn select(self, select: Select<'a>) -> Self
The select clause. This method overrides the previous value
Examples
use sql_query_builder as sql;
let insert_query = sql::Insert::new()
.insert_into("users (login, name)")
.select(
sql::Select::new()
.select("login, name")
.from("users_bk")
.where_clause("active = true"),
)
.as_string();Output
INSERT INTO users (login, name)
SELECT login, name
FROM users_bk
WHERE active = true
sourcepub fn raw(self, raw_sql: &str) -> Self
pub fn raw(self, raw_sql: &str) -> Self
Adds at the beginning a raw SQL query.
Examples
use sql_query_builder as sql;
let raw_query = "insert into users (login, name)";
let insert_query = sql::Insert::new()
.raw(raw_query)
.values("('foo', 'Foo')")
.as_string();Output
insert into users (login, name)
VALUES ('bar', 'Bar')
sourcepub fn raw_after(self, clause: InsertClause, raw_sql: &str) -> Self
pub fn raw_after(self, clause: InsertClause, raw_sql: &str) -> Self
Adds a raw SQL query after a specified clause.
Examples
use sql_query_builder as sql;
let raw = "values ('foo', 'Foo')";
let insert_query = sql::Insert::new()
.insert_into("users (login, name)")
.raw_after(sql::InsertClause::InsertInto, raw)
.as_string();Output
INSERT INTO users (login, name)
values ('foo', 'Foo')
sourcepub fn raw_before(self, clause: InsertClause, raw_sql: &str) -> Self
pub fn raw_before(self, clause: InsertClause, raw_sql: &str) -> Self
Adds a raw SQL query before a specified clause.
Examples
use sql_query_builder as sql;
let raw = "insert into users (login, name)";
let insert_query = sql::Insert::new()
.raw_before(sql::InsertClause::Values, raw)
.values("('bar', 'Bar')")
.as_string();Output
insert into users (login, name)
VALUES ('bar', 'Bar')
source§impl<'a> Insert<'a>
impl<'a> Insert<'a>
sourcepub fn returning(self, output_name: &str) -> Self
pub fn returning(self, output_name: &str) -> Self
The returning clause, this method can be used enabling the feature flag postgresql
sourcepub fn with(self, name: &'a str, query: impl WithQuery + 'static) -> Self
pub fn with(self, name: &'a str, query: impl WithQuery + 'static) -> Self
The with clause, this method can be used enabling the feature flag postgresql
Examples
use sql_query_builder as sql;
let active_users = sql::Select::new().select("*").from("users_bk").where_clause("ative = true");
let insert = sql::Insert::new()
.with("active_users", active_users)
.insert_into("users")
.select(sql::Select::new().select("*").from("active_users"))
.debug();
Output
WITH active_users AS (
SELECT *
FROM users_bk
WHERE ative = true
)
INSERT INTO users
SELECT *
FROM active_users