Skip to content

Commit

Permalink
Remove unused attribute and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Joy-less committed Dec 2, 2024
1 parent 93a06bb commit 3df9abf
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 55 deletions.
9 changes: 2 additions & 7 deletions SQLiteSharp/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class TableAttribute(string name) : Attribute {
public string Name { get; set; } = name;

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

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class IgnoreAttribute : Attribute {
}

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class UniqueAttribute : IndexedAttribute {
public override bool Unique {
Expand All @@ -51,8 +47,7 @@ public override bool Unique {
}

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class MaxLengthAttribute(int length) : Attribute {
public int Value { get; } = length;
public class IgnoreAttribute : Attribute {
}

/// <summary>
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions SQLiteSharp/ColumnMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ public void SetValue(object obj, object? value) {
throw new InvalidProgramException();
}
}
public void SetSqliteValue(object obj, SqliteValue sqliteValue) {
TypeSerializer typeSerializer = Orm.GetTypeSerializer(ClrType);
object? value = typeSerializer.Deserialize(sqliteValue, ClrType);
SetValue(obj, value);
}
public object? GetValue(object obj) {
return ClrMember switch {
PropertyInfo propertyInfo => propertyInfo.GetValue(obj),
FieldInfo fieldInfo => fieldInfo.GetValue(obj),
_ => throw new InvalidProgramException(),
};
}

private static Type GetMemberType(MemberInfo memberInfo) {
return memberInfo switch {
PropertyInfo propertyInfo => propertyInfo.PropertyType,
Expand Down
37 changes: 0 additions & 37 deletions SQLiteSharp/Orm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,40 +44,6 @@ public TypeSerializer GetTypeSerializer(Type type) {
// Serializer not found
throw new InvalidOperationException($"No {nameof(TypeSerializer)} found for '{type}'");
}
public object? ReadColumn(Sqlite3Statement statement, int index, Type type) {
TypeSerializer typeSerializer = GetTypeSerializer(type);
SqliteValue value = SQLiteRaw.GetColumnValue(statement, index);
return typeSerializer.Deserialize(value, type);
}
public void BindParameter(Sqlite3Statement statement, int index, object? value) {
if (value is null) {
SQLiteRaw.BindNull(statement, index);
return;
}

TypeSerializer typeSerializer = GetTypeSerializer(value.GetType());
SqliteValue rawValue = typeSerializer.Serialize(value);

switch (rawValue.SqliteType) {
case SqliteType.Null:
SQLiteRaw.BindNull(statement, index);
break;
case SqliteType.Integer:
SQLiteRaw.BindInt64(statement, index, rawValue.AsInteger);
break;
case SqliteType.Float:
SQLiteRaw.BindDouble(statement, index, rawValue.AsFloat);
break;
case SqliteType.Text:
SQLiteRaw.BindText(statement, index, rawValue.AsText);
break;
case SqliteType.Blob:
SQLiteRaw.BindBlob(statement, index, rawValue.AsBlob);
break;
default:
throw new NotImplementedException($"Cannot bind column type '{rawValue.SqliteType}'");
}
}
public string GetSqlDeclaration(ColumnMap column) {
TypeSerializer typeSerializer = GetTypeSerializer(column.ClrType);

Expand Down Expand Up @@ -110,9 +76,6 @@ public static string GetCollation(MemberInfo memberInfo) {
public static IEnumerable<IndexedAttribute> GetIndexes(MemberInfo memberInfo) {
return memberInfo.GetCustomAttributes<IndexedAttribute>();
}
public static int? GetMaxStringLength(MemberInfo memberInfo) {
return memberInfo.GetCustomAttribute<MaxLengthAttribute>()?.Value;
}
public static Type AsUnderlyingType(Type Type) {
return Nullable.GetUnderlyingType(Type) ?? Type;
}
Expand Down
42 changes: 38 additions & 4 deletions SQLiteSharp/SQLiteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public IEnumerable<object> ExecuteQuery(TableMap map) {
continue;
}
// Read value from found column
object? value = Connection.Orm.ReadColumn(statement, i, column.ClrType);
object? value = ReadColumn(statement, i, column.ClrType);
column.SetValue(obj, value);
}
OnInstanceCreated?.Invoke(obj);
Expand All @@ -68,7 +68,7 @@ public T ExecuteScalar<T>() {
try {
Result result = SQLiteRaw.Step(statement);
if (result is Result.Row) {
object? columnValue = Connection.Orm.ReadColumn(statement, 0, typeof(T));
object? columnValue = ReadColumn(statement, 0, typeof(T));
if (columnValue is not null) {
Value = (T)columnValue;
}
Expand All @@ -92,7 +92,7 @@ public IEnumerable<T> ExecuteQueryScalars<T>() {
throw new InvalidOperationException("QueryScalars should return at least one column");
}
while (SQLiteRaw.Step(statement) is Result.Row) {
object? value = Connection.Orm.ReadColumn(statement, 0, typeof(T));
object? value = ReadColumn(statement, 0, typeof(T));
if (value is null) {
yield return default!;
}
Expand Down Expand Up @@ -121,9 +121,43 @@ private void BindParameters(Sqlite3Statement statement) {
int index = name is not null
? SQLiteRaw.BindParameterIndex(statement, name)
: nextIndex++;
Connection.Orm.BindParameter(statement, index, value);
BindParameter(statement, index, value);
}
}
private void BindParameter(Sqlite3Statement statement, int index, object? value) {
if (value is null) {
SQLiteRaw.BindNull(statement, index);
return;
}

TypeSerializer typeSerializer = Connection.Orm.GetTypeSerializer(value.GetType());
SqliteValue rawValue = typeSerializer.Serialize(value);

switch (rawValue.SqliteType) {
case SqliteType.Null:
SQLiteRaw.BindNull(statement, index);
break;
case SqliteType.Integer:
SQLiteRaw.BindInt64(statement, index, rawValue.AsInteger);
break;
case SqliteType.Float:
SQLiteRaw.BindDouble(statement, index, rawValue.AsFloat);
break;
case SqliteType.Text:
SQLiteRaw.BindText(statement, index, rawValue.AsText);
break;
case SqliteType.Blob:
SQLiteRaw.BindBlob(statement, index, rawValue.AsBlob);
break;
default:
throw new NotImplementedException($"Cannot bind column type '{rawValue.SqliteType}'");
}
}
private object? ReadColumn(Sqlite3Statement statement, int index, Type type) {
TypeSerializer typeSerializer = Connection.Orm.GetTypeSerializer(type);
SqliteValue value = SQLiteRaw.GetColumnValue(statement, index);
return typeSerializer.Deserialize(value, type);
}

private record struct Parameter(string? Name, object? Value) {
public string? Name { get; set; } = Name;
Expand Down
4 changes: 2 additions & 2 deletions SQLiteSharp/SQLiteConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,8 @@ public int Insert(object obj, string? modifier = null) {
int rowCount = Execute(query, values);

if (map.HasAutoIncrementedPrimaryKey) {
long id = SQLiteRaw.GetLastInsertRowid(Handle);
map.SetAutoIncrementedPrimaryKey(obj, id);
long rowId = SQLiteRaw.GetLastInsertRowId(Handle);
map.SetPrimaryKeyValue(obj, rowId);
}

return rowCount;
Expand Down
2 changes: 1 addition & 1 deletion SQLiteSharp/SQLiteRaw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static Result Reset(Sqlite3Statement statement) {
public static Result Finalize(Sqlite3Statement statement) {
return (Result)Sqlite3.sqlite3_finalize(statement);
}
public static long GetLastInsertRowid(Sqlite3DatabaseHandle db) {
public static long GetLastInsertRowId(Sqlite3DatabaseHandle db) {
return Sqlite3.sqlite3_last_insert_rowid(db);
}
public static int BindParameterIndex(Sqlite3Statement statement, string name) {
Expand Down
8 changes: 4 additions & 4 deletions SQLiteSharp/TableMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public TableMap(Type type, Orm? orm = null) {

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

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

MemberInfo[] members = [.. type.GetProperties(), .. type.GetFields()];
List<ColumnMap> columns = new(members.Length);
Expand Down Expand Up @@ -48,8 +48,8 @@ public string GetByPrimaryKeySql {
}
}

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

public ColumnMap? FindColumnByMemberName(string memberName) {
Expand Down

0 comments on commit 3df9abf

Please sign in to comment.