Skip to content

Commit 3df9abf

Browse files
committed
Remove unused attribute and refactoring
1 parent 93a06bb commit 3df9abf

File tree

8 files changed

+53
-55
lines changed

8 files changed

+53
-55
lines changed

SQLiteSharp/Attributes.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class TableAttribute(string name) : Attribute {
55
public string Name { get; set; } = name;
66

77
/// <summary>
8-
/// Flag whether to create the table without <c>rowid</c> (see <see href="https://sqlite.org/withoutrowid.html"/>).<br/>
8+
/// Whether to create the table without <c>rowid</c> (see <see href="https://sqlite.org/withoutrowid.html"/>).<br/>
99
/// The default is <see langword="false"/> so that SQLite adds an implicit <c>rowid</c> to every table created.
1010
/// </summary>
1111
public bool WithoutRowId { get; set; }
@@ -38,10 +38,6 @@ public IndexedAttribute(string name, int order) {
3838
}
3939
}
4040

41-
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
42-
public class IgnoreAttribute : Attribute {
43-
}
44-
4541
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
4642
public class UniqueAttribute : IndexedAttribute {
4743
public override bool Unique {
@@ -51,8 +47,7 @@ public override bool Unique {
5147
}
5248

5349
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
54-
public class MaxLengthAttribute(int length) : Attribute {
55-
public int Value { get; } = length;
50+
public class IgnoreAttribute : Attribute {
5651
}
5752

5853
/// <summary>
File renamed without changes.

SQLiteSharp/ColumnMap.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,19 @@ public void SetValue(object obj, object? value) {
4747
throw new InvalidProgramException();
4848
}
4949
}
50+
public void SetSqliteValue(object obj, SqliteValue sqliteValue) {
51+
TypeSerializer typeSerializer = Orm.GetTypeSerializer(ClrType);
52+
object? value = typeSerializer.Deserialize(sqliteValue, ClrType);
53+
SetValue(obj, value);
54+
}
5055
public object? GetValue(object obj) {
5156
return ClrMember switch {
5257
PropertyInfo propertyInfo => propertyInfo.GetValue(obj),
5358
FieldInfo fieldInfo => fieldInfo.GetValue(obj),
5459
_ => throw new InvalidProgramException(),
5560
};
5661
}
62+
5763
private static Type GetMemberType(MemberInfo memberInfo) {
5864
return memberInfo switch {
5965
PropertyInfo propertyInfo => propertyInfo.PropertyType,

SQLiteSharp/Orm.cs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -44,40 +44,6 @@ public TypeSerializer GetTypeSerializer(Type type) {
4444
// Serializer not found
4545
throw new InvalidOperationException($"No {nameof(TypeSerializer)} found for '{type}'");
4646
}
47-
public object? ReadColumn(Sqlite3Statement statement, int index, Type type) {
48-
TypeSerializer typeSerializer = GetTypeSerializer(type);
49-
SqliteValue value = SQLiteRaw.GetColumnValue(statement, index);
50-
return typeSerializer.Deserialize(value, type);
51-
}
52-
public void BindParameter(Sqlite3Statement statement, int index, object? value) {
53-
if (value is null) {
54-
SQLiteRaw.BindNull(statement, index);
55-
return;
56-
}
57-
58-
TypeSerializer typeSerializer = GetTypeSerializer(value.GetType());
59-
SqliteValue rawValue = typeSerializer.Serialize(value);
60-
61-
switch (rawValue.SqliteType) {
62-
case SqliteType.Null:
63-
SQLiteRaw.BindNull(statement, index);
64-
break;
65-
case SqliteType.Integer:
66-
SQLiteRaw.BindInt64(statement, index, rawValue.AsInteger);
67-
break;
68-
case SqliteType.Float:
69-
SQLiteRaw.BindDouble(statement, index, rawValue.AsFloat);
70-
break;
71-
case SqliteType.Text:
72-
SQLiteRaw.BindText(statement, index, rawValue.AsText);
73-
break;
74-
case SqliteType.Blob:
75-
SQLiteRaw.BindBlob(statement, index, rawValue.AsBlob);
76-
break;
77-
default:
78-
throw new NotImplementedException($"Cannot bind column type '{rawValue.SqliteType}'");
79-
}
80-
}
8147
public string GetSqlDeclaration(ColumnMap column) {
8248
TypeSerializer typeSerializer = GetTypeSerializer(column.ClrType);
8349

@@ -110,9 +76,6 @@ public static string GetCollation(MemberInfo memberInfo) {
11076
public static IEnumerable<IndexedAttribute> GetIndexes(MemberInfo memberInfo) {
11177
return memberInfo.GetCustomAttributes<IndexedAttribute>();
11278
}
113-
public static int? GetMaxStringLength(MemberInfo memberInfo) {
114-
return memberInfo.GetCustomAttribute<MaxLengthAttribute>()?.Value;
115-
}
11679
public static Type AsUnderlyingType(Type Type) {
11780
return Nullable.GetUnderlyingType(Type) ?? Type;
11881
}

SQLiteSharp/SQLiteCommand.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public IEnumerable<object> ExecuteQuery(TableMap map) {
4343
continue;
4444
}
4545
// Read value from found column
46-
object? value = Connection.Orm.ReadColumn(statement, i, column.ClrType);
46+
object? value = ReadColumn(statement, i, column.ClrType);
4747
column.SetValue(obj, value);
4848
}
4949
OnInstanceCreated?.Invoke(obj);
@@ -68,7 +68,7 @@ public T ExecuteScalar<T>() {
6868
try {
6969
Result result = SQLiteRaw.Step(statement);
7070
if (result is Result.Row) {
71-
object? columnValue = Connection.Orm.ReadColumn(statement, 0, typeof(T));
71+
object? columnValue = ReadColumn(statement, 0, typeof(T));
7272
if (columnValue is not null) {
7373
Value = (T)columnValue;
7474
}
@@ -92,7 +92,7 @@ public IEnumerable<T> ExecuteQueryScalars<T>() {
9292
throw new InvalidOperationException("QueryScalars should return at least one column");
9393
}
9494
while (SQLiteRaw.Step(statement) is Result.Row) {
95-
object? value = Connection.Orm.ReadColumn(statement, 0, typeof(T));
95+
object? value = ReadColumn(statement, 0, typeof(T));
9696
if (value is null) {
9797
yield return default!;
9898
}
@@ -121,9 +121,43 @@ private void BindParameters(Sqlite3Statement statement) {
121121
int index = name is not null
122122
? SQLiteRaw.BindParameterIndex(statement, name)
123123
: nextIndex++;
124-
Connection.Orm.BindParameter(statement, index, value);
124+
BindParameter(statement, index, value);
125125
}
126126
}
127+
private void BindParameter(Sqlite3Statement statement, int index, object? value) {
128+
if (value is null) {
129+
SQLiteRaw.BindNull(statement, index);
130+
return;
131+
}
132+
133+
TypeSerializer typeSerializer = Connection.Orm.GetTypeSerializer(value.GetType());
134+
SqliteValue rawValue = typeSerializer.Serialize(value);
135+
136+
switch (rawValue.SqliteType) {
137+
case SqliteType.Null:
138+
SQLiteRaw.BindNull(statement, index);
139+
break;
140+
case SqliteType.Integer:
141+
SQLiteRaw.BindInt64(statement, index, rawValue.AsInteger);
142+
break;
143+
case SqliteType.Float:
144+
SQLiteRaw.BindDouble(statement, index, rawValue.AsFloat);
145+
break;
146+
case SqliteType.Text:
147+
SQLiteRaw.BindText(statement, index, rawValue.AsText);
148+
break;
149+
case SqliteType.Blob:
150+
SQLiteRaw.BindBlob(statement, index, rawValue.AsBlob);
151+
break;
152+
default:
153+
throw new NotImplementedException($"Cannot bind column type '{rawValue.SqliteType}'");
154+
}
155+
}
156+
private object? ReadColumn(Sqlite3Statement statement, int index, Type type) {
157+
TypeSerializer typeSerializer = Connection.Orm.GetTypeSerializer(type);
158+
SqliteValue value = SQLiteRaw.GetColumnValue(statement, index);
159+
return typeSerializer.Deserialize(value, type);
160+
}
127161

128162
private record struct Parameter(string? Name, object? Value) {
129163
public string? Name { get; set; } = Name;

SQLiteSharp/SQLiteConnection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,8 @@ public int Insert(object obj, string? modifier = null) {
584584
int rowCount = Execute(query, values);
585585

586586
if (map.HasAutoIncrementedPrimaryKey) {
587-
long id = SQLiteRaw.GetLastInsertRowid(Handle);
588-
map.SetAutoIncrementedPrimaryKey(obj, id);
587+
long rowId = SQLiteRaw.GetLastInsertRowId(Handle);
588+
map.SetPrimaryKeyValue(obj, rowId);
589589
}
590590

591591
return rowCount;

SQLiteSharp/SQLiteRaw.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static Result Reset(Sqlite3Statement statement) {
3131
public static Result Finalize(Sqlite3Statement statement) {
3232
return (Result)Sqlite3.sqlite3_finalize(statement);
3333
}
34-
public static long GetLastInsertRowid(Sqlite3DatabaseHandle db) {
34+
public static long GetLastInsertRowId(Sqlite3DatabaseHandle db) {
3535
return Sqlite3.sqlite3_last_insert_rowid(db);
3636
}
3737
public static int BindParameterIndex(Sqlite3Statement statement, string name) {

SQLiteSharp/TableMap.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public TableMap(Type type, Orm? orm = null) {
1717

1818
TableAttribute? tableAttribute = type.GetCustomAttribute<TableAttribute>();
1919

20-
TableName = !string.IsNullOrEmpty(tableAttribute?.Name) ? tableAttribute!.Name : Type.Name;
21-
WithoutRowId = tableAttribute is not null && tableAttribute.WithoutRowId;
20+
TableName = tableAttribute?.Name ?? Type.Name;
21+
WithoutRowId = tableAttribute?.WithoutRowId ?? false;
2222

2323
MemberInfo[] members = [.. type.GetProperties(), .. type.GetFields()];
2424
List<ColumnMap> columns = new(members.Length);
@@ -48,8 +48,8 @@ public string GetByPrimaryKeySql {
4848
}
4949
}
5050

51-
public void SetAutoIncrementedPrimaryKey(object obj, long id) {
52-
PrimaryKey!.SetValue(obj, Convert.ChangeType(id, PrimaryKey.ClrType));
51+
public void SetPrimaryKeyValue(object obj, SqliteValue rawValue) {
52+
PrimaryKey?.SetSqliteValue(obj, rawValue);
5353
}
5454

5555
public ColumnMap? FindColumnByMemberName(string memberName) {

0 commit comments

Comments
 (0)