Skip to content

Commit a82b72e

Browse files
committed
More refactoring + custom type serializers
1 parent 246698b commit a82b72e

14 files changed

+858
-736
lines changed

SQLiteSharp/Attributes.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,4 @@ public class CollationAttribute(string collation) : Attribute {
6868
/// </summary>
6969
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
7070
public class NotNullAttribute : Attribute {
71-
}
72-
73-
/// <summary>
74-
/// Store the enum by its string name rather than its integer value.
75-
/// </summary>
76-
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)]
77-
public class StoreByNameAttribute : Attribute {
7871
}

SQLiteSharp/ColumnMap.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System.Reflection;
2+
3+
namespace SQLiteSharp;
4+
5+
public class ColumnMap {
6+
public Orm Orm { get; }
7+
public string Name { get; }
8+
public MemberInfo ClrMember { get; }
9+
public Type ClrType { get; }
10+
public string Collation { get; }
11+
public bool AutoIncrement { get; }
12+
public bool PrimaryKey { get; }
13+
public bool NotNull { get; }
14+
public int? MaxStringLength { get; }
15+
public IndexedAttribute[] Indexes { get; }
16+
17+
public ColumnMap(MemberInfo member, Orm? orm = null) {
18+
Orm = orm ?? Orm.Default;
19+
20+
ClrMember = member;
21+
ClrType = GetMemberType(member);
22+
23+
Name = member.GetCustomAttribute<ColumnAttribute>()?.Name ?? member.Name;
24+
25+
Collation = Orm.GetCollation(member);
26+
27+
PrimaryKey = Orm.IsPrimaryKey(member) || Orm.IsImplicitPrimaryKey(member);
28+
29+
AutoIncrement = Orm.IsAutoIncrement(member) || (PrimaryKey && Orm.IsImplicitAutoIncrementedPrimaryKey(member));
30+
31+
Indexes = Orm.GetIndexes(member).ToArray();
32+
if (Indexes.Length == 0 && !PrimaryKey && Orm.IsImplicitIndex(member)) {
33+
Indexes = [new IndexedAttribute()];
34+
}
35+
36+
NotNull = PrimaryKey || Orm.IsNotNullConstrained(member);
37+
MaxStringLength = Orm.GetMaxStringLength(member);
38+
}
39+
40+
public void SetValue(object obj, object? value) {
41+
switch (ClrMember) {
42+
case PropertyInfo propertyInfo:
43+
propertyInfo.SetValue(obj, value);
44+
break;
45+
case FieldInfo fieldInfo:
46+
fieldInfo.SetValue(obj, value);
47+
break;
48+
default:
49+
throw new InvalidProgramException();
50+
}
51+
}
52+
public object? GetValue(object obj) {
53+
return ClrMember switch {
54+
PropertyInfo propertyInfo => propertyInfo.GetValue(obj),
55+
FieldInfo fieldInfo => fieldInfo.GetValue(obj),
56+
_ => throw new InvalidProgramException(),
57+
};
58+
}
59+
private static Type GetMemberType(MemberInfo memberInfo) {
60+
return memberInfo switch {
61+
PropertyInfo propertyInfo => propertyInfo.PropertyType,
62+
FieldInfo fieldInfo => fieldInfo.FieldType,
63+
_ => throw new InvalidProgramException($"{nameof(ColumnMap)} only supports properties and fields."),
64+
};
65+
}
66+
}

SQLiteSharp/Enums.cs

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,5 @@
11
namespace SQLiteSharp;
22

3-
[Flags]
4-
public enum CreateFlags {
5-
/// <summary>
6-
/// Use the default creation options.
7-
/// </summary>
8-
None = 1,
9-
/// <summary>
10-
/// Create a primary key index for a property called 'Id' (case-sensitive).<br/>
11-
/// This avoids the need for the [<see cref="PrimaryKeyAttribute"/>].
12-
/// </summary>
13-
ImplicitPrimaryKey = 2,
14-
/// <summary>
15-
/// Create indexes for properties ending in 'Id' (case-sensitive).
16-
/// </summary>
17-
ImplicitIndex = 4,
18-
/// <summary>
19-
/// Force the primary key property to be auto incrementing.<br/>
20-
/// This avoids the need for [<see cref="AutoIncrementAttribute"/>].<br/>
21-
/// The primary key must be an integer.
22-
/// </summary>
23-
AutoIncrementPrimaryKey = 8,
24-
/// <summary>
25-
/// Create a virtual table using <see href="https://www.sqlite.org/fts3.html">FTS3</see>.
26-
/// </summary>
27-
FullTextSearch3 = 16,
28-
/// <summary>
29-
/// Create a virtual table using <see href="https://www.sqlite.org/fts3.html">FTS4</see>.
30-
/// </summary>
31-
FullTextSearch4 = 32,
32-
/// <summary>
33-
/// Create a virtual table using <see href="https://www.sqlite.org/fts5.html">FTS5</see>.
34-
/// </summary>
35-
FullTextSearch5 = 64,
36-
}
37-
38-
/// <summary>
39-
/// Flags used when accessing a SQLite database, as defined in <see href="https://www.sqlite.org/c3ref/c_open_autoproxy.html">Flags For File Open Operations</see>.
40-
/// </summary>
41-
[Flags]
42-
public enum OpenFlags {
43-
ReadOnly = 0x00000001,
44-
ReadWrite = 0x00000002,
45-
Create = 0x00000004,
46-
DeleteOnClose = 0x00000008,
47-
Exclusive = 0x00000010,
48-
AutoProxy = 0x00000020,
49-
Uri = 0x00000040,
50-
Memory = 0x00000080,
51-
MainDb = 0x00000100,
52-
TempDb = 0x00000200,
53-
TransientDb = 0x00000400,
54-
MainJournal = 0x00000800,
55-
TempJournal = 0x00001000,
56-
SubJournal = 0x00002000,
57-
SuperJournal = 0x00004000,
58-
NoMutex = 0x00008000,
59-
FullMutex = 0x00010000,
60-
SharedCache = 0x00020000,
61-
PrivateCache = 0x00040000,
62-
Wal = 0x00080000,
63-
NoFollow = 0x01000000,
64-
ExresCode = 0x02000000,
65-
66-
Recommended = Create | ReadWrite | FullMutex | Wal,
67-
}
68-
693
public enum CreateTableResult {
704
Created,
715
Migrated,

SQLiteSharp/Globals.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace SQLiteSharp;
1212
public static class Globals {
1313
/// <summary>
1414
/// Convert an input string to a quoted SQL string that can be safely used in queries.<br/>
15-
/// For example, <c>red "blue" green</c> becomes <c>"red ""blue"" green"</c>.
15+
/// For example, (<c>red "blue" green</c>) becomes (<c>"red ""blue"" green"</c>).
1616
/// </summary>
1717
public static string Quote(string? unsafeString) {
1818
if (unsafeString is null) {

0 commit comments

Comments
 (0)