Struct sql_query_builder::Insert
source · pub struct Insert { /* private fields */ }
Expand description
Builder to contruct a Insert command
Implementations§
source§impl Insert
impl Insert
sourcepub fn debug(self) -> Self
pub fn debug(self) -> Self
Prints the current state of the Insert 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_query = sql::Insert::new()
.insert_into("users (login, name)")
.values("('foo', 'Foo')")
.debug()
.values("('bar', 'Bar')")
.as_string();
Prints to the standard output
-- ------------------------------------------------------------------------------
INSERT INTO users (login, name)
VALUES ('foo', 'Foo')
-- ------------------------------------------------------------------------------
sourcepub fn default_values(self) -> Self
pub fn default_values(self) -> Self
The default values
clause
§Example
let insert_query = sql::Insert::new()
.insert_into("users")
.default_values()
.to_string();
Output
INSERT INTO users DEFAULT VALUES
sourcepub fn on_conflict(self, conflict: &str) -> Self
pub fn on_conflict(self, conflict: &str) -> Self
The on conflict
clause. This method overrides the previous value
§Example
let query = sql::Insert::new()
.insert_into("users (login)")
.on_conflict("do nothing")
.as_string();
Output
INSERT INTO users (login) ON CONFLICT do nothing
sourcepub fn print(self) -> Self
pub fn print(self) -> Self
Prints the current state of the Insert to the standard output similar to debug method, the difference is that this method prints in one line.
sourcepub fn select(self, select: Select) -> Self
pub fn select(self, select: Select) -> Self
The select
clause. This method overrides the previous value
§Example
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.
§Example
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 ('foo', 'Foo')
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.
§Example
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.
§Example
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 Insert
impl Insert
sourcepub fn returning(self, output_name: &str) -> Self
Available on crate features postgresql
and sqlite
only.
pub fn returning(self, output_name: &str) -> Self
postgresql
and sqlite
only.The returning
clause
§Example
let insert_query = sql::Insert::new()
.insert_into("users")
.returning("id")
.returning("login")
.to_string();
Output
INSERT INTO users RETURNING id, login
sourcepub fn with(self, name: &str, query: impl WithQuery + 'static) -> Self
Available on crate features postgresql
and sqlite
only.
pub fn with(self, name: &str, query: impl WithQuery + 'static) -> Self
postgresql
and sqlite
only.The with
clause
§Example
let active_users = sql::Select::new()
.select("*")
.from("users_bk")
.where_clause("ative = true");
let insert_query = sql::Insert::new()
.with("active_users", active_users)
.insert_into("users")
.select(sql::Select::new().select("*").from("active_users"))
.to_string();
Output
WITH active_users AS (
SELECT *
FROM users_bk
WHERE ative = true
)
INSERT INTO users
SELECT *
FROM active_users
source§impl Insert
impl Insert
sourcepub fn insert_or(self, expression: &str) -> Self
Available on crate feature sqlite
only.
pub fn insert_or(self, expression: &str) -> Self
sqlite
only.The insert or <keyword> into
clause
§Example
let insert = sql::Insert::new()
.insert_or("abort into users (login, name)");
let insert = sql::Insert::new()
.insert_or("fail into addresses (state, country)")
.insert_or("abort into users (login, name)");
Output
INSERT OR abort into users (login, name)
sourcepub fn replace_into(self, expression: &str) -> Self
Available on crate feature sqlite
only.
pub fn replace_into(self, expression: &str) -> Self
sqlite
only.The replace into
clause, this method overrides the previous value
§Example
let insert = sql::Insert::new()
.replace_into("users (login, name)");
Output
REPLACE INTO users (login, name)