-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlDialects.cs
More file actions
295 lines (238 loc) · 10.2 KB
/
SqlDialects.cs
File metadata and controls
295 lines (238 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
namespace CodeWorks.SimpleSql;
public interface ISqlDialect
{
string Name { get; }
string Quote(string identifier);
string ParameterJsonCast(string parameterName);
string ParameterVectorCast(string parameterName, DbVectorAttribute vector);
string CastToText(string expressionSql);
string CurrentSchemaSql { get; }
string SetSchemaSql(string schema);
string BuildTableName(string schema, string tableName);
string BuildIndexName(string tableName, IEnumerable<string> columns);
string BuildCreateTableSql(string schema, string tableName);
string BuildTableExistsSql();
string BuildAddColumnSql(string schema, string tableName, string columnName, string sqlType);
string BuildCreateIndexSql(string schema, string tableName, string indexName, IEnumerable<string> columns, bool unique);
string BuildConstraintExistsSql();
string BuildIndexExistsSql();
string BuildSqlType(Type clrType, PropertyInfo prop);
}
public static class SqlDialects
{
public static readonly ISqlDialect Postgres = new PostgresDialect();
public static readonly ISqlDialect SqlServer = new SqlServerDialect();
public static ISqlDialect Detect(IDbConnection db)
{
var typeName = db.GetType().FullName ?? db.GetType().Name;
if (typeName.Contains("Npgsql", StringComparison.OrdinalIgnoreCase))
return Postgres;
if (typeName.Contains("SqlClient", StringComparison.OrdinalIgnoreCase) ||
typeName.Contains("Microsoft.Data.SqlClient", StringComparison.OrdinalIgnoreCase) ||
typeName.Contains("System.Data.SqlClient", StringComparison.OrdinalIgnoreCase))
return SqlServer;
return Postgres;
}
}
internal sealed class PostgresDialect : ISqlDialect
{
public string Name => "postgres";
public string Quote(string identifier) => $"\"{identifier}\"";
public string ParameterJsonCast(string parameterName) => $"{parameterName}::jsonb";
public string ParameterVectorCast(string parameterName, DbVectorAttribute vector)
{
var sqlType = ResolveVectorSqlType(vector);
var castTarget = sqlType.Split('(', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)[0];
return $"{parameterName}::{castTarget}";
}
public string CastToText(string expressionSql) => $"CAST({expressionSql} AS TEXT)";
public string CurrentSchemaSql => "SELECT current_schema();";
public string SetSchemaSql(string schema) => $"SET search_path TO {Quote(schema)};";
public string BuildTableName(string schema, string tableName) => $"{Quote(schema)}.{Quote(tableName)}";
public string BuildIndexName(string tableName, IEnumerable<string> columns) => $"idx_{tableName}_{string.Join("_", columns)}";
public string BuildCreateTableSql(string schema, string tableName)
{
var full = BuildTableName(schema, tableName);
return $@"
CREATE TABLE {full} (
{Quote("id")} UUID PRIMARY KEY DEFAULT gen_random_uuid(),
{Quote("created_at")} TIMESTAMPTZ NOT NULL DEFAULT NOW(),
{Quote("updated_at")} TIMESTAMPTZ NOT NULL DEFAULT NOW()
);";
}
public string BuildTableExistsSql() => @"
SELECT EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_schema = @schema
AND table_name = @table
);";
public string BuildAddColumnSql(string schema, string tableName, string columnName, string sqlType)
{
var full = BuildTableName(schema, tableName);
return $@"ALTER TABLE {full} ADD COLUMN {Quote(columnName)} {sqlType};";
}
public string BuildCreateIndexSql(string schema, string tableName, string indexName, IEnumerable<string> columns, bool unique)
{
var full = BuildTableName(schema, tableName);
var cols = string.Join(", ", columns.Select(Quote));
var uniqueSql = unique ? "UNIQUE " : string.Empty;
return $@"CREATE {uniqueSql}INDEX IF NOT EXISTS {Quote(indexName)} ON {full} ({cols});";
}
public string BuildConstraintExistsSql() => @"
SELECT EXISTS (
SELECT 1
FROM information_schema.table_constraints
WHERE table_schema = @schema
AND table_name = @table
AND constraint_name = @constraint
);";
public string BuildIndexExistsSql() => @"
SELECT EXISTS (
SELECT 1
FROM pg_indexes
WHERE schemaname = @schema
AND tablename = @table
AND indexname = @index
);";
public string BuildSqlType(Type clrType, PropertyInfo prop)
{
if (prop.GetCustomAttribute<JsonColumnAttribute>() != null)
return "JSONB";
if (prop.GetCustomAttribute<DbSearchVectorAttribute>() != null)
return "TSVECTOR";
var vector = prop.GetCustomAttribute<DbVectorAttribute>();
if (vector != null)
return ResolveVectorSqlType(vector);
var type = Nullable.GetUnderlyingType(clrType) ?? clrType;
if (type.IsEnum)
return "TEXT";
if (type == typeof(string))
{
var maxLength = prop.GetCustomAttribute<MaxLengthAttribute>()?.Length;
if (maxLength.HasValue && maxLength.Value > 0)
return $"VARCHAR({maxLength.Value})";
return "TEXT";
}
if (type == typeof(int)) return "INTEGER";
if (type == typeof(long)) return "BIGINT";
if (type == typeof(short)) return "SMALLINT";
if (type == typeof(byte)) return "SMALLINT";
if (type == typeof(decimal)) return "NUMERIC";
if (type == typeof(double)) return "DOUBLE PRECISION";
if (type == typeof(float)) return "REAL";
if (type == typeof(bool)) return "BOOLEAN";
if (type == typeof(DateTime)) return "TIMESTAMP WITH TIME ZONE";
if (type == typeof(DateTimeOffset)) return "TIMESTAMP WITH TIME ZONE";
if (type == typeof(TimeSpan)) return "INTERVAL";
if (type == typeof(DateOnly)) return "DATE";
if (type == typeof(TimeOnly)) return "TIME";
if (type == typeof(Guid)) return "UUID";
return "TEXT";
}
private static string ResolveVectorSqlType(DbVectorAttribute vector)
{
if (!string.IsNullOrWhiteSpace(vector.SqlType))
return vector.SqlType!;
return vector.Dimensions.HasValue
? $"VECTOR({vector.Dimensions.Value})"
: "VECTOR";
}
}
internal sealed class SqlServerDialect : ISqlDialect
{
public string Name => "sqlserver";
public string Quote(string identifier) => $"[{identifier}]";
public string ParameterJsonCast(string parameterName) => parameterName;
public string ParameterVectorCast(string parameterName, DbVectorAttribute vector) => parameterName;
public string CastToText(string expressionSql) => $"CAST({expressionSql} AS NVARCHAR(MAX))";
public string CurrentSchemaSql => "SELECT SCHEMA_NAME();";
public string SetSchemaSql(string schema) => string.Empty;
public string BuildTableName(string schema, string tableName) => $"{Quote(schema)}.{Quote(tableName)}";
public string BuildIndexName(string tableName, IEnumerable<string> columns) => $"idx_{tableName}_{string.Join("_", columns)}";
public string BuildCreateTableSql(string schema, string tableName)
{
var full = BuildTableName(schema, tableName);
return $@"
CREATE TABLE {full} (
{Quote("id")} UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
{Quote("created_at")} DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
{Quote("updated_at")} DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME()
);";
}
public string BuildTableExistsSql() => @"
SELECT CASE WHEN EXISTS (
SELECT 1 FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = @schema
AND TABLE_NAME = @table
) THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END;";
public string BuildAddColumnSql(string schema, string tableName, string columnName, string sqlType)
{
var full = BuildTableName(schema, tableName);
return $@"ALTER TABLE {full} ADD {Quote(columnName)} {sqlType};";
}
public string BuildCreateIndexSql(string schema, string tableName, string indexName, IEnumerable<string> columns, bool unique)
{
var full = BuildTableName(schema, tableName);
var cols = string.Join(", ", columns.Select(Quote));
var uniqueSql = unique ? "UNIQUE " : string.Empty;
return $@"CREATE {uniqueSql}INDEX {Quote(indexName)} ON {full} ({cols});";
}
public string BuildConstraintExistsSql() => @"
SELECT CASE WHEN EXISTS (
SELECT 1
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = @schema
AND TABLE_NAME = @table
AND CONSTRAINT_NAME = @constraint
) THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END;";
public string BuildIndexExistsSql() => @"
SELECT CASE WHEN EXISTS (
SELECT 1
FROM sys.indexes i
JOIN sys.tables t ON i.object_id = t.object_id
JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE s.name = @schema
AND t.name = @table
AND i.name = @index
) THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END;";
public string BuildSqlType(Type clrType, PropertyInfo prop)
{
if (prop.GetCustomAttribute<JsonColumnAttribute>() != null)
return "NVARCHAR(MAX)";
if (prop.GetCustomAttribute<DbSearchVectorAttribute>() != null)
return "NVARCHAR(MAX)";
var vector = prop.GetCustomAttribute<DbVectorAttribute>();
if (vector != null)
return !string.IsNullOrWhiteSpace(vector.SqlType)
? vector.SqlType!
: "NVARCHAR(MAX)";
var type = Nullable.GetUnderlyingType(clrType) ?? clrType;
if (type.IsEnum)
return "NVARCHAR(64)";
if (type == typeof(string))
{
var maxLength = prop.GetCustomAttribute<MaxLengthAttribute>()?.Length;
if (maxLength.HasValue && maxLength.Value > 0)
return $"NVARCHAR({maxLength.Value})";
return "NVARCHAR(MAX)";
}
if (type == typeof(int)) return "INT";
if (type == typeof(long)) return "BIGINT";
if (type == typeof(short)) return "SMALLINT";
if (type == typeof(byte)) return "TINYINT";
if (type == typeof(decimal)) return "DECIMAL(18, 6)";
if (type == typeof(double)) return "FLOAT";
if (type == typeof(float)) return "REAL";
if (type == typeof(bool)) return "BIT";
if (type == typeof(DateTime)) return "DATETIME2";
if (type == typeof(DateTimeOffset)) return "DATETIMEOFFSET";
if (type == typeof(TimeSpan)) return "TIME";
if (type == typeof(DateOnly)) return "DATE";
if (type == typeof(TimeOnly)) return "TIME";
if (type == typeof(Guid)) return "UNIQUEIDENTIFIER";
return "NVARCHAR(MAX)";
}
}