Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for mariaDB version 10.10 and later. #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Libraries/Interceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Data;
using MySql.Data.MySqlClient;

namespace CommandApp
{
/// <summary>
/// Filters out collations with NULL id (e.g. UCA-14.0.0) from SHOW COLLATION command
/// Credit to Jeffraska
/// https://github.com/jeffraska/Jf.MySql.Data.Collations
/// </summary>
public sealed class Interceptor : BaseCommandInterceptor
{
public override bool ExecuteReader(string sql, CommandBehavior behavior, ref MySqlDataReader returnValue)
{
if (!sql.Equals("SHOW COLLATION", StringComparison.OrdinalIgnoreCase))
{
return false;
}

using var command = ActiveConnection.CreateCommand();
command.CommandText = "SHOW COLLATION WHERE id IS NOT NULL";
returnValue = command.ExecuteReader(behavior);
return true;
}
}
}
3 changes: 3 additions & 0 deletions src/Libraries/MySql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Data;
using System.Threading;
using CommandApp;

namespace Oxide.Core.MySql.Libraries
{
Expand Down Expand Up @@ -225,6 +226,8 @@ public Connection OpenDb(string host, int port, string database, string user, st

public Connection OpenDb(string conStr, Plugin plugin, bool persistent = false)
{
Utf8mb3.Enable();
conStr += ";commandinterceptors=CommandApp.Interceptor," + typeof(CommandApp.Interceptor).Assembly.FullName;
Dictionary<string, Connection> connections;
if (!_connections.TryGetValue(plugin?.Name ?? "null", out connections))
{
Expand Down
49 changes: 49 additions & 0 deletions src/Libraries/Utf8mb3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections;
using MySql.Data.MySqlClient;

namespace CommandApp
{
// ReSharper disable once InconsistentNaming
/// Credit to Jeffraska
/// https://github.com/jeffraska/Jf.MySql.Data.Collations
///
public static class Utf8mb3
{
// verified that starting version 6.10.0, CharSetMap.mapping was changed to CharSetMap._mapping
// code will still work if MySql.Data.dll is upgraded
private static readonly Version NewFieldNamingVersion = new Version(6, 10, 0);

public static void Enable()
{
// Add internal mapping of database utf8mb3 charset to .NET framework's UTF-8 encoding
var assembly = System.Reflection.Assembly.GetAssembly(typeof(MySqlConnection));
var connectorVersion = assembly.GetName().Version;

var mappingFieldName = connectorVersion >= NewFieldNamingVersion ? "_mapping" : "mapping";

var mappingField = assembly
.GetType("MySql.Data.MySqlClient.CharSetMap").GetField(mappingFieldName,
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField |
System.Reflection.BindingFlags.Static);

if (mappingField != null)
{
var mappingDictionary = (IDictionary)mappingField.GetValue(null);
var utf8Mapping = mappingDictionary["utf8"];

if (utf8Mapping != null)
{
try
{
mappingDictionary.Add("utf8mb3", utf8Mapping);
}
catch (ArgumentException)
{
// Item already exist
}
}
}
}
}
}
3 changes: 3 additions & 0 deletions src/Oxide.MySql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<NoWarn>NU1701</NoWarn>
</PropertyGroup>
<PropertyGroup>
<LangVersion>9.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Oxide.Core" Version="2.0.*" />
<Reference Include="MySql.Data">
Expand Down