Skip to content

Commit 77e0f0c

Browse files
committed
more docs. simplified tablemappedstatement
1 parent 890b4cf commit 77e0f0c

9 files changed

Lines changed: 231 additions & 50 deletions

File tree

SQLitePCL.pretty.Orm/Interfaces.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ public interface ITableMappedStatement<T> : IStatement
4747
/// The underlying <see cref="ITableMapping&lt;T&gt;"/> used to map the result set to an instance of T.
4848
/// </summary>
4949
ITableMapping<T> Mapping { get; }
50-
51-
/// <summary>
52-
/// Gets the current result value at the current position of the statement enumerator.
53-
/// </summary>
54-
new T Current { get; }
5550
}
5651
}
5752

SQLitePCL.pretty.Orm/Ordering.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SQLitePCL.pretty.Orm
44
{
5+
/*
56
internal sealed class Ordering : IEquatable<Ordering>
67
{
78
public static bool operator ==(Ordering x, Ordering y)
@@ -57,6 +58,6 @@ public override int GetHashCode()
5758
hash = hash * 31 + this.ColumnName.GetHashCode();
5859
return hash;
5960
}
60-
}
61+
} */
6162
}
6263

SQLitePCL.pretty.Orm/TableMappedStatement.cs

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Diagnostics.Contracts;
5+
using System.Linq;
6+
using System.Reactive.Linq;
57

68
namespace SQLitePCL.pretty.Orm
79
{
@@ -10,14 +12,13 @@ namespace SQLitePCL.pretty.Orm
1012
/// </summary>
1113
public static class TableMappedStatement
1214
{
13-
/*
1415
/// <summary>
1516
/// Binds the statement bind variables by name to the corresponding properties on the object <paramref name="obj"/>.
1617
/// </summary>
1718
/// <param name="This">The statement.</param>
1819
/// <param name="obj">The object to bind.</param>
1920
/// <typeparam name="T">The mapped type.</typeparam>
20-
static void Bind<T>(this ITableMappedStatement<T> This, T obj)
21+
public static void Bind<T>(this ITableMappedStatement<T> This, T obj)
2122
{
2223
This.Bind(This.Mapping, obj);
2324
}
@@ -39,14 +40,6 @@ public static void Execute<T>(this ITableMappedStatement<T> This, T obj)
3940
This.ClearBindings();
4041
This.Bind(obj);
4142
This.MoveNext();
42-
}*/
43-
44-
private static IEnumerator<T> Enumerate<T>(this ITableMappedStatement<T> This)
45-
{
46-
while (This.MoveNext())
47-
{
48-
yield return This.Current;
49-
}
5043
}
5144

5245
/// <summary>
@@ -58,14 +51,7 @@ private static IEnumerator<T> Enumerate<T>(this ITableMappedStatement<T> This)
5851
public static IEnumerable<T> Query<T>(this ITableMappedStatement<T> This)
5952
{
6053
Contract.Requires(This != null);
61-
62-
return new DelegatingEnumerable<T>(() =>
63-
{
64-
This.Reset();
65-
66-
// Prevent the statement from being disposed when the enumerator is disposed
67-
return This.Enumerate();
68-
});
54+
return ((IStatement) This).Query().Select(This.Mapping.ToObject);
6955
}
7056

7157
/// <summary>
@@ -81,15 +67,7 @@ public static IEnumerable<T> Query<T>(
8167
Contract.Requires(This != null);
8268
Contract.Requires(values != null);
8369

84-
return new DelegatingEnumerable<T>(() =>
85-
{
86-
This.Reset();
87-
This.ClearBindings();
88-
This.Bind(values);
89-
90-
// Prevent the statement from being disposed when the enumerator is disposed
91-
return This.Enumerate();
92-
});
70+
return ((IStatement) This).Query(values).Select(This.Mapping.ToObject);
9371
}
9472
}
9573

@@ -186,14 +164,6 @@ IReadOnlyList<IResultSetValue> IEnumerator<IReadOnlyList<IResultSetValue>>.Curre
186164
return deleg.Current;
187165
}
188166
}
189-
190-
T ITableMappedStatement<T>.Current
191-
{
192-
get
193-
{
194-
return mapping.ToObject(((IEnumerator<IReadOnlyList<IResultSetValue>>) this).Current);
195-
}
196-
}
197167
}
198168
}
199169

