Skip to content

Commit 3c4e6ab

Browse files
committed
More clean up. s/Orm/ResultSet for the rowToObject function.
1 parent c7f74da commit 3c4e6ab

17 files changed

Lines changed: 98 additions & 90 deletions

SQLitePCL.pretty.Orm/Attributes.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ public NotNullAttribute(object defaultValue)
250250
[AttributeUsage (AttributeTargets.Property)]
251251
public sealed class ForeignKeyAttribute : Attribute
252252
{
253-
private readonly ForeignKeyConstraint constraint;
253+
private readonly string tableName;
254+
private readonly string columnName;
254255

255256
/// <summary>
256257
/// Initializes a new instance of the <see cref="SQLitePCL.pretty.Orm.Attributes.ForeignKeyAttribute"/> class.
@@ -260,9 +261,10 @@ public ForeignKeyAttribute(Type typ)
260261
{
261262
Contract.Requires(typ != null);
262263

263-
var tableName = typ.GetTableName();
264-
var columnName = typ.GetPublicInstanceProperties().Where(x => x.IsPrimaryKey()).Select(x => x.GetColumnName()).First();
265-
this.constraint = new ForeignKeyConstraint(tableName, columnName);
264+
var table = TableMapping.Get(typ);
265+
266+
this.tableName = table.TableName;
267+
this.columnName = table.PrimaryKeyColumn();
266268
}
267269

268270
/// <summary>
@@ -275,13 +277,12 @@ public ForeignKeyAttribute(string tableName, string columnName)
275277
Contract.Requires(tableName != null);
276278
Contract.Requires(columnName != null);
277279

278-
this.constraint = new ForeignKeyConstraint(tableName, columnName);
280+
this.tableName = tableName;
281+
this.columnName = columnName;
279282
}
280283

281-
/// <summary>
282-
/// Gets the foreign key constraint value.
283-
/// </summary>
284-
public ForeignKeyConstraint Value { get { return constraint; } }
284+
public string TableName { get { return tableName; } }
285+
public string ColumnName { get { return columnName; } }
285286
}
286287

287288
internal static class OrmAttributes
@@ -341,8 +342,10 @@ internal static object GetDefaultValue(this PropertyInfo This)
341342

342343
internal static ForeignKeyConstraint GetForeignKeyConstraint(this PropertyInfo This)
343344
{
344-
var fkAttr = This.GetCustomAttributes<ForeignKeyAttribute>(true).FirstOrDefault();
345-
return fkAttr != null ? fkAttr.Value : null;
345+
return
346+
This.GetCustomAttributes<ForeignKeyAttribute>(true)
347+
.Select(x => new ForeignKeyConstraint(x.TableName, x.ColumnName))
348+
.FirstOrDefault();
346349
}
347350

348351
internal static IEnumerable<PropertyInfo> GetNotIgnoredGettableProperties(this Type This)

