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
impl CreateTable
sourcepub fn as_string(&self) -> String
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
)
sourcepub fn column(self, column: &str) -> Self
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
)
sourcepub fn constraint(self, column: &str) -> Self
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)
)
sourcepub fn create_table(self, table_name: &str) -> Self
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
sourcepub fn create_table_if_not_exists(self, table_name: &str) -> Self
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
sourcepub fn debug(self) -> Self
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)
)
-- ------------------------------------------------------------------------------
sourcepub fn foreign_key(self, column: &str) -> Self
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)
)
sourcepub fn primary_key(self, column: &str) -> Self
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)
)
sourcepub fn print(self) -> Self
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.
sourcepub fn raw(self, raw_sql: &str) -> Self
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
)
sourcepub fn raw_after(self, param: CreateTableParams, raw_sql: &str) -> Self
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)
sourcepub fn raw_before(self, param: CreateTableParams, raw_sql: &str) -> Self
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
impl Clone for CreateTable
source§fn clone(&self) -> CreateTable
fn clone(&self) -> CreateTable
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl Debug for CreateTable
impl Debug for CreateTable
source§impl Default for CreateTable
impl Default for CreateTable
source§fn default() -> CreateTable
fn default() -> CreateTable
Auto Trait Implementations§
impl Freeze for CreateTable
impl RefUnwindSafe for CreateTable
impl Send for CreateTable
impl Sync for CreateTable
impl Unpin for CreateTable
impl UnwindSafe for CreateTable
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)