Skip to content

Commit

Permalink
Merge pull request #302 from MediaPortal/MP1-5215-TV_Server_and_Maria…
Browse files Browse the repository at this point in the history
…DB_-_DBNull_problem

MP1-5215: Add MySQL.Interceptor and Read TV Server dependencies in SetupTV
  • Loading branch information
andrewjswan authored Apr 9, 2024
2 parents 0b8e4c9 + 0ab17ad commit bdc5d65
Show file tree
Hide file tree
Showing 19 changed files with 334 additions and 7 deletions.
6 changes: 6 additions & 0 deletions Common-MP-TVE3/External/Gentle.NET/Source/Gentle.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gentle.Provider.MySQL", "Ge
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gentle.Provider.SQLServerCE", "Gentle.Provider.SQLServerCE\Gentle.Provider.SQLServerCE.csproj", "{E3149E66-2BF2-4441-8A72-CDEFDE97C4D1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gentle.Provider.MySQL.Interceptor", "Gentle.Provider.MySQL.Interceptor\Gentle.Provider.MySQL.Interceptor.csproj", "{D8D01EDD-FE20-4F51-9CE1-B5C50244D6CA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -144,6 +146,10 @@ Global
{E3149E66-2BF2-4441-8A72-CDEFDE97C4D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E3149E66-2BF2-4441-8A72-CDEFDE97C4D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E3149E66-2BF2-4441-8A72-CDEFDE97C4D1}.Release|Any CPU.Build.0 = Release|Any CPU
{D8D01EDD-FE20-4F51-9CE1-B5C50244D6CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D8D01EDD-FE20-4F51-9CE1-B5C50244D6CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8D01EDD-FE20-4F51-9CE1-B5C50244D6CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8D01EDD-FE20-4F51-9CE1-B5C50244D6CA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#region Copyright (C) 2024 Team MediaPortal

// Copyright (C) 2024 Team MediaPortal
// http://www.team-mediaportal.com
//
// MediaPortal is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// MediaPortal is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MediaPortal. If not, see <http://www.gnu.org/licenses/>.

#endregion

using System.Reflection;

// This file contains only the assemlbly attributes specific to this assembly.
// The remaining attributes are defined in the shared SolutionInfo.cs file.

[assembly: AssemblyTitle( "MySQL interceptor for Gentle.NET" )]
[assembly: AssemblyDescription( "This inerceptor uses the MySQL .NET Connector library from MySQL AB." )]
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<ProjectType>Local</ProjectType>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{D8D01EDD-FE20-4F51-9CE1-B5C50244D6CA}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ApplicationIcon>
</ApplicationIcon>
<AssemblyKeyContainerName>
</AssemblyKeyContainerName>
<AssemblyName>Gentle.Provider.MySQL.Interceptor</AssemblyName>
<AssemblyOriginatorKeyFile>Gentle.snk</AssemblyOriginatorKeyFile>
<DefaultClientScript>JScript</DefaultClientScript>
<DefaultHTMLPageLayout>Grid</DefaultHTMLPageLayout>
<DefaultTargetSchema>IE50</DefaultTargetSchema>
<DelaySign>false</DelaySign>
<OutputType>Library</OutputType>
<RootNamespace>Gentle.Provider.MySQL.Interceptor</RootNamespace>
<RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent>
<StartupObject>
</StartupObject>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>3.5</OldToolsVersion>
<SignAssembly>true</SignAssembly>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<OutputPath>bin\Debug\</OutputPath>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<BaseAddress>285212672</BaseAddress>
<CheckForOverflowUnderflow>false</CheckForOverflowUnderflow>
<ConfigurationOverrideFile>
</ConfigurationOverrideFile>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DocumentationFile>
</DocumentationFile>
<DebugSymbols>true</DebugSymbols>
<FileAlignment>4096</FileAlignment>
<NoStdLib>false</NoStdLib>
<NoWarn>1591;1701;1702</NoWarn>
<Optimize>false</Optimize>
<RegisterForComInterop>false</RegisterForComInterop>
<RemoveIntegerChecks>false</RemoveIntegerChecks>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<WarningLevel>4</WarningLevel>
<DebugType>full</DebugType>
<ErrorReport>prompt</ErrorReport>
<WarningsAsErrors>;NU1605</WarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<OutputPath>bin\Release\</OutputPath>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<BaseAddress>285212672</BaseAddress>
<CheckForOverflowUnderflow>false</CheckForOverflowUnderflow>
<ConfigurationOverrideFile>
</ConfigurationOverrideFile>
<DefineConstants>TRACE</DefineConstants>
<DocumentationFile>
</DocumentationFile>
<DebugSymbols>false</DebugSymbols>
<FileAlignment>4096</FileAlignment>
<NoStdLib>false</NoStdLib>
<NoWarn>1591;1701;1702</NoWarn>
<Optimize>true</Optimize>
<RegisterForComInterop>false</RegisterForComInterop>
<RemoveIntegerChecks>false</RemoveIntegerChecks>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<WarningLevel>4</WarningLevel>
<DebugType>none</DebugType>
<ErrorReport>prompt</ErrorReport>
<WarningsAsErrors>;NU1605</WarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<Reference Include="MySql.Data">
<HintPath>..\Gentle.Provider.MySQL\lib\MySql.Data.dll</HintPath>
</Reference>
<Reference Include="System">
<Name>System</Name>
</Reference>
<Reference Include="System.Data">
<Name>System.Data</Name>
</Reference>
<Reference Include="System.Xml">
<Name>System.XML</Name>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\SolutionInfo.cs">
<Link>SolutionInfo.cs</Link>
<SubType>Code</SubType>
</Compile>
<Compile Include="AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Interceptor.cs" />
<Compile Include="Utf8mb3.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Gentle.snk" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>
</PreBuildEvent>
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#region Copyright (C) 2024 Team MediaPortal

// Copyright (C) 2024 Team MediaPortal
// http://www.team-mediaportal.com
//
// MediaPortal is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// MediaPortal is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MediaPortal. If not, see <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.Data;
using MySql.Data.MySqlClient;

namespace Gentle.Provider.MySQL.Interceptor
{
/// <summary>
/// Filters out collations with NULL id (e.g. UCA-14.0.0) from SHOW COLLATION command
/// </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;
}

MySqlCommand command = ActiveConnection.CreateCommand();

command.CommandText = "SHOW COLLATION WHERE id IS NOT NULL";
returnValue = command.ExecuteReader(behavior);

return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Gentle.Provider.MySQL.Interceptor

## How it works

Library modifies `SHOW COLLATION` query using `BaseCommandInterceptor` to prevent fetching collations with NULL Id's.
There is also small piece of code which append `utf8mb3` charset to MySql.Data's internal mapping Dictionary allowing
to read fields with utf8mb3 collations.

## Usage

1. Append following line to your connection string

`;commandinterceptors=Gentle.Provider.MySQL.Interceptor.Interceptor,Gentle.Provider.MySQL.Interceptor`

2. Call `Utf8mb3.Enable()` before opening first MySql connection.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#region Copyright (C) 2024 Team MediaPortal

// Copyright (C) 2024 Team MediaPortal
// http://www.team-mediaportal.com
//
// MediaPortal is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// MediaPortal is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MediaPortal. If not, see <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.Collections;
using MySql.Data.MySqlClient;

namespace Gentle.Provider.MySQL.Interceptor
{
// ReSharper disable once InconsistentNaming
public static class Utf8mb3
{
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
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Gentle.Provider.MySQL.Interceptor\Gentle.Provider.MySQL.Interceptor.csproj">
<Project>{d8d01edd-fe20-4f51-9ce1-b5c50244d6ca}</Project>
<Name>Gentle.Provider.MySQL.Interceptor</Name>
</ProjectReference>
<Reference Include="MySql.Data, Version=6.6.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
</Reference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Data;
using Gentle.Common;
using Gentle.Framework;
using Gentle.Provider.MySQL.Interceptor;
using MySql.Data.MySqlClient;

namespace Gentle.Provider.MySQL
Expand Down Expand Up @@ -83,6 +84,7 @@ public override IDbConnection GetConnection()
try
{
IDbConnection dbc = new MySqlConnection( ConnectionString );
Utf8mb3.Enable();
dbc.Open();
Check.VerifyEquals( dbc.State, ConnectionState.Open, Error.NoNewConnection );
return dbc;
Expand Down
8 changes: 6 additions & 2 deletions TvEngine3/TVLibrary/Setup/setup.nsi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region Copyright (C) 2005-2023 Team MediaPortal
#region Copyright (C) 2005-2024 Team MediaPortal
/*
// Copyright (C) 2005-2023 Team MediaPortal
// Copyright (C) 2005-2024 Team MediaPortal
// http://www.team-mediaportal.com
//
// MediaPortal is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -541,6 +541,7 @@ ${MementoSection} "MediaPortal TV Server" SecServer
File "${git_TVServer}\TVDatabase\references\Gentle.Common.DLL"
File "${git_TVServer}\TVDatabase\references\Gentle.Framework.DLL"
File "${git_TVServer}\TVDatabase\references\Gentle.Provider.MySQL.dll"
File "${git_TVServer}\TVDatabase\references\Gentle.Provider.MySQL.Interceptor.dll"
File "${git_TVServer}\TVDatabase\references\MySql.Data.dll"
File "${git_TVServer}\TVDatabase\references\Gentle.Provider.SQLServer.dll"
File "${git_TVServer}\TVDatabase\references\Gentle.Provider.SQLite.dll"
Expand Down Expand Up @@ -792,6 +793,7 @@ ${MementoSectionEnd}
Delete "$INSTDIR\Gentle.Common.DLL"
Delete "$INSTDIR\Gentle.Framework.DLL"
Delete "$INSTDIR\Gentle.Provider.MySQL.dll"
Delete "$INSTDIR\Gentle.Provider.MySQL.Interceptor.dll"
Delete "$INSTDIR\MySql.Data.dll"
Delete "$INSTDIR\Gentle.Provider.SQLServer.dll"
Delete "$INSTDIR\Gentle.Provider.SQLite.dll"
Expand Down Expand Up @@ -881,6 +883,7 @@ ${MementoSection} "MediaPortal TV Client plugin" SecClient
File "${git_TVServer}\TVDatabase\references\Gentle.Common.DLL"
File "${git_TVServer}\TVDatabase\references\Gentle.Framework.DLL"
File "${git_TVServer}\TVDatabase\references\Gentle.Provider.MySQL.dll"
File "${git_TVServer}\TVDatabase\references\Gentle.Provider.MySQL.Interceptor.dll"
File "${git_TVServer}\TVDatabase\references\MySql.Data.dll"
File "${git_TVServer}\TVDatabase\references\Gentle.Provider.SQLServer.dll"
File "${git_TVServer}\TVDatabase\references\Gentle.Provider.SQLite.dll"
Expand Down Expand Up @@ -939,6 +942,7 @@ ${MementoSectionEnd}
Delete "$MPdir.Base\Gentle.Common.DLL"
Delete "$MPdir.Base\Gentle.Framework.DLL"
Delete "$MPdir.Base\Gentle.Provider.MySQL.dll"
Delete "$MPdir.Base\Gentle.Provider.MySQL.Interceptor.dll"
Delete "$MPdir.Base\MySql.Data.dll"
Delete "$MPdir.Base\Gentle.Provider.SQLServer.dll"
Delete "$MPdir.Base\Gentle.Provider.SQLite.dll"
Expand Down
Loading

0 comments on commit bdc5d65

Please sign in to comment.