Skip to content

Commit 72c1995

Browse files
committed
Add contracts. Add equality semantics TableMapping.
1 parent a7e08e4 commit 72c1995

10 files changed

Lines changed: 135 additions & 10 deletions

SQLitePCL.pretty.Orm/DatabaseConnection.Delete.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public static bool TryDelete<T>(
6161
{
6262
Contract.Requires(This != null);
6363
Contract.Requires(tableMapping != null);
64+
Contract.Requires(resultSelector != null);
6465

6566
var result = This.YieldDeleteAll(tableMapping, new long[] { primaryKey }, resultSelector).FirstOrDefault();
6667
if (result.Value != null)
@@ -92,6 +93,7 @@ public static IReadOnlyDictionary<long,T> DeleteAll<T>(
9293
Contract.Requires(This != null);
9394
Contract.Requires(tableMapping != null);
9495
Contract.Requires(primaryKeys != null);
96+
Contract.Requires(resultSelector != null);
9597

9698
return This.RunInTransaction(_ =>
9799
This.YieldDeleteAll(tableMapping, primaryKeys, resultSelector)
@@ -121,6 +123,7 @@ public static Task<IReadOnlyDictionary<long,T>> DeleteAllAsync<T>(
121123
Contract.Requires(This != null);
122124
Contract.Requires(tableMapping != null);
123125
Contract.Requires(primaryKeys != null);
126+
Contract.Requires(resultSelector != null);
124127

125128
return This.Use((db,_) => db.DeleteAll<T>(tableMapping, primaryKeys, resultSelector), ct);
126129
}
@@ -142,6 +145,7 @@ public static Task<IReadOnlyDictionary<long,T>> DeleteAllAsync<T>(
142145
Contract.Requires(This != null);
143146
Contract.Requires(tableMapping != null);
144147
Contract.Requires(primaryKeys != null);
148+
Contract.Requires(resultSelector != null);
145149

146150
return This.DeleteAllAsync(tableMapping, primaryKeys, resultSelector, CancellationToken.None);
147151
}

SQLitePCL.pretty.Orm/DatabaseConnection.Find.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public static bool TryFind<T>(
7070
{
7171
Contract.Requires(This != null);
7272
Contract.Requires(tableMapping != null);
73+
Contract.Requires(resultSelector != null);
7374

7475
var result = This.YieldFindAll(tableMapping, new long[] { primaryKey }, resultSelector).FirstOrDefault();
7576

@@ -100,6 +101,7 @@ public static IReadOnlyDictionary<long,T> FindAll<T>(
100101
Contract.Requires(This != null);
101102
Contract.Requires(tableMapping != null);
102103
Contract.Requires(primaryKeys != null);
104+
Contract.Requires(resultSelector != null);
103105

104106
return This.YieldFindAll(tableMapping, primaryKeys, resultSelector)
105107
.Where(kvp => kvp.Value != null)
@@ -128,6 +130,7 @@ public static Task<IReadOnlyDictionary<long,T>> FindAllAsync<T>(
128130
Contract.Requires(This != null);
129131
Contract.Requires(tableMapping != null);
130132
Contract.Requires(primaryKeys != null);
133+
Contract.Requires(resultSelector != null);
131134

132135
return This.Use((db,_) => db.FindAll(tableMapping, primaryKeys, resultSelector), ct);
133136
}
@@ -149,6 +152,7 @@ public static Task<IReadOnlyDictionary<long,T>> FindAllAsync<T>(
149152
Contract.Requires(This != null);
150153
Contract.Requires(tableMapping != null);
151154
Contract.Requires(primaryKeys != null);
155+
Contract.Requires(resultSelector != null);
152156

153157
return This.FindAllAsync(tableMapping, primaryKeys, resultSelector, CancellationToken.None);
154158
}

SQLitePCL.pretty.Orm/DatabaseConnection.InsertOrReplace.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public static T InsertOrReplace<T>(
6262
Contract.Requires(This != null);
6363
Contract.Requires(tableMapping != null);
6464
Contract.Requires(obj != null);
65+
Contract.Requires(resultSelector != null);
6566

6667
return This.YieldInsertOrReplaceAll(tableMapping, new T[] {obj}, resultSelector).First().Value;
6768
}
@@ -83,6 +84,7 @@ public static IReadOnlyDictionary<T,T> InsertOrReplaceAll<T>(
8384
Contract.Requires(This != null);
8485
Contract.Requires(tableMapping != null);
8586
Contract.Requires(objects != null);
87+
Contract.Requires(resultSelector != null);
8688

8789
return This.RunInTransaction(_ =>
8890
This.YieldInsertOrReplaceAll(tableMapping, objects, resultSelector)
@@ -111,6 +113,7 @@ public static Task<IReadOnlyDictionary<T,T>> InsertOrReplaceAllAsync<T>(
111113
Contract.Requires(This != null);
112114
Contract.Requires(tableMapping != null);
113115
Contract.Requires(objects != null);
116+
Contract.Requires(resultSelector != null);
114117

115118
return This.Use((db, _) => db.InsertOrReplaceAll(tableMapping, objects, resultSelector), ct);
116119
}
@@ -132,6 +135,7 @@ public static Task<IReadOnlyDictionary<T,T>> InsertOrReplaceAllAsync<T>(
132135
Contract.Requires(This != null);
133136
Contract.Requires(tableMapping != null);
134137
Contract.Requires(objects != null);
138+
Contract.Requires(resultSelector != null);
135139

136140
return This.InsertOrReplaceAllAsync(tableMapping, objects, resultSelector, CancellationToken.None);
137141
}

SQLitePCL.pretty.Orm/Orm.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.Contracts;
34
using SQLitePCL.pretty.Orm.Attributes;
45
using System.Reflection;
56

@@ -17,6 +18,9 @@ public static Func<IReadOnlyList<IResultSetValue>,T> ResultSetRowToObject<T>()
1718

1819
public static Func<IReadOnlyList<IResultSetValue>,T> ResultSetRowToObject<TBuilder, T>(Func<TBuilder> builder, Func<TBuilder,T> build)
1920
{
21+
Contract.Requires(builder != null);
22+
Contract.Requires(build != null);
23+
2024
var columns = new Dictionary<string, PropertyInfo>();
2125
var typ = typeof(TBuilder);
2226

SQLitePCL.pretty.Orm/SqlQuery.Limit.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics.Contracts;
23
using System.Linq.Expressions;
34
using System.Collections.Generic;
45

@@ -33,6 +34,7 @@ internal LimitClause(string table, string selection, Expression where, IReadOnly
3334
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
3435
public LimitClause Take(int n)
3536
{
37+
Contract.Requires(n >= 0);
3638
return new LimitClause(table, selection, where, ordering, n, offset);
3739
}
3840

@@ -43,6 +45,7 @@ public LimitClause Take(int n)
4345
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
4446
public LimitClause Skip(int n)
4547
{
48+
Contract.Requires(n >= 0);
4649
return new LimitClause(table, selection, where, ordering, limit, n);
4750
}
4851

SQLitePCL.pretty.Orm/SqlQuery.OrderBy.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reflection;
55

66
using SQLitePCL.pretty.Orm.Attributes;
7+
using System.Diagnostics.Contracts;
78

89
namespace SQLitePCL.pretty.Orm
910
{
@@ -26,11 +27,13 @@ internal OrderByClause(string table, string selection, Expression where, IReadOn
2627

2728
public OrderByClause<T> ThenBy<TValue>(Expression<Func<T, TValue>> orderExpr)
2829
{
30+
Contract.Requires(orderExpr != null);
2931
return AddOrderBy(orderExpr, true);
3032
}
3133

3234
public OrderByClause<T> ThenByDescending<TValue>(Expression<Func<T, TValue>> orderExpr)
3335
{
36+
Contract.Requires(orderExpr != null);
3437
return AddOrderBy(orderExpr, false);
3538
}
3639

@@ -41,6 +44,7 @@ public OrderByClause<T> ThenByDescending<TValue>(Expression<Func<T, TValue>> ord
4144
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
4245
public LimitClause Take(int n)
4346
{
47+
Contract.Requires(n >= 0);
4448
return new LimitClause(table, selection, where, this.ordering, n, null);
4549
}
4650

@@ -51,6 +55,7 @@ public LimitClause Take(int n)
5155
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
5256
public LimitClause Skip(int n)
5357
{
58+
Contract.Requires(n >= 0);
5459
return new LimitClause(table, selection, where, this.ordering, null, n);
5560
}
5661

@@ -62,6 +67,7 @@ public LimitClause Skip(int n)
6267
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
6368
public LimitClause ElementAt(int index)
6469
{
70+
Contract.Requires(index >= 0);
6571
return Skip(index).Take(1);
6672
}
6773

SQLitePCL.pretty.Orm/SqlQuery.Where.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics.Contracts;
23
using System.Linq.Expressions;
34
using System.Linq;
45
using System.Collections.Generic;
@@ -22,53 +23,64 @@ internal WhereClause(string table, string selection, Expression where)
2223

2324
public OrderByClause<T> OrderBy<TValue>(Expression<Func<T, TValue>> orderExpr)
2425
{
26+
Contract.Requires(orderExpr != null);
2527
return CreateOrderBy(orderExpr, true);
2628
}
2729

2830
public OrderByClause<T> OrderByDescending<TValue>(Expression<Func<T, TValue>> orderExpr)
2931
{
32+
Contract.Requires(orderExpr != null);
3033
return CreateOrderBy(orderExpr, false);
3134
}
3235

3336
private OrderByClause<T> CreateOrderBy<TValue>(Expression<Func<T, TValue>> orderExpr, bool asc)
3437
{
38+
Contract.Requires(orderExpr != null);
39+
3540
var orderBy = new List<Tuple<string, bool>>();
3641
orderBy.Add(orderExpr.CompileOrderByExpression(asc));
3742
return new OrderByClause<T>(table, selection, where, orderBy);
3843
}
3944

4045
public WhereClause<T> Where<U,V,W,X,Y,Z>(Expression<Func<T,U,V,W,X,Y,Z,bool>> predExpr)
4146
{
47+
Contract.Requires(predExpr != null);
4248
return this.Where((LambdaExpression) predExpr);
4349
}
4450

4551
public WhereClause<T> Where<U,V,W,X,Y>(Expression<Func<T,U,V,W,X,Y,bool>> predExpr)
4652
{
53+
Contract.Requires(predExpr != null);
4754
return this.Where((LambdaExpression) predExpr);
4855
}
4956

5057
public WhereClause<T> Where<U,V,W,X>(Expression<Func<T,U,V,W,X,bool>> predExpr)
5158
{
59+
Contract.Requires(predExpr != null);
5260
return this.Where((LambdaExpression) predExpr);
5361
}
5462

5563
public WhereClause<T> Where<U,V,W>(Expression<Func<T,U,V,W,bool>> predExpr)
5664
{
65+
Contract.Requires(predExpr != null);
5766
return this.Where((LambdaExpression) predExpr);
5867
}
5968

6069
public WhereClause<T> Where<U,V>(Expression<Func<T,U,V,bool>> predExpr)
6170
{
71+
Contract.Requires(predExpr != null);
6272
return this.Where((LambdaExpression) predExpr);
6373
}
6474

6575
public WhereClause<T> Where<U>(Expression<Func<T,U,bool>> predExpr)
6676
{
77+
Contract.Requires(predExpr != null);
6778
return this.Where((LambdaExpression) predExpr);
6879
}
6980

7081
public WhereClause<T> Where(Expression<Func<T, bool>> predExpr)
7182
{
83+
Contract.Requires(predExpr != null);
7284
return this.Where((LambdaExpression) predExpr);
7385
}
7486

@@ -86,6 +98,7 @@ private WhereClause<T> Where(LambdaExpression lambda)
8698
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
8799
public LimitClause Take(int n)
88100
{
101+
Contract.Requires(n >= 0);
89102
return new LimitClause(table, selection, where, new List<Tuple<string, bool>>(), n, null);
90103
}
91104

@@ -96,6 +109,7 @@ public LimitClause Take(int n)
96109
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
97110
public LimitClause Skip(int n)
98111
{
112+
Contract.Requires(n >= 0);
99113
return new LimitClause(table, selection, where, new List<Tuple<string, bool>>(), null, n);
100114
}
101115

@@ -107,6 +121,7 @@ public LimitClause Skip(int n)
107121
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
108122
public LimitClause ElementAt(int index)
109123
{
124+
Contract.Requires(index >= 0);
110125
return Skip(index).Take(1);
111126
}
112127

SQLitePCL.pretty.Orm/SqlQuery.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,24 @@ public static partial class SqlQuery
7171
{
7272
public static IEnumerable<IReadOnlyList<IResultSetValue>> Query(this IDatabaseConnection This, ISqlQuery query)
7373
{
74+
Contract.Requires(This != null);
75+
Contract.Requires(query != null);
7476
return This.Query(query.ToSql());
7577
}
7678

7779
public static IEnumerable<IReadOnlyList<IResultSetValue>> Query(
7880
this IDatabaseConnection This, ISqlQuery query, params object[] values)
7981
{
82+
Contract.Requires(This != null);
83+
Contract.Requires(query != null);
84+
Contract.Requires(values != null);
8085
return This.Query(query.ToSql(), values);
8186
}
8287

8388
public static IStatement PrepareStatement(this IDatabaseConnection This, ISqlQuery query)
8489
{
85-
return This.PrepareStatement(query.ToSql());
86-
}
87-
88-
public static IStatement PrepareStatement<T>(this IDatabaseConnection This, ISqlQuery query, TableMapping mapping)
89-
{
90+
Contract.Requires(This != null);
91+
Contract.Requires(query != null);
9092
return This.PrepareStatement(query.ToSql());
9193
}
9294

0 commit comments

Comments
 (0)