-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrm.Utilities.cs
More file actions
42 lines (37 loc) · 1.55 KB
/
Orm.Utilities.cs
File metadata and controls
42 lines (37 loc) · 1.55 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
using System.Data;
using Dapper;
using SwiftSQL.Dialects;
namespace SwiftSQL;
public static partial class Orm
{
public static async Task<bool> TableExistsAsync(
IDbConnection connection,
ISqlDialect dialect,
string tableName,
IDbTransaction? transaction = null,
CancellationToken cancellationToken = default)
{
var sql = dialect.Name switch
{
"SQLite" => "SELECT name FROM sqlite_master WHERE type = 'table' AND name = @name;",
"MySQL" => "SELECT table_name FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = @name;",
"PostgreSQL" => "SELECT tablename FROM pg_tables WHERE schemaname = current_schema() AND tablename = @name;",
_ => "SELECT 1 WHERE 1 = 0;"
};
var result = await connection.ExecuteScalarAsync<string?>(
new CommandDefinition(sql, new { name = tableName }, transaction, cancellationToken: cancellationToken)
);
return result is not null;
}
public static Task RenameTableAsync(
IDbConnection connection,
ISqlDialect dialect,
string fromTableName,
string toTableName,
IDbTransaction? transaction = null,
CancellationToken cancellationToken = default)
{
var sql = $"ALTER TABLE {dialect.QuoteIdentifier(fromTableName)} RENAME TO {dialect.QuoteIdentifier(toTableName)};";
return connection.ExecuteAsync(new CommandDefinition(sql, transaction: transaction, cancellationToken: cancellationToken));
}
}