Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade SeaQuery at SeaORM root to v0.27 #985

Merged
merged 16 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ log = { version = "^0.4" }
tracing = { version = "^0.1", features = ["log"] }
rust_decimal = { version = "^1", optional = true }
sea-orm-macros = { version = "^0.10.0", path = "sea-orm-macros", optional = true }
sea-query = { version = "^0.26.3", features = ["thread-safe"] }
sea-query = { version = "^0.27", git = "https://github.com/SeaQL/sea-query", features = ["thread-safe"] }
sea-query-binder = { version = "^0.2", git = "https://github.com/SeaQL/sea-query", optional = true }
sea-strum = { version = "^0.23", features = ["derive", "sea-orm"] }
serde = { version = "^1.0", features = ["derive"] }
serde_json = { version = "^1.0", optional = true }
Expand Down Expand Up @@ -70,29 +71,46 @@ default = [
]
macros = ["sea-orm-macros"]
mock = []
with-json = ["serde_json", "sea-query/with-json", "chrono/serde", "time/serde", "sqlx?/json"]
with-chrono = ["chrono", "sea-query/with-chrono", "sqlx?/chrono"]
with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal", "sqlx?/decimal"]
with-uuid = ["uuid", "sea-query/with-uuid", "sqlx?/uuid"]
with-time = ["time", "sea-query/with-time", "sqlx?/time"]
with-json = ["serde_json", "sea-query/with-json", "chrono?/serde", "time?/serde", "sea-query-binder?/with-json", "sqlx?/json"]
with-chrono = ["chrono", "sea-query/with-chrono", "sea-query-binder?/with-chrono", "sqlx?/chrono"]
with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal", "sea-query-binder?/with-rust_decimal", "sqlx?/decimal"]
with-uuid = ["uuid", "sea-query/with-uuid", "sea-query-binder?/with-uuid", "sqlx?/uuid"]
with-time = ["time", "sea-query/with-time", "sea-query-binder?/with-time", "sqlx?/time"]
sqlx-dep = []
sqlx-all = ["sqlx-mysql", "sqlx-postgres", "sqlx-sqlite"]
sqlx-mysql = ["sqlx-dep", "sea-query/sqlx-mysql", "sqlx/mysql"]
sqlx-postgres = ["sqlx-dep", "sea-query/sqlx-postgres", "sqlx/postgres"]
sqlx-sqlite = ["sqlx-dep", "sea-query/sqlx-sqlite", "sqlx/sqlite"]
sqlx-mysql = ["sqlx-dep", "sea-query-binder/sqlx-mysql", "sqlx/mysql"]
sqlx-postgres = ["sqlx-dep", "sea-query-binder/sqlx-postgres", "sqlx/postgres"]
sqlx-sqlite = ["sqlx-dep", "sea-query-binder/sqlx-sqlite", "sqlx/sqlite"]
runtime-async-std = []
runtime-async-std-native-tls = [
"sqlx/runtime-async-std-native-tls",
"sea-query-binder/runtime-async-std-native-tls",
"runtime-async-std",
]
runtime-async-std-rustls = [
"sqlx/runtime-async-std-rustls",
"runtime-async-std",
]
runtime-actix = []
runtime-actix-native-tls = ["sqlx/runtime-actix-native-tls", "runtime-actix"]
runtime-actix-rustls = ["sqlx/runtime-actix-rustls", "runtime-actix"]
runtime-actix-native-tls = [
"sqlx/runtime-actix-native-tls",
"sea-query-binder/runtime-actix-native-tls",
"runtime-actix",
]
runtime-actix-rustls = [
"sqlx/runtime-actix-rustls",
"sea-query-binder/runtime-actix-rustls",
"runtime-actix",
]
runtime-tokio = []
runtime-tokio-native-tls = ["sqlx/runtime-tokio-native-tls", "runtime-tokio"]
runtime-tokio-rustls = ["sqlx/runtime-tokio-rustls", "runtime-tokio"]
runtime-tokio-native-tls = [
"sqlx/runtime-tokio-native-tls",
"sea-query-binder/runtime-tokio-native-tls",
"runtime-tokio",
]
runtime-tokio-rustls = [
"sqlx/runtime-tokio-rustls",
"sea-query-binder/runtime-tokio-rustls",
"runtime-tokio",
]
tests-cfg = []
4 changes: 4 additions & 0 deletions issues/324/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ macro_rules! into_sea_query_value {
stringify!($newtype).to_owned()
}

