66using System . Threading . Tasks ;
77namespace SQLitePCL . pretty . Orm
88{
9- public static partial class TableMapping
9+ public static partial class DatabaseConnection
1010 {
11-
1211 /// <summary>
1312 /// Prepares a SQLite statement that can be bound to an object primary key to delete row from the database.
1413 /// </summary>
1514 /// <returns>A prepared statement.</returns>
1615 /// <param name="This">The database connection</param>
1716 /// <param name="tableMapping">The table mapping.</param>
18- /// <typeparam name="T">The mapped type</typeparam>
19- public static IStatement PrepareDeleteStatement < T > ( this IDatabaseConnection This , ITableMapping < T > tableMapping )
17+ public static IStatement PrepareDeleteStatement ( this IDatabaseConnection This , TableMapping tableMapping )
2018 {
2119 Contract . Requires ( This != null ) ;
2220 Contract . Requires ( tableMapping != null ) ;
2321
2422 return This . PrepareDelete ( tableMapping . TableName , tableMapping . PrimaryKeyColumn ( ) ) ;
2523 }
2624
27- private static IEnumerable < KeyValuePair < long , T > > YieldDeleteAll < T > ( this IDatabaseConnection This , ITableMapping < T > tableMapping , IEnumerable < long > primaryKeys )
25+ private static IEnumerable < KeyValuePair < long , T > > YieldDeleteAll < T > (
26+ this IDatabaseConnection This ,
27+ TableMapping tableMapping ,
28+ IEnumerable < long > primaryKeys ,
29+ Func < IReadOnlyList < IResultSetValue > , T > resultSelector )
2830 {
2931 using ( var deleteStmt = This . PrepareDeleteStatement ( tableMapping ) )
3032 using ( var findStmt = This . PrepareFindStatement ( tableMapping ) )
@@ -35,7 +37,7 @@ private static IEnumerable<KeyValuePair<long,T>> YieldDeleteAll<T>(this IDatabas
3537 {
3638 deleteStmt . Execute ( primaryKey ) ;
3739 return x ;
38- } ) . FirstOrDefault ( ) ;
40+ } ) . Select ( resultSelector ) . FirstOrDefault ( ) ;
3941 yield return new KeyValuePair < long , T > ( primaryKey , result ) ;
4042 }
4143 }
@@ -50,12 +52,17 @@ private static IEnumerable<KeyValuePair<long,T>> YieldDeleteAll<T>(this IDatabas
5052 /// <param name="primaryKey">A primary key.</param>
5153 /// <param name="deleted">If found in the database, the deleted object.</param>
5254 /// <typeparam name="T">The mapped type.</typeparam>
53- public static bool TryDelete < T > ( this IDatabaseConnection This , ITableMapping < T > tableMapping , long primaryKey , out T deleted )
55+ public static bool TryDelete < T > (
56+ this IDatabaseConnection This ,
57+ TableMapping tableMapping ,
58+ long primaryKey ,
59+ Func < IReadOnlyList < IResultSetValue > , T > resultSelector ,
60+ out T deleted )
5461 {
5562 Contract . Requires ( This != null ) ;
5663 Contract . Requires ( tableMapping != null ) ;
5764
58- var result = This . YieldDeleteAll ( tableMapping , new long [ ] { primaryKey } ) . FirstOrDefault ( ) ;
65+ var result = This . YieldDeleteAll ( tableMapping , new long [ ] { primaryKey } , resultSelector ) . FirstOrDefault ( ) ;
5966 if ( result . Value != null )
6067 {
6168 deleted = result . Value ;
@@ -76,18 +83,25 @@ public static bool TryDelete<T>(this IDatabaseConnection This, ITableMapping<T>
7683 /// <param name="tableMapping">The table mapping.</param>
7784 /// <param name="primaryKeys">An IEnumerable of primary keys to delete.</param>
7885 /// <typeparam name="T">The mapped type.</typeparam>
79- public static IReadOnlyDictionary < long , T > DeleteAll < T > ( this IDatabaseConnection This , ITableMapping < T > tableMapping , IEnumerable < long > primaryKeys )
86+ public static IReadOnlyDictionary < long , T > DeleteAll < T > (
87+ this IDatabaseConnection This ,
88+ TableMapping tableMapping ,
89+ IEnumerable < long > primaryKeys ,
90+ Func < IReadOnlyList < IResultSetValue > , T > resultSelector )
8091 {
8192 Contract . Requires ( This != null ) ;
8293 Contract . Requires ( tableMapping != null ) ;
8394 Contract . Requires ( primaryKeys != null ) ;
8495
8596 return This . RunInTransaction ( _ =>
86- This . YieldDeleteAll ( tableMapping , primaryKeys )
97+ This . YieldDeleteAll ( tableMapping , primaryKeys , resultSelector )
8798 . Where ( kvp => kvp . Value != null )
8899 . ToDictionary ( kvp => kvp . Key , kvp => kvp . Value ) ) ;
89100 }
101+ }
90102
103+ public static partial class AsyncDatabaseConnection
104+ {
91105 /// <summary>
92106 /// Deletes all object instances specified by their primary keys.
93107 /// </summary>
@@ -97,13 +111,18 @@ public static IReadOnlyDictionary<long,T> DeleteAll<T>(this IDatabaseConnection
97111 /// <param name="primaryKeys">An IEnumerable of primary keys to delete.</param>
98112 /// <param name="ct">A cancellation token that can be used to cancel the operation.</param>
99113 /// <typeparam name="T">The mapped type.</typeparam>
100- public static Task < IReadOnlyDictionary < long , T > > DeleteAllAsync < T > ( this IAsyncDatabaseConnection This , ITableMapping < T > tableMapping , IEnumerable < long > primaryKeys , CancellationToken ct )
114+ public static Task < IReadOnlyDictionary < long , T > > DeleteAllAsync < T > (
115+ this IAsyncDatabaseConnection This ,
116+ TableMapping tableMapping ,
117+ IEnumerable < long > primaryKeys ,
118+ Func < IReadOnlyList < IResultSetValue > , T > resultSelector ,
119+ CancellationToken ct )
101120 {
102121 Contract . Requires ( This != null ) ;
103122 Contract . Requires ( tableMapping != null ) ;
104123 Contract . Requires ( primaryKeys != null ) ;
105124
106- return This . Use ( ( db , _ ) => db . DeleteAll < T > ( tableMapping , primaryKeys ) , ct ) ;
125+ return This . Use ( ( db , _ ) => db . DeleteAll < T > ( tableMapping , primaryKeys , resultSelector ) , ct ) ;
107126 }
108127
109128 /// <summary>
@@ -114,58 +133,17 @@ public static Task<IReadOnlyDictionary<long,T>> DeleteAllAsync<T>(this IAsyncDat
114133 /// <param name="tableMapping">The table mapping.</param>
115134 /// <param name="primaryKeys">An IEnumerable of primary keys to delete.</param>
116135 /// <typeparam name="T">The mapped type.</typeparam>
117- public static Task < IReadOnlyDictionary < long , T > > DeleteAllAsync < T > ( this IAsyncDatabaseConnection This , ITableMapping < T > tableMapping , IEnumerable < long > primaryKeys )
136+ public static Task < IReadOnlyDictionary < long , T > > DeleteAllAsync < T > (
137+ this IAsyncDatabaseConnection This ,
138+ TableMapping tableMapping ,
139+ IEnumerable < long > primaryKeys ,
140+ Func < IReadOnlyList < IResultSetValue > , T > resultSelector )
118141 {
119142 Contract . Requires ( This != null ) ;
120143 Contract . Requires ( tableMapping != null ) ;
121144 Contract . Requires ( primaryKeys != null ) ;
122145
123- return This . DeleteAllAsync ( tableMapping , primaryKeys , CancellationToken . None ) ;
124- }
125-
126- /// <summary>
127- /// Deletes all rows in a given table.
128- /// </summary>
129- /// <param name="This">The database connection.</param>
130- /// <param name="tableMapping">The table mapping.</param>
131- /// <typeparam name="T">The mapped type.</typeparam>
132- public static void DeleteAllRows < T > ( this IDatabaseConnection This , ITableMapping < T > tableMapping )
133- {
134- Contract . Requires ( This != null ) ;
135- Contract . Requires ( tableMapping != null ) ;
136-
137- This . DeleteAll ( tableMapping . TableName ) ;
138- }
139-
140- /// <summary>
141- /// Deletes all rows in a given table, asynchronously.
142- /// </summary>
143- /// <returns>A task that completes when all rows are deleted succesfully.</returns>
144- /// <param name="This">The database connection.</param>
145- /// <param name="tableMapping">he table mapping.</param>
146- /// <param name="ct">A cancellation token that can be used to cancel the operation.</param>
147- /// <typeparam name="T">The mapped type.</typeparam>
148- public static Task DeleteAllRowsAsync < T > ( this IAsyncDatabaseConnection This , ITableMapping < T > tableMapping , CancellationToken ct )
149- {
150- Contract . Requires ( This != null ) ;
151- Contract . Requires ( tableMapping != null ) ;
152-
153- return This . Use ( ( db , _ ) => db . DeleteAllRows ( tableMapping ) , ct ) ;
154- }
155-
156- /// <summary>
157- /// Deletes all rows in a given table, asynchronously.
158- /// </summary>
159- /// <returns>A task that completes when all rows are deleted succesfully.</returns>
160- /// <param name="This">The database connection.</param>
161- /// <param name="tableMapping">he table mapping.</param>
162- /// <typeparam name="T">The mapped type.</typeparam>
163- public static Task DeleteAllRowsAsync < T > ( this IAsyncDatabaseConnection This , ITableMapping < T > tableMapping )
164- {
165- Contract . Requires ( This != null ) ;
166- Contract . Requires ( tableMapping != null ) ;
167-
168- return This . DeleteAllRowsAsync ( tableMapping , CancellationToken . None ) ;
146+ return This . DeleteAllAsync ( tableMapping , primaryKeys , resultSelector , CancellationToken . None ) ;
169147 }
170148 }
171149}
0 commit comments