Skip to content

Commit f4270d9

Browse files
authored
Merge branch 'main' into add-transaction-support
2 parents 0e31442 + 59e23a2 commit f4270d9

7 files changed

Lines changed: 329 additions & 250 deletions

File tree

CodeGenerator/CodeGenerator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ private string FindEnumSchema(Enum e)
152152
return schemaTables.Key;
153153
}
154154
}
155-
// TODO fix why not mapping default schema to string.Empty
156155
throw new InvalidDataException($"No enum {e.Name} schema found.");
157156
}
158157

Drivers/ColumnMapping.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ namespace SqlcGenCsharp.Drivers;
66
public record DbTypeInfo(int? Length = null, string? NpgsqlTypeOverride = null);
77

88
public class ColumnMapping(
9-
string csharpType,
109
Dictionary<string, DbTypeInfo> dbTypes, Func<int, string> readerFn, Func<int, string>? readerArrayFn = null)
1110
{
12-
public string CsharpType { get; } = csharpType;
1311
public Func<int, string> ReaderFn { get; } = readerFn;
1412
public Func<int, string>? ReaderArrayFn { get; } = readerArrayFn;
1513
public Dictionary<string, DbTypeInfo> DbTypes { get; } = dbTypes;

Drivers/DbDriver.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public abstract class DbDriver
4646
"NpgsqlCircle"
4747
];
4848

49-
protected abstract List<ColumnMapping> ColumnMappings { get; }
49+
protected abstract Dictionary<string, ColumnMapping> ColumnMappings { get; }
5050

5151
protected const string TransformQueryForSliceArgsImpl = """
5252
public static string TransformQueryForSliceArgs(string originalSql, int sliceSize, string paramName)
@@ -167,10 +167,10 @@ private string GetCsharpTypeWithoutNullableSuffix(Column column, Query? query)
167167
}
168168

169169
foreach (var columnMapping in ColumnMappings
170-
.Where(columnMapping => DoesColumnMappingApply(columnMapping, column)))
170+
.Where(columnMapping => DoesColumnMappingApply(columnMapping.Value, column)))
171171
{
172-
if (column.IsArray || column.IsSqlcSlice) return $"{columnMapping.CsharpType}[]";
173-
return columnMapping.CsharpType;
172+
if (column.IsArray || column.IsSqlcSlice) return $"{columnMapping.Key}[]";
173+
return columnMapping.Key;
174174
}
175175

176176
throw new NotSupportedException($"Column {column.Name} has unsupported column type: {column.Type.Name}");
@@ -198,7 +198,7 @@ private static bool DoesColumnMappingApply(ColumnMapping columnMapping, Column c
198198

199199
private string GetColumnReader(OverrideOption overrideOption, int ordinal)
200200
{
201-
var columnMapping = ColumnMappings.Find(c => c.CsharpType == overrideOption.CsharpType.Type);
201+
var columnMapping = ColumnMappings.GetValueOrDefault(overrideOption.CsharpType.Type);
202202
if (columnMapping is not null)
203203
return columnMapping.ReaderFn(ordinal);
204204
throw new NotSupportedException($"Column {overrideOption.Column} has unsupported column type: {overrideOption.CsharpType.Type}");
@@ -219,7 +219,7 @@ public string GetColumnReader(Column column, int ordinal, Query? query)
219219
return GetColumnReader(foundOverride, ordinal);
220220
}
221221

222-
foreach (var columnMapping in ColumnMappings
222+
foreach (var columnMapping in ColumnMappings.Values
223223
.Where(columnMapping => DoesColumnMappingApply(columnMapping, column)))
224224
{
225225
if (column.IsArray)
@@ -232,7 +232,7 @@ public string GetColumnReader(Column column, int ordinal, Query? query)
232232
protected string? GetColumnDbTypeOverride(Column column)
233233
{
234234
var columnType = column.Type.Name.ToLower();
235-
foreach (var columnMapping in ColumnMappings)
235+
foreach (var columnMapping in ColumnMappings.Values)
236236
{
237237
if (columnMapping.DbTypes.TryGetValue(columnType, out var dbTypeOverride))
238238
return dbTypeOverride.NpgsqlTypeOverride;
@@ -252,10 +252,12 @@ public bool IsTypeNullable(string csharpType)
252252
return Options.DotnetFramework.IsDotnetCore(); // non-primitives in .Net Core are inherently nullable
253253
}
254254

255-
/*
256-
Since there is no indication of the primary key column in SQLC protobuf (assuming it is a single column),
257-
this method uses a few heuristics to assess the data type of the id column
258-
*/
255+
/// <summary>
256+
/// Since there is no indication of the primary key column in SQLC protobuf (assuming it is a single column),
257+
/// this method uses a few heuristics to assess the data type of the id column
258+
/// </summary>
259+
/// <param name="query"></param>
260+
/// <returns>The data type of the id column</returns>
259261
public string GetIdColumnType(Query query)
260262
{
261263
var tableColumns = Tables[query.InsertIntoTable.Schema][query.InsertIntoTable.Name].Columns;

Drivers/Generators/CommonGen.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static string GetSqlTransformations(Query query, string queryTextConstant
9292
""";
9393
}
9494