fn array_type() -> sea_orm::sea_query::ArrayType {
sea_orm::sea_query::ArrayType::$name
}

fn column_type() -> sea_orm::sea_query::ColumnType {
sea_orm::sea_query::ColumnType::$name
}
Expand Down
4 changes: 4 additions & 0 deletions issues/400/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ impl<T> sea_orm::sea_query::ValueType for AccountId<T> {
stringify!(AccountId).to_owned()
}

fn array_type() -> sea_orm::sea_query::ArrayType {
sea_orm::sea_query::ArrayType::Uuid
}

fn column_type() -> sea_orm::sea_query::ColumnType {
sea_orm::sea_query::ColumnType::Uuid
}
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ clap = { version = "^3.2", features = ["env", "derive"] }
dotenvy = { version = "^0.15", optional = true }
async-std = { version = "^1.9", features = [ "attributes", "tokio1" ], optional = true }
sea-orm-codegen = { version = "^0.10.0", path = "../sea-orm-codegen", optional = true }
sea-schema = { version = "^0.9.3" }
sea-schema = { version = "^0.10.0", git = "https://github.com/SeaQL/sea-schema", branch = "sea-query-0.27" }
sqlx = { version = "^0.6", default-features = false, features = [ "mysql", "postgres" ], optional = true }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing = { version = "0.1" }
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ name = "sea_orm_codegen"
path = "src/lib.rs"

