generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMySqlDatabase.cs
78 lines (67 loc) · 3.77 KB
/
MySqlDatabase.cs
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
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Mapping.Converters;
using CoreEx.Results;
using Microsoft.Extensions.Logging;
using MySql.Data.MySqlClient;
using System;
using System.Linq;
using System.Data.Common;
namespace CoreEx.Database.MySql
{
/// <summary>
/// Provides <see href="https://dev.mysql.com/">MySQL</see> database access functionality.
/// </summary>
/// <param name="create">The function to create the <see cref="MySqlConnection"/>.</param>
/// <param name="logger">The optional <see cref="ILogger"/>.</param>
/// <param name="invoker">The optional <see cref="DatabaseInvoker"/>.</param>
public class MySqlDatabase(Func<MySqlConnection> create, ILogger<MySqlDatabase>? logger = null, DatabaseInvoker? invoker = null) : Database<MySqlConnection>(create, MySqlClientFactory.Instance, logger, invoker)
{
/// <summary>
/// Gets the default <see cref="DuplicateErrorNumbers"/>.
/// </summary>
/// <remarks>See <see href="https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html"/>.</remarks>
public static int[] DefaultDuplicateErrorNumbers { get; } = [1022, 1062, 1088, 1291, 1586, 1859];
/// <inheritdoc/>
public override IConverter RowVersionConverter => EncodedStringToDateTimeConverter.Default;
/// <summary>
/// Indicates whether to transform the <see cref="MySqlException"/> into an <see cref="Abstractions.IExtendedException"/> equivalent based on the <see cref="MySqlException.Number"/>.
/// </summary>
/// <remarks>Transforms and throws the <see cref="Abstractions.IExtendedException"/> equivalent from the <see cref="MySqlException"/> known list.</remarks>
public bool ThrowTransformedException { get; set; } = true;
/// <summary>
/// Indicates whether to check the <see cref="DuplicateErrorNumbers"/> when catching the <see cref="MySqlException"/>.
/// </summary>
public bool CheckDuplicateErrorNumbers { get; set; } = true;
/// <summary>
/// Gets or sets the list of known <see cref="MySqlException.Number"/> values that are considered a duplicate error.
/// </summary>
/// <remarks>Overrides the <see cref="DefaultDuplicateErrorNumbers"/>.</remarks>
public int[]? DuplicateErrorNumbers { get; set; }
/// <inheritdoc/>
protected override Result? OnDbException(DbException dbex)
{
if (ThrowTransformedException && dbex is MySqlException sex)
{
var msg = sex.Message?.TrimEnd();
if (string.IsNullOrEmpty(msg))
msg = null;
switch (sex.Number)
{
case 56001: return Result.Fail(new ValidationException(msg, sex));
case 56002: return Result.Fail(new BusinessException(msg, sex));
case 56003: return Result.Fail(new AuthorizationException(msg, sex));
case 56004: return Result.Fail(new ConcurrencyException(msg, sex));
case 56005: return Result.Fail(new NotFoundException(msg, sex));
case 56006: return Result.Fail(new ConflictException(msg, sex));
case 56007: return Result.Fail(new DuplicateException(msg, sex));
case 56010: return Result.Fail(new DataConsistencyException(msg, sex));
default:
if (CheckDuplicateErrorNumbers && (DuplicateErrorNumbers ?? DefaultDuplicateErrorNumbers).Contains(sex.Number))
return Result.Fail(new DuplicateException(null, sex));
break;
}
}
return base.OnDbException(dbex);
}
}
}