Pustalorc.MySqlConnectorWrapper
3.0.1
See the version list below for details.
dotnet add package Pustalorc.MySqlConnectorWrapper --version 3.0.1
NuGet\Install-Package Pustalorc.MySqlConnectorWrapper -Version 3.0.1
<PackageReference Include="Pustalorc.MySqlConnectorWrapper" Version="3.0.1" />
paket add Pustalorc.MySqlConnectorWrapper --version 3.0.1
#r "nuget: Pustalorc.MySqlConnectorWrapper, 3.0.1"
// Install Pustalorc.MySqlConnectorWrapper as a Cake Addin #addin nuget:?package=Pustalorc.MySqlConnectorWrapper&version=3.0.1 // Install Pustalorc.MySqlConnectorWrapper as a Cake Tool #tool nuget:?package=Pustalorc.MySqlConnectorWrapper&version=3.0.1
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 ConnectionStringFormat => "SERVER={0};DATABASE={1};UID={2};PASSWORD={3};PORT={4};";
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 use the connector.
There are 2 ways to access or use a creator, instantiating the base class, or inheriting the base class into another class.
For simplicity's sake, I will cover instantiation only.
Instantiating the wrapper is simple, you simply have to provide the type used for the configuration, in this example it's DatabaseConfig
, and a secondary type for the key to be able to identify the queries and their results in cache. For this example we will use string
for the second type.
Here's an example on how that should look like and work:
public class StorageService
{
public ConnectorWrapper<DatabaseConfig, string> Database;
public Database(DatabaseConfig config)
{
Database = new ConnectorWrapper<DatabaseConfig, string>(config, StringComparer.OrdinalIgnoreCase);
if (Database.TestConnection(out var exception))
CheckCreateSchema();
else
Log.Exception(exception);
}
private void CheckCreateSchema()
{
// Since we dont make the query cacheable, we do not need a unique key, setting it to "" should be enough.
var output = Database.ExecuteQuery(Database.CreateQuery("", $"SHOW TABLES LIKE '{Configuration.TableName}';", EQueryType.Scalar));
if (output.Output != null) return;
Database.ExecuteQuery(Database.CreateQuery("", $"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 a key, 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 (defaults to an empty list).
Once you have an instance of ConnectorWrapper
and a CheckCreateSchema()
, all that's left for you to do is to write as many methods as you need 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.
There's also ExecuteTransaction()
and ExecuteTransactionAsync()
for running multiple queries, both synchronously and asynchronously respectively. (Note, with a transaction, on any of the callbacks you can make validations to the db and cause a rollback by throwing an exception).
Here's an example of a fully implemented system:
public class StorageService
{
public ConnectorWrapper<DatabaseConfig, string> Database;
public Database(DatabaseConfig config)
{
Database = new ConnectorWrapper<DatabaseConfig, string>(config, StringComparer.OrdinalIgnoreCase);
if (Database.TestConnection(out var exception))
CheckCreateSchema();
else
Log.Exception(exception);
}
private void CheckCreateSchema()
{
// Since we dont make the query cacheable, we do not need a unique key, setting it to "" should be enough.
var output = Database.ExecuteQuery(Database.CreateQuery("", $"SHOW TABLES LIKE '{Configuration.TableName}';", EQueryType.Scalar));
if (output.Output != null) return;
Database.ExecuteQuery(Database.CreateQuery("", $"CREATE TABLE `{Configuration.TableName}` (`Id` INT NOT NULL, PRIMARY KEY (`Id`));", EQueryType.NonQuery));
}
public void AddNewRow()
{
Database.ExecuteQuery(Database.CreateQuery("", $"INSERT INTO `{Configuration.TableName}` (`Id`) VALUES(0);", EQueryType.NonQuery));
}
public int GetRow(int id)
{
var output = Database.ExecuteQuery(Database.CreateQuery("", $"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 or needs MySqlParameter
s, you can insert them with the query and they'll automatically be added before execution.
For example:
Database.ExecuteQuery(Database.CreateQuery("", $"INSERT INTO `{Configuration.TableName}` (`Id`) VALUES(@id);", EQueryType.NonQuery, queryParameters: 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
- MySqlConnector (>= 1.3.12)
- Pustalorc.FrequencyCache (>= 1.2.0)
-
.NETFramework 4.8
- MySqlConnector (>= 1.3.12)
- Pustalorc.FrequencyCache (>= 1.2.0)
-
.NETStandard 2.0
- MySqlConnector (>= 1.3.12)
- Pustalorc.FrequencyCache (>= 1.2.0)
-
.NETStandard 2.1
- MySqlConnector (>= 1.3.12)
- Pustalorc.FrequencyCache (>= 1.2.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 |
Changed open/close calls to validation calls.