forked from mikependon/RepoDB
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMySqlConnectorDbHelper.cs
More file actions
264 lines (227 loc) · 9.2 KB
/
Copy pathMySqlConnectorDbHelper.cs
File metadata and controls
264 lines (227 loc) · 9.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
#nullable enable
using System.Data;
using System.Data.Common;
using System.Text.RegularExpressions;
using MySqlConnector;
using RepoDb.DbSettings;
using RepoDb.Enumerations;
using RepoDb.Extensions;
using RepoDb.Interfaces;
using RepoDb.Resolvers;
namespace RepoDb.DbHelpers;
/// <summary>
/// A helper class for database specially for the direct access. This class is only meant for MySql.
/// </summary>
public sealed class MySqlConnectorDbHelper : BaseDbHelper
{
private readonly IDbSetting m_dbSetting = DbSettingMapper.Get<MySqlConnection>() ?? throw new InvalidOperationException();
/// <summary>
/// Creates a new instance of <see cref="MySqlConnectorDbHelper"/> class.
/// </summary>
public MySqlConnectorDbHelper()
: this(new MySqlConnectorDbTypeNameToClientTypeResolver())
{ }
/// <summary>
/// Creates a new instance of <see cref="MySqlConnectorDbHelper"/> class.
/// </summary>
/// <param name="dbTypeResolver">The type resolver to be used.</param>
public MySqlConnectorDbHelper(IResolver<string, Type> dbTypeResolver)
: base(dbTypeResolver)
{
}
#region Helpers
/// <summary>
///
/// </summary>
/// <returns></returns>
private static string GetCommandText()
{
return $@"SELECT COLUMN_NAME AS ColumnName
, CASE WHEN COLUMN_KEY = 'PRI' THEN 1 ELSE 0 END AS IsPrimary
, CASE WHEN EXTRA LIKE '%auto_increment%' THEN 1 ELSE 0 END AS IsIdentity
, CASE WHEN IS_NULLABLE = 'YES' THEN 1 ELSE 0 END AS IsNullable
, DATA_TYPE AS ColumnType /*COLUMN_TYPE AS ColumnType*/
, CHARACTER_MAXIMUM_LENGTH AS Size
, COALESCE(NUMERIC_PRECISION, DATETIME_PRECISION) AS `Precision`
, NUMERIC_SCALE AS Scale
, DATA_TYPE AS DatabaseType
, CASE WHEN COLUMN_DEFAULT IS NOT NULL THEN 1 ELSE 0 END AS HasDefaultValue
, CASE
WHEN EXTRA LIKE '%VIRTUAL%' THEN 1
WHEN EXTRA LIKE '%STORED%' THEN 1
WHEN EXTRA LIKE '%ON UPDATE%' THEN 1
ELSE 0
END AS IsComputed
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = @TableSchema
AND TABLE_NAME = @TableName
ORDER BY ORDINAL_POSITION;";
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private static readonly HashSet<string> BlobTypes = new([
"blob",
"blobasarray",
"binary",
"longtext",
"mediumtext",
"longblob",
"mediumblob",
"tinyblob",
"varbinary"
], StringComparer.OrdinalIgnoreCase);
/// <summary>
///
/// </summary>
/// <param name="reader"></param>
/// <returns></returns>
private DbField ReaderToDbField(DbDataReader reader)
{
var columnType = reader.GetString(4);
int? size = BlobTypes.Contains(columnType) ? null : reader.IsDBNull(5) ? (int?)null : reader.GetInt32(5);
return new DbField(reader.GetString(0),
reader.GetBoolean(1),
reader.GetBoolean(2),
reader.GetBoolean(3),
DbTypeResolver.Resolve(columnType)!,
size,
reader.IsDBNull(6) ? (byte?)null : byte.Parse(reader.GetInt32(6).ToString()),
reader.IsDBNull(7) ? (byte?)null : byte.Parse(reader.GetInt32(7).ToString()),
reader.GetString(8),
reader.GetBoolean(9),
reader.GetBoolean(10),
"MYSQLC");
}
#endregion
#region Methods
#region GetFields
/// <summary>
/// Gets the list of <see cref="DbField"/> of the table.
/// </summary>
/// <param name="connection">The instance of the connection object.</param>
/// <param name="tableName">The name of the target table.</param>
/// <param name="transaction">The transaction object that is currently in used.</param>
/// <returns>A list of <see cref="DbField"/> of the target table.</returns>
public override DbFieldCollection GetFields(IDbConnection connection,
string tableName,
IDbTransaction? transaction = null)
{
// Variables
var commandText = GetCommandText();
var param = new
{
TableSchema = connection.Database,
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting)
};
// Iterate and extract
using var reader = (DbDataReader)connection.ExecuteReader(commandText, param, transaction: transaction);
var dbFields = new List<DbField>();
// Iterate the list of the fields
while (reader.Read())
{
dbFields.Add(ReaderToDbField(reader));
}
// Return the list of fields
return new(dbFields);
}
/// <summary>
/// Gets the list of <see cref="DbField"/> of the table in an asynchronous way.
/// </summary>
/// <param name="connection">The instance of the connection object.</param>
/// <param name="tableName">The name of the target table.</param>
/// <param name="transaction">The transaction object that is currently in used.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
/// <returns>A list of <see cref="DbField"/> of the target table.</returns>
public override async ValueTask<DbFieldCollection> GetFieldsAsync(IDbConnection connection,
string tableName,
IDbTransaction? transaction = null,
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
// Variables
var commandText = GetCommandText();
var param = new
{
TableSchema = connection.Database,
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting).AsUnquoted(m_dbSetting)
};
// Iterate and extract
using var reader = (DbDataReader)await connection.ExecuteReaderAsync(commandText, param, transaction: transaction,
cancellationToken: cancellationToken);
var dbFields = new List<DbField>();
// Iterate the list of the fields
while (await reader.ReadAsync(cancellationToken))
{
dbFields.Add(ReaderToDbField(reader));
}
// Return the list of fields
return new(dbFields);
}
#endregion
#region GetSchemaObjects
private const string GetSchemaQuery = @"
SELECT
table_type AS `Type`,
table_name AS `Name`,
table_schema AS `Schema`
FROM information_schema.tables
WHERE table_schema = DATABASE()";
public override IEnumerable<DbSchemaObject> GetSchemaObjects(IDbConnection connection, IDbTransaction? transaction = null)
{
return connection.ExecuteQuery<(string Type, string Name, string Schema)>(GetSchemaQuery, transaction)
.Select(MapSchemaQueryResult);
}
public override async ValueTask<IEnumerable<DbSchemaObject>> GetSchemaObjectsAsync(IDbConnection connection, IDbTransaction? transaction = null, CancellationToken cancellationToken = default)
{
var results = await connection.ExecuteQueryAsync<(string Type, string Name, string Schema)>(GetSchemaQuery, transaction, cancellationToken: cancellationToken);
return results.Select(MapSchemaQueryResult);
}
private static DbSchemaObject MapSchemaQueryResult((string Type, string Name, string Schema) r) =>
new DbSchemaObject
{
Type = r.Type switch
{
"BASE TABLE" => DbSchemaType.Table,
"VIEW" => DbSchemaType.View,
_ => throw new NotSupportedException($"Unsupported schema object type: {r.Type}")
},
Name = r.Name,
Schema = r.Schema
};
#endregion
#endregion
private const string MySqlRuntimeInfoQuery = @"
SELECT VERSION() AS Version;
SHOW VARIABLES LIKE 'version_comment';
";
public override DbRuntimeSetting GetDbConnectionRuntimeInformation(IDbConnection connection, IDbTransaction? transaction)
{
using var rdr = (MySqlDataReader)connection.ExecuteReader(MySqlRuntimeInfoQuery, transaction: transaction);
string? versionString = null;
string? versionComment = null;
if (rdr.Read())
{
versionString = rdr.GetString(0);
}
if (rdr.NextResult() && rdr.Read())
{
versionComment = rdr.GetString(1); // second column = 'Value' from SHOW VARIABLES LIKE ...
}
var engineName = versionComment?.Contains("MariaDB", StringComparison.OrdinalIgnoreCase) == true
? "MariaDB"
: "MySQL";
var versionMatch = Regex.Match(versionString ?? "", @"\d+(\.\d+)+");
var parsedVersion = versionMatch.Success
? Version.Parse(versionMatch.Value)
: new Version(0, 0);
return new()
{
EngineName = engineName,
EngineVersion = parsedVersion,
//CompatibilityVersion = null, // Not really applicable for MySQL
ParameterTypeMap = null // No TVPs
};
}
}