This crate is still under construction, apis may subject to change.
For now only the basic function is accomplished, check it out below.
When defining structs, make sure keep the field sequence consistent with the sequence in database as bulk insert(insert_many) depends on it. ```rust use ssql::prelude::*; use serde::{Deserialize, Serialize};
struct Person {
#[ssql(primary_key)]
id: i32,
email: Option
struct Posts { id: i32, post: String, #[ssql(foreignkey = "SCHEMA1.Person.id")] // if it belongs to default schema, just write TABLE.COLUMN personid: i32, }
async fn get<'a>(client: &'a mut tiberius::Client
// return a vector of struct
let vec1 = query.get_struct::<Posts>(&mut client).await?;
let (vec1, vec2) = query.get_struct_2::<Person, Posts>(&mut client).await?;
// return a vector of serde_json::Value;
let vec1 = query.get_serialized::<Person>(&mut client).await?;
// with polars feature enabled, return DataFrame;
let (df1, df2) = query.get_dataframe_2::<Person, Posts>(&mut client).await?;
let new_p = Person {
id: 2,
email: Some("a@a.com".to_string()),
};
//insert with data in this instance.
new_p.insert(&mut client);
// delete it based on its primary key mark.
// like here i mark id with #[ssql(primary_key)]
new_p.delete(&mut client);
// update it based on its primary key mark.
new_p.update(&mut client);
// insert many accepts anything that can turn into iterator and return specific type, here is <Person>
let vec = vec![new_p.clone(), new_p.clone()];
Person::insert_many(vec, &mut client);
let it = vec![1, 2, 3].into_iter().zip(
vec!["a", "b", "c"].into_iter()
).map(|(id, email)| Person {
id,
email: email.to_string(),
});
Person::insert_many(it, &mut client);
// structs reflecting complex raw query
// leave the table attribute empty
#[derive(ORM, Debug, Default, Serialize, Deserialize)]
#[ssql(table)]
pub struct PersonRaw {
#[ssql(primary_key)]
pub(crate) id: i32,
pub(crate) email: String,
dt: Option<NaiveDateTime>
}
let query = PersonRaw::query()
.raw("SELECT * FROM Person where id = @p1", &[&1]);
let data = query.get_struct::<PersonRaw>(&mut client).await;
}
```
- handling multiple relationships
- build filter pattern
- support raw sql string query
- handle non-manual input key like auto-generated id