Pustalorc.MySqlConnectorWrapper
2.3.0
See the version list below for details.
dotnet add package Pustalorc.MySqlConnectorWrapper --version 2.3.0
NuGet\Install-Package Pustalorc.MySqlConnectorWrapper -Version 2.3.0
<PackageReference Include="Pustalorc.MySqlConnectorWrapper" Version="2.3.0" />
paket add Pustalorc.MySqlConnectorWrapper --version 2.3.0
#r "nuget: Pustalorc.MySqlConnectorWrapper, 2.3.0"
// Install Pustalorc.MySqlConnectorWrapper as a Cake Addin #addin nuget:?package=Pustalorc.MySqlConnectorWrapper&version=2.3.0 // Install Pustalorc.MySqlConnectorWrapper as a Cake Tool #tool nuget:?package=Pustalorc.MySqlConnectorWrapper&version=2.3.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.
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($"SHOW TABLES LIKE '{Configuration.TableName}';", EQueryType.Scalar));
if (output.Output != null) return;
ExecuteQuery(new Query($"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($"SHOW TABLES LIKE '{Configuration.TableName}';", EQueryType.Scalar));
if (output.Output != null) return;
ExecuteQuery(new Query($"CREATE TABLE `{Configuration.TableName}` (`Id` INT NOT NULL, PRIMARY KEY (`Id`));", EQueryType.NonQuery));
}
public void AddNewRow()
{
ExecuteQuery(new Query($"INSERT INTO `{Configuration.TableName}` (`Id`) VALUES(0);", EQueryType.NonQuery));
}
public int GetRow(int id)
{
var output = ExecuteQuery(new Query($"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 MySqlParameter
s, you can insert them with the query and they'll automatically be added before execution.
For example:
ExecuteQuery(new Query($"INSERT INTO `{Configuration.TableName}` (`Id`) VALUES(@id);", EQueryType.NonQuery, new MySqlParameter("@id", 0)));
Product | Versions 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. |
-
.NETFramework 4.6.1
- MySql.Data (>= 8.0.21)
- Pustalorc.FrequencyCache (>= 1.1.0)
-
.NETFramework 4.8
- MySql.Data (>= 8.0.21)
- Pustalorc.FrequencyCache (>= 1.1.0)
-
.NETStandard 2.0
- MySql.Data (>= 8.0.21)
- Pustalorc.FrequencyCache (>= 1.1.0)
-
.NETStandard 2.1
- MySql.Data (>= 8.0.21)
- Pustalorc.FrequencyCache (>= 1.1.0)
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 | 73 | 8/12/2024 |
5.0.3 | 316 | 3/11/2023 |
5.0.2 | 259 | 2/9/2023 |
5.0.1 | 349 | 12/6/2022 |
5.0.0 | 427 | 7/27/2022 |
4.1.2 | 440 | 6/5/2022 |
4.1.1 | 432 | 6/5/2022 |
4.1.0 | 482 | 3/7/2022 |
4.0.0 | 680 | 2/11/2022 |
3.0.2 | 347 | 12/17/2021 |
3.0.1 | 308 | 10/12/2021 |
3.0.0 | 327 | 9/24/2021 |
2.3.0 | 543 | 10/5/2020 |
2.2.1 | 515 | 7/9/2020 |
2.2.0 | 505 | 7/7/2020 |
Update to MySql.Data 8.0.21
Update to Pustalorc.FrequencyCache 1.1.0