Pustalorc.MySqlConnectorWrapper 2.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Pustalorc.MySqlConnectorWrapper --version 2.2.0                
NuGet\Install-Package Pustalorc.MySqlConnectorWrapper -Version 2.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Pustalorc.MySqlConnectorWrapper" Version="2.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Pustalorc.MySqlConnectorWrapper --version 2.2.0                
#r "nuget: Pustalorc.MySqlConnectorWrapper, 2.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Pustalorc.MySqlConnectorWrapper as a Cake Addin
#addin nuget:?package=Pustalorc.MySqlConnectorWrapper&version=2.2.0

// Install Pustalorc.MySqlConnectorWrapper as a Cake Tool
#tool nuget:?package=Pustalorc.MySqlConnectorWrapper&version=2.2.0                

Before you begin, please make sure that your current solution has installed the nuget package of this library.


Configuration Setup

Firstly, you need a configuration for the connector to use. Without a configuration, the connector will not know what server to connect to, which database to use, which user to use, etc.

You will want to create a class and have it inherit the Interface IConnectorConfiguration. For example:

public class DatabaseConfig : IConnectorConfiguration
{
    public string DatabaseAddress => "localhost";
    public ushort DatabasePort => 3306;
    public string DatabaseUsername => "root";
    public string DatabasePassword => "myPassword";
    public string DatabaseName => "database";
    public string ConnectionStringExtras => "";
    public bool UseCache => true;
    public double CacheRefreshRequestInterval => 1250;
    public ulong CacheSize => 15;
    public string TableName => "test";
}

Note: These are { get; } only properties, but you can make them { get; set; } if you wish to be able to modify them mid run-time, or if you wish to serialise them into a configuration file.

Example configuration #2.


Connector setup

Once you have a default configuration and you think you are ready to move on, you can then start creating your own Database class.

This class will serve as another layer of wrapping, one that will check data against anything stored, or simply just request query executions.

public class Database : ConnectorWrapper<DatabaseConfig>
{
    public Database(DatabaseConfig config) : base(config)
    {
    }
}

It should be noted that since the wrapper doesn't know what tables should be in the database, in your Database class you will need a CheckCreateSchema() method in order to check and create the necessary tables with the correct structure.

Here's an example on how that should look like and work:

public class Database : ConnectorWrapper<DatabaseConfig>
{
    public Database(DatabaseConfig config) : base(config)
    {
        CheckCreateSchema();
    }

    private void CheckCreateSchema()
    {
        var output = ExecuteQuery(new Query(null, $"SHOW TABLES LIKE '{Configuration.TableName}';", EQueryType.Scalar));
        if (output.Output != null) return;

        ExecuteQuery(new Query(null, $"CREATE TABLE `{Configuration.TableName}` (`Id` INT NOT NULL, PRIMARY KEY (`Id`));", EQueryType.NonQuery));
    }
}

As you noticed, we created new instances of a class called Query. This class holds the basic information of a query, so you can store it if you need to. It holds an identifier (which can be nullable, but if so will default to the query string), a query string, the query type, if the query should be cached (defaults to false), the parameters of the query (defaults to an empty list), and the callbacks for that query.

Once you have a class that inherits from ConnectorWrapper and a CheckCreateSchema(), all that's left to do is for you to write as many methods as you need to use to add data, retrieve data or similar.

Each one of those methods should call ExecuteQuery() if it will fully be synchronous, or ExecuteQueryAsync() if it will run asynchronously.

public class Database : ConnectorWrapper<DatabaseConfig>
{
    public Database(DatabaseConfig config) : base(config)
    {
        CheckCreateSchema();
    }

    private void CheckCreateSchema()
    {
        var output = ExecuteQuery(new Query(null, $"SHOW TABLES LIKE '{Configuration.TableName}';", EQueryType.Scalar));
        if (output.Output != null) return;

        ExecuteQuery(new Query(null, $"CREATE TABLE `{Configuration.TableName}` (`Id` INT NOT NULL, PRIMARY KEY (`Id`));", EQueryType.NonQuery));
    }

    public void AddNewRow()
    {
        ExecuteQuery(new Query(null, $"INSERT INTO `{Configuration.TableName}` (`Id`) VALUES(0);", EQueryType.NonQuery));
    }

    public int GetRow(int id)
    {
        var output = ExecuteQuery(new Query(null, $"SELECT * FROM `{Configuration.TableName}` WHERE `id`=0;", EQueryType.Scalar)).Output;
        if (output == null) return -1;
        return int.TryParse(output.ToString(), out var result) ? result : -1;
    }
}

Finally, if your query uses MySqlParameters, you can insert them with the query and they'll automatically be added before execution.

For example:

ExecuteQuery(new Query(null, $"INSERT INTO `{Configuration.TableName}` (`Id`) VALUES(@id);", EQueryType.NonQuery, queryParameters: new MySqlParameter("@id", 0)));
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.0.0-dev.1 71 8/12/2024
5.0.3 314 3/11/2023
5.0.2 257 2/9/2023
5.0.1 347 12/6/2022
5.0.0 425 7/27/2022
4.1.2 438 6/5/2022
4.1.1 430 6/5/2022
4.1.0 480 3/7/2022
4.0.0 678 2/11/2022
3.0.2 345 12/17/2021
3.0.1 306 10/12/2021
3.0.0 325 9/24/2021
2.3.0 541 10/5/2020
2.2.1 513 7/9/2020
2.2.0 503 7/7/2020

First nuget release.