Skip to content

Commit 08faef0

Browse files
add generate-csproj config tests (#53)
* refactored column mapping functions * test generate-csproj config * change linting to prefer var usage
1 parent 2111edb commit 08faef0

File tree

7 files changed

+168
-214
lines changed

7 files changed

+168
-214
lines changed

.editorconfig

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ dotnet_code_quality_unused_parameters = all:silent
6767
#### C# Coding Conventions ####
6868

6969
# var preferences
70-
csharp_style_var_elsewhere = false
71-
csharp_style_var_for_built_in_types = false
72-
csharp_style_var_when_type_is_apparent = false
70+
csharp_style_var_elsewhere = true
71+
csharp_style_var_for_built_in_types = true
72+
csharp_style_var_when_type_is_apparent = true
7373

7474
# Expression-bodied members
7575
csharp_style_expression_bodied_accessors = true

.github/workflows/main.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ jobs:
101101
strategy:
102102
matrix:
103103
file-per-query: [ 'true', 'false' ]
104+
generate-csproj: [ 'true', 'false' ]
104105

105106
steps:
106107
- uses: actions/checkout@v4
@@ -121,17 +122,19 @@ jobs:
121122
- name: Updating configuration
122123
env:
123124
FILE_PER_QUERY: ${{ matrix.file-per-query }}
125+
GENERATE_CSPROJ: ${{ matrix.generate-csproj }}
124126
run: |
125127
./scripts/wasm/update_sha.sh sqlc.ci.yaml
126-
./scripts/update_config.sh sqlc.ci.yaml $FILE_PER_QUERY
128+
./scripts/update_config.sh sqlc.ci.yaml $FILE_PER_QUERY $GENERATE_CSPROJ
127129
128130
- name: Sqlc generate using Wasm plugin
129131
env:
130132
FILE_PER_QUERY: ${{ matrix.file-per-query }}
133+
GENERATE_CSPROJ: ${{ matrix.generate-csproj }}
131134
run: |
132135
rm MySqlConnectorExample/*.cs NpgsqlExample/*.cs
133136
sqlc -f sqlc.ci.yaml generate
134-
./scripts/run_codegen_tests.sh $FILE_PER_QUERY
137+
./scripts/run_codegen_tests.sh $FILE_PER_QUERY $GENERATE_CSPROJ
135138
136139
end2end-tests:
137140
name: End-to-End Tests

Drivers/DbDriver.cs

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Microsoft.CodeAnalysis.CSharp.Syntax;
22
using Plugin;
3+
using System;
34
using System.Collections.Generic;
45
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
6+
using static System.String;
57

68
namespace SqlcGenCsharp.Drivers;
79

@@ -18,17 +20,42 @@ public virtual UsingDirectiveSyntax[] GetUsingDirectives()
1820
];
1921
}
2022

23+
protected abstract List<(string, Func<int, string>, HashSet<string>)> GetColumnMapping();
24+
2125
public string AddNullableSuffix(string csharpType, bool notNull)
2226
{
2327
if (notNull) return csharpType;
2428
if (Utils.IsCsharpPrimitive(csharpType)) return $"{csharpType}?";
2529
return DotnetFramework.LatestDotnetSupported() ? $"{csharpType}?" : csharpType;
2630
}
2731

28-
public abstract string GetColumnType(Column column);
32+
public string GetColumnType(Column column)
33+
{
34+
var columnCsharpType = IsNullOrEmpty(column.Type.Name) ? "object" : GetTypeWithoutNullableSuffix();
35+
return AddNullableSuffix(columnCsharpType, column.NotNull);
2936

30-
public abstract string GetColumnReader(Column column, int ordinal);
37+
string GetTypeWithoutNullableSuffix()
38+
{
39+
var columnType = column.Type.Name.ToLower();
40+
foreach (var (csharpType, _, dbTypes) in GetColumnMapping())
41+
{
42+
if (dbTypes.Contains(columnType))
43+
return csharpType;
44+
}
45+
throw new NotSupportedException($"Unsupported column type: {column.Type.Name}");
46+
}
47+
}
3148

49+
public string GetColumnReader(Column column, int ordinal)
50+
{
51+
var columnType = column.Type.Name.ToLower();
52+
foreach (var (_, getDataReader, dbTypes) in GetColumnMapping())
53+
{
54+
if (dbTypes.Contains(columnType))
55+
return getDataReader(ordinal);
56+
}
57+
throw new NotSupportedException($"Unsupported column type: {column.Type.Name}");
58+
}
3259
public abstract string TransformQueryText(Query query);
3360

3461
public abstract (string, string) EstablishConnection();

Drivers/MySqlConnectorDriver.cs

+39-91
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,51 @@
66
using System.Linq;
77
using System.Text.RegularExpressions;
88
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
9-
using static System.String;
109
using OneDeclareGen = SqlcGenCsharp.Drivers.Generators.OneDeclareGen;
1110

1211
namespace SqlcGenCsharp.Drivers;
1312

1413
public partial class MySqlConnectorDriver(DotnetFramework dotnetFramework) : DbDriver(dotnetFramework)
1514
{
15+
protected override List<(string, Func<int, string>, HashSet<string>)> GetColumnMapping()
16+
{
17+
return
18+
[
19+
("long", ordinal => $"reader.GetInt64({ordinal})", ["bigint"]),
20+
("byte[]", ordinal => $"Utils.GetBytes(reader, {ordinal})", [
21+
"binary",
22+
"bit",
23+
"blob",
24+
"longblob",
25+
"mediumblob",
26+
"tinyblob",
27+
"varbinary"
28+
]),
29+
("string", ordinal => $"reader.GetString({ordinal})", [
30+
"char",
31+
"date",
32+
"datetime",
33+
"decimal",
34+
"longtext",
35+
"mediumtext",
36+
"text",
37+
"time",
38+
"timestamp",
39+
"tinytext",
40+
"varchar"
41+
]),
42+
("int", ordinal => $"reader.GetInt32({ordinal})", [
43+
"int",
44+
"mediumint",
45+
"smallint",
46+
"tinyint",
47+
"year"
48+
]),
49+
("double", ordinal => $"reader.GetDouble({ordinal})", ["double", "float"]),
50+
("object", ordinal => $"reader.GetString({ordinal})", ["json"])
51+
];
52+
}
53+
1654
public override UsingDirectiveSyntax[] GetUsingDirectives()
1755
{
1856
return base.GetUsingDirectives()
@@ -32,96 +70,6 @@ public override string CreateSqlCommand(string sqlTextConstant)
3270
return $"var {Variable.Command.Name()} = new MySqlCommand({sqlTextConstant}, {Variable.Connection.Name()})";
3371
}
3472

35-
public override string GetColumnType(Column column)
36-
{
37-
var csharpType = IsNullOrEmpty(column.Type.Name) ? "object" : GetTypeWithoutNullableSuffix();
38-
return AddNullableSuffix(csharpType, column.NotNull);
39-
40-
string GetTypeWithoutNullableSuffix()
41-
{
42-
switch (column.Type.Name.ToLower())
43-
{
44-
case "bigint":
45-
return "long";
46-
case "binary":
47-
case "bit":
48-
case "blob":
49-
case "longblob":
50-
case "mediumblob":
51-
case "tinyblob":
52-
case "varbinary":
53-
return "byte[]";
54-
case "char":
55-
case "date":
56-
case "datetime":
57-
case "decimal":
58-
case "longtext":
59-
case "mediumtext":
60-
case "text":
61-
case "time":
62-
case "timestamp":
63-
case "tinytext":
64-
case "varchar":
65-
return "string";
66-
case "double":
67-
case "float":
68-
return "double";
69-
case "int":
70-
case "mediumint":
71-
case "smallint":
72-
case "tinyint":
73-
case "year":
74-
return "int";
75-
case "json":
76-
// Assuming JSON is represented as a string or a specific class
77-
return "object";
78-
default:
79-
throw new NotSupportedException($"Unsupported column type: {column.Type.Name}");
80-
}
81-
}
82-
}
83-
84-
public override string GetColumnReader(Column column, int ordinal)
85-
{
86-
switch (column.Type.Name.ToLower())
87-
{
88-
case "bigint":
89-
return $"reader.GetInt64({ordinal})";
90-
case "binary":
91-
case "bit":
92-
case "blob":
93-
case "longblob":
94-
case "mediumblob":
95-
case "tinyblob":
96-
case "varbinary":
97-
return $"Utils.GetBytes(reader, {ordinal})";
98-
case "char":
99-
case "date":
100-
case "datetime":
101-
case "decimal":
102-
case "longtext":
103-
case "mediumtext":
104-
case "text":
105-
case "time":
106-
case "timestamp":
107-
case "tinytext":
108-
case "varchar":
109-
case "json":
110-
return $"reader.GetString({ordinal})";
111-
case "double":
112-
case "float":
113-
return $"reader.GetDouble({ordinal})";
114-
case "int":
115-
case "mediumint":
116-
case "smallint":
117-
case "tinyint":
118-
case "year":
119-
return $"reader.GetInt32({ordinal})";
120-
default:
121-
throw new NotSupportedException($"Unsupported column type: {column.Type.Name}");
122-
}
123-
}
124-
12573
public override string TransformQueryText(Query query)
12674
{
12775
var counter = 0;

Drivers/NpgsqlDriver.cs

+29-98
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,35 @@ namespace SqlcGenCsharp.Drivers;
1111

1212
public class NpgsqlDriver(DotnetFramework dotnetFramework) : DbDriver(dotnetFramework)
1313
{
14+
protected override List<(string, Func<int, string>, HashSet<string>)> GetColumnMapping()
15+
{
16+
return
17+
[
18+
("long", ordinal => $"reader.GetInt64({ordinal})", ["serial", "bigserial"]),
19+
("byte[]", ordinal => $"Utils.GetBytes(reader, {ordinal})",
20+
["binary", "bit", "bytea", "blob", "longblob", "mediumblob", "tinyblob", "varbinary"]),
21+
("string", ordinal => $"reader.GetString({ordinal})",
22+
[
23+
"char",
24+
"date",
25+
"datetime",
26+
"longtext",
27+
"mediumtext",
28+
"text",
29+
"bpchar",
30+
"time",
31+
"timestamp",
32+
"tinytext",
33+
"varchar"
34+
]),
35+
("object", ordinal => $"reader.GetString({ordinal})", ["json"]),
36+
("int", ordinal => $"reader.GetInt32({ordinal})", ["int2", "int4", "int8"]),
37+
("float", ordinal => $"reader.GetFloat({ordinal})", ["numeric", "float4", "float8"]),
38+
("decimal", ordinal => $"reader.GetDecimal({ordinal})", ["decimal"]),
39+
("bool", ordinal => $"reader.GetBoolean({ordinal})", ["bool", "boolean"])
40+
];
41+
}
42+
1443
public override UsingDirectiveSyntax[] GetUsingDirectives()
1544
{
1645
return base.GetUsingDirectives()
@@ -30,104 +59,6 @@ public override string CreateSqlCommand(string sqlTextConstant)
3059
return $"var {Variable.Command.Name()} = {Variable.Connection.Name()}.CreateCommand({sqlTextConstant})";
3160
}
3261

33-
public override string GetColumnType(Column column)
34-
{
35-
var csharpType = string.IsNullOrEmpty(column.Type.Name) ? "object" : GetTypeWithoutNullableSuffix();
36-
return AddNullableSuffix(csharpType, column.NotNull);
37-
38-
string GetTypeWithoutNullableSuffix()
39-
{
40-
switch (column.Type.Name.ToLower())
41-
{
42-
case "serial":
43-
case "bigserial":
44-
return "long";
45-
case "bit":
46-
case "bytea":
47-
return "byte[]";
48-
case "char":
49-
case "bpchar":
50-
case "varchar":
51-
case "text":
52-
case "date":
53-
case "time":
54-
case "timestamp":
55-
return "string";
56-
case "decimal":
57-
return "decimal";
58-
case "numeric":
59-
case "float4":
60-
case "float8":
61-
return "float";
62-
case "int2":
63-
case "int4":
64-
case "int8":
65-
return "int";
66-
case "json":
67-
return "object";
68-
case "bool":
69-
case "boolean":
70-
return "bool";
71-
default:
72-
throw new NotSupportedException($"Unsupported column type: {column.Type.Name}");
73-
}
74-
}
75-
}
76-
77-
public override string GetColumnReader(Column column, int ordinal)
78-
{
79-
switch (column.Type.Name.ToLower())
80-
{
81-
case "serial":
82-
case "bigserial":
83-
return $"reader.GetInt64({ordinal})";
84-
case "binary":
85-
case "bit":
86-
case "bytea":
87-
case "blob":
88-
case "longblob":
89-
case "mediumblob":
90-
case "tinyblob":
91-
case "varbinary":
92-
return $"Utils.GetBytes({Variable.Reader.Name()}, {ordinal})";
93-
case "char":
94-
case "date":
95-
case "datetime":
96-
case "longtext":
97-
case "mediumtext":
98-
case "text":
99-
case "bpchar":
100-
case "time":
101-
case "timestamp":
102-
case "tinytext":
103-
case "varchar":
104-
case "json":
105-
return $"reader.GetString({ordinal})";
106-
case "double":
107-
return $"reader.GetDouble({ordinal})";
108-
case "numeric":
109-
case "float4":
110-
case "float8":
111-
return $"reader.GetFloat({ordinal})";
112-
case "decimal":
113-
return $"reader.GetDecimal({ordinal})";
114-
case "bool":
115-
case "boolean":
116-
return $"reader.GetBoolean({ordinal})";
117-
case "int":
118-
case "int2":
119-
case "int4":
120-
case "int8":
121-
case "mediumint":
122-
case "smallint":
123-
case "tinyint":
124-
case "year":
125-
return $"reader.GetInt32({ordinal})";
126-
default:
127-
throw new NotSupportedException($"Unsupported column type: {column.Type.Name}");
128-
}
129-
}
130-
13162
public override string TransformQueryText(Query query)
13263
{
13364
var queryText = query.Text;

0 commit comments

Comments
 (0)