Skip to content

Commit

Permalink
Added support for convert expressions in mapping entity properties on…
Browse files Browse the repository at this point in the history
… table columns
  • Loading branch information
Basim108 committed Mar 3, 2021
1 parent 3e3aed2 commit 0711e64
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/Hrimsoft.SqlBulk.PostgreSql/Options/EntityProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,14 @@ public EntityProfile(Type entityType)
#region mapping information

private string _tableName;

/// <summary>
/// A table name that represents this entity in the database
/// </summary>
public string TableName {
get {
public string TableName
{
get
{
if (string.IsNullOrWhiteSpace(_tableName))
_tableName = $"\"{EntityType.Name.ToSnakeCase()}\"";

Expand Down Expand Up @@ -113,7 +116,8 @@ public void ToTable(string dbTableName, string schema)
this.TableName = string.IsNullOrWhiteSpace(dbTableName)
? $"\"{EntityType.Name.ToSnakeCase()}\""
: $"\"{dbTableName}\"";
if (!string.IsNullOrWhiteSpace(schema)) {
if (!string.IsNullOrWhiteSpace(schema))
{
this.TableName = $"\"{schema}\".{this.TableName}";
}
}
Expand Down Expand Up @@ -193,7 +197,9 @@ public PropertyProfile HasProperty<TEntity, TProperty>(string column, Expression
propertyName = memberExpression.Member.Name;
else if (propertyExpression.Body is ParameterExpression parameterExpression)
propertyName = parameterExpression.Name;

else if (propertyExpression.Body is UnaryExpression unaryExpression &&
unaryExpression.Operand is MemberExpression operand)
propertyName = operand.Member.Name;
if (string.IsNullOrWhiteSpace(propertyName))
propertyName = column;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,47 @@ create table "unit_tests"."entity_with_unique_columns"
constraint business_identity unique (record_id, sensor_id)
);


create table "unit_tests"."entity_with_int_enum"
(
id serial not null
constraint entity_with_int_enum_pk primary key,
some_enum_value integer
);

create table "unit_tests"."entity_with_string_enum"
(
id serial not null
constraint entity_with_string_enum_pk primary key,
some_enum_value varchar
);

-- ------------- after update tests ----------------

--drop table "unit_tests"."after_update_tests";
create table "unit_tests"."after_update_tests"
(
id serial not null
constraint after_update_tests_pk primary key,
record text,
sensor text,
value integer
);

--drop trigger "after_update_tests_change_record_trigger" on "unit_tests"."after_update_tests";
--drop function after_update_tests_change_record_after_update;

create or replace function after_update_tests_change_record_after_update() returns trigger as $$
begin
new."record" = 'new-value-changed-by-trigger';
new."value" = new.value - 1;
return new;
end
$$ language plpgsql;

create trigger after_update_tests_change_record_trigger before update on "unit_tests"."after_update_tests"
for each row execute procedure after_update_tests_change_record_after_update();

--drop table "unit_tests"."simple_test_entity";
create table "unit_tests"."simple_test_entity"
(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=options_005Centityprofiletests/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Hrimsoft.SqlBulk.PostgreSql.Tests.Options
{
public class EntityProfileTests
public class EntityProfileHasPropertyTests
{
[Test]
public void HasProperty_should_pass_binary_expressions()
Expand All @@ -16,6 +16,13 @@ public void HasProperty_should_pass_binary_expressions()
Assert.DoesNotThrow(() => entityProfile.HasProperty<TestEntity, int>("value", x => x.Id + 10));
}

[Test]
public void HasProperty_should_pass_convert_expressions()
{
var entityProfile = new EntityProfile(typeof(TestEntity));
Assert.DoesNotThrow(() => entityProfile.HasProperty<TestEntity, long>(x => (long)x.Id));
}

[Test]
public void HasProperty_should_pass_constant_expressions()
{
Expand Down

0 comments on commit 0711e64

Please sign in to comment.