Skip to content

Commit f545bbe

Browse files
committed
fix all unit tests
1 parent 34e8335 commit f545bbe

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

nuget/Sqlite_net.SourceGenerator/SQLiteFastColumnSetterGenerator.cs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static bool HasBaseClass (ClassDeclarationSyntax classDecl)
133133
// Include property if not ignored
134134
if (!ignore) {
135135
var columnName = GetColumnName (member);
136-
properties.Add (new PropertyInfo (member.Name, member.Type.ToDisplayString (), columnName, IsEnum(member)));
136+
properties.Add (new PropertyInfo (member.Name, member.Type.ToDisplayString (), columnName, GetEnumInfo(member)));
137137
}
138138
}
139139
}
@@ -167,20 +167,21 @@ static bool HasBaseClass (ClassDeclarationSyntax classDecl)
167167
properties);
168168
}
169169

170-
private static bool IsEnum (IPropertySymbol member)
170+
private static EnumInfo? GetEnumInfo (IPropertySymbol member)
171171
{
172-
173172
var type = member.Type;
174173
if (type is INamedTypeSymbol named &&
175174
named.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T) {
176175
type = named.TypeArguments[0];
177176
}
178177

179178
if (type.TypeKind == TypeKind.Enum) {
180-
return true;
179+
var storeAsText = type.GetAttributes ()
180+
.Any (attr => IsStoreAsTextAttribute(attr.AttributeClass));
181+
return new EnumInfo (storeAsText);
181182
}
182183

183-
return false;
184+
return null;
184185
}
185186

186187
private static bool HasSQLiteAttribute (INamedTypeSymbol? classSymbol)
@@ -210,7 +211,7 @@ private static bool HasSQLiteAttribute (INamedTypeSymbol? classSymbol)
210211
}
211212
}
212213

213-
private static bool HasTableAttribute (INamedTypeSymbol? classSymbol)
214+
private static bool HasTableAttribute (INamedTypeSymbol? classSymbol)
214215
{
215216
if (classSymbol != null && cachedHasTableAttribute.TryGetValue (classSymbol, out var result)) {
216217
return result;
@@ -259,6 +260,21 @@ private static bool IsTableAttribute (INamedTypeSymbol? attributeClass)
259260
}
260261
}
261262

263+
private static bool IsStoreAsTextAttribute (INamedTypeSymbol? attributeClass)
264+
{
265+
while (true) {
266+
if (attributeClass == null) {
267+
return false;
268+
}
269+
270+
if (IsSQLiteNamespace (attributeClass) && attributeClass.Name == "StoreAsTextAttribute") {
271+
return true;
272+
}
273+
274+
attributeClass = attributeClass.BaseType;
275+
}
276+
}
277+
262278
private static bool IsIgnoreAttribute (INamedTypeSymbol? attributeClass)
263279
{
264280
while (true) {
@@ -476,12 +492,21 @@ static void GeneratePropertySetter(StringBuilder sb, PropertyInfo property)
476492
break;
477493

478494
default:
479-
if (property.IsEnum) {
495+
if (property.Enum != null) {
480496
// For other types, try to use a generic approach
481497
sb.AppendLine ($" // Enum setter for {propertyType}");
482-
sb.AppendLine ($" var value = SQLite3.ColumnInt(stmt, index);");
483-
sb.AppendLine ($" typedObj.{ property.PropertyName} = ({ propertyType})value;");
484-
}
498+
if (property.Enum.StoreAsText) {
499+
sb.AppendLine ($" var value = SQLite3.ColumnString(stmt, index);");
500+
sb.AppendLine ($" if (value != null)");
501+
sb.AppendLine ($" {{");
502+
sb.AppendLine ($" typedObj.{property.PropertyName} = ({propertyType})Enum.Parse(typeof({propertyType}), value, ignoreCase: true);");
503+
sb.AppendLine ($" }}");
504+
}
505+
else {
506+
sb.AppendLine ($" var value = SQLite3.ColumnInt(stmt, index);");
507+
sb.AppendLine ($" typedObj.{property.PropertyName} = ({propertyType})value;");
508+
}
509+
}
485510
else {
486511
// For other types, try to use a generic approach
487512
sb.AppendLine ($" // Generic setter for {propertyType}");
@@ -497,5 +522,6 @@ static void GeneratePropertySetter(StringBuilder sb, PropertyInfo property)
497522
}
498523

499524
record ClassInfo(string ClassName, string Namespace, List<PropertyInfo> Properties);
500-
record PropertyInfo(string PropertyName, string TypeName, string ColumnName, bool IsEnum);
525+
record PropertyInfo(string PropertyName, string TypeName, string ColumnName, EnumInfo? Enum);
526+
record EnumInfo (bool StoreAsText);
501527
}

0 commit comments

Comments
 (0)