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

DatabaseWrapper: First draft of achieving independency from locales #20

Open
wants to merge 11 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
24 changes: 22 additions & 2 deletions src/DatabaseWrapper.Core/DatabaseClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Data;
using static System.FormattableString;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DatabaseWrapper.Core;
using ExpressionTree;
using System.Threading;
using System.Data.Common;
using System.Runtime.InteropServices;

namespace DatabaseWrapper.Core
{
Expand Down Expand Up @@ -302,20 +305,37 @@ public abstract class DatabaseClientBase
/// <param name="token">Cancellation token.</param>
public abstract Task TruncateAsync(string tableName, CancellationToken token = default);

/// <summary>
/// Execute a query.
/// </summary>
/// <param name="queryAndParameters">A tuple of the aatabase query defined outside of the database client and query parameters.</param>
/// <returns>A DataTable containing the results.</returns>
public abstract DataTable Query((string Query, IEnumerable<KeyValuePair<string,object>> Parameters) queryAndParameters);

/// <summary>
/// Execute a query.
/// </summary>
/// <param name="query">Database query defined outside of the database client.</param>
/// <returns>A DataTable containing the results.</returns>
public abstract DataTable Query(string query);
public DataTable Query(string query)
=> Query((query, null));

/// <summary>
/// Execute a query.
/// </summary>
/// <param name="queryAndParameters">A tuple of the aatabase query defined outside of the database client and query parameters.</param>
/// <param name="token">Cancellation token.</param>
/// <returns>A DataTable containing the results.</returns>
public abstract Task<DataTable> QueryAsync((string Query, IEnumerable<KeyValuePair<string,object>> Parameters) queryAndParameters, CancellationToken token = default(CancellationToken));

/// <summary>
/// Execute a query.
/// </summary>
/// <param name="query">Database query defined outside of the database client.</param>
/// <param name="token">Cancellation token.</param>
/// <returns>A DataTable containing the results.</returns>
public abstract Task<DataTable> QueryAsync(string query, CancellationToken token = default);
public Task<DataTable> QueryAsync(string query, CancellationToken token = default(CancellationToken))
=> QueryAsync((query, null), token);

/// <summary>
/// Determine if records exist by filter.
Expand Down
291 changes: 283 additions & 8 deletions src/DatabaseWrapper.Core/DatabaseHelperBase.cs

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions src/DatabaseWrapper.Core/DatabaseWrapper.Core.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 30 additions & 5 deletions src/DatabaseWrapper.Mysql/DatabaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,28 @@ private set
private string _CountColumnName = "__count__";
private string _SumColumnName = "__sum__";
private MysqlHelper _Helper = new MysqlHelper();


static readonly Dictionary<SqlDbType, MySqlDbType> DbTypeMapping = new Dictionary<SqlDbType, MySqlDbType>()
{
{ SqlDbType.Int, MySqlDbType.Int32 },
{ SqlDbType.BigInt, MySqlDbType.Int64 },
{ SqlDbType.Float, MySqlDbType.Double },
{ SqlDbType.UniqueIdentifier, MySqlDbType.Guid },
{ SqlDbType.Image, MySqlDbType.Blob },
{ SqlDbType.DateTimeOffset, MySqlDbType.DateTime },
{ SqlDbType.NText, MySqlDbType.Text },
{ SqlDbType.NVarChar, MySqlDbType.VarChar },
{ SqlDbType.Bit, MySqlDbType.Bit },
{ SqlDbType.DateTime, MySqlDbType.DateTime }
};

static MySqlParameter CreateMySqlParameter(string name, SqlDbType type)
{
var real_type = DbTypeMapping[type];
var r = new MySqlParameter(name, real_type);
return r;
}

#endregion

#region Constructors-and-Factories
Expand Down Expand Up @@ -794,10 +815,11 @@ public override async Task TruncateAsync(string tableName, CancellationToken tok
/// <summary>
/// Execute a query.
/// </summary>
/// <param name="query">Database query defined outside of the database client.</param>
/// <param name="queryAndParameters">Database query defined outside of the database client, and, the corresponding parameters.</param>
/// <returns>A DataTable containing the results.</returns>
public override DataTable Query(string query)
public override DataTable Query((string Query, IEnumerable<KeyValuePair<string,object>> Parameters) queryAndParameters)
{
(var query, var parameters) = queryAndParameters;
if (String.IsNullOrEmpty(query)) throw new ArgumentNullException(query);
if (query.Length > MaxStatementLength) throw new ArgumentException("Query exceeds maximum statement length of " + MaxStatementLength + " characters.");

Expand All @@ -822,6 +844,7 @@ public override DataTable Query(string query)
cmd.CommandText = query;
#pragma warning restore CA2100 // Review SQL queries for security vulnerabilities

DatabaseHelperBase.AddParameters(cmd, CreateMySqlParameter, parameters);
using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
Expand Down Expand Up @@ -872,11 +895,12 @@ public override DataTable Query(string query)
/// <summary>
/// Execute a query.
/// </summary>
/// <param name="query">Database query defined outside of the database client.</param>
/// <param name="queryAndParameters">Database query defined outside of the database client, and, the corresponding parameters.</param>
/// <param name="token">Cancellation token.</param>
/// <returns>A DataTable containing the results.</returns>
public override async Task<DataTable> QueryAsync(string query, CancellationToken token = default)
public override async Task<DataTable> QueryAsync((string Query, IEnumerable<KeyValuePair<string,object>> Parameters) queryAndParameters, CancellationToken token = default)
{
(var query, var parameters) = queryAndParameters;
if (String.IsNullOrEmpty(query)) throw new ArgumentNullException(query);
if (query.Length > MaxStatementLength) throw new ArgumentException("Query exceeds maximum statement length of " + MaxStatementLength + " characters.");

Expand All @@ -901,6 +925,7 @@ public override async Task<DataTable> QueryAsync(string query, CancellationToken
cmd.CommandText = query;
#pragma warning restore CA2100 // Review SQL queries for security vulnerabilities

DatabaseHelperBase.AddParameters(cmd, CreateMySqlParameter, parameters);
using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
Expand Down
15 changes: 11 additions & 4 deletions src/DatabaseWrapper.Mysql/DatabaseWrapper.Mysql.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading