Support MySQL / SQLite
This commit is contained in:
parent
201d6fe9f9
commit
93623f0d02
@ -2,6 +2,15 @@ use proc_macro2::{Ident, TokenStream};
|
|||||||
use quote::quote;
|
use quote::quote;
|
||||||
|
|
||||||
pub fn expand_derive_from_json_query_result(ident: Ident) -> syn::Result<TokenStream> {
|
pub fn expand_derive_from_json_query_result(ident: Ident) -> syn::Result<TokenStream> {
|
||||||
|
let impl_not_u8 = if cfg!(feature = "postgres-array") {
|
||||||
|
quote!(
|
||||||
|
#[automatically_derived]
|
||||||
|
impl sea_orm::sea_query::value::with_array::NotU8 for #ident {}
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
quote!()
|
||||||
|
};
|
||||||
|
|
||||||
Ok(quote!(
|
Ok(quote!(
|
||||||
#[automatically_derived]
|
#[automatically_derived]
|
||||||
impl sea_orm::TryGetableFromJson for #ident {}
|
impl sea_orm::TryGetableFromJson for #ident {}
|
||||||
@ -43,5 +52,7 @@ pub fn expand_derive_from_json_query_result(ident: Ident) -> syn::Result<TokenSt
|
|||||||
sea_orm::Value::Json(None)
|
sea_orm::Value::Json(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#impl_not_u8
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -281,6 +281,7 @@ pub trait ColumnTrait: IdenStatic + Iterable + FromStr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Cast value of an enum column as enum type; do nothing if `self` is not an enum.
|
/// Cast value of an enum column as enum type; do nothing if `self` is not an enum.
|
||||||
|
/// Will also transform `Array(Vec<Json>)` into `Json(Vec<Json>)` if the column type is `Json`.
|
||||||
fn save_enum_as(&self, val: Expr) -> SimpleExpr {
|
fn save_enum_as(&self, val: Expr) -> SimpleExpr {
|
||||||
cast_enum_as(val, self, |col, enum_name, col_type| {
|
cast_enum_as(val, self, |col, enum_name, col_type| {
|
||||||
let type_name = match col_type {
|
let type_name = match col_type {
|
||||||
@ -412,9 +413,41 @@ where
|
|||||||
{
|
{
|
||||||
let col_def = col.def();
|
let col_def = col.def();
|
||||||
let col_type = col_def.get_column_type();
|
let col_type = col_def.get_column_type();
|
||||||
match col_type.get_enum_name() {
|
|
||||||
Some(enum_name) => f(expr, SeaRc::clone(enum_name), col_type),
|
match col_type {
|
||||||
None => expr.into(),
|
#[cfg(all(feature = "with-json", feature = "postgres-array"))]
|
||||||
|
ColumnType::Json | ColumnType::JsonBinary => {
|
||||||
|
use sea_query::ArrayType;
|
||||||
|
use serde_json::Value as Json;
|
||||||
|
|
||||||
|
#[allow(clippy::boxed_local)]
|
||||||
|
fn unbox<T>(boxed: Box<T>) -> T {
|
||||||
|
*boxed
|
||||||
|
}
|
||||||
|
|
||||||
|
let expr = expr.into();
|
||||||
|
match expr {
|
||||||
|
SimpleExpr::Value(Value::Array(ArrayType::Json, Some(json_vec))) => {
|
||||||
|
// flatten Array(Vec<Json>) into Json
|
||||||
|
let json_vec: Vec<Json> = json_vec
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|val| match val {
|
||||||
|
Value::Json(Some(json)) => Some(unbox(json)),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
SimpleExpr::Value(Value::Json(Some(Box::new(json_vec.into()))))
|
||||||
|
}
|
||||||
|
SimpleExpr::Value(Value::Array(ArrayType::Json, None)) => {
|
||||||
|
SimpleExpr::Value(Value::Json(None))
|
||||||
|
}
|
||||||
|
_ => expr,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => match col_type.get_enum_name() {
|
||||||
|
Some(enum_name) => f(expr, SeaRc::clone(enum_name), col_type),
|
||||||
|
None => expr.into(),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,12 +23,12 @@ async fn main() -> Result<(), DbErr> {
|
|||||||
insert_active_enum(&ctx.db).await?;
|
insert_active_enum(&ctx.db).await?;
|
||||||
insert_active_enum_child(&ctx.db).await?;
|
insert_active_enum_child(&ctx.db).await?;
|
||||||
|
|
||||||
if cfg!(feature = "sqlx-postgres") {
|
#[cfg(feature = "sqlx-postgres")]
|
||||||
insert_active_enum_vec(&ctx.db).await?;
|
insert_active_enum_vec(&ctx.db).await?;
|
||||||
}
|
|
||||||
|
|
||||||
find_related_active_enum(&ctx.db).await?;
|
find_related_active_enum(&ctx.db).await?;
|
||||||
find_linked_active_enum(&ctx.db).await?;
|
find_linked_active_enum(&ctx.db).await?;
|
||||||
|
|
||||||
ctx.delete().await;
|
ctx.delete().await;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -4,35 +4,27 @@ pub mod json_string_vec {
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
||||||
#[sea_orm(table_name = "json_vec")]
|
#[sea_orm(table_name = "json_string_vec")]
|
||||||
pub struct Model {
|
pub struct Model {
|
||||||
#[sea_orm(primary_key)]
|
#[sea_orm(primary_key)]
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub str_vec: Option<StringVec>,
|
pub str_vec: Option<StringVec>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)]
|
||||||
|
pub struct StringVec(pub Vec<String>);
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
pub enum Relation {}
|
pub enum Relation {}
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)]
|
|
||||||
pub struct StringVec(pub Vec<String>);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod json_struct_vec {
|
pub mod json_struct_vec {
|
||||||
use sea_orm::entity::prelude::*;
|
use sea_orm::entity::prelude::*;
|
||||||
use sea_orm_macros::FromJsonQueryResult;
|
use sea_orm_macros::FromJsonQueryResult;
|
||||||
use sea_query::with_array::NotU8;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)]
|
|
||||||
pub struct JsonColumn {
|
|
||||||
pub value: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NotU8 for JsonColumn {}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
||||||
#[sea_orm(table_name = "json_struct_vec")]
|
#[sea_orm(table_name = "json_struct_vec")]
|
||||||
pub struct Model {
|
pub struct Model {
|
||||||
@ -42,6 +34,11 @@ pub mod json_struct_vec {
|
|||||||
pub struct_vec: Vec<JsonColumn>,
|
pub struct_vec: Vec<JsonColumn>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)]
|
||||||
|
pub struct JsonColumn {
|
||||||
|
pub value: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
pub enum Relation {}
|
pub enum Relation {}
|
||||||
|
|
||||||
|
@ -44,6 +44,7 @@ pub use event_trigger::Entity as EventTrigger;
|
|||||||
pub use insert_default::Entity as InsertDefault;
|
pub use insert_default::Entity as InsertDefault;
|
||||||
pub use json_struct::Entity as JsonStruct;
|
pub use json_struct::Entity as JsonStruct;
|
||||||
pub use json_vec::Entity as JsonVec;
|
pub use json_vec::Entity as JsonVec;
|
||||||
|
pub use json_vec_derive::json_string_vec::Entity as JsonStringVec;
|
||||||
pub use json_vec_derive::json_struct_vec::Entity as JsonStructVec;
|
pub use json_vec_derive::json_struct_vec::Entity as JsonStructVec;
|
||||||
pub use metadata::Entity as Metadata;
|
pub use metadata::Entity as Metadata;
|
||||||
pub use pi::Entity as Pi;
|
pub use pi::Entity as Pi;
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::common::features::json_vec_derive::json_struct_vec;
|
|
||||||
use crate::common::setup::{create_enum, create_table, create_table_without_asserts};
|
use crate::common::setup::{create_enum, create_table, create_table_without_asserts};
|
||||||
use sea_orm::{
|
use sea_orm::{
|
||||||
error::*, sea_query, ConnectionTrait, DatabaseConnection, DbBackend, DbConn, EntityName,
|
error::*, sea_query, ConnectionTrait, DatabaseConnection, DbBackend, DbConn, EntityName,
|
||||||
@ -20,7 +19,6 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
|
|||||||
create_byte_primary_key_table(db).await?;
|
create_byte_primary_key_table(db).await?;
|
||||||
create_satellites_table(db).await?;
|
create_satellites_table(db).await?;
|
||||||
create_transaction_log_table(db).await?;
|
create_transaction_log_table(db).await?;
|
||||||
create_json_struct_table(db).await?;
|
|
||||||
|
|
||||||
let create_enum_stmts = match db_backend {
|
let create_enum_stmts = match db_backend {
|
||||||
DbBackend::MySql | DbBackend::Sqlite => Vec::new(),
|
DbBackend::MySql | DbBackend::Sqlite => Vec::new(),
|
||||||
@ -51,12 +49,15 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
|
|||||||
create_dyn_table_name_lazy_static_table(db).await?;
|
create_dyn_table_name_lazy_static_table(db).await?;
|
||||||
create_value_type_table(db).await?;
|
create_value_type_table(db).await?;
|
||||||
|
|
||||||
|
create_json_vec_table(db).await?;
|
||||||
|
create_json_struct_table(db).await?;
|
||||||
|
create_json_string_vec_table(db).await?;
|
||||||
|
create_json_struct_vec_table(db).await?;
|
||||||
|
|
||||||
if DbBackend::Postgres == db_backend {
|
if DbBackend::Postgres == db_backend {
|
||||||
create_value_type_postgres_table(db).await?;
|
create_value_type_postgres_table(db).await?;
|
||||||
create_collection_table(db).await?;
|
create_collection_table(db).await?;
|
||||||
create_event_trigger_table(db).await?;
|
create_event_trigger_table(db).await?;
|
||||||
create_json_vec_table(db).await?;
|
|
||||||
create_json_struct_vec_table(db).await?;
|
|
||||||
create_categories_table(db).await?;
|
create_categories_table(db).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -323,26 +324,6 @@ pub async fn create_json_vec_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
|||||||
create_table(db, &create_table_stmt, JsonVec).await
|
create_table(db, &create_table_stmt, JsonVec).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_json_struct_vec_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
|
||||||
let create_table_stmt = sea_query::Table::create()
|
|
||||||
.table(json_struct_vec::Entity.table_ref())
|
|
||||||
.col(
|
|
||||||
ColumnDef::new(json_struct_vec::Column::Id)
|
|
||||||
.integer()
|
|
||||||
.not_null()
|
|
||||||
.auto_increment()
|
|
||||||
.primary_key(),
|
|
||||||
)
|
|
||||||
.col(
|
|
||||||
ColumnDef::new(json_struct_vec::Column::StructVec)
|
|
||||||
.json_binary()
|
|
||||||
.not_null(),
|
|
||||||
)
|
|
||||||
.to_owned();
|
|
||||||
|
|
||||||
create_table(db, &create_table_stmt, JsonStructVec).await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn create_json_struct_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
pub async fn create_json_struct_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
||||||
let stmt = sea_query::Table::create()
|
let stmt = sea_query::Table::create()
|
||||||
.table(json_struct::Entity)
|
.table(json_struct::Entity)
|
||||||
@ -365,6 +346,42 @@ pub async fn create_json_struct_table(db: &DbConn) -> Result<ExecResult, DbErr>
|
|||||||
create_table(db, &stmt, JsonStruct).await
|
create_table(db, &stmt, JsonStruct).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn create_json_string_vec_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
||||||
|
let create_table_stmt = sea_query::Table::create()
|
||||||
|
.table(JsonStringVec.table_ref())
|
||||||
|
.col(
|
||||||
|
ColumnDef::new(json_vec_derive::json_string_vec::Column::Id)
|
||||||
|
.integer()
|
||||||
|
.not_null()
|
||||||
|
.auto_increment()
|
||||||
|
.primary_key(),
|
||||||
|
)
|
||||||
|
.col(ColumnDef::new(json_vec_derive::json_string_vec::Column::StrVec).json())
|
||||||
|
.to_owned();
|
||||||
|
|
||||||
|
create_table(db, &create_table_stmt, JsonStringVec).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_json_struct_vec_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
||||||
|
let create_table_stmt = sea_query::Table::create()
|
||||||
|
.table(JsonStructVec.table_ref())
|
||||||
|
.col(
|
||||||
|
ColumnDef::new(json_vec_derive::json_struct_vec::Column::Id)
|
||||||
|
.integer()
|
||||||
|
.not_null()
|
||||||
|
.auto_increment()
|
||||||
|
.primary_key(),
|
||||||
|
)
|
||||||
|
.col(
|
||||||
|
ColumnDef::new(json_vec_derive::json_struct_vec::Column::StructVec)
|
||||||
|
.json_binary()
|
||||||
|
.not_null(),
|
||||||
|
)
|
||||||
|
.to_owned();
|
||||||
|
|
||||||
|
create_table(db, &create_table_stmt, JsonStructVec).await
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn create_collection_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
pub async fn create_collection_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
||||||
db.execute(sea_orm::Statement::from_string(
|
db.execute(sea_orm::Statement::from_string(
|
||||||
db.get_database_backend(),
|
db.get_database_backend(),
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
use sea_orm::{
|
use sea_orm::{
|
||||||
ColumnTrait, ColumnType, ConnectionTrait, Database, DatabaseBackend, DatabaseConnection,
|
ColumnTrait, ColumnType, ConnectOptions, ConnectionTrait, Database, DatabaseBackend,
|
||||||
DbBackend, DbConn, DbErr, EntityTrait, ExecResult, Iterable, Schema, Statement,
|
DatabaseConnection, DbBackend, DbConn, DbErr, EntityTrait, ExecResult, Iterable, Schema,
|
||||||
|
Statement,
|
||||||
};
|
};
|
||||||
use sea_query::{
|
use sea_query::{
|
||||||
extension::postgres::{Type, TypeCreateStatement},
|
extension::postgres::{Type, TypeCreateStatement},
|
||||||
@ -48,7 +49,9 @@ pub async fn setup(base_url: &str, db_name: &str) -> DatabaseConnection {
|
|||||||
let url = format!("{base_url}/{db_name}");
|
let url = format!("{base_url}/{db_name}");
|
||||||
Database::connect(&url).await.unwrap()
|
Database::connect(&url).await.unwrap()
|
||||||
} else {
|
} else {
|
||||||
Database::connect(base_url).await.unwrap()
|
let mut options: ConnectOptions = base_url.into();
|
||||||
|
options.sqlx_logging(false);
|
||||||
|
Database::connect(options).await.unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,13 +5,17 @@ use pretty_assertions::assert_eq;
|
|||||||
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
|
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
|
||||||
|
|
||||||
#[sea_orm_macros::test]
|
#[sea_orm_macros::test]
|
||||||
#[cfg(feature = "sqlx-postgres")]
|
#[cfg(any(
|
||||||
|
feature = "sqlx-mysql",
|
||||||
|
feature = "sqlx-sqlite",
|
||||||
|
feature = "sqlx-postgres"
|
||||||
|
))]
|
||||||
async fn main() -> Result<(), DbErr> {
|
async fn main() -> Result<(), DbErr> {
|
||||||
let ctx = TestContext::new("json_vec_tests").await;
|
let ctx = TestContext::new("json_vec_tests").await;
|
||||||
create_tables(&ctx.db).await?;
|
create_tables(&ctx.db).await?;
|
||||||
insert_json_vec(&ctx.db).await?;
|
insert_json_vec(&ctx.db).await?;
|
||||||
|
|
||||||
insert_json_string_vec_derive(&ctx.db).await?;
|
insert_json_string_vec_derive(&ctx.db).await?;
|
||||||
|
insert_json_struct_vec_derive(&ctx.db).await?;
|
||||||
|
|
||||||
ctx.delete().await;
|
ctx.delete().await;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user