pub struct Insert { /* private fields */ }Expand description
Builder of Insert command.
Basic API
use sql_query_builder as sql;
let query = sql::Insert::new()
.insert_into("users (login, name)")
.values("('foo', 'Foo')")
.values("('bar', 'Bar')")
.as_string();
Output
INSERT INTO users (login, name) VALUES ('foo', 'Foo'), ('bar', 'Bar')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 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 insert_into(self, table_name: &str) -> Self
pub fn insert_into(self, table_name: &str) -> Self
The insert into clause. This method overrides the previous value
§Example
let insert = sql::Insert::new()
.insert_into("users (login, name)");
let insert = sql::Insert::new()
.insert_into("addresses (state, country)")
.insert_into("users (login, name)");
§Mutually exclusive methods
Avaliable on sqlite only
Methods Insert::insert_into, Insert::insert_or and Insert::replace_into are mutually exclusive, the last called will overrides the previous ones.
let insert = sql::Insert::new()
.insert_into("users (login, name)")
.replace_into("users (login, name)");Output
REPLACE INTO users (login, name)Avaliable on mysql only
Methods Insert::insert_into and the splitted version (Insert::insert, Insert::into, Insert::column) are mutually exclusive, the last called will overrides the previous ones.
let insert = sql::Insert::new()
.insert_into("users (login, name)")
.insert("low_priority")
.into("users")
.column("login");Output
INSERT low_priority INTO users (login)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 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 = trueSourcepub 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 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 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 query = sql::Insert::new()
.raw_before(sql::InsertClause::Values, raw)
.values("('bar', 'Bar')")
.as_string();
Output
insert into users (login, name) VALUES ('bar', 'Bar')Sourcepub fn values(self, expression: &str) -> Self
pub fn values(self, expression: &str) -> Self
The values clause
§Example
let query = sql::Insert::new()
.insert_into("users (login, name)")
.values("('foo', 'Foo')")
.values("('bar', 'Bar')")
.as_string();
Output
INSERT INTO users (login, name) VALUES ('foo', 'Foo'), ('bar', 'Bar')§Composable methods
The methods Insert::values, Insert::row are composable, when both was used each line will receive the contructor clause row.
§Example
let query = sql::Insert::new()
.values("('foo', 'Foo')")
.row("('bar', 'Bar')")
.as_string();Output
VALUES ROW('foo', 'Foo'), ROW('bar', 'Bar')§Mutually exclusive methods
The methods Insert::values/Insert::row, Insert::select and Insert::set are mutually exclusive, the last called will overrides the previous ones.
Insert::row and Insert::set are available on crate feature mysql only.
§Example
let query = sql::Insert::new()
.values("('foo', 'Foo')")
.row("('bar', 'Bar')")
.set("login = 'foo'")
.set("name = 'Foo'")
.as_string();Output
SET login = 'foo', name = 'Foo'Source§impl Insert
impl Insert
Sourcepub fn on_conflict(self, conflict: &str) -> Self
Available on crate features postgresql and sqlite only.
pub fn on_conflict(self, conflict: &str) -> Self
postgresql and sqlite only.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 nothingSourcepub 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 query = sql::Insert::new()
.insert_into("users")
.returning("id")
.returning("login")
.to_string();
Output
INSERT INTO users RETURNING id, loginSourcepub fn with(
self,
name: &str,
query: impl WithQuery + 'static + Send + Sync,
) -> Self
Available on crate features postgresql and sqlite only.
pub fn with( self, name: &str, query: impl WithQuery + 'static + Send + Sync, ) -> Self
postgresql and sqlite only.The with clause
§Example
let active_users = sql::Select::new()
.select("*")
.from("users_bk")
.where_clause("ative = true");
let 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_usersSource§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, table_name: &str) -> Self
Available on crate feature sqlite only.
pub fn replace_into(self, table_name: &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)Source§impl Insert
impl Insert
Sourcepub fn column(self, column_name: &str) -> Self
Available on crate feature mysql only.
pub fn column(self, column_name: &str) -> Self
mysql only.Defines the columns of the table used to insert values.
§Example
let query = sql::Insert::new()
.into("users")
.column("login")
.column("name")
.as_string();
Outputs
INTO users (login, name)Sourcepub fn insert(self, modifier: &str) -> Self
Available on crate feature mysql only.
pub fn insert(self, modifier: &str) -> Self
mysql only.The insert clause, used to defined modifiers to change de insert execution
§Example
let query = sql::Insert::new()
.insert("LOW_PRIORITY")
.into("users")
.column("login")
.as_string();
Outputs
INSERT LOW_PRIORITY INTO users (login)Sourcepub fn into(self, table: &str) -> Self
Available on crate feature mysql only.
pub fn into(self, table: &str) -> Self
mysql only.The into clause, defines the name of the table to be used
§Example
let query = sql::Insert::new()
.into("users")
.column("login")
.as_string();
Outputs
INTO users (login)Sourcepub fn partition(self, name: &str) -> Self
Available on crate feature mysql only.
pub fn partition(self, name: &str) -> Self
mysql only.The partition clause
§Example
let query = sql::Insert::new()
.into("employees")
.partition("p1")
.to_string();
Output
INTO employees PARTITION (p1)Sourcepub fn on_duplicate_key_update(self, assignment: &str) -> Self
Available on crate feature mysql only.
pub fn on_duplicate_key_update(self, assignment: &str) -> Self
mysql only.The ON DUPLICATE KEY UPDATE clause
§Example
let query = sql::Insert::new()
.insert_into("t1 (a, b, c)")
.values("(1, 2, 3)")
.on_duplicate_key_update("c = c+1")
.as_string();
Output
INSERT INTO t1 (a, b, c)
VALUES (1, 2, 3)
ON DUPLICATE KEY UPDATE c = c+1Sourcepub fn row(self, expression: &str) -> Self
Available on crate feature mysql only.
pub fn row(self, expression: &str) -> Self
mysql only.The values clause with the row constructor clause
§Example
let query = sql::Insert::new()
.insert_into("users (login, name)")
.row("('foo', 'Foo')")
.row("('bar', 'Bar')")
.as_string();
Output
INSERT INTO users (login, name) VALUES ROW('foo', 'Foo'), ROW('bar', 'Bar')