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

Commit 741b756

Browse files
committed
Add docs for Soft Deletes + Check Constraints
1 parent 954c6f9 commit 741b756

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,6 +2316,60 @@ Assert.That(db.Select<Shipper>(), Has.Count.EqualTo(0));
23162316
Assert.That(db.Select<ShipperType>(), Has.Count.EqualTo(0));
23172317
```
23182318

2319+
### Soft Deletes
2320+
2321+
Select Filters let you specify a custom `SelectFilter` that lets you modify queries that use `SqlExpression<T>` before they're executed. This could be used to make working with "Soft Deletes" Tables easier where it can be made to apply a custom `x.IsDeleted != true` condition on every `SqlExpression`.
2322+
2323+
By either using a `SelectFilter` on concrete POCO Table Types, e.g:
2324+
2325+
```csharp
2326+
SqlExpression<Table1>.SelectFilter = q => q.Where(x => x.IsDeleted != true);
2327+
SqlExpression<Table2>.SelectFilter = q => q.Where(x => x.IsDeleted != true);
2328+
```
2329+
2330+
Or alternatively using generic delegate that applies to all SqlExpressions, but you'll only have access to a
2331+
`IUntypedSqlExpression` which offers a limited API surface area but will still let you execute a custom filter
2332+
for all `SqlExpression<T>` that could be used to add a condition for all tables implementing a custom
2333+
`ISoftDelete` interface with:
2334+
2335+
```csharp
2336+
OrmLiteConfig.SqlExpressionSelectFilter = q =>
2337+
{
2338+
if (q.ModelDef.ModelType.HasInterface(typeof(ISoftDelete)))
2339+
{
2340+
q.Where<ISoftDelete>(x => x.IsDeleted != true);
2341+
}
2342+
};
2343+
```
2344+
2345+
Both solutions above will transparently add the `x.IsDeleted != true` to all `SqlExpression<T>` based queries
2346+
so it only returns results which aren't `IsDeleted` from any of queries below:
2347+
2348+
```csharp
2349+
var results = db.Select(db.From<Table>());
2350+
var result = db.Single(db.From<Table>().Where(x => x.Name == "foo"));
2351+
var result = db.Single(x => x.Name == "foo");
2352+
```
2353+
2354+
### Check Constraints
2355+
2356+
OrmLite includes support for [SQL Check Constraints](https://en.wikipedia.org/wiki/Check_constraint) which will create your Table schema with the `[CheckConstraint]` specified, e.g:
2357+
2358+
```csharp
2359+
public class Table
2360+
{
2361+
[AutoIncrement]
2362+
public int Id { get; set; }
2363+
2364+
[Required]
2365+
[CheckConstraint("Age > 1")]
2366+
public int Age { get; set; }
2367+
2368+
[CheckConstraint("Name IS NOT NULL")]
2369+
public string Name { get; set; }
2370+
}
2371+
```
2372+
23192373
## SQL Server Features
23202374

23212375
### Memory Optimized Tables

0 commit comments

Comments
 (0)