Function scooby::postgres::statements::insert_into
source · pub fn insert_into(table_name: impl Into<TableName>) -> BareInsertInto
Expand description
Start building a new INSERT INTO
statement with the given table name.
Returns a BareInsertInto
structure which requires that you specify
what type of a VALUES
clause you wish to have:
- For
DEFAULT VALUES
, calldefault_values
- For
VALUES (...)
with unspecified columns, callvalues
- For
(...) VALUES (...)
, callcolumns
First two options will give you an InsertInto
structure directly
Option 3 will expect you to specify at least one set of values through values
method
Call to_string
on the final InsertInto
structure to finalize and get an SQL string.
Supported clauses
Clause | Method |
---|---|
VALUES | values |
ON CONFLICT | on_conflict |
RETURNING | returning |
Specifying a WITH
clause
To create an INSERT INTO
statement with a WITH
clause, start with with
instead of this function.
Examples
use scooby::postgres::insert_into;
let sql = insert_into("Dummy").default_values().to_string();
assert_eq!(sql, "INSERT INTO Dummy DEFAULT VALUES")
use scooby::postgres::{insert_into, Parameters};
let mut params = Parameters::new();
let sql = insert_into("Rectangle")
.columns(("width", "height"))
.values([params.next_array()])
.returning("id")
.to_string();
assert_eq!(sql, "INSERT INTO Rectangle (width, height) VALUES ($1, $2) RETURNING id");