Struct CreateIndex

Source
pub struct CreateIndex { /* private fields */ }
Expand description

Builder to contruct a CreateIndex command. Available only for the crate features postgresql and sqlite.

Basic API

use sql_query_builder as sql;

let query = sql::CreateIndex::new()
  .create_index("users_name_idx")
  .on("users")
  .column("name")
  .as_string();

Output

CREATE INDEX users_name_idx ON users (name)

Implementations§

Source§

impl CreateIndex

Source

pub fn as_string(&self) -> String

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

§Example
let query = sql::CreateIndex::new()
  .create_index("users_name_idx")
  .as_string();

Output

CREATE INDEX users_name_idx
Source

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

Defines the column of the table used to create the index

§Example
 let query = sql::CreateIndex::new()
   .on("users")
   .column("login")
   .column("name")
   .as_string();

Outputs

ON users (login, name)
Source

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

Defines a create index parameter, this method overrides the previous value

§Example
 let query = sql::CreateIndex::new()
   .create_index("users_name_idx")
   .create_index("orders_product_name_idx")
   .as_string();

Outputs

CREATE INDEX orders_product_name_idx
Source

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

Defines a create index parameter with the if not exists modifier, this method overrides the previous value

§Example
let query = sql::CreateIndex::new()
  .create_index("users_name_idx")
  .create_index_if_not_exists("orders_product_name_idx")
  .to_string();

Outputs

CREATE INDEX IF NOT EXISTS orders_product_name_idx
Source

pub fn debug(self) -> Self

Prints the current state of the CreateIndex 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::CreateIndex::new()
  .create_index("users_name_idx")
  .on("users")
  .column("name")
  .debug()
  .as_string();

Prints to the standard output

-- ------------------------------------------------------------------------------
CREATE INDEX users_name_idx
ON users (name)
-- ------------------------------------------------------------------------------
Source

pub fn new() -> Self

Creates instance of the CreateIndex command

Source

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

Defines the on table_name clause, this method overrides the previous value

§Example
 let query = sql::CreateIndex::new()
   .on("users")
   .on("orders")
   .as_string();

Outputs

ON orders
Source

pub fn print(self) -> Self

Prints the current state of the CreateIndex 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 index command.

§Example
let create_index_query = sql::CreateIndex::new()
  .raw("/* start index command */")
  .create_index("users_name_idx")
  .as_string();

Output

/* start index command */ CREATE INDEX users_name_idx
Source

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

Adds a raw SQL query after a specified parameter.

The CreateIndexParams::CreateIndex works both to .create_index and .create_index_if_not_exists methods.

§Example
let raw = "/* after create index */";

let query = sql::CreateIndex::new()
  .create_index("users_name_idx")
  .raw_after(sql::CreateIndexParams::CreateIndex, raw)
  .on("users")
  .column("name")
  .as_string();

Output

CREATE INDEX users_name_idx
/* after create index */
ON users (name)
Source

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

Adds a raw SQL query before a specified parameter.

The CreateIndexParams::CreateIndex works both to .create_index and .create_index_if_not_exists methods.

§Example
let raw = "/* before create index */";

let query = sql::CreateIndex::new()
  .raw_before(sql::CreateIndexParams::CreateIndex, raw)
  .create_index("users_name_idx")
  .on("users")
  .column("name")
  .as_string();

Output

/* before create index */
CREATE INDEX users_name_idx
ON users (name)
Source

pub fn unique(self) -> Self

Defines the unique parameter

§Example
let query = sql::CreateIndex::new()
  .create_index("users_name_idx")
  .unique()
  .to_string();

Outputs

CREATE UNIQUE INDEX users_name_idx
Source

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

The method will concatenate multiples calls using the and operator. This method is un alias of where_clause.

§Example
let select_query = sql::CreateIndex::new()
  .create_index("users_name_idx")
  .on("users")
  .column("name")
  .where_and("created_at >= $1")
  .as_string();

Outputs

CREATE INDEX users_name_idx
ON users (name)
WHERE created_at >= $1
Source

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

The where clause, this method will concatenate multiples calls using the and operator. If you intended to use the or operator you should use the where_or method

§Example
let select_query = sql::CreateIndex::new()
  .create_index("users_name_idx")
  .on("users")
  .column("name")
  .where_clause("status = 'active'")
  .as_string();

Outputs

CREATE INDEX users_name_idx
ON users (name)
WHERE status = 'active'
Source

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

The where clause that concatenate multiples calls using the OR operator. If you intended to use the and operator you should use the where_clause method

§Example
let select_query = sql::CreateIndex::new()
  .create_index("users_name_idx")
  .on("users")
  .column("name")
  .where_clause("created_at >= $1")
  .where_or("status = 'active'")
  .as_string();

Outputs

CREATE INDEX users_name_idx
ON users (name)
WHERE
  created_at >= $1
  OR status = 'active'
Source§

impl CreateIndex

Source

pub fn concurrently(self) -> Self

Available on crate feature postgresql only.

Defines the concurrently parameter

§Example
let query = sql::CreateIndex::new()
  .create_index("users_name_idx")
  .concurrently()
  .to_string();

Outputs

CREATE INDEX CONCURRENTLY users_name_idx
Source

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

Available on crate feature postgresql only.

Defines the include parameter

§Example
 let query = sql::CreateIndex::new()
   .include("login")
   .include("name")
   .as_string();

Outputs

INCLUDE (login, name)
Source

pub fn only(self) -> Self

Available on crate feature postgresql only.

Defines the only parameter

§Example
let query = sql::CreateIndex::new()
  .on("users")
  .only()
  .to_string();

Outputs

ON ONLY users
Source

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

Available on crate feature postgresql only.

Defines the index method to be used to create the index, this method overrides the previous value

§Example
 let query = sql::CreateIndex::new()
   .using("btree")
   .using("gist")
   .as_string();

Outputs

USING gist

Trait Implementations§

Source§

impl Clone for CreateIndex

Source§

fn clone(&self) -> CreateIndex

Returns a duplicate 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 CreateIndex

Source§

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

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

impl Default for CreateIndex

Source§

fn default() -> CreateIndex

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

impl Display for CreateIndex

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§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

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

Source§

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§

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>,

Source§

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>,

Source§

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.