Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit a388a32

Browse files
mitch-tofimythz
authored andcommitted
Addition of a Filter to ORMConfig to enable users access to the Ref Sql Statement (#615)
* Added in the ability to Filter/Adjust the Ref Sql Queries before being executed, a use case is being able to append a AND clause to the end allow for the filteration of "soft deleted" records * removed unused operators * - Removed redundant Invoke Methods - Added Tests to demonstration the usage of the SqlSelectRefFilter
1 parent 5cbe510 commit a388a32

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

src/ServiceStack.OrmLite/OrmLiteConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ public static IOrmLiteExecFilter ExecFilter
176176
public static Action<IDbCommand, object> InsertFilter { get; set; }
177177
public static Action<IDbCommand, object> UpdateFilter { get; set; }
178178
public static Action<IUntypedSqlExpression> SqlExpressionSelectFilter { get; set; }
179+
public static Func<Type, string, string> SqlSelectRefFilter { get; set; }
180+
179181

180182
public static Func<string, string> StringFilter { get; set; }
181183

src/ServiceStack.OrmLite/Support/LoadList.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ protected string GetRefListSql(ModelDefinition refModelDef, FieldDefinition refF
5454
$"WHERE {dialectProvider.GetQuotedColumnName(refField)} " +
5555
$"IN ({subSql})";
5656

57+
if (OrmLiteConfig.SqlSelectRefFilter != null)
58+
sqlRef = OrmLiteConfig.SqlSelectRefFilter(refModelDef.ModelType, sqlRef);
59+
5760
return sqlRef;
5861
}
5962

@@ -98,6 +101,9 @@ protected string GetRefSelfSql(ModelDefinition modelDef, FieldDefinition refSelf
98101
$"WHERE {dialectProvider.GetQuotedColumnName(refModelDef.PrimaryKey)} " +
99102
$"IN ({subSqlRef})";
100103

104+
if (OrmLiteConfig.SqlSelectRefFilter != null)
105+
sqlRef = OrmLiteConfig.SqlSelectRefFilter(refModelDef.ModelType, sqlRef);
106+
101107
return sqlRef;
102108
}
103109

@@ -107,6 +113,10 @@ protected string GetRefFieldSql(ModelDefinition refModelDef, FieldDefinition ref
107113
$"FROM {dialectProvider.GetQuotedTableName(refModelDef)} " +
108114
$"WHERE {dialectProvider.GetQuotedColumnName(refField)} " +
109115
$"IN ({subSql})";
116+
117+
if (OrmLiteConfig.SqlSelectRefFilter != null)
118+
sqlRef = OrmLiteConfig.SqlSelectRefFilter(refModelDef.ModelType, sqlRef);
119+
110120
return sqlRef;
111121
}
112122

src/ServiceStack.OrmLite/Support/LoadReferences.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,20 @@ protected string GetRefListSql(Type refType)
3838
var sqlFilter = dialectProvider.GetQuotedColumnName(refField.FieldName) + "={0}";
3939
var sql = dialectProvider.ToSelectStatement(refType, sqlFilter, pkValue);
4040

41+
if (OrmLiteConfig.SqlSelectRefFilter != null)
42+
sql = OrmLiteConfig.SqlSelectRefFilter(refType, sql);
43+
4144
return sql;
4245
}
4346

4447
protected string GetRefFieldSql(Type refType, FieldDefinition refField)
4548
{
4649
var sqlFilter = dialectProvider.GetQuotedColumnName(refField.FieldName) + "={0}";
4750
var sql = dialectProvider.ToSelectStatement(refType, sqlFilter, pkValue);
51+
52+
if (OrmLiteConfig.SqlSelectRefFilter != null)
53+
sql = OrmLiteConfig.SqlSelectRefFilter(refType, sql);
54+
4855
return sql;
4956
}
5057

@@ -57,6 +64,10 @@ protected string GetRefSelfSql(Type refType, FieldDefinition refSelf, ModelDefin
5764

5865
var sqlFilter = dialectProvider.GetQuotedColumnName(refModelDef.PrimaryKey.FieldName) + "={0}";
5966
var sql = dialectProvider.ToSelectStatement(refType, sqlFilter, refPkValue);
67+
68+
if (OrmLiteConfig.SqlSelectRefFilter != null)
69+
sql = OrmLiteConfig.SqlSelectRefFilter(refType, sql);
70+
6071
return sql;
6172
}
6273
}

tests/ServiceStack.OrmLite.Tests/SoftDeleteTests.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,70 @@ public void Can_get_active_products_using_SoftDelete_SqlExpression()
119119

120120
OrmLiteConfig.SqlExpressionSelectFilter = null;
121121
}
122+
123+
[Test]
124+
public void Can_get_active_vendor_and_active_references_using_SoftDelete_ref_filter()
125+
{
126+
OrmLiteConfig.SqlExpressionSelectFilter = q =>
127+
{
128+
if (q.ModelDef.ModelType.HasInterface(typeof(ISoftDelete)))
129+
{
130+
q.Where<ISoftDelete>(x => !x.IsDeleted);
131+
}
132+
};
133+
134+
OrmLiteConfig.SqlSelectRefFilter = (type, sql) =>
135+
{
136+
var meta = type.GetModelMetadata();
137+
if (type.HasInterface(typeof(ISoftDelete)))
138+
{
139+
sql += $" AND (\"{meta.ModelName}\".\"IsDeleted\" = 0)";
140+
}
141+
142+
return sql;
143+
};
144+
145+
using (var db = OpenDbConnection())
146+
{
147+
InitData(db);
148+
149+
var vendors = db.LoadSelect<Vendor>();
150+
151+
Assert.That(vendors.Count, Is.EqualTo(1));
152+
Assert.That(vendors[0].Name, Is.EqualTo("Active Vendor"));
153+
Assert.That(vendors[0].Products.Count, Is.EqualTo(1));
154+
}
155+
156+
OrmLiteConfig.SqlExpressionSelectFilter = null;
157+
OrmLiteConfig.SqlSelectRefFilter = null;
158+
}
159+
160+
[Test]
161+
public void Can_get_single_vendor_and__load_active_references_using_soft_delete_ref_filter()
162+
{
163+
OrmLiteConfig.SqlSelectRefFilter = (type, sql) =>
164+
{
165+
var meta = type.GetModelMetadata();
166+
if (type.HasInterface(typeof(ISoftDelete)))
167+
{
168+
sql += $" AND (\"{meta.ModelName}\".\"IsDeleted\" = 0)";
169+
}
170+
171+
return sql;
172+
};
173+
174+
using (var db = OpenDbConnection())
175+
{
176+
InitData(db);
177+
178+
var vendor = db.Single<Vendor>(v=>v.Name == "Active Vendor");
179+
db.LoadReferences(vendor);
180+
181+
Assert.That(vendor.Name, Is.EqualTo("Active Vendor"));
182+
Assert.That(vendor.Products.Count, Is.EqualTo(1));
183+
}
184+
185+
OrmLiteConfig.SqlSelectRefFilter = null;
186+
}
122187
}
123188
}

0 commit comments

Comments
 (0)