44using System . Linq ;
55using System . Threading ;
66using System . Threading . Tasks ;
7+ using System . Runtime . CompilerServices ;
8+
9+
710namespace SQLitePCL . pretty . Orm
811{
912 public static partial class DatabaseConnection
1013 {
14+ private static readonly ConditionalWeakTable < TableMapping , string > deleteQueries =
15+ new ConditionalWeakTable < TableMapping , string > ( ) ;
16+
1117 /// <summary>
1218 /// Prepares a SQLite statement that can be bound to an object primary key to delete row from the database.
1319 /// </summary>
1420 /// <returns>A prepared statement.</returns>
1521 /// <param name="This">The database connection</param>
1622 /// <param name="tableMapping">The table mapping.</param>
17- public static IStatement PrepareDeleteStatement ( this IDatabaseConnection This , TableMapping tableMapping )
23+ public static IStatement PrepareDeleteStatement < T > ( this IDatabaseConnection This )
1824 {
1925 Contract . Requires ( This != null ) ;
20- Contract . Requires ( tableMapping != null ) ;
26+
27+ var tableMapping = TableMapping . Create < T > ( ) ;
28+ var sql = deleteQueries . GetValue ( tableMapping , mapping =>
29+ {
30+ var primaryKeyColumn = mapping . PrimaryKeyColumn ( ) ;
31+ return SQLBuilder . DeleteUsingPrimaryKey ( mapping . TableName , primaryKeyColumn ) ;
32+ } ) ;
33+
2134
22- return This . PrepareDelete ( tableMapping . TableName , tableMapping . PrimaryKeyColumn ( ) ) ;
35+ return This . PrepareStatement ( sql ) ;
2336 }
2437
2538 private static IEnumerable < KeyValuePair < long , T > > YieldDeleteAll < T > (
2639 this IDatabaseConnection This ,
27- TableMapping tableMapping ,
2840 IEnumerable < long > primaryKeys ,
2941 Func < IReadOnlyList < IResultSetValue > , T > resultSelector )
3042 {
31- using ( var deleteStmt = This . PrepareDeleteStatement ( tableMapping ) )
32- using ( var findStmt = This . PrepareFindStatement ( tableMapping ) )
43+ using ( var deleteStmt = This . PrepareDeleteStatement < T > ( ) )
44+ using ( var findStmt = This . PrepareFindStatement < T > ( ) )
3345 {
3446 foreach ( var primaryKey in primaryKeys )
3547 {
@@ -54,16 +66,14 @@ private static IEnumerable<KeyValuePair<long,T>> YieldDeleteAll<T>(
5466 /// <typeparam name="T">The mapped type.</typeparam>
5567 public static bool TryDelete < T > (
5668 this IDatabaseConnection This ,
57- TableMapping tableMapping ,
5869 long primaryKey ,
5970 Func < IReadOnlyList < IResultSetValue > , T > resultSelector ,
6071 out T deleted )
6172 {
6273 Contract . Requires ( This != null ) ;
63- Contract . Requires ( tableMapping != null ) ;
6474 Contract . Requires ( resultSelector != null ) ;
6575
66- var result = This . YieldDeleteAll ( tableMapping , new long [ ] { primaryKey } , resultSelector ) . FirstOrDefault ( ) ;
76+ var result = This . YieldDeleteAll ( new long [ ] { primaryKey } , resultSelector ) . FirstOrDefault ( ) ;
6777 if ( result . Value != null )
6878 {
6979 deleted = result . Value ;
@@ -85,21 +95,47 @@ public static bool TryDelete<T>(
8595 /// <param name="primaryKeys">An IEnumerable of primary keys to delete.</param>
8696 /// <typeparam name="T">The mapped type.</typeparam>
8797 public static IReadOnlyDictionary < long , T > DeleteAll < T > (
88- this IDatabaseConnection This ,
89- TableMapping tableMapping ,
98+ this IDatabaseConnection This ,
9099 IEnumerable < long > primaryKeys ,
91100 Func < IReadOnlyList < IResultSetValue > , T > resultSelector )
92101 {
93102 Contract . Requires ( This != null ) ;
94- Contract . Requires ( tableMapping != null ) ;
95103 Contract . Requires ( primaryKeys != null ) ;
96104 Contract . Requires ( resultSelector != null ) ;
97105
98106 return This . RunInTransaction ( _ =>
99- This . YieldDeleteAll ( tableMapping , primaryKeys , resultSelector )
107+ This . YieldDeleteAll ( primaryKeys , resultSelector )
100108 . Where ( kvp => kvp . Value != null )
101109 . ToDictionary ( kvp => kvp . Key , kvp => kvp . Value ) ) ;
102110 }
111+
112+ /// <summary>
113+ /// Drops the table if it exists. Otherwise this is a no-op.
114+ /// </summary>
115+ /// <param name="This">The database connection.</param>
116+ /// <param name="table">The table name.</param>
117+ /// <seealso href="https://www.sqlite.org/lang_droptable.html"/>
118+ public static void DropTableIfExists < T > ( this IDatabaseConnection This )
119+ {
120+ Contract . Requires ( This != null ) ;
121+
122+ var tableMapping = TableMapping . Create < T > ( ) ;
123+ This . Execute ( SQLBuilder . DropTableIfExists ( tableMapping . TableName ) ) ;
124+ }
125+
126+
127+ /// <summary>
128+ /// Deletes all rows in a given table.
129+ /// </summary>
130+ /// <param name="This">The database connection.</param>
131+ /// <param name="table">The table name.</param>
132+ public static void DeleteAllRows < T > ( this IDatabaseConnection This )
133+ {
134+ Contract . Requires ( This != null ) ;
135+
136+ var tableMapping = TableMapping . Create < T > ( ) ;
137+ This . Execute ( SQLBuilder . DeleteAll ( tableMapping . TableName ) ) ;
138+ }
103139 }
104140
105141 public static partial class AsyncDatabaseConnection
@@ -115,17 +151,15 @@ public static partial class AsyncDatabaseConnection
115151 /// <typeparam name="T">The mapped type.</typeparam>
116152 public static Task < IReadOnlyDictionary < long , T > > DeleteAllAsync < T > (
117153 this IAsyncDatabaseConnection This ,
118- TableMapping tableMapping ,
119154 IEnumerable < long > primaryKeys ,
120155 Func < IReadOnlyList < IResultSetValue > , T > resultSelector ,
121156 CancellationToken ct )
122157 {
123158 Contract . Requires ( This != null ) ;
124- Contract . Requires ( tableMapping != null ) ;
125159 Contract . Requires ( primaryKeys != null ) ;
126160 Contract . Requires ( resultSelector != null ) ;
127161
128- return This . Use ( ( db , _ ) => db . DeleteAll < T > ( tableMapping , primaryKeys , resultSelector ) , ct ) ;
162+ return This . Use ( ( db , _ ) => db . DeleteAll < T > ( primaryKeys , resultSelector ) , ct ) ;
129163 }
130164
131165 /// <summary>
@@ -138,16 +172,66 @@ public static Task<IReadOnlyDictionary<long,T>> DeleteAllAsync<T>(
138172 /// <typeparam name="T">The mapped type.</typeparam>
139173 public static Task < IReadOnlyDictionary < long , T > > DeleteAllAsync < T > (
140174 this IAsyncDatabaseConnection This ,
141- TableMapping tableMapping ,
142175 IEnumerable < long > primaryKeys ,
143176 Func < IReadOnlyList < IResultSetValue > , T > resultSelector )
144177 {
145178 Contract . Requires ( This != null ) ;
146- Contract . Requires ( tableMapping != null ) ;
147179 Contract . Requires ( primaryKeys != null ) ;
148180 Contract . Requires ( resultSelector != null ) ;
149181
150- return This . DeleteAllAsync ( tableMapping , primaryKeys , resultSelector , CancellationToken . None ) ;
182+ return This . DeleteAllAsync ( primaryKeys , resultSelector , CancellationToken . None ) ;
183+ }
184+
185+
186+ /// <summary>
187+ /// Drops the table if exists async.
188+ /// </summary>
189+ /// <returns>The table if exists async.</returns>
190+ /// <param name="This">This.</param>
191+ /// <param name="table">The table name.</param>
192+ /// <param name="ct">Ct.</param>
193+ public static Task DropTableIfExistsAsync < T > ( this IAsyncDatabaseConnection This , CancellationToken ct )
194+ {
195+ Contract . Requires ( This != null ) ;
196+
197+ return This . Use ( ( db , _ ) => db . DropTableIfExists < T > ( ) , ct ) ;
198+ }
199+
200+ /// <summary>
201+ /// Drops the table if exists async.
202+ /// </summary>
203+ /// <returns>The table if exists async.</returns>
204+ /// <param name="This">This.</param>
205+ /// <param name="table">The table name.</param>
206+ public static Task DropTableIfExistsAsync < T > ( this IAsyncDatabaseConnection This )
207+ {
208+ Contract . Requires ( This != null ) ;
209+ return This . DropTableIfExistsAsync < T > ( CancellationToken . None ) ;
210+ }
211+
212+ /// <summary>
213+ /// Deletes all rows in a given table, asynchronously.
214+ /// </summary>
215+ /// <returns>A task that completes when all rows are deleted succesfully.</returns>
216+ /// <param name="This">The database connection.</param>
217+ /// <param name="table">The table name.</param>
218+ /// <param name="ct">A cancellation token that can be used to cancel the operation.</param>
219+ public static Task DeleteAllRowsAsync < T > ( this IAsyncDatabaseConnection This , CancellationToken ct )
220+ {
221+ Contract . Requires ( This != null ) ;
222+ return This . Use ( ( db , _ ) => db . DeleteAllRows < T > ( ) , ct ) ;
223+ }
224+
225+ /// <summary>
226+ /// Deletes all rows in a given table, asynchronously.
227+ /// </summary>
228+ /// <returns>A task that completes when all rows are deleted succesfully.</returns>
229+ /// <param name="This">The database connection.</param>
230+ /// <param name="table">The table name.</param>
231+ public static Task DeleteAllRowsAsync < T > ( this IAsyncDatabaseConnection This )
232+ {
233+ Contract . Requires ( This != null ) ;
234+ return This . DeleteAllRowsAsync < T > ( CancellationToken . None ) ;
151235 }
152236 }
153237}
0 commit comments