Skip to content

Commit ab7fd69

Browse files
committed
Various improvements
1 parent a0ab237 commit ab7fd69

2 files changed

Lines changed: 37 additions & 103 deletions

File tree

SQLiteSharp/SQLite.cs

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class NotNullConstraintViolationException : SQLiteException {
2020
public NotNullConstraintViolationException(SQLite3.Result result, string message, TableMapping? mapping, object? obj)
2121
: base(result, message) {
2222
if (mapping is not null && obj is not null) {
23-
Columns = mapping.Columns.Where(column => !column.IsNullable && column.GetValue(obj) is null);
23+
Columns = mapping.Columns.Where(column => column.NotNull && column.GetValue(obj) is null);
2424
}
2525
}
2626
public NotNullConstraintViolationException(SQLite3.Result result, string message)
@@ -46,7 +46,7 @@ public enum SQLiteOpenFlags {
4646
[Flags]
4747
public enum CreateFlags {
4848
/// <summary>
49-
/// Use the default creation options
49+
/// Use the default creation options.
5050
/// </summary>
5151
None = 0x000,
5252
/// <summary>
@@ -59,22 +59,21 @@ public enum CreateFlags {
5959
/// </summary>
6060
ImplicitIndex = 0x002,
6161
/// <summary>
62-
/// Create a primary key for a property called 'Id' and
63-
/// create an indices for properties ending in 'Id' (case-insensitive).
62+
/// Create a primary key for a property called 'Id' and create indices for properties ending in 'Id' (case-insensitive).
6463
/// </summary>
65-
AllImplicit = 0x003,
64+
AllImplicit = ImplicitPrimaryKey | ImplicitIndex,
6665
/// <summary>
6766
/// Force the primary key property to be auto incrementing.
68-
/// This avoids the need for the [AutoIncrement] attribute.
69-
/// The primary key property on the class should have type int or long.
67+
/// This avoids the need for [<see cref="AutoIncrementAttribute"/>].
68+
/// The primary key property type should be <see cref="int"/> or <see cref="long"/>.
7069
/// </summary>
7170
AutoIncrementPrimaryKey = 0x004,
7271
/// <summary>
73-
/// Create virtual table using FTS3
72+
/// Create a virtual table using FTS3.
7473
/// </summary>
7574
FullTextSearch3 = 0x100,
7675
/// <summary>
77-
/// Create virtual table using FTS4
76+
/// Create a virtual table using FTS4.
7877
/// </summary>
7978
FullTextSearch4 = 0x200
8079
}
@@ -105,7 +104,7 @@ public interface ISQLiteConnection : IDisposable {
105104
int CreateIndex<T>(Expression<Func<T, object>> property, bool unique = false);
106105
CreateTableResult CreateTable<T>(CreateFlags createFlags = CreateFlags.None);
107106
CreateTableResult CreateTable(Type type, CreateFlags createFlags = CreateFlags.None);
108-
CreateTablesResult CreateTables(CreateFlags createFlags = CreateFlags.None, params IEnumerable<Type> types);
107+
CreateTablesResult CreateTables(IEnumerable<Type> types, CreateFlags createFlags = CreateFlags.None);
109108
IEnumerable<T> DeferredQuery<T>(string query, params IEnumerable<object?> parameters) where T : new();
110109
IEnumerable<object> DeferredQuery(TableMapping map, string query, params IEnumerable<object?> parameters);
111110
int Delete(object objectToDelete);
@@ -530,7 +529,7 @@ public CreateTableResult CreateTable(Type type, CreateFlags createFlags = Create
530529
/// <returns>
531530
/// Whether the table was created or migrated for each type.
532531
/// </returns>
533-
public CreateTablesResult CreateTables(CreateFlags createFlags = CreateFlags.None, params IEnumerable<Type> types) {
532+
public CreateTablesResult CreateTables(IEnumerable<Type> types, CreateFlags createFlags = CreateFlags.None) {
534533
CreateTablesResult result = new();
535534
foreach (Type type in types) {
536535
CreateTableResult oneResult = CreateTable(type, createFlags);
@@ -1261,7 +1260,7 @@ public int Insert(object obj, string? modifier = null) {
12611260

12621261
TableMapping map = GetMapping(obj.GetType());
12631262

1264-
if (map.PrimaryKey is not null && map.PrimaryKey.IsAutoGuid) {
1263+
if (map.PrimaryKey is not null && map.PrimaryKey.AutoGuid) {
12651264
if (Equals(map.PrimaryKey.GetValue(obj), Guid.Empty)) {
12661265
map.PrimaryKey.SetValue(obj, Guid.NewGuid());
12671266
}
@@ -1554,22 +1553,6 @@ public int Delete(object primaryKey, TableMapping map) {
15541553
return count;
15551554
}
15561555

1557-
/// <summary>
1558-
/// Deletes all the objects from the specified table.
1559-
/// WARNING WARNING: Let me repeat. It deletes ALL the objects from the
1560-
/// specified table. Do you really want to do that?
1561-
/// </summary>
1562-
/// <returns>
1563-
/// The number of objects deleted.
1564-
/// </returns>
1565-
/// <typeparam name='T'>
1566-
/// The type of objects to delete.
1567-
/// </typeparam>
1568-
public int DeleteAll<T>() {
1569-
TableMapping map = GetMapping<T>();
1570-
return DeleteAll(map);
1571-
}
1572-
15731556
/// <summary>
15741557
/// Deletes all the objects from the specified table.<br/>
15751558
/// WARNING: To be clear, it deletes ALL the objects from the specified table. Do you really want that?
@@ -1587,6 +1570,10 @@ public int DeleteAll(TableMapping map) {
15871570
OnTableChanged(map, NotifyTableChangedAction.Delete);
15881571
return count;
15891572
}
1573+
/// <inheritdoc cref="DeleteAll(TableMapping)"/>
1574+
public int DeleteAll<T>() {
1575+
return DeleteAll(GetMapping<T>());
1576+
}
15901577

15911578
/// <summary>
15921579
/// Backup the entire database to the specified path.
@@ -1850,10 +1837,10 @@ public TableMapping(Type type, CreateFlags createFlags = CreateFlags.None) {
18501837
}
18511838
Columns = [.. columns];
18521839
foreach (Column column in Columns) {
1853-
if (column.IsAutoIncrement && column.IsPrimaryKey) {
1840+
if (column.AutoIncrement && column.PrimaryKey) {
18541841
_autoIncrementedPrimaryKey = column;
18551842
}
1856-
if (column.IsPrimaryKey) {
1843+
if (column.PrimaryKey) {
18571844
PrimaryKey = column;
18581845
}
18591846
}
@@ -1888,13 +1875,13 @@ public class Column {
18881875
public string PropertyName { get => _memberInfo.Name; }
18891876
public Type ColumnType { get; }
18901877
public string Collation { get; }
1891-
public bool IsAutoIncrement { get; }
1892-
public bool IsAutoGuid { get; }
1893-
public bool IsPrimaryKey { get; }
1894-
public IEnumerable<IndexedAttribute> Indices { get; }
1895-
public bool IsNullable { get; }
1878+
public bool AutoIncrement { get; }
1879+
public bool AutoGuid { get; }
1880+
public bool PrimaryKey { get; }
1881+
public bool NotNull { get; }
18961882
public int? MaxStringLength { get; }
18971883
public bool StoreAsText { get; }
1884+
public IEnumerable<IndexedAttribute> Indices { get; }
18981885

18991886
private readonly MemberInfo _memberInfo;
19001887

@@ -1908,18 +1895,18 @@ public Column(MemberInfo member, CreateFlags createFlags = CreateFlags.None) {
19081895
ColumnType = Nullable.GetUnderlyingType(memberType) ?? memberType;
19091896
Collation = Orm.GetCollation(member);
19101897

1911-
IsPrimaryKey = Orm.IsPrimaryKey(member)
1898+
PrimaryKey = Orm.IsPrimaryKey(member)
19121899
|| (createFlags.HasFlag(CreateFlags.ImplicitPrimaryKey) && string.Equals(member.Name, Orm.ImplicitPrimaryKeyName, StringComparison.OrdinalIgnoreCase));
19131900

1914-
bool isAutoIncrement = Orm.IsAutoIncrement(member) || (IsPrimaryKey && ((createFlags & CreateFlags.AutoIncrementPrimaryKey) == CreateFlags.AutoIncrementPrimaryKey));
1915-
IsAutoGuid = isAutoIncrement && ColumnType == typeof(Guid);
1916-
IsAutoIncrement = isAutoIncrement && !IsAutoGuid;
1901+
bool isAutoIncrement = Orm.IsAutoIncrement(member) || (PrimaryKey && ((createFlags & CreateFlags.AutoIncrementPrimaryKey) == CreateFlags.AutoIncrementPrimaryKey));
1902+
AutoGuid = isAutoIncrement && ColumnType == typeof(Guid);
1903+
AutoIncrement = isAutoIncrement && !AutoGuid;
19171904

19181905
Indices = Orm.GetIndices(member);
1919-
if (!Indices.Any() && !IsPrimaryKey && createFlags.HasFlag(CreateFlags.ImplicitIndex) && Name.EndsWith(Orm.ImplicitIndexSuffix, StringComparison.OrdinalIgnoreCase)) {
1906+
if (!Indices.Any() && !PrimaryKey && createFlags.HasFlag(CreateFlags.ImplicitIndex) && Name.EndsWith(Orm.ImplicitIndexSuffix, StringComparison.OrdinalIgnoreCase)) {
19201907
Indices = [new IndexedAttribute()];
19211908
}
1922-
IsNullable = !(IsPrimaryKey || Orm.IsMarkedNotNull(member));
1909+
NotNull = PrimaryKey || Orm.IsMarkedNotNull(member);
19231910
MaxStringLength = Orm.MaxStringLength(member);
19241911

19251912
StoreAsText = memberType.GetCustomAttribute<StoreAsTextAttribute>() is not null;
@@ -1979,13 +1966,13 @@ public static class Orm {
19791966
public static string SqlDecl(TableMapping.Column column) {
19801967
string decl = $"\"{column.Name}\" {SqlType(column)} ";
19811968

1982-
if (column.IsPrimaryKey) {
1969+
if (column.PrimaryKey) {
19831970
decl += "primary key ";
19841971
}
1985-
if (column.IsAutoIncrement) {
1972+
if (column.AutoIncrement) {
19861973
decl += "autoincrement ";
19871974
}
1988-
if (!column.IsNullable) {
1975+
if (column.NotNull) {
19891976
decl += "not null ";
19901977
}
19911978
if (!string.IsNullOrEmpty(column.Collation)) {
@@ -2720,8 +2707,8 @@ public int Delete(Expression<Func<T, bool>>? predicateExpression) {
27202707
string commandText = $"delete from \"{Table.TableName}\" where {CompileExpression(predicate!, parameters).CommandText}";
27212708
SQLiteCommand command = Connection.CreateCommand(commandText, parameters);
27222709

2723-
int result = command.ExecuteNonQuery();
2724-
return result;
2710+
int rowCount = command.ExecuteNonQuery();
2711+
return rowCount;
27252712
}
27262713

27272714
/// <summary>

SQLiteSharp/SQLiteAsync.cs

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public interface ISQLiteAsyncConnection {
2020
Task<int> CreateIndexAsync(string indexName, string tableName, string[] columnNames, bool unique = false);
2121
Task<int> CreateIndexAsync<T>(Expression<Func<T, object>> property, bool unique = false);
2222
Task<CreateTableResult> CreateTableAsync<T>(CreateFlags createFlags = CreateFlags.None) where T : new();
23-
Task<CreateTableResult> CreateTableAsync(Type ty, CreateFlags createFlags = CreateFlags.None);
24-
Task<CreateTablesResult> CreateTablesAsync(CreateFlags createFlags = CreateFlags.None, params Type[] types);
23+
Task<CreateTableResult> CreateTableAsync(Type type, CreateFlags createFlags = CreateFlags.None);
24+
Task<CreateTablesResult> CreateTablesAsync(IEnumerable<Type> types, CreateFlags createFlags = CreateFlags.None);
2525
Task<IEnumerable<T>> DeferredQueryAsync<T>(string query, params IEnumerable<object?> parameters) where T : new();
2626
Task<IEnumerable<object>> DeferredQueryAsync(TableMapping map, string query, params IEnumerable<object?> parameters);
2727
Task<int> DeleteAllAsync<T>();
@@ -241,8 +241,8 @@ public Task<CreateTableResult> CreateTableAsync(Type type, CreateFlags createFla
241241
/// <returns>
242242
/// Whether the table was created or migrated for each type.
243243
/// </returns>
244-
public Task<CreateTablesResult> CreateTablesAsync(CreateFlags createFlags = CreateFlags.None, params Type[] types) {
245-
return LockAsync(connection => connection.CreateTables(createFlags, types));
244+
public Task<CreateTablesResult> CreateTablesAsync(IEnumerable<Type> types, CreateFlags createFlags = CreateFlags.None) {
245+
return LockAsync(connection => connection.CreateTables(types, createFlags));
246246
}
247247
/// <summary>
248248
/// Executes a "drop table" on the database. This is non-recoverable.
@@ -680,59 +680,6 @@ public Task<int> ExecuteAsync(string query, params IEnumerable<object?> paramete
680680
return LockAsync(connection => connection.Execute(query, parameters));
681681
}
682682

683-
/// <summary>
684-
/// Inserts all specified objects.
685-
/// </summary>
686-
/// <param name="objects">
687-
/// An <see cref="IEnumerable"/> of the objects to insert.
688-
/// <param name="runInTransaction"/>
689-
/// A boolean indicating if the inserts should be wrapped in a transaction.
690-
/// </param>
691-
/// <returns>
692-
/// The number of rows added to the table.
693-
/// </returns>
694-
public Task<int> InsertAllAsync(IEnumerable objects, bool runInTransaction = true) {
695-
return LockAsync(connection => connection.InsertAll(objects, runInTransaction));
696-
}
697-
698-
/// <summary>
699-
/// Inserts all specified objects.
700-
/// </summary>
701-
/// <param name="objects">
702-
/// An <see cref="IEnumerable"/> of the objects to insert.
703-
/// </param>
704-
/// <param name="extra">
705-
/// Literal SQL code that gets placed into the command. INSERT {extra} INTO ...
706-
/// </param>
707-
/// <param name="runInTransaction">
708-
/// A boolean indicating if the inserts should be wrapped in a transaction.
709-
/// </param>
710-
/// <returns>
711-
/// The number of rows added to the table.
712-
/// </returns>
713-
public Task<int> InsertAllAsync(IEnumerable objects, string modifier, bool runInTransaction = true) {
714-
return LockAsync(connection => connection.InsertAll(objects, modifier, runInTransaction));
715-
}
716-
717-
/// <summary>
718-
/// Inserts all specified objects.
719-
/// </summary>
720-
/// <param name="objects">
721-
/// An <see cref="IEnumerable"/> of the objects to insert.
722-
/// </param>
723-
/// <param name="objType">
724-
/// The type of object to insert.
725-
/// </param>
726-
/// <param name="runInTransaction">
727-
/// A boolean indicating if the inserts should be wrapped in a transaction.
728-
/// </param>
729-
/// <returns>
730-
/// The number of rows added to the table.
731-
/// </returns>
732-
public Task<int> InsertAllAsync(IEnumerable objects, Type objType, bool runInTransaction = true) {
733-
return LockAsync(connection => connection.InsertAll(objects, objType, runInTransaction));
734-
}
735-
736683
/// <summary>
737684
/// Executes <paramref name="action"/> within a (possibly nested) transaction by wrapping it in a SAVEPOINT. If an
738685
/// exception occurs the whole transaction is rolled back, not just the current savepoint. The exception

0 commit comments

Comments
 (0)