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

source

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)
source

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
source

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
source

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)
-- ------------------------------------------------------------------------------
source

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
source

pub fn new() -> Self

Creates instance of the AlterTable command

source

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.

source

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
source

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
source

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

source

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

Available on crate features 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

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

Available on crate features postgresql and sqlite only.

Changes the name of the table

§Example
let query = sql::AlterTable::new()
  .alter_table("users")
  .rename_to("users_old")
  .to_string();

Outputs

ALTER TABLE users RENAME TO users_old
source§

impl AlterTable

source

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

Available on crate feature 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

source§

fn clone(&self) -> AlterTable

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 AlterTable

source§

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

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

impl Default for AlterTable

source§

fn default() -> AlterTable

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

impl Display for AlterTable

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> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.