SQLitePCL.pretty.Orm/ColumnMapping.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace SQLitePCL.pretty.Orm
66
/// <summary>
77
/// Provides the mapping between a column's SQL and CLR representation.
88
/// </summary>
9-
public sealed class ColumnMapping: IEquatable<ColumnMapping>
9+
internal sealed class ColumnMapping: IEquatable<ColumnMapping>
1010
{
1111
/// <summary>
1212
/// Indicates whether the two ColumnMapping instances are equal to each other.

SQLitePCL.pretty.Orm/DatabaseConnection.Delete.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static IStatement PrepareDeleteStatement<T>(this IDatabaseConnection This
2424
{
2525
Contract.Requires(This != null);
2626

27-
var tableMapping = TableMapping.Create<T>();
27+
var tableMapping = TableMapping.Get<T>();
2828
var sql = deleteQueries.GetValue(tableMapping, mapping =>
2929
{
3030
var primaryKeyColumn = mapping.PrimaryKeyColumn();
@@ -119,7 +119,7 @@ public static void DropTableIfExists<T>(this IDatabaseConnection This)
119119
{
120120
Contract.Requires(This != null);
121121

122-
var tableMapping = TableMapping.Create<T>();
122+
var tableMapping = TableMapping.Get<T>();
123123
This.Execute(SQLBuilder.DropTableIfExists(tableMapping.TableName));
124124
}
125125

@@ -133,7 +133,7 @@ public static void DeleteAllRows<T>(this IDatabaseConnection This)
133133
{
134134
Contract.Requires(This != null);
135135

136-
var tableMapping = TableMapping.Create<T>();
136+
var tableMapping = TableMapping.Get<T>();
137137
This.Execute(SQLBuilder.DeleteAll(tableMapping.TableName));
138138
}
139139
}

SQLitePCL.pretty.Orm/DatabaseConnection.Find.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static IStatement PrepareFindStatement<T>(this IDatabaseConnection This)
2323
{
2424
Contract.Requires(This != null);
2525

26-
var tableMapping = TableMapping.Create<T>();
26+
var tableMapping = TableMapping.Get<T>();
2727
var sql = findQueries.GetValue(tableMapping, mapping =>
2828
{
2929
var column = mapping.PrimaryKeyColumn();

SQLitePCL.pretty.Orm/DatabaseConnection.InsertOrReplace.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static IStatement PrepareInsertOrReplaceStatement<T>(this IDatabaseConnec
2323
{
2424
Contract.Requires(This != null);
2525

26-
var sql = insertOrReplaceQueries.GetValue(TableMapping.Create<T>(), mapping =>
26+
var sql = insertOrReplaceQueries.GetValue(TableMapping.Get<T>(), mapping =>
2727
{
2828
var column = mapping.PrimaryKeyColumn();
2929
return SQLBuilder.InsertOrReplace(mapping.TableName, mapping.Columns.Select(x => x.Key));
@@ -32,19 +32,19 @@ public static IStatement PrepareInsertOrReplaceStatement<T>(this IDatabaseConnec
3232
return This.PrepareStatement(sql);
3333
}
3434

35-
private static IEnumerable<KeyValuePair<TInserted, TResult>> YieldInsertOrReplaceAll<TInserted, TResult>(
35+
private static IEnumerable<KeyValuePair<T,T>> YieldInsertOrReplaceAll<T>(
3636
this IDatabaseConnection This,
37-
IEnumerable<TInserted> objects,
38-
Func<IReadOnlyList<IResultSetValue>,TResult> resultSelector)
37+
IEnumerable<T> objects,
38+
Func<IReadOnlyList<IResultSetValue>,T> resultSelector)
3939
{
40-
using (var insertOrReplaceStmt = This.PrepareInsertOrReplaceStatement<TInserted>())
41-
using (var findStmt = This.PrepareFindByRowId(TableMapping.Create<TInserted>().TableName))
40+
using (var insertOrReplaceStmt = This.PrepareInsertOrReplaceStatement<T>())
41+
using (var findStmt = This.PrepareFindByRowId(TableMapping.Get<T>().TableName))
4242
{
4343
foreach (var obj in objects)
4444
{
4545
insertOrReplaceStmt.ExecuteWithProperties(obj);
4646
var rowId = This.LastInsertedRowId;
47-
yield return new KeyValuePair<TInserted, TResult>(obj, findStmt.Query(rowId).Select(resultSelector).First());
47+
yield return new KeyValuePair<T,T>(obj, findStmt.Query(rowId).Select(resultSelector).First());
4848
}
4949
}
5050
}
@@ -57,16 +57,16 @@ private static IEnumerable<KeyValuePair<TInserted, TResult>> YieldInsertOrReplac
5757
/// <param name="tableMapping">The table mapping.</param>
5858
/// <param name="obj">The object to insert.</param>
5959
/// <typeparam name="T">The mapped type.</typeparam>
60-
public static TResult InsertOrReplace<TInserted, TResult>(
60+
public static T InsertOrReplace<T>(
6161
this IDatabaseConnection This,
62-
TInserted obj,
63-
Func<IReadOnlyList<IResultSetValue>,TResult> resultSelector)
62+
T obj,
63+
Func<IReadOnlyList<IResultSetValue>,T> resultSelector)
6464
{
6565
Contract.Requires(This != null);
6666
Contract.Requires(obj != null);
6767
Contract.Requires(resultSelector != null);
6868

69-
return This.YieldInsertOrReplaceAll(new TInserted[] {obj}, resultSelector).First().Value;
69+
return This.YieldInsertOrReplaceAll(new T[] {obj}, resultSelector).First().Value;
7070
}
7171

7272
/// <summary>
@@ -77,10 +77,10 @@ public static TResult InsertOrReplace<TInserted, TResult>(
7777
/// <param name="tableMapping">The table mapping.</param>
7878
/// <param name="objects">The objects to be inserted into the database.</param>
7979
/// <typeparam name="T">The mapped type.</typeparam>
80-
public static IReadOnlyDictionary<TInserted,TResult> InsertOrReplaceAll<TInserted, TResult>(
80+
public static IReadOnlyDictionary<T,T> InsertOrReplaceAll<T>(
8181
this IDatabaseConnection This,
82-
IEnumerable<TInserted> objects,
83-
Func<IReadOnlyList<IResultSetValue>,TResult> resultSelector)
82+
IEnumerable<T> objects,
83+
Func<IReadOnlyList<IResultSetValue>,T> resultSelector)
8484
{
8585
Contract.Requires(This != null);
8686
Contract.Requires(objects != null);
@@ -103,10 +103,10 @@ public static partial class AsyncDatabaseConnection
103103
/// <param name="objects">The objects to be inserted into the database.</param>
104104
/// <param name="ct">A cancellation token that can be used to cancel the operation</param>
105105
/// <typeparam name="T">The mapped type.</typeparam>
106-
public static Task<IReadOnlyDictionary<TInserted, TResult>> InsertOrReplaceAllAsync<TInserted, TResult>(
106+
public static Task<IReadOnlyDictionary<T,T>> InsertOrReplaceAllAsync<T>(
107107
this IAsyncDatabaseConnection This,
108-
IEnumerable<TInserted> objects,
109-
Func<IReadOnlyList<IResultSetValue>,TResult> resultSelector,
108+
IEnumerable<T> objects,
109+
Func<IReadOnlyList<IResultSetValue>,T> resultSelector,
110110
CancellationToken ct)
111111
{
112112
Contract.Requires(This != null);
@@ -124,10 +124,10 @@ public static Task<IReadOnlyDictionary<TInserted, TResult>> InsertOrReplaceAllAs
124124
/// <param name="tableMapping">The table mapping.</param>
125125
/// <param name="objects">The objects to be inserted into the database.</param>
126126
/// <typeparam name="T">The mapped type.</typeparam>
127-
public static Task<IReadOnlyDictionary<TInserted, TResult>> InsertOrReplaceAllAsync<TInserted, TResult>(
127+
public static Task<IReadOnlyDictionary<T,T>> InsertOrReplaceAllAsync<T>(
128128
this IAsyncDatabaseConnection This,
129-
IEnumerable<TInserted> objects,
130-
Func<IReadOnlyList<IResultSetValue>,TResult> resultSelector)
129+
IEnumerable<T> objects,
130+
Func<IReadOnlyList<IResultSetValue>,T> resultSelector)
131131
{
132132
Contract.Requires(This != null);
133133
Contract.Requires(objects != null);

SQLitePCL.pretty.Orm/DatabaseConnection.Tables.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static void InitTable<T>(this IDatabaseConnection This)
3838

3939
This.RunInTransaction(_ =>
4040
{
41-
var tableMapping = TableMapping.Create<T>();
41+
var tableMapping = TableMapping.Get<T>();
4242
This.CreateTableIfNotExists(tableMapping.TableName, CreateFlags.None, tableMapping.Columns);
4343

4444
if (This.Changes != 0)

SQLitePCL.pretty.Orm/ForeignKeyConstraint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace SQLitePCL.pretty.Orm
55
/// <summary>
66
/// Foreign key constraint table, column name tuple.
77
/// </summary>
8-
public sealed class ForeignKeyConstraint : IEquatable<ForeignKeyConstraint>
8+
internal sealed class ForeignKeyConstraint : IEquatable<ForeignKeyConstraint>
99
{
1010
/// <summary>
1111
/// Indicates whether the two ForeignKeyConstraint instances are equal to each other.

SQLitePCL.pretty.Orm/IndexInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace SQLitePCL.pretty.Orm
77
/// <summary>
88
/// Details about a Table index.
99
/// </summary>
10-
public sealed class IndexInfo : IEquatable<IndexInfo>
10+
internal sealed class IndexInfo : IEquatable<IndexInfo>
1111
{
1212
/// <summary>
1313
/// Indicates whether the two IndexInfo instances are equal to each other.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66

77
namespace SQLitePCL.pretty.Orm
88
{
9-
public static partial class Orm
9+
public static partial class ResultSet
1010
{
11-
public static Func<IReadOnlyList<IResultSetValue>,T> ResultSetRowToObject<T>()
11+
public static Func<IReadOnlyList<IResultSetValue>,T> RowToObject<T>()
1212
{
1313
Func<T> builder = () => Activator.CreateInstance<T>();
1414
Func<T, T> build = obj => obj;
1515

16-
return ResultSetRowToObject(builder, build);
16+
return RowToObject(builder, build);
1717
}
1818

19-
public static Func<IReadOnlyList<IResultSetValue>,T> ResultSetRowToObject<TBuilder, T>(Func<TBuilder> builder, Func<TBuilder,T> build)
19+
public static Func<IReadOnlyList<IResultSetValue>,T> RowToObject<TBuilder, T>(Func<TBuilder> builder, Func<TBuilder,T> build)
2020
{
2121
Contract.Requires(builder != null);
2222
Contract.Requires(build != null);

SQLitePCL.pretty.Orm/SQLitePCL.pretty.Orm.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@
149149
<Compile Include="SqlQuery.OrderBy.cs" />
150150
<Compile Include="SqlQuery.Where.cs" />
151151
<Compile Include="Statement.cs" />
152-
<Compile Include="Orm.cs" />
153152
<Compile Include="DatabaseConnection.Delete.cs" />
154153
<Compile Include="DatabaseConnection.Find.cs" />
155154
<Compile Include="DatabaseConnection.InsertOrReplace.cs" />
156155
<Compile Include="SqlQuery.From.cs" />
156+
<Compile Include="ResultSet.cs" />
157157
</ItemGroup>
158158
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
159159
<ItemGroup>

0 commit comments

Comments
 (0)