pub struct AlterTable { /* private fields */ }Expand description
Builder of AlterTable command.
Basic API
use sql_query_builder as sql;
let query = sql::AlterTable::new()
.alter_table("users")
.add("COLUMN id serial primary key")
.as_string();
Output
ALTER TABLE users ADD COLUMN id serial primary keyImplementations§
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, this method overrides the previous value
§Example
let query = sql::AlterTable::new()
.add("COLUMN age int not null")
.as_string();
Outputs
ADD COLUMN age int not null§Available on crate feature postgresql and mysql only.
Multiples call of this method will build the SQL respecting the order of the calls
§Example
let query = sql::AlterTable::new()
.add("COLUMN login varchar not null")
.add("CONSTRAINT login_unique unique(login)")
.as_string();
Outputs
ADD COLUMN login varchar not null,
ADD CONSTRAINT login_unique unique(login)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 usersSourcepub 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_oldSourcepub 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")
.debug()
.as_string();Prints to the standard output
-- ------------------------------------------------------------------------------
ALTER TABLE users
ADD name varchar(100) not null
-- ------------------------------------------------------------------------------Sourcepub fn drop(self, drop_exp: &str) -> Self
pub fn drop(self, drop_exp: &str) -> Self
Drops columns or table constraints, this method overrides the previous value.
§Example
let query = sql::AlterTable::new()
.drop("column login")
.as_string();
Outputs
DROP column login§Available on crate feature postgresql and mysql only.
Multiples call of this method will build the SQL respecting the order of the calls
§Example
let query = sql::AlterTable::new()
.drop("column login")
.drop("constraint login_unique")
.as_string();
Outputs
DROP column login, DROP constraint login_uniqueSourcepub 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.
§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_columnSourcepub 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 nullSourcepub 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 command */";
let query = sql::AlterTable::new()
.raw_before(sql::AlterTableAction::AlterTable, raw)
.alter_table("users")
.as_string();
Output
/* alter table command */ ALTER TABLE usersSource§impl AlterTable
impl AlterTable
Sourcepub fn rename(self, action: &str) -> Self
Available on crate features postgresql and sqlite and mysql only.
pub fn rename(self, action: &str) -> Self
postgresql and sqlite and mysql only.Changes the column name or table constraints, this method overrides the previous value
§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§Available on crate feature mysql only.
Changes the table name, column name 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("TO users_old")
.rename("COLUMN name TO full_name")
.to_string();
Outputs
ALTER TABLE users
RENAME TO users_old,
RENAME COLUMN name TO full_nameSource§impl AlterTable
impl AlterTable
Sourcepub fn rename_to(self, table_name: &str) -> Self
Available on crate features postgresql and sqlite only.
pub fn rename_to(self, table_name: &str) -> Self
postgresql and sqlite only.Changes the name of the table, this method overrides the previous value
§Example
let query = sql::AlterTable::new()
.alter_table("users")
.rename_to("users_old")
.to_string();
Outputs
ALTER TABLE users RENAME TO users_oldSource§impl AlterTable
impl AlterTable
Sourcepub fn alter(self, alter_exp: &str) -> Self
Available on crate features postgresql and mysql only.
pub fn alter(self, alter_exp: &str) -> Self
postgresql and mysql 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 more