Skip to content

Commit 586c926

Browse files
committed
Fixed things with Default Json Type
1 parent ec3593d commit 586c926

File tree

9 files changed

+72
-69
lines changed

9 files changed

+72
-69
lines changed

my-postgres-core/src/connection/postgres_connect_inner.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ impl PostgresConnectionInner {
249249
ctx: &crate::RequestContext,
250250
) -> Result<u64, MyPostgresError> {
251251
let mut start_connection = false;
252-
let mut sw = StopWatch::new();
253-
sw.start();
252+
let sw = StopWatch::new();
253+
254254
loop {
255255
if start_connection {
256256
self.start_connection().await;
@@ -276,7 +276,6 @@ impl PostgresConnectionInner {
276276
match result {
277277
Ok(result) => {
278278
if ctx.is_debug {
279-
sw.pause();
280279
println!(
281280
"SQL: {} executed in {}",
282281
&sql.sql,
@@ -306,8 +305,7 @@ impl PostgresConnectionInner {
306305

307306
let is_debug = std::env::var("DEBUG_SQL").is_ok();
308307

309-
let mut sw = StopWatch::new();
310-
sw.start();
308+
let sw = StopWatch::new();
311309

312310
loop {
313311
if start_connection {
@@ -334,7 +332,6 @@ impl PostgresConnectionInner {
334332
match result {
335333
Ok(result) => {
336334
if is_debug {
337-
sw.pause();
338335
println!(
339336
"SQL: {} is executed in {}",
340337
&sql.sql,

my-postgres-macros/src/attributes/sql_type.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub enum SqlType {
88
Timestamp,
99
#[value("jsonb")]
1010
JsonB,
11+
#[value("json")]
12+
Json,
1113
}
1214

1315
#[attribute_name("sql_type")]

my-postgres-macros/src/db_enum/generate.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ impl EnumType {
5555
EnumType::I64 => quote!(i32).into(),
5656
}
5757
}
58+
59+
pub fn get_sql_type_enum_case(&self)->proc_macro2::TokenStream{
60+
match self{
61+
EnumType::U8 => quote!(my_postgres::table_schema::TableColumnType::SmallInt).into(),
62+
EnumType::I8 => quote!(my_postgres::table_schema::TableColumnType::SmallInt).into(),
63+
EnumType::U16 => quote!(my_postgres::table_schema::TableColumnType::SmallInt).into(),
64+
EnumType::I16 => quote!(my_postgres::table_schema::TableColumnType::SmallInt).into(),
65+
EnumType::U32 => quote!(my_postgres::table_schema::TableColumnType::Integer).into(),
66+
EnumType::I32 => quote!(my_postgres::table_schema::TableColumnType::Integer).into(),
67+
EnumType::U64 => quote!(my_postgres::table_schema::TableColumnType::BigInt).into(),
68+
EnumType::I64 => quote!(my_postgres::table_schema::TableColumnType::BigInt).into(),
69+
}
70+
}
5871
}
5972

6073
pub fn generate(
@@ -85,6 +98,8 @@ pub fn generate(
8598

8699
let sql_db_type = enum_type.get_compliant_with_db_type();
87100

101+
let sql_type_enum_case = enum_type.get_sql_type_enum_case();
102+
88103
let from_db_result = if type_name.to_string() == sql_db_type.to_string() {
89104
quote! {
90105
Self::from_db_value(result)
@@ -141,6 +156,10 @@ pub fn generate(
141156
}
142157
}
143158

159+
fn get_sql_type() -> my_postgres::table_schema::TableColumnType {
160+
#sql_type_enum_case
161+
}
162+
144163
#fn_fill_select_type
145164

146165
}
@@ -170,18 +189,6 @@ pub fn generate(
170189
}
171190
}
172191

173-
impl my_postgres::table_schema::SqlTypeProvider for #enum_name {
174-
fn get_sql_type(
175-
_metadata: Option<my_postgres::SqlValueMetadata>,
176-
) -> my_postgres::table_schema::TableColumnType {
177-
use my_postgres::table_schema::*;
178-
#type_name::get_sql_type(None)
179-
}
180-
}
181-
182-
183-
184-
185192
}
186193
.into();
187194

my-postgres-macros/src/db_enum/generate_as_string.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ pub fn generate_as_string(ast: &syn::DeriveInput) -> Result<proc_macro::TokenStr
5050
}
5151
}
5252

53+
fn get_sql_type() -> my_postgres::table_schema::TableColumnType {
54+
my_postgres::table_schema::TableColumnType::Jsonb
55+
}
56+
5357
#fn_fill_select_type
5458
}
5559

@@ -80,14 +84,6 @@ pub fn generate_as_string(ast: &syn::DeriveInput) -> Result<proc_macro::TokenStr
8084
}
8185
}
8286

83-
impl my_postgres::table_schema::SqlTypeProvider for #enum_name {
84-
fn get_sql_type(
85-
_metadata: Option<my_postgres::SqlValueMetadata>,
86-
) -> my_postgres::table_schema::TableColumnType {
87-
my_postgres::table_schema::TableColumnType::Text
88-
}
89-
}
90-
9187
}
9288
.into();
9389

my-postgres-macros/src/db_enum/generate_as_string_with_model.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ pub fn generate_as_string_with_model(ast: &syn::DeriveInput) -> Result<proc_macr
5151
pub fn fill_select_part(sql: &mut my_postgres::sql::SelectBuilder, column_name: #db_field_type, metadata: &Option<my_postgres::SqlValueMetadata>) {
5252
#select_part
5353
}
54+
55+
fn get_sql_type() -> my_postgres::table_schema::TableColumnType {
56+
my_postgres::table_schema::TableColumnType::Jsonb
57+
}
5458
}
5559

5660
impl<'s> my_postgres::sql_select::FromDbRow<'s, #enum_name> for #enum_name{
@@ -73,15 +77,6 @@ pub fn generate_as_string_with_model(ast: &syn::DeriveInput) -> Result<proc_macr
7377
}
7478

7579
}
76-
77-
impl my_postgres::table_schema::SqlTypeProvider for #enum_name {
78-
fn get_sql_type(
79-
_metadata: Option<my_postgres::SqlValueMetadata>,
80-
) -> my_postgres::table_schema::TableColumnType {
81-
my_postgres::table_schema::TableColumnType::Jsonb
82-
}
83-
}
84-
8580

8681
}
8782
.into();

my-postgres-macros/src/db_enum/generate_with_model.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ pub fn generate_with_model(ast: &syn::DeriveInput) -> Result<TokenStream, syn::E
4848
#select_part
4949
}
5050

