using Dapper;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Tiobon.Core.Common.DB.Dapper.Const;
using Tiobon.Core.Common.DB.Dapper.DBManager;
using Tiobon.Core.Common.DB.Dapper.Enums;
using Tiobon.Core.Common.DB.Dapper.Extensions;
using Tiobon.Core.Common.DB.Dapper.Utilities;
using System.Threading.Tasks;
namespace Tiobon.Core.Common.DB.Dapper;
public class SqlDapper : ISqlDapper
{
private string _connectionString;
private IDbConnection _connection { get; set; }
public IDbConnection Connection
{
get
{
if (_connection == null || _connection.State == ConnectionState.Closed)
{
_connection = DBServerProvider.GetDbConnection(_connectionString);
}
return _connection;
}
}
public SqlDapper()
{
_connectionString = DBServerProvider.GetConnectionString();
}
///
/// string mySql = "Data Source=132.232.2.109;Database=mysql;User
/// ID=root;Password=mysql;pooling=true;CharSet=utf8;port=3306;sslmode=none";
/// this.conn = new MySql.Data.MySqlClient.MySqlConnection(mySql);
///
///
public SqlDapper(string connKeyName)
{
_connectionString = DBServerProvider.GetConnectionString(connKeyName);
}
///
/// var p = new object();
// p.Add("@a", 11);
//p.Add("@b", dbType: DbType.Int32, direction: ParameterDirection.Output);
//p.Add("@c", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
// ///
///
///
///
///
///
public List QueryList(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false) where T : class
{
return Execute((conn, dbTransaction) =>
{
return conn.Query(cmd, param, dbTransaction, commandType: commandType ?? CommandType.Text).ToList();
}, beginTransaction);
}
public async Task> QueryListAsync(string cmd, object param = null, CommandType? commandType = null, bool beginTransaction = false, int? commandTimeout = null) where T : class
{
return await Execute(async (conn, dbTransaction) =>
{
return (await conn.QueryAsync(cmd, param, dbTransaction, commandType: commandType ?? CommandType.Text)).ToList();
}, beginTransaction);
}
public T QueryFirst(string cmd, object param = null, CommandType? commandType = null, bool beginTransaction = false) where T : class
{
List list = QueryList(cmd, param, commandType: commandType ?? CommandType.Text, beginTransaction: beginTransaction).ToList();
return list.Count == 0 ? null : list[0];
}
public async Task QueryFirstAsync(string cmd, object param = null, CommandType? commandType = null, bool beginTransaction = false, int? commandTimeout = null) where T : class
{
var data = await QueryListAsync(cmd, param, commandType, beginTransaction, commandTimeout);
List list = data.ToList();
return !list.Any() ? null : list[0];
}
public object ExecuteScalar(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false)
{
try
{
return Execute((conn, dbTransaction) =>
{
return conn.ExecuteScalar(cmd, param, dbTransaction, commandType: commandType ?? CommandType.Text);
}, beginTransaction);
}
catch (Exception)
{
throw;
}
}
public async Task