95-
public string InstantiateDataclass(Column[] columns, string returnInterface, Query? query)
95+
public string InstantiateDataclass(Column[] columns, string returnInterface, Query query)
9696
{
9797
var columnsInit = new List<string>();
9898
var actualOrdinal = 0;
@@ -120,7 +120,7 @@ public string InstantiateDataclass(Column[] columns, string returnInterface, Que
120120

121121
return InstantiateDataclassInternal(returnInterface, columnsInit);
122122

123-
string[] GetAsEmbeddedTableColumnAssignment(Column tableColumn, int ordinal, Query? query)
123+
string[] GetAsEmbeddedTableColumnAssignment(Column tableColumn, int ordinal, Query query)
124124
{
125125
var schemaName = tableColumn.EmbedTable.Schema == dbDriver.DefaultSchema ? string.Empty : tableColumn.EmbedTable.Schema;
126126
var tableColumns = dbDriver.Tables[schemaName][tableColumn.EmbedTable.Name].Columns;

Drivers/MySqlConnectorDriver.cs

Lines changed: 97 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -22,84 +22,106 @@ public partial class MySqlConnectorDriver(
2222
public const string ByteCsvConverter = "ByteCsvConverter";
2323
public const string ByteArrayCsvConverter = "ByteArrayCsvConverter";
2424

25-
protected override List<ColumnMapping> ColumnMappings { get; } =
26-
[
27-
new("bool",
28-
new Dictionary<string, DbTypeInfo>
29-
{
30-
{ "tinyint", new DbTypeInfo(Length: 1) }
31-
}, ordinal => $"reader.GetBoolean({ordinal})"),
32-
new("short",
33-
new Dictionary<string, DbTypeInfo>
34-
{
35-
{ "tinyint", new DbTypeInfo() },
36-
{ "smallint", new DbTypeInfo() },
37-
{ "year", new DbTypeInfo() }
38-
}, ordinal => $"reader.GetInt16({ordinal})"),
39-
new("long",
40-
new Dictionary<string, DbTypeInfo>
41-
{
42-
{ "bigint", new DbTypeInfo() }
43-
}, ordinal => $"reader.GetInt64({ordinal})"),
44-
new("byte",
45-
new Dictionary<string, DbTypeInfo>
46-
{
47-
{ "bit", new DbTypeInfo() }
48-
}, ordinal => $"reader.GetFieldValue<byte>({ordinal})"),
49-
new("byte[]",
50-
new Dictionary<string, DbTypeInfo>
51-
{
52-
{ "binary", new DbTypeInfo() },
53-
{ "blob", new DbTypeInfo() },
54-
{ "longblob", new DbTypeInfo() },
55-
{ "mediumblob", new DbTypeInfo() },
56-
{ "tinyblob", new DbTypeInfo() },
57-
{ "varbinary", new DbTypeInfo() }
58-
}, ordinal => $"reader.GetFieldValue<byte[]>({ordinal})"),
59-
new("string",
60-
new Dictionary<string, DbTypeInfo>
61-
{
62-
{ "char", new DbTypeInfo() },
63-
{ "longtext", new DbTypeInfo() },
64-
{ "mediumtext", new DbTypeInfo() },
65-
{ "text", new DbTypeInfo() },
66-
{ "time", new DbTypeInfo() },
67-
{ "tinytext", new DbTypeInfo() },
68-
{ "varchar", new DbTypeInfo() },
69-
{ "var_string", new DbTypeInfo() },
70-
{ "json", new DbTypeInfo() },
71-
}, ordinal => $"reader.GetString({ordinal})"),
72-
new("DateTime",
73-
new Dictionary<string, DbTypeInfo>
74-
{
75-
{ "date", new DbTypeInfo() },
76-
{ "datetime", new DbTypeInfo() },
77-
{ "timestamp", new DbTypeInfo() }
78-
}, ordinal => $"reader.GetDateTime({ordinal})"),
79-
new("int",
80-
new Dictionary<string, DbTypeInfo>
81-
{
82-
{ "int", new DbTypeInfo() },
83-
{ "mediumint", new DbTypeInfo() },
84-
}, ordinal => $"reader.GetInt32({ordinal})"),
85-
new("double",
86-
new Dictionary<string, DbTypeInfo>
87-
{
88-
{ "double", new DbTypeInfo() },
89-
{ "float", new DbTypeInfo() }
90-
}, ordinal => $"reader.GetDouble({ordinal})"),
91-
new("decimal",
92-
new Dictionary<string, DbTypeInfo>
93-
{
94-
{ "decimal", new DbTypeInfo() }
95-
}, ordinal => $"reader.GetDecimal({ordinal})"),
96-
// last item in the dictionary - enforce TODO
97-
new("object",
25+
protected override Dictionary<string, ColumnMapping> ColumnMappings { get; } =
26+
new()
27+
{
28+
["bool"] = new ColumnMapping(
29+
new Dictionary<string, DbTypeInfo>
30+
{
31+
{ "tinyint", new DbTypeInfo(Length: 1) }
32+
},
33+
ordinal => $"reader.GetBoolean({ordinal})"
34+
),
35+
["short"] = new ColumnMapping(
36+
new Dictionary<string, DbTypeInfo>
37+
{
38+
{ "tinyint", new DbTypeInfo() },
39+
{ "smallint", new DbTypeInfo() },
40+
{ "year", new DbTypeInfo() }
41+
},
42+
ordinal => $"reader.GetInt16({ordinal})"
43+
),
44+
["long"] = new ColumnMapping(
45+
new Dictionary<string, DbTypeInfo>
46+
{
47+
{ "bigint", new DbTypeInfo() }
48+
},
49+
ordinal => $"reader.GetInt64({ordinal})"
50+
),
51+
["byte"] = new ColumnMapping(
52+
new Dictionary<string, DbTypeInfo>
53+
{
54+
{ "bit", new DbTypeInfo() }
55+
},
56+
ordinal => $"reader.GetFieldValue<byte>({ordinal})"
57+
),
58+
["byte[]"] = new ColumnMapping(
59+
new Dictionary<string, DbTypeInfo>
60+
{
61+
{ "binary", new DbTypeInfo() },
62+
{ "blob", new DbTypeInfo() },
63+
{ "longblob", new DbTypeInfo() },
64+
{ "mediumblob", new DbTypeInfo() },
65+
{ "tinyblob", new DbTypeInfo() },
66+
{ "varbinary", new DbTypeInfo() }
67+
},
68+
ordinal => $"reader.GetFieldValue<byte[]>({ordinal})"
69+
),
70+
["string"] = new ColumnMapping(
71+
new Dictionary<string, DbTypeInfo>
72+
{
73+
{ "char", new DbTypeInfo() },
74+
{ "longtext", new DbTypeInfo() },
75+
{ "mediumtext", new DbTypeInfo() },
76+
{ "text", new DbTypeInfo() },
77+
{ "time", new DbTypeInfo() },
78+
{ "tinytext", new DbTypeInfo() },
79+
{ "varchar", new DbTypeInfo() },
80+
{ "var_string", new DbTypeInfo() },
81+
{ "json", new DbTypeInfo() }
82+
},
83+
ordinal => $"reader.GetString({ordinal})"
84+
),
85+
["DateTime"] = new ColumnMapping(
86+
new Dictionary<string, DbTypeInfo>
87+
{
88+
{ "date", new DbTypeInfo() },
89+
{ "datetime", new DbTypeInfo() },
90+
{ "timestamp", new DbTypeInfo() }
91+
},
92+
ordinal => $"reader.GetDateTime({ordinal})"
93+
),
94+
["int"] = new ColumnMapping(
95+
new Dictionary<string, DbTypeInfo>
96+
{
97+
{ "int", new DbTypeInfo() },
98+
{ "mediumint", new DbTypeInfo() }
99+
},
100+
ordinal => $"reader.GetInt32({ordinal})"
101+
),
102+
["double"] = new ColumnMapping(
103+
new Dictionary<string, DbTypeInfo>
104+
{
105+
{ "double", new DbTypeInfo() },
106+
{ "float", new DbTypeInfo() }
107+
},
108+
ordinal => $"reader.GetDouble({ordinal})"
109+
),
110+
["decimal"] = new ColumnMapping(
111+
new Dictionary<string, DbTypeInfo>
112+
{
113+
{ "decimal", new DbTypeInfo() }
114+
},
115+
ordinal => $"reader.GetDecimal({ordinal})"
116+
),
117+
["object"] = new ColumnMapping(
98118
new Dictionary<string, DbTypeInfo>
99119
{
100120
{ "any", new DbTypeInfo() }
101-
}, ordinal => $"reader.GetValue({ordinal})")
102-
];
121+
},
122+
ordinal => $"reader.GetValue({ordinal})"
123+
)
124+
};
103125

104126
public override string TransactionClassName => "MySqlTransaction";
105127

0 commit comments

Comments
 (0)