This commit is contained in:
Billy Chan 2021-08-28 20:00:59 +08:00
parent 8cfa14233d
commit 484da8f6b6
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
26 changed files with 73 additions and 2 deletions

View File

@ -27,6 +27,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}

View File

@ -28,6 +28,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
false
}

View File

@ -27,6 +27,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}

View File

@ -29,6 +29,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}

View File

@ -27,6 +27,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}

View File

@ -117,6 +117,18 @@ impl Entity {
format_ident!("{}", auto_increment)
}
pub fn get_primary_key_rs_type(&self) -> TokenStream {
if let Some(primary_key) = self.primary_keys.first() {
self.columns
.iter()
.find(|col| col.name.eq(&primary_key.name))
.unwrap()
.get_rs_type()
} else {
TokenStream::new()
}
}
pub fn get_conjunct_relations_via_snake_case(&self) -> Vec<Ident> {
self.conjunct_relations
.iter()
@ -151,7 +163,7 @@ mod tests {
columns: vec![
Column {
name: "id".to_owned(),
col_type: ColumnType::String(None),
col_type: ColumnType::Integer(None),
auto_increment: false,
not_null: false,
unique: false,
@ -373,6 +385,16 @@ mod tests {
);
}
#[test]
fn test_get_primary_key_rs_type() {
let entity = setup();
assert_eq!(
entity.get_primary_key_rs_type().to_string(),
entity.columns[0].get_rs_type().to_string()
);
}
#[test]
fn test_get_conjunct_relations_via_snake_case() {
let entity = setup();

View File

@ -173,8 +173,11 @@ impl EntityWriter {
pub fn gen_impl_primary_key(entity: &Entity) -> TokenStream {
let primary_key_auto_increment = entity.get_primary_key_auto_increment();
let value_type = entity.get_primary_key_rs_type();
quote! {
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = #value_type;
fn auto_increment() -> bool {
#primary_key_auto_increment
}

View File

@ -29,6 +29,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}

View File

@ -30,6 +30,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
false
}

View File

@ -29,6 +29,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}

View File

@ -31,6 +31,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}

View File

@ -31,6 +31,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}

View File

@ -1,6 +1,6 @@
use std::str::FromStr;
use crate::{EntityName, IdenStatic, Iterable};
use sea_query::{DynIden, Expr, SeaRc, SelectStatement, SimpleExpr, Value};
use std::str::FromStr;
#[derive(Debug, Clone)]
pub struct ColumnDef {

View File

@ -2,6 +2,8 @@ use super::{ColumnTrait, IdenStatic, Iterable};
//LINT: composite primary key cannot auto increment
pub trait PrimaryKeyTrait: IdenStatic + Iterable {
type ValueType: Sized;
fn auto_increment() -> bool;
}

View File

@ -28,6 +28,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}

View File

@ -29,6 +29,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
false
}

View File

@ -35,6 +35,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
false
}

View File

@ -41,6 +41,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}

View File

@ -30,6 +30,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}

View File

@ -31,6 +31,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}

View File

@ -29,6 +29,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}

View File

@ -35,6 +35,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}

View File

@ -28,6 +28,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
false
}

View File

@ -29,6 +29,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}

View File

@ -33,6 +33,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}

View File

@ -33,6 +33,8 @@ pub enum PrimaryKey {
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}