Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
Renamed to model_db_column_name
Browse files Browse the repository at this point in the history
  • Loading branch information
amigin committed Jul 31, 2023
1 parent da10096 commit aabd8d1
Show file tree
Hide file tree
Showing 20 changed files with 72 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/fn_impl_insert/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ pub fn fn_get_column_name(
) -> Result<Vec<proc_macro2::TokenStream>, syn::Error> {
let mut result = Vec::new();
for (i, prop) in fields.iter().enumerate() {
let field_name = prop.get_db_field_name_as_string()?;
let field_name = prop.get_db_column_name_as_string()?;

let other_field_name =
if let Some(model_field_name) = prop.get_model_db_field_name_as_string() {
if let Some(model_field_name) = prop.get_model_db_column_name_as_string() {
let name = model_field_name.unwrap_as_string_value()?.as_str();

quote::quote! {Some(#name.into())}
Expand Down
2 changes: 1 addition & 1 deletion src/fn_impl_select/fn_fill_group_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn get_group_by_fields(

for prop in fields {
if prop.attrs.has_attr("group_by") {
group_by.push(prop.get_db_field_name_as_string()?);
group_by.push(prop.get_db_column_name_as_string()?);
continue;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/fn_impl_select/fn_fill_order_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ pub fn fn_get_order_by_fields(
if !order_by_desc.is_empty() {
for field in order_by_desc {
result.push(' ');
let value = field.get_db_field_name_as_string()?;
let value = field.get_db_column_name_as_string()?;
result.push_str(value);
}
result.push_str(" DESC");
} else if !order_by.is_empty() {
for field in order_by {
result.push(' ');
result.push_str(field.get_db_field_name_as_string()?);
result.push_str(field.get_db_column_name_as_string()?);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/fn_impl_select/fn_fill_select_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn fn_fill_select_fields(
sql.push_str(#attr_value);
});
} else {
let db_field_name = prop.get_db_field_name_as_token()?;
let db_field_name = prop.get_db_column_name_as_token()?;

let metadata = prop.get_field_metadata()?;

Expand Down
2 changes: 1 addition & 1 deletion src/fn_impl_select/fn_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn fn_from(fields: &[StructProperty]) -> Result<Vec<proc_macro2::TokenStream
for field in fields {
let name_ident = field.get_field_name_ident();

let db_field_name = field.get_db_field_name_as_string()?;
let db_field_name = field.get_db_column_name_as_string()?;

let metadata = field.get_field_metadata()?;

Expand Down
4 changes: 2 additions & 2 deletions src/fn_impl_update/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ fn get_columns(fields: &UpdateFields) -> Result<proc_macro2::TokenStream, syn::E
let mut line = Vec::with_capacity(fields.get_fields_amount());
let mut no: usize = 0;
for field in fields.get_fields_with_no_primary_key() {
let db_field_name = field.get_db_field_name_as_string()?;
let related_name = match field.get_model_db_field_name_as_string() {
let db_field_name = field.get_db_column_name_as_string()?;
let related_name = match field.get_model_db_column_name_as_string() {
Some(value) => {
let value = value.unwrap_as_string_value()?.as_str();
quote::quote!(Some(#value.into()))
Expand Down
2 changes: 1 addition & 1 deletion src/fn_impl_where_model/fn_fill_where.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn fn_fill_where<'s>(
let prop_name_ident = struct_property.get_field_name_ident();
let metadata = struct_property.get_field_metadata()?;

let db_field_name = struct_property.get_db_field_name_as_string()?;
let db_field_name = struct_property.get_db_column_name_as_string()?;

let op = fill_op(struct_property)?;

Expand Down
20 changes: 10 additions & 10 deletions src/postgres_struct_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ pub trait PostgresStructPropertyExt<'s> {

fn get_sql_type(&self) -> Result<&ParamValue, syn::Error>;

fn get_db_field_name_as_token(&self) -> Result<proc_macro2::TokenStream, syn::Error>;
fn get_db_column_name_as_token(&self) -> Result<proc_macro2::TokenStream, syn::Error>;

fn get_db_field_name_as_string(&self) -> Result<&str, syn::Error>;
fn get_db_column_name_as_string(&self) -> Result<&str, syn::Error>;

fn get_model_db_field_name_as_string(&self) -> Option<&ParamValue>;
fn get_model_db_column_name_as_string(&self) -> Option<&ParamValue>;

fn has_json_attr(&self) -> bool;

Expand Down Expand Up @@ -222,7 +222,7 @@ impl<'s> PostgresStructPropertyExt<'s> for StructProperty<'s> {
return Ok(None);
}

fn get_db_field_name_as_token(&self) -> Result<proc_macro2::TokenStream, syn::Error> {
fn get_db_column_name_as_token(&self) -> Result<proc_macro2::TokenStream, syn::Error> {
if let Ok(attr) = self.attrs.get_attr(ATTR_DB_FIELD_NAME) {
if let Ok(result) = attr.get_from_single_or_named("name") {
let name = result.get_any_value_as_str()?.to_token_stream();
Expand All @@ -235,7 +235,7 @@ impl<'s> PostgresStructPropertyExt<'s> for StructProperty<'s> {
Ok(quote::quote!(#name))
}

fn get_db_field_name_as_string(&self) -> Result<&str, syn::Error> {
fn get_db_column_name_as_string(&self) -> Result<&str, syn::Error> {
if let Ok(attr) = self
.attrs
.get_single_or_named_param(ATTR_DB_FIELD_NAME, "name")
Expand All @@ -246,10 +246,10 @@ impl<'s> PostgresStructPropertyExt<'s> for StructProperty<'s> {
Ok(self.name.as_str())
}

fn get_model_db_field_name_as_string(&self) -> Option<&ParamValue> {
fn get_model_db_column_name_as_string(&self) -> Option<&ParamValue> {
if let Ok(attr) = self
.attrs
.get_named_param(ATTR_DB_FIELD_NAME, "model_field_name")
.get_named_param(ATTR_DB_FIELD_NAME, "model_db_column_name")
{
return Some(attr);
}
Expand Down Expand Up @@ -298,15 +298,15 @@ impl<'s> PostgresStructPropertyExt<'s> for StructProperty<'s> {
index_name,
is_unique,
order: order.to_string(),
name: self.get_db_field_name_as_string()?.to_string(),
name: self.get_db_column_name_as_string()?.to_string(),
})
}

Ok(Some(result))
}

fn get_field_metadata(&self) -> Result<proc_macro2::TokenStream, syn::Error> {
let model_field_name = self.get_model_db_field_name_as_string();
let model_field_name = self.get_model_db_column_name_as_string();

let sql_type = self.get_sql_type();

Expand Down Expand Up @@ -343,7 +343,7 @@ impl<'s> PostgresStructPropertyExt<'s> for StructProperty<'s> {

let result = ETagData {
field_name: self.get_field_name_ident(),
column_name: self.get_db_field_name_as_string()?,
column_name: self.get_db_column_name_as_string()?,
};

Ok(Some(result))
Expand Down
2 changes: 1 addition & 1 deletion src/table_schema/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn impl_db_columns(
let mut last_primary_key_id = 0;

for field in fields {
let field_name = field.get_db_field_name_as_string()?;
let field_name = field.get_db_column_name_as_string()?;
let sql_type = get_sql_type(field, &field.ty)?;
let is_option: bool = field.ty.is_option();

Expand Down
35 changes: 35 additions & 0 deletions tests/src/dto/bulk_insert_cases.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#[cfg(test)]
mod tests {
use my_postgres::sql_select::BulkSelectBuilder;
use my_postgres_macros::{BulkSelectDbEntity, SelectDbEntity, WhereDbModel};

#[derive(BulkSelectDbEntity, SelectDbEntity)]
pub struct BulkKeyValueDto {
pub line_no: i32,
pub client_id: String,
pub key: String,
pub value: String,
}

#[derive(WhereDbModel)]
pub struct WhereDbModel {
pub client_id: String,
pub key: String,
}

#[test]
fn tests() {
let mut keys: Vec<WhereDbModel> = Vec::new();

keys.push(WhereDbModel {
client_id: "test".to_string(),
key: "test".to_string(),
});

let builder = BulkSelectBuilder::new("test", keys);

let sql = builder.build_sql::<BulkKeyValueDto>();

assert_eq!("SELECT 0::int as \"line_no\",client_id,key,value FROM test WHERE client_id=$1 AND key=$1\n", sql.sql);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ mod tests {
#[test]
fn test_select_sql_with_no_where() {
let select_builder = SelectBuilder::from_select_model::<KeyValue>();
let sql = select_builder.build_select_sql::<TestWhereModel>("table_name", None);
let sql = select_builder.to_sql_string::<TestWhereModel>("table_name", None);

assert_eq!("SELECT client_id,key,value FROM table_name", sql.sql);
assert_eq!(sql.values.len(), 0);
Expand All @@ -110,7 +110,7 @@ mod tests {
let where_model = TestWhereModel { a: 6 };

let select_builder = SelectBuilder::from_select_model::<KeyValue>();
let sql = select_builder.build_select_sql("table_name", Some(&where_model));
let sql = select_builder.to_sql_string("table_name", Some(&where_model));

assert_eq!(
"SELECT client_id,key,value FROM table_name WHERE a=6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mod tests {
#[test]
fn test_select_sql_with_no_where() {
let select_builder = SelectBuilder::from_select_model::<KeyValue>();
let sql = select_builder.build_select_sql::<TestWhereModel>("table_name", None);
let sql = select_builder.to_sql_string::<TestWhereModel>("table_name", None);

assert_eq!("SELECT client_id,key,value FROM table_name", sql.sql);
assert_eq!(sql.values.len(), 0);
Expand All @@ -112,7 +112,7 @@ mod tests {
let where_model = TestWhereModel { a: 6 };

let select_builder = SelectBuilder::from_select_model::<KeyValue>();
let sql = select_builder.build_select_sql("table_name", Some(&where_model));
let sql = select_builder.to_sql_string("table_name", Some(&where_model));

assert_eq!(
"SELECT client_id,key,value FROM table_name WHERE a=6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ mod tests {
fn test_select_sql_with_no_where() {
let select_builder = SelectBuilder::from_select_model::<KeyValue>();

let sql = select_builder.build_select_sql::<TestWhereModel>("table_name", None);
let sql = select_builder.to_sql_string::<TestWhereModel>("table_name", None);

assert_eq!(
"SELECT client_id,key,value,field_model FROM table_name",
Expand All @@ -132,7 +132,7 @@ mod tests {

let select_builder = SelectBuilder::from_select_model::<KeyValue>();

let sql = select_builder.build_select_sql("table_name", Some(&where_model));
let sql = select_builder.to_sql_string("table_name", Some(&where_model));

assert_eq!(
"SELECT client_id,key,value,field_model FROM table_name WHERE a=6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ mod tests {
#[test]
fn test_select_sql_with_no_where() {
let select_builder = SelectBuilder::from_select_model::<KeyValue>();
let sql = select_builder.build_select_sql::<TestWhereModel>("table_name", None);
let sql = select_builder.to_sql_string::<TestWhereModel>("table_name", None);

assert_eq!("SELECT client_id,key,value FROM table_name", sql.sql);
assert_eq!(sql.values.len(), 0);
Expand All @@ -111,7 +111,7 @@ mod tests {
let where_model = TestWhereModel { a: 6 };

let select_builder = SelectBuilder::from_select_model::<KeyValue>();
let sql = select_builder.build_select_sql("table_name", Some(&where_model));
let sql = select_builder.to_sql_string("table_name", Some(&where_model));

assert_eq!(
"SELECT client_id,key,value FROM table_name WHERE a=6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ mod tests {
#[test]
fn test_select_sql_with_no_where() {
let select_builder = SelectBuilder::from_select_model::<KeyValue>();
let sql = select_builder.build_select_sql::<TestWhereModel>("table_name", None);
let sql = select_builder.to_sql_string::<TestWhereModel>("table_name", None);

assert_eq!("SELECT client_id,key,value FROM table_name", sql.sql);
assert_eq!(sql.values.len(), 0);
Expand All @@ -115,7 +115,7 @@ mod tests {
let where_model = TestWhereModel { a: 6 };

let select_builder = SelectBuilder::from_select_model::<KeyValue>();
let sql = select_builder.build_select_sql("table_name", Some(&where_model));
let sql = select_builder.to_sql_string("table_name", Some(&where_model));

assert_eq!(
"SELECT client_id,key,value FROM table_name WHERE a=6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod test {
fn test_with_empty_where() {
let sql = SelectBuilder::from_select_model::<KeyValue>();

let sql = sql.build_select_sql::<WhereAllModel>("test", None);
let sql = sql.to_sql_string::<WhereAllModel>("test", None);

println!("{}", sql.sql);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/dto/enum_as_where_case/where_with_limit_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod tests {
offset: 3,
};

let sql = select_builder.build_select_sql("test", Some(&where_model));
let sql = select_builder.to_sql_string("test", Some(&where_model));

println!("{}", sql.sql);
}
Expand Down
1 change: 1 addition & 0 deletions tests/src/dto/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod bulk_insert_cases;
mod e_tag_tests;
mod enum_as_update_case;
mod enum_as_where_case;
Expand Down
4 changes: 2 additions & 2 deletions tests/src/dto/test_basic_cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ mod tests {
#[test]
fn test_select_sql_with_no_where() {
let select_builder = SelectBuilder::from_select_model::<KeyValue>();
let sql = select_builder.build_select_sql::<TestWhereModel>("table_name", None);
let sql = select_builder.to_sql_string::<TestWhereModel>("table_name", None);

assert_eq!("SELECT client_id,key,value FROM table_name", sql.sql);
assert_eq!(sql.values.len(), 0);
Expand All @@ -102,7 +102,7 @@ mod tests {
let where_model = TestWhereModel { a: 6 };

let select_builder = SelectBuilder::from_select_model::<KeyValue>();
let sql = select_builder.build_select_sql("table_name", Some(&where_model));
let sql = select_builder.to_sql_string("table_name", Some(&where_model));

assert_eq!(
"SELECT client_id,key,value FROM table_name WHERE a=6",
Expand Down
2 changes: 1 addition & 1 deletion tests/src/dto/test_json_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ mod tests {
let where_model = WhereModel { limit: 10 };

let select_builder = SelectBuilder::from_select_model::<TestJsonField>();
let sql = select_builder.build_select_sql("test", Some(&where_model));
let sql = select_builder.to_sql_string("test", Some(&where_model));

println!("{}", sql.sql);
}
Expand Down

0 comments on commit aabd8d1

Please sign in to comment.