Skip to content

Commit 2845edc

Browse files
committed
fixing enum
1 parent 8356ecc commit 2845edc

File tree

2 files changed

+158
-149
lines changed

2 files changed

+158
-149
lines changed

nuget/Sqlite_net.SourceGenerator/SQLiteFastColumnSetterGenerator.cs

Lines changed: 19 additions & 10 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));
136+
properties.Add (new PropertyInfo (member.Name, member.Type.ToDisplayString (), columnName, member.Type.TypeKind == TypeKind.Enum));
137137
}
138138
}
139139
}
@@ -460,17 +460,26 @@ static void GeneratePropertySetter(StringBuilder sb, PropertyInfo property)
460460
break;
461461

462462
default:
463-
// For other types, try to use a generic approach
464-
sb.AppendLine($" // Generic setter for {propertyType}");
465-
sb.AppendLine($" var value = SQLite3.ColumnString(stmt, index);");
466-
sb.AppendLine($" if (value != null)");
467-
sb.AppendLine($" {{");
468-
sb.AppendLine($" typedObj.{property.PropertyName} = ({propertyType})Convert.ChangeType(value, typeof({propertyType}));");
469-
sb.AppendLine($" }}");
470-
break;
463+
if (property.IsEnum) {
464+
// For other types, try to use a generic approach
465+
sb.AppendLine ($" // Enum setter for {propertyType}");
466+
sb.AppendLine ($" var value = SQLite3.ColumnInt(stmt, index);");
467+
sb.AppendLine ($" typedObj.{ property.PropertyName} = ({ propertyType})value;");
468+
}
469+
else {
470+
// For other types, try to use a generic approach
471+
sb.AppendLine ($" // Generic setter for {propertyType}");
472+
sb.AppendLine ($" var value = SQLite3.ColumnString(stmt, index);");
473+
sb.AppendLine ($" if (value != null)");
474+
sb.AppendLine ($" {{");
475+
sb.AppendLine ($" typedObj.{property.PropertyName} = ({propertyType})Convert.ChangeType(value, typeof({propertyType}));");
476+
sb.AppendLine ($" }}");
477+
}
478+
479+
break;
471480
}
472481
}
473482

