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:

  1. For DEFAULT VALUES, call default_values
  2. For VALUES (...) with unspecified columns, call values
  3. For (...) VALUES (...), call columns

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

ClauseMethod
VALUESvalues
ON CONFLICTon_conflict
RETURNINGreturning

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");