sql_tools

Trait QueryBuilder

Source
pub trait QueryBuilder {
    // Required methods
    fn select(&self, table: &str, columns: Vec<&str>) -> SelectProps;
    fn update(&self, table: &str) -> UpdateProps;
    fn insert<T: ToSQLData>(
        &self,
        table: &str,
        data: Vec<Vec<T>>,
    ) -> InsertProps;
    fn create(&self) -> CreateProps;
}
Expand description

Trait used for the SQL Database types found in SQLVariation to implement basic SQL queries.

Required Methods§

Source

fn select(&self, table: &str, columns: Vec<&str>) -> SelectProps

Creates a new SelectProps to start building out a select query.

For the select method, you add the table you want to select from, then the columns in a vector. If you want to select all, simply input vec!["*"]. You can add a where_clause::WhereSelect to filter out the rows you want, just like writing a SQL query.

let conn = OracleConnect::new(connection_string, username, password);
let data: Vec<Vec<SQLDataTypes>> = conn
    .select("regional_sales", vec!["product_id", "revenue", "sale_date"])
    .where_in("product_id", vec!["1001", "4567"])
    .and_not("city", vec!["Austin", "Dallas"])
    .build()?;
data.iter().for_each(|row: &Vec<SQLDataTypes>| { println!("{:?}", row) });

Is the same as:

SELECT product_id, revenue, sale_date FROM regional_sales
WHERE product_id IN ('1001', '4567')
AND city NOT IN ('Austin', 'Dallas');
Source

fn update(&self, table: &str) -> UpdateProps

Creates a new UpdateProps to start building out an update query.

Updates a table’s column(s) based on criteria set in the optional where clauses. Updates can return Ok() or the number of rows that were updated.

let conn = OracleConnect::new(connection_string, username, password);
conn.update("global_sales")
    .set("continent", "North America")
    .where_in("country", vec!["Canada", "United States", "Mexico"])
    .build()?;
// If you want to get the number of rows that were updated
let count: usize = conn
    .update("global_sales")
    .set("continent", "North America")
    .where_in("country", vec!["Canada", "United States", "Mexico"])
    .build_return_count()?;
Source

fn insert<T: ToSQLData>(&self, table: &str, data: Vec<Vec<T>>) -> InsertProps

Creates a new InsertProps to start building out an insert query.

Inserts a grid (two-dimensional vector) of data into your database. Can take any type that has the ToSQLData trait implemented. If the table does not exist, it will automatically create a new table (will have an abort option in a future update).

let conn = OracleConnect::new(connection_string, username, password);
let data: Vec<Vec<&str>> = vec![
    vec!["Column_A", "Column_B", "Column_C"],
    vec!["a1", "b1", "c1"],
    vec!["a2", "b2", "c2"],
    vec!["a3", "b3", "c3"],
];
conn.insert("my_table", data).build()?;
Source

fn create(&self) -> CreateProps

Creates a new CreateProps to start building a create query.

Creates a table using a vector of the CreateColumns struct and the CreateDataTypes to apply the correct types to the new columns.

let conn = OracleConnect::new(connection_string, username, password);
let columns = vec![
        CreateColumns{ name: "Column_A".to_string(), data_type: CreateDataTypes::VARCHAR(20 as usize) },
        CreateColumns{ name: "Column_B".to_string(), data_type: CreateDataTypes::NUMBER },
        CreateColumns{ name: "Column_C".to_string(), data_type: CreateDataTypes::FLOAT },
    ];

    conn.create()
        .table("my_table", columns)
        .build()?;

You can add a column after you initiated the create table process.

let my_table: CreateTable = conn.create()
    .table("my_table", columns);

if add_date_col == true {
    my_table.add_column("Column_D".to_string(), CreateDataTypes::DATE);
}

my_table.build()?;

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§