474483
record ClassInfo(string ClassName, string Namespace, List<PropertyInfo> Properties);
475-
record PropertyInfo(string PropertyName, string TypeName, string ColumnName);
484+
record PropertyInfo(string PropertyName, string TypeName, string ColumnName, bool IsEnum);
476485
}
Lines changed: 139 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,143 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Text;
6-
7-
#if NETFX_CORE
8-
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
9-
using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute;
10-
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
11-
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
12-
#else
13-
using NUnit.Framework;
14-
#endif
15-
16-
namespace SQLite.Tests
17-
{
18-
[TestFixture]
19-
public class EnumCacheTests
20-
{
21-
[StoreAsText]
22-
public enum TestEnumStoreAsText
23-
{
24-
Value1,
25-
26-
Value2,
27-
28-
Value3
29-
}
30-
31-
public enum TestEnumStoreAsInt
32-
{
33-
Value1,
34-
35-
Value2,
36-
37-
Value3
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
7+
#if NETFX_CORE
8+
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
9+
using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute;
10+
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
11+
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
12+
#else
13+
using NUnit.Framework;
14+
#endif
15+
16+
namespace SQLite.Tests
17+
{
18+
[TestFixture]
19+
public class EnumCacheTests
20+
{
21+
[StoreAsText]
22+
public enum TestEnumStoreAsText
23+
{
24+
Value1,
25+
26+
Value2,
27+
28+
Value3
29+
}
30+
31+
public enum TestEnumStoreAsInt
32+
{
33+
Value1,
34+
35+
Value2,
36+
37+
Value3
38+
}
39+
40+
public enum TestByteEnumStoreAsInt : byte
41+
{
42+
Value1,
43+
44+
Value2,
45+
46+
Value3
47+
}
48+
49+
public enum TestEnumWithRepeats
50+
{
51+
Value1 = 1,
52+
53+
Value2 = 2,
54+
55+
Value2Again = 2,
56+
57+
Value3 = 3,
58+
}
59+
60+
[StoreAsText]
61+
public enum TestEnumWithRepeatsAsText
62+
{
63+
Value1 = 1,
64+
65+
Value2 = 2,
66+
67+
Value2Again = 2,
68+
69+
Value3 = 3,
70+
}
71+
72+
public class TestClassThusNotEnum
73+
{
74+
75+
}
76+
77+
[Test]
78+
public void ShouldReturnTrueForEnumStoreAsText()
79+
{
80+
var info = EnumCache.GetInfo<TestEnumStoreAsText>();
81+
82+
Assert.IsTrue(info.IsEnum);
83+
Assert.IsTrue(info.StoreAsText);
84+
Assert.IsNotNull(info.EnumValues);
85+
86+
var values = Enum.GetValues(typeof(TestEnumStoreAsText)).Cast<object>().ToList();
87+
88+
for (int i = 0; i < values.Count; i++)
89+
{
90+
Assert.AreEqual(values[i].ToString(), info.EnumValues[i]);
91+
}
3892
}
3993

40-
public enum TestByteEnumStoreAsInt : byte
41-
{
42-
Value1,
43-
44-
Value2,
45-
46-
Value3
47-
}
48-
49-
public enum TestEnumWithRepeats
50-
{
51-
Value1 = 1,
52-
53-
Value2 = 2,
54-
55-
Value2Again = 2,
56-
57-
Value3 = 3,
58-
}
59-
60-
[StoreAsText]
61-
public enum TestEnumWithRepeatsAsText
62-
{
63-
Value1 = 1,
64-
65-
Value2 = 2,
66-
67-
Value2Again = 2,
68-
69-
Value3 = 3,
70-
}
71-
72-
public class TestClassThusNotEnum
73-
{
74-
75-
}
76-
77-
[Test]
78-
public void ShouldReturnTrueForEnumStoreAsText()
79-
{
80-
var info = EnumCache.GetInfo<TestEnumStoreAsText>();
81-
82-
Assert.IsTrue(info.IsEnum);
83-
Assert.IsTrue(info.StoreAsText);
84-
Assert.IsNotNull(info.EnumValues);
85-
86-
var values = Enum.GetValues(typeof(TestEnumStoreAsText)).Cast<object>().ToList();
87-
88-
for (int i = 0; i < values.Count; i++)
89-
{
90-
Assert.AreEqual(values[i].ToString(), info.EnumValues[i]);
91-
}
92-
}
93-
94-
[Test]
95-
public void ShouldReturnTrueForEnumStoreAsInt()
96-
{
97-
var info = EnumCache.GetInfo<TestEnumStoreAsInt>();
98-
99-
Assert.IsTrue(info.IsEnum);
100-
Assert.IsFalse(info.StoreAsText);
101-
Assert.IsNull(info.EnumValues);
94+
[Test]
95+
public void ShouldReturnTrueForEnumStoreAsInt()
96+
{
97+
var info = EnumCache.GetInfo<TestEnumStoreAsInt>();
98+
99+
Assert.IsTrue(info.IsEnum);
100+
Assert.IsFalse(info.StoreAsText);
101+
Assert.IsNull(info.EnumValues);
102102
}
103103

104-
[Test]
105-
public void ShouldReturnTrueForByteEnumStoreAsInt()
106-
{
107-
var info = EnumCache.GetInfo<TestByteEnumStoreAsInt>();
108-
109-
Assert.IsTrue(info.IsEnum);
110-
Assert.IsFalse(info.StoreAsText);
111-
}
112-
113-
[Test]
114-
public void ShouldReturnFalseForClass()
115-
{
116-
var info = EnumCache.GetInfo<TestClassThusNotEnum>();
117-
118-
Assert.IsFalse(info.IsEnum);
119-
Assert.IsFalse(info.StoreAsText);
120-
Assert.IsNull(info.EnumValues);
121-
}
122-
123-
[Test]
124-
public void Issue598_EnumsWithRepeatedValues ()
125-
{
126-
var info = EnumCache.GetInfo<TestEnumWithRepeats> ();
127-
128-
Assert.IsTrue (info.IsEnum);
129-
Assert.IsFalse (info.StoreAsText);
130-
Assert.IsNull (info.EnumValues);
131-
}
132-
133-
[Test]
134-
public void Issue598_EnumsWithRepeatedValuesAsText ()
135-
{
136-
var info = EnumCache.GetInfo<TestEnumWithRepeatsAsText> ();
137-
138-
Assert.IsTrue (info.IsEnum);
139-
Assert.IsTrue (info.StoreAsText);
140-
Assert.IsNotNull (info.EnumValues);
141-
}
142-
}
143-
}
104+
[Test]
105+
public void ShouldReturnTrueForByteEnumStoreAsInt()
106+
{
107+
var info = EnumCache.GetInfo<TestByteEnumStoreAsInt>();
108+
109+
Assert.IsTrue(info.IsEnum);
110+
Assert.IsFalse(info.StoreAsText);
111+
}
112+
113+
[Test]
114+
public void ShouldReturnFalseForClass()
115+
{
116+
var info = EnumCache.GetInfo<TestClassThusNotEnum>();
117+
118+
Assert.IsFalse(info.IsEnum);
119+
Assert.IsFalse(info.StoreAsText);
120+
Assert.IsNull(info.EnumValues);
121+
}
122+
123+
[Test]
124+
public void Issue598_EnumsWithRepeatedValues ()
125+
{
126+
var info = EnumCache.GetInfo<TestEnumWithRepeats> ();
127+
128+
Assert.IsTrue (info.IsEnum);
129+
Assert.IsFalse (info.StoreAsText);
130+
Assert.IsNull (info.EnumValues);
131+
}
132+
133+
[Test]
134+
public void Issue598_EnumsWithRepeatedValuesAsText ()
135+
{
136+
var info = EnumCache.GetInfo<TestEnumWithRepeatsAsText> ();
137+
138+
Assert.IsTrue (info.IsEnum);
139+
Assert.IsTrue (info.StoreAsText);
140+
Assert.IsNotNull (info.EnumValues);
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)