pub trait SsqlMarker: Send + Sync {
// Required methods
fn query<'a>() -> QueryBuilderI<'a, Self>
where Self: Sized;
fn insert_many<I: IntoIterator<Item = Self> + Send>(
iter: I,
conn: &mut Client<Compat<TcpStream>>,
) -> impl Future<Output = SsqlResult<u64>> + Send
where I::IntoIter: Send,
Self: Sized;
fn insert(
self,
conn: &mut Client<Compat<TcpStream>>,
) -> impl Future<Output = SsqlResult<()>> + Send;
fn insert_ignore_pk(
self,
conn: &mut Client<Compat<TcpStream>>,
) -> impl Future<Output = SsqlResult<()>> + Send
where Self: Sized;
fn delete(
self,
conn: &mut Client<Compat<TcpStream>>,
) -> impl Future<Output = SsqlResult<()>> + Send
where Self: Sized;
fn update(
&self,
conn: &mut Client<Compat<TcpStream>>,
) -> impl Future<Output = SsqlResult<()>> + Send
where Self: Sized;
// Provided methods
fn raw_query<'a>(
sql: &str,
params: &[&'a dyn ToSql],
) -> RawQueryBuilder<'a, Self>
where Self: Sized { ... }
fn col(field: &'static str) -> SsqlResult<ColExpr>
where Self: Sized { ... }
}
Expand description
a trait automatically derived via #[derive(ORM)]
macro, all these methods are available.
Required Methods§
Sourcefn query<'a>() -> QueryBuilderI<'a, Self>where
Self: Sized,
fn query<'a>() -> QueryBuilderI<'a, Self>where
Self: Sized,
Generate a query builder for the struct.
Sourcefn insert_many<I: IntoIterator<Item = Self> + Send>(
iter: I,
conn: &mut Client<Compat<TcpStream>>,
) -> impl Future<Output = SsqlResult<u64>> + Send
fn insert_many<I: IntoIterator<Item = Self> + Send>( iter: I, conn: &mut Client<Compat<TcpStream>>, ) -> impl Future<Output = SsqlResult<u64>> + Send
Bulk insert, takes everything that can be turned into iterator that generate specific structs.
// example1:
Person::insert_many(vec![Person{id: 1,email: Some("a@gmail.com".to_string())},
Person{id: 2,email: Some("b@gmail.com".to_string())}], &mut conn).await;
// example2:
Person::insert_many((1..=3).zip(vec!["a@gmail.com", "b@gmail.com", "c@gmail.com"])
.map(|(idx, mail)| {
Person {
id: idx,
email: Some(mail.to_string()),
}
}), &mut conn).await
Sourcefn insert(
self,
conn: &mut Client<Compat<TcpStream>>,
) -> impl Future<Output = SsqlResult<()>> + Send
fn insert( self, conn: &mut Client<Compat<TcpStream>>, ) -> impl Future<Output = SsqlResult<()>> + Send
Insert one item, consume self.
#[derive(ORM)]
#[ssql(table = person)]
struct Person{
id: i32,
email: Option<String>,
}
let person = Person{id: 1,email: Some("a@gmail.com".to_string())};
person.insert(&mut conn).await;
SQL: INSERT INTO person (id, email) VALUES ( 1, 'a@gmail.com')
Sourcefn insert_ignore_pk(
self,
conn: &mut Client<Compat<TcpStream>>,
) -> impl Future<Output = SsqlResult<()>> + Sendwhere
Self: Sized,
fn insert_ignore_pk(
self,
conn: &mut Client<Compat<TcpStream>>,
) -> impl Future<Output = SsqlResult<()>> + Sendwhere
Self: Sized,
Insert one item while ignoring the primary key.
Specified for those using Identity
or Auto-Increment
as primary key.
If primary key is not set, this fn will perform as same as insert
#[derive(ORM)]
#[ssql(table = person)]
struct Person{
#[ssql(primary_key)]
id: i32,
email: Option<String>,
}
let person = Person{id: 1,email: Some("a@gmail.com".to_string())};
person.insert(&mut conn).await;
SQL: INSERT INTO person (email) VALUES ('a@gmail.com')
Sourcefn delete(
self,
conn: &mut Client<Compat<TcpStream>>,
) -> impl Future<Output = SsqlResult<()>> + Sendwhere
Self: Sized,
fn delete(
self,
conn: &mut Client<Compat<TcpStream>>,
) -> impl Future<Output = SsqlResult<()>> + Sendwhere
Self: Sized,
Delete one item based on primary key, consume self. Will panic if primary key is not set.
#[derive(ORM)]
#[ssql(table = person)]
struct Person{
#[ssql(primary_key)]
id: i32,
email: Option<String>,
}
async fn _test(mut conn: Client<Compat<TcpStream>>) {
let person = Person{id: 1,email: Some("a@gmail.com".to_string())};
person.delete(&mut conn).await;
}
SQL: DELETE FROM person WHERE id = 1
Sourcefn update(
&self,
conn: &mut Client<Compat<TcpStream>>,
) -> impl Future<Output = SsqlResult<()>> + Sendwhere
Self: Sized,
fn update(
&self,
conn: &mut Client<Compat<TcpStream>>,
) -> impl Future<Output = SsqlResult<()>> + Sendwhere
Self: Sized,
Update one item based on primary key, borrow self. Will panic if primary key is not set.
#[derive(ORM)]
#[ssql(table = person)]
struct Person{
#[ssql(primary_key)]
id: i32,
email: Option<String>,
}
async fn _test(mut conn: Client<Compat<TcpStream>>) {
let person = Person{id: 1,email: Some("a@gmail.com".to_string())};
person.update(&mut conn).await;
}
SQL: UPDATE person SET email = 'a@gmail.com' WHERE id = 1
Provided Methods§
Sourcefn raw_query<'a>(
sql: &str,
params: &[&'a dyn ToSql],
) -> RawQueryBuilder<'a, Self>where
Self: Sized,
fn raw_query<'a>(
sql: &str,
params: &[&'a dyn ToSql],
) -> RawQueryBuilder<'a, Self>where
Self: Sized,
Generate raw query instance for the struct that can be used to perform query with raw SQL string.
#[derive(ORM)]
#[ssql(table)]
pub struct PersonRaw {
#[ssql(primary_key)]
id: i32,
email: String,
dt: Option<NaiveDateTime>
}
// make sure all fields in the struct are present in the query.
let query = PersonRaw::raw_query("SELECT id, email, dt FROM Person WHERE id = @p1", &[&1]);
Sourcefn col(field: &'static str) -> SsqlResult<ColExpr>where
Self: Sized,
fn col(field: &'static str) -> SsqlResult<ColExpr>where
Self: Sized,
Generate a Column Expression that can be used in filtering and ordering.
This method will failed if the given column name is no present in the struct.
Thus it returns SsqlResult
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.