Skip to content

Commit 967515d

Browse files
committed
Support for non-autoincrement pk when the pk is non-nullable. Add more tests. More API clean up.
1 parent 8f55357 commit 967515d

8 files changed

Lines changed: 198 additions & 199 deletions

File tree

SQLitePCL.pretty.Orm/SQLBuilder.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,18 @@ internal static string CreateTableIfNotExists(string tableName, CreateFlags crea
9797
return query;
9898
}
9999

100-
internal static string SqlDecl (string columnName, ColumnMapping columnMapping)
100+
internal static string SqlDecl(string columnName, ColumnMapping columnMapping)
101101
{
102102
string decl = "\"" + columnName + "\" " + columnMapping.Metadata.DeclaredType + " ";
103103

104-
// Only specify a column as primary key if it is autoincrement
105-
if (columnMapping.Metadata.IsPrimaryKeyPart && columnMapping.Metadata.IsAutoIncrement)
104+
if (columnMapping.Metadata.IsPrimaryKeyPart && columnMapping.Metadata.IsAutoIncrement)
106105
{
107106
decl += "PRIMARY KEY AUTOINCREMENT NOT NULL ";
108107
}
108+
else if (columnMapping.Metadata.IsPrimaryKeyPart)
109+
{
110+
decl += "PRIMARY KEY NOT NULL ";
111+
}
109112
else if (columnMapping.Metadata.HasNotNullConstraint)
110113
{
111114
decl += "NOT NULL DEFAULT \"" + columnMapping.DefaultValue.ToString() + "\" ";

SQLitePCL.pretty.Orm/TableMapping.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ internal static void Bind<T>(this IStatement This, ITableMapping<T> tableMapping
4949
}
5050
}
5151

52-
public static TableQuery<T> CreateQuery<T>(this ITableMapping<T> This)
52+
public static TableQuery<T> Query<T>(this ITableMapping<T> This)
5353
{
5454
return new TableQuery<T>(This, "*", null, new List<Ordering>(), null, null);
5555
}
@@ -218,13 +218,20 @@ private static TableColumnMetadata CreateColumnMetadata(this PropertyInfo This)
218218
var isAutoInc = false;
219219
if (isPK)
220220
{
221-
if (columnType != typeof(Nullable<long>))
221+
// Only use autoincrement if the primary key is nullable otherwise.
222+
if (columnType == typeof(Nullable<long>))
223+
{
224+
isAutoInc = true;
225+
226+
}
227+
else if (columnType == typeof(long))
228+
{
229+
isAutoInc = false;
230+
}
231+
else
222232
{
223233
throw new ArgumentException("Primary key value must be of type Nullable<long>.");
224234
}
225-
226-
// Only allow autoincrement on nullable long primary keys
227-
isAutoInc = true;
228235
}
229236

230237
// Though technically not required to be not null by SQLite, we enforce that the primary key is always not null

SQLitePCL.pretty.Orm/TableQuery.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public override string ToString()
106106
/// Converts the query to a count query.
107107
/// </summary>
108108
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
109-
public TableQuery<T> ToCountQuery()
109+
public TableQuery<T> Count()
110110
{
111111
return new TableQuery<T>(_mapping, "count(*)", _where, _orderBy, _limit, _offset);
112112
}
@@ -446,8 +446,6 @@ private static string CompileNullBinaryExpression(BinaryExpression expression, s
446446
else { throw new NotSupportedException("Cannot compile Null-BinaryExpression with type " + expression.NodeType.ToString()); }
447447
}
448448

449-
450-
451449
private static string GetSqlName (Expression expr)
452450
{
453451
var n = expr.NodeType;
@@ -471,7 +469,7 @@ private static string GetSqlName (Expression expr)
471469
/// </summary>
472470
public static class TableQuery
473471
{
474-
public static ITableMappedStatement<T> PrepareQuery<T>(this IDatabaseConnection This, TableQuery<T> query)
472+
public static ITableMappedStatement<T> PrepareStatement<T>(this IDatabaseConnection This, TableQuery<T> query)
475473
{
476474
return new TableMappedStatement<T>(This.PrepareStatement(query.ToString()), query.Mapping);
477475
}
@@ -491,19 +489,19 @@ public static IObservable<T> Query<T>(this IAsyncDatabaseConnection This, TableQ
491489
return This.Query(query.ToString()).Select(query.Mapping.ToObject);
492490
}
493491

494-
public static IStatement PrepareCount<T>(this IDatabaseConnection This, TableQuery<T> query)
492+
public static IStatement PrepareCountStatement<T>(this IDatabaseConnection This, TableQuery<T> query)
495493
{
496-
return This.PrepareStatement(query.ToCountQuery().ToString());
494+
return This.PrepareStatement(query.Count().ToString());
497495
}
498496

499-
public static Task<IAsyncStatement> PrepareCountAsync<T>(this IAsyncDatabaseConnection This, TableQuery<T> query)
497+
public static Task<IAsyncStatement> PrepareCountStatemenAsync<T>(this IAsyncDatabaseConnection This, TableQuery<T> query)
500498
{
501-
return This.PrepareStatementAsync(query.ToCountQuery().ToString());
499+
return This.PrepareStatementAsync(query.Count().ToString());
502500
}
503501

504502
public static int Count<T>(this IDatabaseConnection This, TableQuery<T> query)
505503
{
506-
return This.Query(query.ToCountQuery().ToString()).SelectScalarInt().First();
504+
return This.Query(query.Count().ToString()).SelectScalarInt().First();
507505
}
508506

509507
public static Task<int> CountAsync<T>(this IAsyncDatabaseConnection This, TableQuery<T> query)
@@ -513,12 +511,12 @@ public static Task<int> CountAsync<T>(this IAsyncDatabaseConnection This, TableQ
513511

514512
public static Task<int> CountAsync<T>(this IAsyncDatabaseConnection This, TableQuery<T> query, CancellationToken ct)
515513
{
516-
return This.Query(query.ToCountQuery().ToString()).SelectScalarInt().FirstAsync().ToTask(ct);
514+
return This.Query(query.Count().ToString()).SelectScalarInt().FirstAsync().ToTask(ct);
517515
}
518516

519517
public static int Count<T>(this IDatabaseConnection This, TableQuery<T> query, params object[] values)
520518
{
521-
return This.Query(query.ToCountQuery().ToString(), values).SelectScalarInt().First();
519+
return This.Query(query.Count().ToString(), values).SelectScalarInt().First();
522520
}
523521

524522
public static Task<int> CountAsync<T>(this IAsyncDatabaseConnection This, TableQuery<T> query, params object[] values)
@@ -528,7 +526,7 @@ public static Task<int> CountAsync<T>(this IAsyncDatabaseConnection This, TableQ
528526

529527
public static Task<int> CountAsync<T>(this IAsyncDatabaseConnection This, TableQuery<T> query, CancellationToken ct, params object[] values)
530528
{
531-
return This.Query(query.ToCountQuery().ToString(), values).SelectScalarInt().FirstAsync().ToTask(ct);
529+
return This.Query(query.Count().ToString(), values).SelectScalarInt().FirstAsync().ToTask(ct);
532530
}
533531
}
534532
}

SQLitePCL.pretty.tests/Orm/BooleanTests.cs

Lines changed: 0 additions & 71 deletions
This file was deleted.

SQLitePCL.pretty.tests/Orm/CollateTest.cs

Lines changed: 0 additions & 108 deletions
This file was deleted.

SQLitePCL.pretty.tests/OrmTests/TableMappingTests.Create.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,27 @@ public void TestCreateWithExplicitTableName()
143143
Assert.AreEqual(tableWithExplicitName.TableName, "ExplicitTableName");
144144
}
145145

146+
public class TestObjectWithNonAutoIncrementPrimaryKey
147+
{
148+
[PrimaryKey]
149+
public long Id { get; set; }
150+
}
151+
152+
[Test]
153+
public void TestCreateWithNonAutoIncrementPrimaryKey()
154+
{
155+
var table = TableMapping.Create<TestObjectWithNonAutoIncrementPrimaryKey>();
156+
157+
var id = table.Columns["Id"];
158+
Assert.AreEqual(id.ClrType, typeof(long));
159+
// No way to test the PropertyInfo directly
160+
Assert.AreEqual(id.Metadata.CollationSequence, "BINARY");
161+
Assert.AreEqual(id.Metadata.DeclaredType, "INTEGER");
162+
Assert.IsTrue(id.Metadata.HasNotNullConstraint);
163+
Assert.IsFalse(id.Metadata.IsAutoIncrement);
164+
Assert.IsTrue(id.Metadata.IsPrimaryKeyPart);
165+
}
166+
146167
public class TestObjectWithUnsupportedPropertyType
147168
{
148169
[PrimaryKey]

0 commit comments

Comments
 (0)