Struct sql_query_builder::AlterTable
source · pub struct AlterTable { /* private fields */ }Expand description
Builder to contruct a AlterTable command
Basic API
use sql_query_builder as sql;
let query = sql::AlterTable::new()
.alter_table("users")
.add("COLUMN id serial primary key")
.add("COLUMN login varchar(40) not null")
.drop("CONSTRAINT users_login_key")
.as_string();
Output (indented for readability)
ALTER TABLE users
ADD COLUMN id serial primary key,
ADD COLUMN login varchar(40) not null,
DROP CONSTRAINT users_login_key
Implementations§
source§impl AlterTable
impl AlterTable
sourcepub fn add(self, add_exp: &str) -> Self
pub fn add(self, add_exp: &str) -> Self
Adds columns or table constraints. Multiples call of this method will build the SQL respecting the order of the calls
§Example
let query = sql::AlterTable::new()
.add("COLUMN age int not null")
.add("CONSTRAINT age check(age >= 0)")
.as_string();
Outputs
ADD COLUMN age int not null,
ADD CONSTRAINT age check(age >= 0)
sourcepub fn alter_table(self, table_name: &str) -> Self
pub fn alter_table(self, table_name: &str) -> Self
Defines the name of the table to be altered, this method overrides the previous value
§Example
let query = sql::AlterTable::new()
.alter_table("users")
.as_string();
Outputs
ALTER TABLE users
sourcepub fn as_string(&self) -> String
pub fn as_string(&self) -> String
Gets the current state of the AlterTable and returns it as string
§Example
let query = sql::AlterTable::new()
.alter_table("users")
.rename_to("users_old")
.as_string();
Output
ALTER TABLE users RENAME TO users_old
sourcepub fn debug(self) -> Self
pub fn debug(self) -> Self
Prints the current state of the AlterTable 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::AlterTable::new()
.alter_table("users")
.add("name varchar(100) not null")
.add("login varchar(40) not null")
.add("constraint users_login_key unique(login)")
.debug()
.as_string();Prints to the standard output
-- ------------------------------------------------------------------------------
ALTER TABLE users
ADD name varchar(100) not null,
ADD login varchar(40) not null,
ADD constraint users_login_key unique(login)
-- ------------------------------------------------------------------------------
sourcepub fn drop(self, drop_exp: &str) -> Self
pub fn drop(self, drop_exp: &str) -> Self
Drops columns or table constraints. Multiples call of this method will build the SQL respecting the order of the calls
§Example
let query = sql::AlterTable::new()
.drop("column login")
.as_string();
Outputs
DROP column login
sourcepub fn new() -> Self
pub fn new() -> Self
Creates instance of the AlterTable command
sourcepub fn print(self) -> Self
pub fn print(self) -> Self
Prints the current state of the AlterTable 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 alter table signature like the example below.
§Example
let create_table_query = sql::AlterTable::new()
.raw("ALTER TABLE IF EXISTS users")
.drop("legacy_column")
.as_string();
Output
ALTER TABLE IF EXISTS users DROP legacy_column
sourcepub fn raw_after(self, param: AlterTableAction, raw_sql: &str) -> Self
pub fn raw_after(self, param: AlterTableAction, raw_sql: &str) -> Self
Adds a raw SQL query after a specified parameter.
§Example
let raw = "ADD COLUMN name varchar(100) not null";
let query = sql::AlterTable::new()
.alter_table("users")
.raw_after(sql::AlterTableAction::AlterTable, raw)
.as_string();
Output
ALTER TABLE users ADD COLUMN name varchar(100) not null
sourcepub fn raw_before(self, action: AlterTableAction, raw_sql: &str) -> Self
pub fn raw_before(self, action: AlterTableAction, raw_sql: &str) -> Self
Adds a raw SQL query before a specified parameter.
§Example
let raw = "ALTER TABLE users";
let query = sql::AlterTable::new()
.raw_before(sql::AlterTableAction::AlterTable, raw)
.add("COLUMN id")
.as_string();
Output
ALTER TABLE users RENAME TO users_old
source§impl AlterTable
impl AlterTable
sourcepub fn rename(self, rename_exp: &str) -> Self
Available on crate features postgresql and sqlite only.
pub fn rename(self, rename_exp: &str) -> Self
postgresql and sqlite only.Changes the column names or table constraints. Multiples call of this method will build the SQL respecting the order of the calls
§Example
let query = sql::AlterTable::new()
.alter_table("users")
.rename("COLUMN address TO city")
.to_string();
Outputs
ALTER TABLE users RENAME COLUMN address TO city
source§impl AlterTable
impl AlterTable
sourcepub fn alter(self, alter_exp: &str) -> Self
Available on crate feature postgresql only.
pub fn alter(self, alter_exp: &str) -> Self
postgresql only.Alter columns or table constraints. Multiples call of this method will build the SQL respecting the order of the calls
§Example
let query = sql::AlterTable::new()
.alter("COLUMN created_at SET DEFAULT now()")
.to_string();
Outputs
ALTER COLUMN created_at SET DEFAULT now()
Trait Implementations§
source§impl Clone for AlterTable
impl Clone for AlterTable
source§fn clone(&self) -> AlterTable
fn clone(&self) -> AlterTable
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl Debug for AlterTable
impl Debug for AlterTable
source§impl Default for AlterTable
impl Default for AlterTable
source§fn default() -> AlterTable
fn default() -> AlterTable
Auto Trait Implementations§
impl Freeze for AlterTable
impl RefUnwindSafe for AlterTable
impl Send for AlterTable
impl Sync for AlterTable
impl Unpin for AlterTable
impl UnwindSafe for AlterTable
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)