51+
fn get_sql_type() -> my_postgres::table_schema::TableColumnType {
52+
my_postgres::table_schema::TableColumnType::Jsonb
53+
}
54+
5155
}
5256

5357
impl my_postgres::sql_update::SqlUpdateValueProvider for #enum_name{

my-postgres-macros/src/my_postgres_json_model/generate.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ pub fn generate(ast: &syn::DeriveInput) -> Result<proc_macro::TokenStream, syn::
2020
pub fn to_string(&self)->String{
2121
serde_json::to_string(self).unwrap()
2222
}
23+
24+
fn get_sql_type() -> my_postgres::table_schema::TableColumnType {
25+
my_postgres::table_schema::TableColumnType::Jsonb
26+
}
2327
}
2428
});
2529

my-postgres-macros/src/postgres_struct_ext.rs

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::str::FromStr;
2+
13
use proc_macro2::TokenStream;
24
use types_reader::{AnyValueAsStr, MacrosAttribute, PropertyType, StructProperty};
35

@@ -84,7 +86,7 @@ pub trait PostgresStructPropertyExt<'s> {
8486
&self,
8587
expected: &[&'static str],
8688
) -> Result<Option<&str>, syn::Error>;
87-
fn get_sql_type_attr_value(&self, expected: &[&'static str]) -> Result<&str, syn::Error>;
89+
fn get_sql_type_attr_value(&self, expected: &[SqlType]) -> Result<SqlType, syn::Error>;
8890

8991
fn get_sql_type_as_token_stream(&self) -> Result<proc_macro2::TokenStream, syn::Error>;
9092

@@ -341,25 +343,21 @@ impl<'s> PostgresStructPropertyExt<'s> for StructProperty<'s> {
341343
Ok(())
342344
}
343345

344-
fn get_sql_type_attr_value(&self, expected: &[&'static str]) -> Result<&str, syn::Error> {
345-
let sql_type = self.attrs.get_single_or_named_param("sql_type", "name")?;
346-
347-
let as_str = sql_type.as_str()?;
348-
349-
if expected.is_empty() {
350-
return Ok(as_str);
351-
}
346+
fn get_sql_type_attr_value(&self, expected: &[SqlType]) -> Result<SqlType, syn::Error> {
347+
let sql_type: SqlTypeAttribute = self.get_attribute()?;
352348

353349
for exp in expected {
354-
if *exp == as_str {
355-
return Ok(as_str);
350+
if exp.as_str() == sql_type.name.as_str() {
351+
return Ok(sql_type.name);
356352
}
357353
}
358354

359-
Err(sql_type.throw_error(&format!(
355+
let expected: Vec<&str> = expected.iter().map(|itm| itm.as_str()).collect();
356+
357+
self.throw_error(&format!(
360358
"sql_type attribute should have one of the following values: {:?}",
361359
expected
362-
)))
360+
))
363361
}
364362

365363
fn try_get_sql_type_attr_value(
@@ -470,11 +468,13 @@ impl<'s> PostgresStructPropertyExt<'s> for StructProperty<'s> {
470468
quote::quote!(my_postgres::table_schema::TableColumnType::Text)
471469
}
472470
PropertyType::DateTime => {
473-
match self.get_sql_type_attr_value(&["timestamp", "bigint"])? {
474-
"timestamp" => {
471+
match self.get_sql_type_attr_value(&[SqlType::Timestamp, SqlType::Bigint])? {
472+
SqlType::Timestamp => {
475473
quote::quote!(my_postgres::table_schema::TableColumnType::Timestamp)
476474
}
477-
"bigint" => quote::quote!(my_postgres::table_schema::TableColumnType::BigInt),
475+
SqlType::Bigint => {
476+
quote::quote!(my_postgres::table_schema::TableColumnType::BigInt)
477+
}
478478
_ => {
479479
panic!("DateTime type should have sql_type attribute set with 'timestamp' value or 'bigint'");
480480
}
@@ -484,13 +484,14 @@ impl<'s> PostgresStructPropertyExt<'s> for StructProperty<'s> {
484484
panic!("OptionOf should be unwrapped before");
485485
}
486486
PropertyType::VecOf(_property_type) => {
487-
get_json_or_json_b(self.try_get_sql_type_attr_value(&["json", "jsonb"])?)
487+
get_json_or_json_b(self.get_sql_type_attr_value(&[SqlType::Json, SqlType::JsonB])?)
488488
}
489-
PropertyType::Struct(_, _type_path) => {
490-
get_json_or_json_b(self.try_get_sql_type_attr_value(&["json", "jsonb"])?)
489+
PropertyType::Struct(name, _type_path) => {
490+
let tp_as_token = TokenStream::from_str(name).unwrap();
491+
quote::quote! (#tp_as_token::get_sql_type())
491492
}
492493
PropertyType::HashMap(_property_type, _property_type1) => {
493-
get_json_or_json_b(self.try_get_sql_type_attr_value(&["json", "jsonb"])?)
494+
get_json_or_json_b(self.get_sql_type_attr_value(&[SqlType::Json, SqlType::JsonB])?)
494495
}
495496
PropertyType::RefTo { .. } => {
496497
panic!("RefTo is not supported for sql");
@@ -505,20 +506,16 @@ impl<'s> PostgresStructPropertyExt<'s> for StructProperty<'s> {
505506
}
506507
}
507508

508-
fn get_json_or_json_b(value: Option<&str>) -> proc_macro2::TokenStream {
509-
if let Some(value) = value {
510-
match value {
511-
"json" => {
512-
quote::quote!(my_postgres::table_schema::TableColumnType::Timestamp)
513-
}
514-
"jsonb" => {
515-
quote::quote!(my_postgres::table_schema::TableColumnType::BigInt)
516-
}
517-
_ => {
518-
panic!("DateTime type should have sql_type attribute set with 'timestamp' value or 'bigint'");
519-
}
509+
fn get_json_or_json_b(value: SqlType) -> proc_macro2::TokenStream {
510+
match value {
511+
SqlType::Json => {
512+
quote::quote!(my_postgres::table_schema::TableColumnType::Timestamp)
513+
}
514+
SqlType::JsonB => {
515+
quote::quote!(my_postgres::table_schema::TableColumnType::BigInt)
516+
}
517+
_ => {
518+
panic!("sql_type attribute must be 'timestamp' or 'bigint'");
520519
}
521-
} else {
522-
quote::quote!(my_postgres::table_schema::TableColumnType::Jsonb)
523520
}
524521
}

my-postgres-tests/src/dto/e_tag_tests/case_from_life.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct DiscountCodeDto {
2020
pub date_ends: DateTimeAsMicroseconds,
2121
#[sql_type("timestamp")]
2222
pub date_starts: DateTimeAsMicroseconds,
23+
#[sql_type("json")]
2324
pub trading_package_ids: Vec<String>,
2425
pub usage_limit: Option<i32>,
2526
pub usage_count: i32,

0 commit comments

Comments
 (0)