[dependencies]
sea-query = { version = "^0.26.3" }
sea-query = { version = "^0.27", git = "https://github.com/SeaQL/sea-query", features = ["thread-safe"] }
syn = { version = "^1", default-features = false, features = [
"derive",
"parsing",
Expand Down
11 changes: 6 additions & 5 deletions sea-orm-codegen/src/entity/active_enum.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use heck::CamelCase;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use sea_query::DynIden;

use crate::WithSerde;

#[derive(Clone, Debug)]
pub struct ActiveEnum {
pub(crate) enum_name: String,
pub(crate) values: Vec<String>,
pub(crate) enum_name: DynIden,
pub(crate) values: Vec<DynIden>,
}

impl ActiveEnum {
pub fn impl_active_enum(&self, with_serde: &WithSerde, with_copy_enums: bool) -> TokenStream {
let enum_name = &self.enum_name;
let enum_name = &self.enum_name.to_string();
let enum_iden = format_ident!("{}", enum_name.to_camel_case());
let values = &self.values;
let variants = self.values.iter().map(|v| v.trim()).map(|v| {
let values: Vec<String> = self.values.iter().map(|v| v.to_string()).collect();
let variants = values.iter().map(|v| v.trim()).map(|v| {
if v.chars().all(|c| c.is_numeric()) {
format_ident!("_{}", v)
} else {
Expand Down
6 changes: 3 additions & 3 deletions sea-orm-codegen/src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Column {
ColumnType::Uuid => "Uuid".to_owned(),
ColumnType::Binary(_) | ColumnType::VarBinary(_) => "Vec<u8>".to_owned(),
ColumnType::Boolean => "bool".to_owned(),
ColumnType::Enum(name, _) => name.to_camel_case(),
ColumnType::Enum { name, .. } => name.to_string().to_camel_case(),
_ => unimplemented!(),
}
.parse()
Expand Down Expand Up @@ -146,8 +146,8 @@ impl Column {
let s = s.to_string();
quote! { ColumnType::Custom(#s.to_owned()).def() }
}
ColumnType::Enum(enum_name, _) => {
let enum_ident = format_ident!("{}", enum_name.to_camel_case());
ColumnType::Enum { name, .. } => {
let enum_ident = format_ident!("{}", name.to_string().to_camel_case());
quote! { #enum_ident::db_type() }
}
#[allow(unreachable_patterns)]
Expand Down
3 changes: 2 additions & 1 deletion sea-orm-codegen/src/entity/relation.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::util::unpack_table_ref;
use heck::{CamelCase, SnakeCase};
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};
Expand Down Expand Up @@ -155,7 +156,7 @@ impl Relation {
impl From<&TableForeignKey> for Relation {
fn from(tbl_fk: &TableForeignKey) -> Self {
let ref_table = match tbl_fk.get_ref_table() {
Some(s) => s,
Some(s) => unpack_table_ref(s),
None => panic!("RefTable should not be empty"),
};
let columns = tbl_fk.get_columns();
Expand Down
14 changes: 7 additions & 7 deletions sea-orm-codegen/src/entity/transformer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
ActiveEnum, Column, ConjunctRelation, Entity, EntityWriter, Error, PrimaryKey, Relation,
RelationType,
util::unpack_table_ref, ActiveEnum, Column, ConjunctRelation, Entity, EntityWriter, Error,
PrimaryKey, Relation, RelationType,
};
use sea_query::{ColumnSpec, TableCreateStatement};
use std::collections::HashMap;
Expand Down Expand Up @@ -59,12 +59,12 @@ impl EntityTransformer {
col
})
.map(|col| {
if let sea_query::ColumnType::Enum(enum_name, values) = &col.col_type {
if let sea_query::ColumnType::Enum { name, variants } = &col.col_type {
enums.insert(
enum_name.clone(),
name.to_string(),
ActiveEnum {
enum_name: enum_name.clone(),
values: values.clone(),
enum_name: name.clone(),
values: variants.clone(),
},
);
}
Expand All @@ -77,7 +77,7 @@ impl EntityTransformer {
.iter()
.map(|fk_create_stmt| fk_create_stmt.get_foreign_key())
.map(|tbl_fk| {
let ref_tbl = tbl_fk.get_ref_table().unwrap();
let ref_tbl = unpack_table_ref(tbl_fk.get_ref_table().unwrap());
if let Some(count) = ref_table_counts.get_mut(&ref_tbl) {
if *count == 0 {
*count = 1;
Expand Down
4 changes: 2 additions & 2 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ impl EntityWriter {
.columns
.iter()
.fold(TokenStream::new(), |mut ts, col| {
if let sea_query::ColumnType::Enum(enum_name, _) = &col.col_type {
let enum_name = format_ident!("{}", enum_name.to_camel_case());
if let sea_query::ColumnType::Enum { name, .. } = &col.col_type {
let enum_name = format_ident!("{}", name.to_string().to_camel_case());
ts.extend(vec![quote! {
use super::sea_orm_active_enums::#enum_name;
}]);
Expand Down
15 changes: 15 additions & 0 deletions sea-orm-codegen/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use sea_query::TableRef;

pub(crate) fn escape_rust_keyword<T>(string: T) -> String
where
T: ToString,
Expand All @@ -21,3 +23,16 @@ pub(crate) const RUST_KEYWORDS: [&str; 49] = [
];

pub(crate) const RUST_SPECIAL_KEYWORDS: [&str; 3] = ["crate", "Self", "self"];

pub(crate) fn unpack_table_ref(table_ref: &TableRef) -> String {
match table_ref {
TableRef::Table(tbl)
| TableRef::SchemaTable(_, tbl)
| TableRef::DatabaseSchemaTable(_, _, tbl)
| TableRef::TableAlias(tbl, _)
| TableRef::SchemaTableAlias(_, tbl, _)
| TableRef::DatabaseSchemaTableAlias(_, _, tbl, _)
| TableRef::SubQuery(_, tbl)
| TableRef::ValuesList(_, tbl) => tbl.to_string(),
}
}
61 changes: 57 additions & 4 deletions sea-orm-macros/src/derives/active_enum.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use heck::CamelCase;
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned};
use quote::{format_ident, quote, quote_spanned};
use syn::{parse, punctuated::Punctuated, token::Comma, Expr, Lit, LitInt, LitStr, Meta, UnOp};

enum Error {
Expand Down Expand Up @@ -59,7 +59,10 @@ impl ActiveEnum {
match s.as_ref() {
"Enum" => {
db_type = Ok(quote! {
Enum(Self::name(), Self::values())
Enum {
name: Self::name(),
variants: Self::iden_values(),
}
})
}
_ => {
Expand Down Expand Up @@ -227,13 +230,59 @@ impl ActiveEnum {
quote! { v }
};

let enum_name_iden = format_ident!("{}Enum", ident);

let str_variants: Vec<String> = variants
.iter()
.filter_map(|variant| {
variant
.string_value
.as_ref()
.map(|string_value| string_value.value())
})
.collect();

let impl_enum_variant_iden = if !str_variants.is_empty() {
let enum_variant_iden = format_ident!("{}Variant", ident);
let enum_variants: Vec<syn::Ident> = str_variants
.iter()
.map(|v| format_ident!("{}", v.to_camel_case()))
.collect();

quote!(
#[derive(Debug, Clone, PartialEq, Eq, EnumIter, Iden)]
pub enum #enum_variant_iden {
#(
#[iden = #str_variants]
#enum_variants,
)*
}

impl #ident {
pub fn iden_values() -> Vec<sea_orm::sea_query::DynIden> {
<#enum_variant_iden as sea_orm::strum::IntoEnumIterator>::iter()
.map(|v| sea_orm::sea_query::SeaRc::new(v) as sea_orm::sea_query::DynIden)
.collect()
}
}
)
} else {
quote!()
};

quote!(
#[derive(Debug, Clone, PartialEq, Eq, Iden)]
#[iden = #enum_name]
pub struct #enum_name_iden;

#impl_enum_variant_iden

#[automatically_derived]
impl sea_orm::ActiveEnum for #ident {
type Value = #rs_type;

fn name() -> String {
#enum_name.to_owned()
fn name() -> sea_orm::sea_query::DynIden {
sea_orm::sea_query::SeaRc::new(#enum_name_iden) as sea_orm::sea_query::DynIden
}

fn to_value(&self) -> Self::Value {
Expand Down Expand Up @@ -286,6 +335,10 @@ impl ActiveEnum {
<<Self as sea_orm::ActiveEnum>::Value as sea_orm::sea_query::ValueType>::type_name()
}

fn array_type() -> sea_orm::sea_query::ArrayType {
unimplemented!("Array of Enum is not supported")
}

fn column_type() -> sea_orm::sea_query::ColumnType {
<Self as sea_orm::ActiveEnum>::db_type()
.get_column_type()
Expand Down
4 changes: 4 additions & 0 deletions sea-orm-macros/src/derives/try_getable_from_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ pub fn expand_derive_from_json_query_result(ident: Ident) -> syn::Result<TokenSt
stringify!(#ident).to_owned()
}

fn array_type() -> sea_orm::sea_query::ArrayType {
sea_orm::sea_query::ArrayType::Json
}

fn column_type() -> sea_orm::sea_query::ColumnType {
sea_orm::sea_query::ColumnType::Json
}
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ clap = { version = "^3.2", features = ["env", "derive"] }
dotenvy = { version = "^0.15" }
sea-orm = { version = "^0.10.0", path = "../", default-features = false, features = ["macros"] }
sea-orm-cli = { version = "^0.10.0", path = "../sea-orm-cli", default-features = false }
sea-schema = { version = "^0.9.3" }
sea-schema = { version = "^0.10.0", git = "https://github.com/SeaQL/sea-schema", branch = "sea-query-0.27" }
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

Expand Down
18 changes: 9 additions & 9 deletions src/driver/sqlx_mysql.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use sea_query::Values;
use std::{future::Future, pin::Pin, sync::Arc};

use sqlx::{
mysql::{MySqlArguments, MySqlConnectOptions, MySqlQueryResult, MySqlRow},
mysql::{MySqlConnectOptions, MySqlQueryResult, MySqlRow},
MySql, MySqlPool,
};

sea_query::sea_query_driver_mysql!();
use sea_query_driver_mysql::bind_query;
use sea_query_binder::SqlxValues;
use tracing::instrument;

use crate::{
Expand Down Expand Up @@ -203,10 +203,10 @@ impl From<MySqlQueryResult> for ExecResult {
}
}

pub(crate) fn sqlx_query(stmt: &Statement) -> sqlx::query::Query<'_, MySql, MySqlArguments> {
let mut query = sqlx::query(&stmt.sql);
if let Some(values) = &stmt.values {
query = bind_query(query, values);
}
query
pub(crate) fn sqlx_query(stmt: &Statement) -> sqlx::query::Query<'_, MySql, SqlxValues> {
let values = stmt
.values
.as_ref()
.map_or(Values(Vec::new()), |values| values.clone());
sqlx::query_with(&stmt.sql, SqlxValues(values))
}
Loading