Example
This commit is contained in:
parent
9080d16c24
commit
98b0e9df95
@ -1,6 +1,7 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
".",
|
".",
|
||||||
|
"examples/sqlx-mysql",
|
||||||
]
|
]
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
|
10
examples/sqlx-mysql/Cargo.toml
Normal file
10
examples/sqlx-mysql/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "sea-orm-sqlx-mysql-example"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2018"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
async-std = { version = "^1.9", features = [ "attributes" ] }
|
||||||
|
sea-orm = { path = "../../", features = [ "sqlx-mysql", "runtime-async-std-native-tls" ] }
|
||||||
|
# sea-query = { path = "../../../sea-query" }
|
18
examples/sqlx-mysql/src/main.rs
Normal file
18
examples/sqlx-mysql/src/main.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
use sea_orm::{tests_cfg::*, Database, Entity};
|
||||||
|
|
||||||
|
#[async_std::main]
|
||||||
|
async fn main() {
|
||||||
|
let mut db = Database::default();
|
||||||
|
db.connect("mysql://sea:sea@localhost/bakery")
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
println!("{:?}", db);
|
||||||
|
println!("");
|
||||||
|
|
||||||
|
let rows = cake::Cake::find().all(&db).await.unwrap();
|
||||||
|
|
||||||
|
for row in rows.iter() {
|
||||||
|
println!("{:?}", row);
|
||||||
|
println!("");
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,7 @@
|
|||||||
|
mod select;
|
||||||
|
|
||||||
|
pub use select::*;
|
||||||
|
|
||||||
use crate::{DatabaseConnection, QueryResult};
|
use crate::{DatabaseConnection, QueryResult};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use sea_query::{inject_parameters, MySqlQueryBuilder, Values};
|
use sea_query::{inject_parameters, MySqlQueryBuilder, Values};
|
||||||
|
16
src/connector/select.rs
Normal file
16
src/connector/select.rs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
use crate::{Connection, Database, Entity, QueryErr, QueryResult, Select};
|
||||||
|
|
||||||
|
impl<E: 'static> Select<'_, E>
|
||||||
|
where
|
||||||
|
E: Entity,
|
||||||
|
{
|
||||||
|
pub async fn one(self, db: &Database) -> Result<QueryResult, QueryErr> {
|
||||||
|
let builder = db.get_query_builder_backend();
|
||||||
|
db.get_connection().query_one(self.build(builder)).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn all(self, db: &Database) -> Result<Vec<QueryResult>, QueryErr> {
|
||||||
|
let builder = db.get_query_builder_backend();
|
||||||
|
db.get_connection().query_all(self.build(builder)).await
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
use crate::{Connection, ConnectionErr, Connector, SqlxMySqlConnector, SqlxMySqlPoolConnection};
|
use crate::{Connection, ConnectionErr, Connector, SqlxMySqlConnector, SqlxMySqlPoolConnection};
|
||||||
|
use sea_query::{GenericBuilder, MySqlQueryBuilder};
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Database {
|
pub struct Database {
|
||||||
@ -48,4 +49,11 @@ impl Database {
|
|||||||
DatabaseConnection::Disconnected => panic!("Disconnected"),
|
DatabaseConnection::Disconnected => panic!("Disconnected"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_query_builder_backend(&self) -> impl GenericBuilder {
|
||||||
|
match &self.connection {
|
||||||
|
DatabaseConnection::SqlxMySqlPoolConnection(_) => MySqlQueryBuilder::default(),
|
||||||
|
DatabaseConnection::Disconnected => panic!("Disconnected"),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ mod database;
|
|||||||
mod driver;
|
mod driver;
|
||||||
mod entity;
|
mod entity;
|
||||||
mod query;
|
mod query;
|
||||||
pub(crate) mod tests_cfg;
|
pub mod tests_cfg;
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
pub use connector::*;
|
pub use connector::*;
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
use sqlx::mysql::MySqlRow;
|
use sqlx::mysql::MySqlRow;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct QueryResult {
|
pub struct QueryResult {
|
||||||
pub(crate) row: QueryResultRow,
|
pub(crate) row: QueryResultRow,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub(crate) enum QueryResultRow {
|
pub(crate) enum QueryResultRow {
|
||||||
SqlxMySql(MySqlRow),
|
SqlxMySql(MySqlRow),
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,12 @@ use strum::EnumIter;
|
|||||||
#[derive(Iden, Default, Debug)]
|
#[derive(Iden, Default, Debug)]
|
||||||
pub struct Cake;
|
pub struct Cake;
|
||||||
|
|
||||||
|
#[derive(Debug, Default, PartialEq)]
|
||||||
|
pub struct CakeModel {
|
||||||
|
pub id: Option<i32>,
|
||||||
|
pub name: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Iden, EnumIter)]
|
#[derive(Iden, EnumIter)]
|
||||||
pub enum CakeColumn {
|
pub enum CakeColumn {
|
||||||
Id,
|
Id,
|
||||||
@ -14,12 +20,6 @@ pub enum CakeColumn {
|
|||||||
#[derive(EnumIter)]
|
#[derive(EnumIter)]
|
||||||
pub enum CakeRelation {}
|
pub enum CakeRelation {}
|
||||||
|
|
||||||
#[derive(Debug, Default, PartialEq)]
|
|
||||||
pub struct CakeModel {
|
|
||||||
pub id: Option<i32>,
|
|
||||||
pub name: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Entity for Cake {
|
impl Entity for Cake {
|
||||||
type Model = CakeModel;
|
type Model = CakeModel;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user