SQL Tools
A rust crate meant to make SQL queries simple and communication between various SQL versions easy. The goal is to have most major variations of SQL compatible, however only Oracle SQL and SQLite are available right now. This is an evolution of oracle_sql_tools. This project is constantly changing, check out the change logs to see what's new.
How To Use
In your cargo.toml file:
[]
= "<CURRENT_VERSION>"
# chrono is required if you're working with dates
= "0.4.41"
To start using SQL Tools, you need a new connection.
Oracle SQL
use OracleConnect;
let username = "new_user";
let password = "password123!";
let connection_string = "my-secure.connection/database";
let conn = new?;
SQLite
You can open a SQLite connection in a db file or open a connection in memory.
use SQLiteConnect;
// Open connection from a file
let path = "<PATH_TO_DB_FILE>";
let conn = from_path;
// Open connection from memory
let conn = in_memory;
Once you established a connection type, you can use the various methods in this crate to interact with your database. These options are select, update, insert, create, delete, and alter. The data types that are supported by default can be found in the docs under the ToSQLData trait. You can implement ToSQLData for your own enum or struct to make integration into your application easy.
SQLDataTypes
This is the enum that is used to apply the proper type to the data that's being selected, updated, or inserted.
SELECT
For the select method, you add the table you want to select from, then you can choose specific columns, functions, varchars, or everything from a particular table. To see these options more in-depth, look at the Column enum. You can add a conjunction statement to filter out the rows you want, just like a SQL query.
let product_ids = Values;
let state = Like;
let cities = Values;
let columns = vec!;
let data: = conn
.select
.inner_join
.where_in
.and
.and_not
.build?;
data.iter.for_each;
UPDATE
Updates a table's column(s) based on criteria set with an optional conjunction statement. Updates can return Ok() or the number of rows that were updated.
let north_american_countries = vec!;
let na_countries_formatted = north_american_countries.iter.map.;
let countries = Values;
conn.update
.set
.where_in
.build?;
// If you want to get the number of rows that were updated
let count: usize = conn
.update
.set
.where_in
.build_return_count?;
INSERT
Inserts a grid (two-dimensional vector) of data into your database. Can take any type that has the ToSQLData trait implemented.
let data: = vec!;
conn.insert?.build?;
If the table does not exist, you can add the create_table() method to automatically create the table.
conn.insert?.create_table.build?;
If you have a grid of strings that have integers, dates, etc.. that you want to be formatted properly before being inserted into a table then you want to add the .format_grid_strings() method.
let data: = vec!;
conn.insert?
// Will convert the "date_sold" column into chrono::NaiveDateTime
// and the "price" column into f64.
.format_grid_strings?
.build?;
CREATE
Creates a table using a vector of the CreateColumns struct and the CreateDataTypes to apply the correct types to the new columns.
let columns = vec!;
conn.create
.table
.build?;
You can add a column after you initiated the create table process.
let my_table: CreateTable = conn.create
.table;
if add_date_col == true
my_table.build?;
DELETE
Deletes rows in a table based on the where methods added to the DeleteProps. If no where methods are added, it will delete all data in the table.
let terminated = Query;
conn.delete
.where_in
.build?;
ALTER
Alters a table by renaming it or adding, modifying, dropping, or renaming a column.
// renaming a table
conn.alter
.table
.rename
.build?;
// Adding a column
let column = AlterColumns ;
conn.alter
.table
.add
.build?;
// Modifying a column (very similar to adding)
conn.alter
.table
.modify
.build?;
// Dropping a column
conn.alter
.table
.drop
.build?;
// Renaming a column
conn.alter
.table
.rename_column
.build?;
Where
Conjunction statements are split into 4 categories via the WhereArg enum to prevent SQL injections, potential issues with NULL values, and for more intentional query structure.
Valuesis a vector of any data type that has is implemented for theToSQLDatatrait. This would be used as if you have a basic WHERE clause that you have set values for.
.where_in
WHERE column IN ('one', 'two', 'three');
Likeis used the same way as a SQL LIKE statement.
.where_in
WHERE column IN ('Hello Wor%');
Queryis when you want to select values from another table.
.where_in
WHERE column IN (SELECT value FROM another_table)
NULLis for selecting NULL values.
.where_in
WHERE column IS NULL
ToSQLData
ToSQLData is the trait that is used to convert various data types to SQLDataTypes.
To implement a local enum:
To implement a foreign enum:
use SomeForeignType;
;