Struct sql_query_builder::CreateTable

source ·
pub struct CreateTable { /* private fields */ }
Expand description

Builder to contruct a CreateTable command

Basic API

use sql_query_builder as sql;

let query = sql::CreateTable::new()
  .create_table("users")
  .column("id serial primary key")
  .column("login varchar(40) not null")
  .constraint("users_login_key unique(login)")
  .as_string();

Output (indented for readability)

CREATE TABLE users (
  id serial primary key,
  login varchar(40) not null,
  created_at timestamp not null,
  CONSTRAINT users_login_key unique(login)
)

Implementations§

source§

impl CreateTable

source

pub fn as_string(&self) -> String

Gets the current state of the CreateTable and returns it as string

§Example
let query = sql::CreateTable::new()
  .create_table("users")
  .column("name varchar(100) not null")
  .as_string();

Output

CREATE TABLE users (
  name varchar(100) not null
)
source

pub fn column(self, column: &str) -> Self

Define a column to be passed as arguments to the create table command, multiples call will concatenates all column parameters

§Example
let query = sql::CreateTable::new()
  .column("id serial not null primary key")
  .column("name varchar(100) not null")
  .as_string();

Outputs

(
  id serial not null primary key,
  name varchar(100) not null
)
source

pub fn constraint(self, column: &str) -> Self

Defines a table constraint, multiples call will concatenates all constraints

§Example
let query = sql::CreateTable::new()
  .constraint("users_id_key PRIMARY KEY(id)")
  .constraint("users_login_key UNIQUE(login)")
  .as_string();

Outputs

(
  CONSTRAINT users_id_key PRIMARY KEY(id),
  CONSTRAINT users_login_key UNIQUE(login)
)
source

pub fn create_table(self, table_name: &str) -> Self

Defines a create table signature. Multiples calls will overrides the previous value

§Example
 let create_table = sql::CreateTable::new()
   .create_table("users")
   .create_table("orders");

Outputs

CREATE TABLE orders
source

pub fn create_table_if_not_exists(self, table_name: &str) -> Self

Defines a create table signature with the modifer if not exists. Multiples calls will overrides the previous value

§Example
let create_table = sql::CreateTable::new()
  .create_table("users")
  .create_table_if_not_exists("orders");

Outputs

CREATE TABLE IF NOT EXISTS orders
source

pub fn debug(self) -> Self

Prints the current state of the CreateTable 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::CreateTable::new()
  .create_table("users")
  .column("name varchar(100) not null")
  .column("login varchar(40) not null")
  .constraint("users_login_key unique(login)")
  .debug()
  .as_string();

Prints to the standard output

-- ------------------------------------------------------------------------------
CREATE TABLE users (
  name varchar(100) not null,
  login varchar(40) not null,
  CONSTRAINT users_login_key unique(login)
)
-- ------------------------------------------------------------------------------
source

pub fn foreign_key(self, column: &str) -> Self

Defines a foreign key constraint, multiples call will concatenates all foreign keys

§Example
let query = sql::CreateTable::new()
  .foreign_key("(user_id) refereces users")
  .foreign_key("(address_id) refereces address(id)")
  .as_string();

Outputs

(
  FOREIGN KEY(user_id) refereces users,
  FOREIGN KEY(address_id) refereces address (id)
)
source

pub fn new() -> Self

Creates instance of the CreateTable command

source

pub fn primary_key(self, column: &str) -> Self

Defines a primary key constraint. Multiples calls will overrides the previous value

§Example
let query = sql::CreateTable::new()
  .create_table("users")
  .primary_key("id")
  .as_string();

Outputs

CREATE TABLE users (
  PRIMARY KEY(id)
)
source

pub fn print(self) -> Self

Prints the current state of the CreateTable to the standard output similar to debug method, the difference is that this method prints in one line.

source

pub fn raw(self, raw_sql: &str) -> Self

Adds at the beginning a raw SQL query. Is useful to create a more complex create table signature like the example below.

§Example
let create_table_query = sql::CreateTable::new()
  .raw("CREATE LOCAL TEMP TABLE IF NOT EXISTS users_temp")
  .column("login VARCHAR(40) NOT NULL")
  .as_string();

Output

CREATE LOCAL TEMP TABLE IF NOT EXISTS users_temp (
  login VARCHAR(40) NOT NULL
)
source

pub fn raw_after(self, param: CreateTableParams, raw_sql: &str) -> Self

Adds a raw SQL query after a specified parameter.

§Example
let raw = "(name varchar(100) not null)";

let query = sql::CreateTable::new()
  .create_table("users")
  .raw_after(sql::CreateTableParams::CreateTable, raw)
  .as_string();

Output

CREATE TABLE users (name varchar(100) not null)
source

pub fn raw_before(self, param: CreateTableParams, raw_sql: &str) -> Self

Adds a raw SQL query before a specified parameter.

§Example
let raw = "name varchar(100) not null, ";

let query = sql::CreateTable::new()
  .raw_before(sql::CreateTableParams::Column, raw)
  .column("login varchar(40) not null")
  .as_string();

Output

(name varchar(100) not null, login varchar(40) not null)

Trait Implementations§

source§

impl Clone for CreateTable

source§

fn clone(&self) -> CreateTable

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for CreateTable

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for CreateTable

source§

fn default() -> CreateTable

Returns the “default value” for a type. Read more
source§

impl Display for CreateTable

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.