SQLitePCL.pretty.Orm/TableMapping.Delete.cs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ namespace SQLitePCL.pretty.Orm
77
{
88
public static partial class TableMapping
99
{
10+
11+
/// <summary>
12+
/// Prepares a SQLite statement that can be bound to an object primary key to delete row from the database.
13+
/// </summary>
14+
/// <returns>A prepared statement.</returns>
15+
/// <param name="This">The database connection</param>
16+
/// <param name="tableMapping">The table mapping.</param>
17+
/// <typeparam name="T">The mapped type</typeparam>
1018
public static ITableMappedStatement<T> PrepareDeleteStatement<T>(this IDatabaseConnection This, ITableMapping<T> tableMapping)
1119
{
1220
return new TableMappedStatement<T>(
@@ -31,6 +39,15 @@ private static IEnumerable<KeyValuePair<long,T>> YieldDeleteAll<T>(this IDatabas
3139
}
3240
}
3341

42+
/// <summary>
43+
/// Tries to delete the object in the database with the provided primary key.
44+
/// </summary>
45+
/// <returns><c>true</c>, if an object was found and deleted, <c>false</c> otherwise.</returns>
46+
/// <param name="This">The database connection.</param>
47+
/// <param name="tableMapping">The table mapping.</param>
48+
/// <param name="primaryKey">A primary key.</param>
49+
/// <param name="deleted">If found in the database, the deleted object.</param>
50+
/// <typeparam name="T">The mapped type.</typeparam>
3451
public static bool TryDelete<T>(this IDatabaseConnection This, ITableMapping<T> tableMapping, long primaryKey, out T deleted)
3552
{
3653
var result = This.YieldDeleteAll(tableMapping, new long[] { primaryKey }).FirstOrDefault();
@@ -45,7 +62,15 @@ public static bool TryDelete<T>(this IDatabaseConnection This, ITableMapping<T>
4562
return false;
4663
}
4764
}
48-
65+
66+
/// <summary>
67+
/// Deleted all object instances specified by their primary keys.
68+
/// </summary>
69+
/// <returns>A dictionary mapping the primary key to its value if found in the database.</returns>
70+
/// <param name="This">The database connection.</param>
71+
/// <param name="tableMapping">The table mapping.</param>
72+
/// <param name="primaryKeys">An IEnumerable of primary keys to delete.</param>
73+
/// <typeparam name="T">The mapped type.</typeparam>
4974
public static IReadOnlyDictionary<long,T> DeleteAll<T>(this IDatabaseConnection This, ITableMapping<T> tableMapping, IEnumerable<long> primaryKeys)
5075
{
5176
return This.RunInTransaction(_ =>
@@ -54,26 +79,64 @@ public static IReadOnlyDictionary<long,T> DeleteAll<T>(this IDatabaseConnection
5479
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
5580
}
5681

82+
/// <summary>
83+
/// Deletes all object instances specified by their primary keys.
84+
/// </summary>
85+
/// <returns>A task that completes with a dictionary mapping the primary key to its value if found in the database.</returns>
86+
/// <param name="This">The database connection.</param>
87+
/// <param name="tableMapping">The table mapping.</param>
88+
/// <param name="primaryKeys">An IEnumerable of primary keys to delete.</param>
89+
/// <param name="ct">A cancellation token that can be used to cancel the operation.</param>
90+
/// <typeparam name="T">The mapped type.</typeparam>
5791
public static Task<IReadOnlyDictionary<long,T>> DeleteAllAsync<T>(this IAsyncDatabaseConnection This, ITableMapping<T> tableMapping, IEnumerable<long> primaryKeys, CancellationToken ct)
5892
{
5993
return This.Use((db,_) => db.DeleteAll<T>(tableMapping, primaryKeys), ct);
6094
}
6195

96+
/// <summary>
97+
/// Deletes all object instances specified by their primary keys.
98+
/// </summary>
99+
/// <returns>A task that completes with a dictionary mapping the primary key to its value if found in the database.</returns>
100+
/// <param name="This">The database connection.</param>
101+
/// <param name="tableMapping">The table mapping.</param>
102+
/// <param name="primaryKeys">An IEnumerable of primary keys to delete.</param>
103+
/// <typeparam name="T">The mapped type.</typeparam>
62104
public static Task<IReadOnlyDictionary<long,T>> DeleteAllAsync<T>(this IAsyncDatabaseConnection This, ITableMapping<T> tableMapping, IEnumerable<long> primaryKeys)
63105
{
64106
return This.DeleteAllAsync(tableMapping, primaryKeys, CancellationToken.None);
65107
}
66108

109+
/// <summary>
110+
/// Deletes all rows in a given table.
111+
/// </summary>
112+
/// <param name="This">The database connection.</param>
113+
/// <param name="tableMapping">The table mapping.</param>
114+
/// <typeparam name="T">The mapped type.</typeparam>
67115
public static void DeleteAllRows<T>(this IDatabaseConnection This, ITableMapping<T> tableMapping)
68116
{
69117
This.DeleteAll(tableMapping.TableName);
70118
}
71119

120+
/// <summary>
121+
/// Deletes all rows in a given table, asynchronously.
122+
/// </summary>
123+
/// <returns>A task that completes when all rows are deleted succesfully.</returns>
124+
/// <param name="This">The database connection.</param>
125+
/// <param name="tableMapping">he table mapping.</param>
126+
/// <param name="ct">A cancellation token that can be used to cancel the operation.</param>
127+
/// <typeparam name="T">The mapped type.</typeparam>
72128
public static Task DeleteAllRowsAsync<T>(this IAsyncDatabaseConnection This, ITableMapping<T> tableMapping, CancellationToken ct)
73129
{
74130
return This.Use((db, _) => db.DeleteAllRows(tableMapping), ct);
75131
}
76132

133+
/// <summary>
134+
/// Deletes all rows in a given table, asynchronously.
135+
/// </summary>
136+
/// <returns>A task that completes when all rows are deleted succesfully.</returns>
137+
/// <param name="This">The database connection.</param>
138+
/// <param name="tableMapping">he table mapping.</param>
139+
/// <typeparam name="T">The mapped type.</typeparam>
77140
public static Task DeleteAllRowsAsync<T>(this IAsyncDatabaseConnection This, ITableMapping<T> tableMapping)
78141
{
79142
return This.DeleteAllRowsAsync(tableMapping, CancellationToken.None);

SQLitePCL.pretty.Orm/TableMapping.Find.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ private static string Find(this ITableMapping This)
2121
});
2222
}
2323

24+
/// <summary>
25+
/// Prepares a SQLite statement that can be bound to an object primary key to retrieve an instance from the database.
26+
/// </summary>
27+
/// <returns>A prepared statement.</returns>
28+
/// <param name="This">The database connection</param>
29+
/// <param name="tableMapping">The table mapping.</param>
30+
/// <typeparam name="T">The mapped type</typeparam>
2431
public static ITableMappedStatement<T> PrepareFindStatement<T>(this IDatabaseConnection This, ITableMapping<T> tableMapping)
2532
{
2633
return new TableMappedStatement<T>(This.PrepareStatement(tableMapping.Find()), tableMapping);
@@ -38,6 +45,15 @@ private static IEnumerable<KeyValuePair<long,T>> YieldFindAll<T>(this IDatabaseC
3845
}
3946
}
4047

48+
/// <summary>
49+
/// Tries to find an object in the database with the provided primary key.
50+
/// </summary>
51+
/// <returns><c>true</c>, if an object was found, <c>false</c> otherwise.</returns>
52+
/// <param name="This">The database connection.</param>
53+
/// <param name="tableMapping">The table mapping.</param>
54+
/// <param name="primaryKey">A primary key.</param>
55+
/// <param name="value">If found in the database, the found object.</param>
56+
/// <typeparam name="T">The mapped type.</typeparam>
4157
public static bool TryFind<T>(this IDatabaseConnection This, ITableMapping<T> tableMapping, long primaryKey, out T value)
4258
{
4359
var result = This.YieldFindAll(tableMapping, new long[] { primaryKey }).FirstOrDefault();
@@ -52,13 +68,30 @@ public static bool TryFind<T>(this IDatabaseConnection This, ITableMapping<T> ta
5268
return false;
5369
}
5470

71+
/// <summary>
72+
/// Finds all object instances specified by their primary keys.
73+
/// </summary>
74+
/// <returns>A dictionary mapping the primary key to its value if found in the database.</returns>
75+
/// <param name="This">The database connection.</param>
76+
/// <param name="tableMapping">The table mapping.</param>
77+
/// <param name="primaryKeys">An IEnumerable of primary keys to find.</param>
78+
/// <typeparam name="T">The mapped type.</typeparam>
5579
public static IReadOnlyDictionary<long,T> FindAll<T>(this IDatabaseConnection This, ITableMapping<T> tableMapping, IEnumerable<long> primaryKeys)
5680
{
5781
return This.YieldFindAll(tableMapping, primaryKeys)
5882
.Where(kvp => kvp.Value != null)
5983
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
6084
}
6185

86+
/// <summary>
87+
/// Finds all object instances specified by their primary keys.
88+
/// </summary>
89+
/// <returns>A task that completes with a dictionary mapping the primary key to its value if found in the database.</returns>
90+
/// <param name="This">The database connection.</param>
91+
/// <param name="tableMapping">The table mapping.</param>
92+
/// <param name="primaryKeys">An IEnumerable of primary keys to find.</param>
93+
/// <param name="ct">A cancellation token that can be used to cancel the operation</param>
94+
/// <typeparam name="T">The mapped type.</typeparam>
6295
public static Task<IReadOnlyDictionary<long,T>> FindAllAsync<T>(
6396
this IAsyncDatabaseConnection This,
6497
ITableMapping<T> tableMapping,
@@ -68,14 +101,21 @@ public static Task<IReadOnlyDictionary<long,T>> FindAllAsync<T>(
68101
return This.Use((db,_) => db.FindAll(tableMapping, primaryKeys), ct);
69102
}
70103

104+
/// <summary>
105+
/// Finds all object instances specified by their primary keys.
106+
/// </summary>
107+
/// <returns>A task that completes with a dictionary mapping the primary key to its value if found in the database.</returns>
108+
/// <param name="This">The database connection.</param>
109+
/// <param name="tableMapping">The table mapping.</param>
110+
/// <param name="primaryKeys">An IEnumerable of primary keys to find.</param>
111+
/// <typeparam name="T">The mapped type.</typeparam>
71112
public static Task<IReadOnlyDictionary<long,T>> FindAllAsync<T>(
72113
this IAsyncDatabaseConnection This,
73114
ITableMapping<T> tableMapping,
74115
IEnumerable<long> primaryKeys)
75116
{
76117
return This.FindAllAsync(tableMapping, primaryKeys, CancellationToken.None);
77118
}
78-
79119
}
80120
}
81121

SQLitePCL.pretty.Orm/TableMapping.InsertOrReplace.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ private static string InsertOrReplace<T>(this ITableMapping<T> tableMapping)
1313
return SQLBuilder.InsertOrReplace(tableMapping.TableName, tableMapping.Columns.Select(x => x.Key));
1414
}
1515

16+
/// <summary>
17+
/// Prepares a SQLite statement that can be bound to an object of type T to insert into the database.
18+
/// </summary>
19+
/// <returns>A prepared statement.</returns>
20+
/// <param name="This">The database connection</param>
21+
/// <param name="tableMapping">The table mapping.</param>
22+
/// <typeparam name="T">The mapped type</typeparam>
1623
public static ITableMappedStatement<T> PrepareInsertOrReplaceStatement<T>(this IDatabaseConnection This, ITableMapping<T> tableMapping)
1724
{
1825
return new TableMappedStatement<T>(This.PrepareStatement(tableMapping.InsertOrReplace()), tableMapping);
@@ -31,18 +38,43 @@ private static IEnumerable<KeyValuePair<T,T>> YieldInsertOrReplaceAll<T>(this ID
3138
}
3239
}
3340
}
34-
41+
42+
/// <summary>
43+
/// Inserts an object into the database, replacing the existing entry if the primary key already exists.
44+
/// </summary>
45+
/// <returns>The object inserted into the database including it's primary key.</returns>
46+
/// <param name="This">The database connection.</param>
47+
/// <param name="tableMapping">The table mapping.</param>
48+
/// <param name="obj">The object to insert.</param>
49+
/// <typeparam name="T">The mapped type.</typeparam>
3550
public static T InsertOrReplace<T>(this IDatabaseConnection This, ITableMapping<T> tableMapping, T obj)
3651
{
3752
return This.YieldInsertOrReplaceAll(tableMapping, new T[] {obj}).First().Value;
3853
}
3954

55+
/// <summary>
56+
/// Inserts the objects into the database, replacing existing entries if the given primary keys already exist.
57+
/// </summary>
58+
/// <returns>A dictionary mapping the provided objects to the objects that were inserted into the database.</returns>
59+
/// <param name="This">The database connection.</param>
60+
/// <param name="tableMapping">The table mapping.</param>
61+
/// <param name="objects">The objects to be inserted into the database.</param>
62+
/// <typeparam name="T">The mapped type.</typeparam>
4063
public static IReadOnlyDictionary<T,T> InsertOrReplaceAll<T>(this IDatabaseConnection This, ITableMapping<T> tableMapping, IEnumerable<T> objects)
4164
{
4265
return This.RunInTransaction(_ =>
4366
This.YieldInsertOrReplaceAll(tableMapping, objects).ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
4467
}
4568

69+
/// <summary>
70+
/// Inserts the objects into the database, replacing existing entries if the given primary keys already exist.
71+
/// </summary>
72+
/// <returns>A Task that completes with a dictionary mapping the provided objects to the objects that were inserted into the database.</returns>
73+
/// <param name="This">The database connection.</param>
74+
/// <param name="tableMapping">The table mapping.</param>
75+
/// <param name="objects">The objects to be inserted into the database.</param>
76+
/// <param name="ct">A cancellation token that can be used to cancel the operation</param>
77+
/// <typeparam name="T">The mapped type.</typeparam>
4678
public static Task<IReadOnlyDictionary<T,T>> InsertOrReplaceAllAsync<T>(
4779
this IAsyncDatabaseConnection This,
4880
ITableMapping<T> tableMapping,
@@ -52,6 +84,14 @@ public static Task<IReadOnlyDictionary<T,T>> InsertOrReplaceAllAsync<T>(
5284
return This.Use((db, _) => db.InsertOrReplaceAll(tableMapping, objects), ct);
5385
}
5486

87+
/// <summary>
88+
/// Inserts the objects into the database, replacing existing entries if the given primary keys already exist.
89+
/// </summary>
90+
/// <returns>A Task that completes with a dictionary mapping the provided objects to the objects that were inserted into the database.</returns>
91+
/// <param name="This">The database connection.</param>
92+
/// <param name="tableMapping">The table mapping.</param>
93+
/// <param name="objects">The objects to be inserted into the database.</param>
94+
/// <typeparam name="T">The mapped type.</typeparam>
5595
public static Task<IReadOnlyDictionary<T,T>> InsertOrReplaceAllAsync<T>(
5696
this IAsyncDatabaseConnection This,
5797
ITableMapping<T> tableMapping,

0 commit comments

Comments
 (0)