using Common;
using Common.LambdaToSQL;
using Common.Models;
using Dapper;
using Data.Interfaces;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
namespace Data.Dapper
{
public class MySqlDatabase : IDatabase
{
private string _connString;
public MySqlDatabase(string connString = "")
{
_connString = connString.IsEmpty() ? ConfigHelper.GetConnectionString("mysql") : connString;
}
#region 属性
///
/// 获取 数据库连接串
///
private IDbConnection Connection
{
get
{
var dbconnection = new MySqlConnection(_connString);
if (dbconnection.State == ConnectionState.Closed)
{
dbconnection.Open();
}
return dbconnection;
}
}
#endregion 属性
#region 查询
///
/// 集合查询
///
///
///
///
///
public IEnumerable FindList(string sql, object para = null) where T : class, new()
{
using (var db = Connection)
{
return db.Query(sql, para).ToList();
}
}
///
/// 分页集合查询
///
///
///
///
public PaginationDTO> FindPageList(string sql, PaginationQuery pagination, object para = null) where T : class, new()
{
using (var db = Connection)
{
var orderBy = "";
if (!string.IsNullOrEmpty(pagination.Sidx))
{
if (pagination.Sord.ToUpper().IndexOf("ASC", StringComparison.Ordinal) + pagination.Sord.ToUpper().IndexOf("DESC", StringComparison.Ordinal) > 0)
{
orderBy = "Order By " + pagination.Sord;
}
else
{
orderBy = "Order By " + pagination.Sidx + " " + pagination.Sord.ToUpper();
}
}
else
{
orderBy = "Order By (Select 0)";
}
var sqls = $@"{sql} {orderBy} limit { (pagination.Page - 1) * pagination.Rows},{ pagination.Rows}";
string selectCountSql = "Select Count(*) From (" + sql + ") AS t";
try
{
return new PaginationDTO>
{
Data = db.Query(sqls, para).ToList(),
Total = Convert.ToInt32(db.ExecuteScalar(selectCountSql, para)),
Rows = pagination.Rows
};
}
catch (Exception e)
{
return null;
}
}
}
///
/// 分页查询-Lambda
///
///
///
///
///
public PaginationDTO> FindPageList(Expression> condition, PaginationQuery pagination) where T : class, new()
{
var lambda = new LambdaExpConditions();
lambda.AddAndWhere(condition);
string where = lambda.Where();
string sql = DatabaseCommon.SelectSql(where).ToString();
return FindPageList(sql, pagination);
}
///
/// 集合查询(Lambda)
///
///
///
///
public IEnumerable FindList(Expression> condition) where T : class, new()
{
var lambda = new LambdaExpConditions();
lambda.AddAndWhere(condition);
string where = lambda.Where();
string sql = DatabaseCommon.SelectSql(where).ToString();
return this.FindList(sql);
}
///
/// 查询实体对象
///
///
///
///
///
public T FindEntity(string sql, object para = null)
{
using (var db = Connection)
{
return db.QueryFirstOrDefault(sql, para);
}
}
///
/// 集合查询(Lambda)
///
///
///
///
public T FindEntity(Expression> condition) where T : class, new()
{
var lambda = new LambdaExpConditions();
lambda.AddAndWhere(condition);
string where = lambda.Where();
string sql = DatabaseCommon.SelectSql(where).ToString();
return FindEntity(sql);
}
///
/// 数据条数
///
///
///
///
public int Count(Expression> condition) where T : class, new()
{
var lambda = new LambdaExpConditions();
lambda.AddAndWhere(condition);
string where = lambda.Where();
string sql = DatabaseCommon.SelectCountSql(where).ToString();
using (var db = Connection)
{
return Convert.ToInt32(db.ExecuteScalar(sql));
}
}
///
/// 数据条数
///
///
///
///
public int Count(string sql, object para)
{
using (var db = Connection)
{
return Convert.ToInt32(db.ExecuteScalar(sql, para));
}
}
#endregion
#region 编辑
///
/// 新增sql
///
/// sql语句
/// 参数化
public int Insert(string sql, object para = null)
{
using (var db = Connection)
{
return db.Execute(sql, para);
}
}
///
/// 实体新增
///
/// sql语句
/// 参数化
public int Insert(T model) where T : class, new()
{
using (var db = Connection)
{
string sql = DatabaseCommon.InsertSql().ToString();
return db.Execute(sql, model);
}
}
///
/// 批量实体新增
///
/// sql语句
/// 参数化
public int Insert(List list) where T : class, new()
{
using (var db = Connection)
{
string sql = DatabaseCommon.InsertSql().ToString();
return db.Execute(sql, list);
}
}
///
/// 修改sql
///
/// sql语句
/// 参数化
public int Update(string sql, object para = null)
{
using (var db = Connection)
{
return db.Execute(sql, para);
}
}
///
/// 修改lammbda
///
///
///
///
public int Update(T t) where T : class, new()
{
using (var db = Connection)
{
string sql = DatabaseCommon.UpdateSql(t).ToString();
if (sql.IsEmpty())
return 0;
return db.Execute(sql, t);
}
}
#endregion
#region 删除
public int Delete(string sql, object para = null)
{
using (var db = Connection)
{
return db.Execute(sql, para);
}
}
///
/// 泛型删除
///
///
///
///
public int Delete(T t) where T : class, new()
{
using (var db = Connection)
{
string sql = DatabaseCommon.DeleteSql(t).ToString();
return db.Execute(sql, t);
}
}
///
/// 泛型删除
///
///
///
///
public int Delete(int id) where T : class, new()
{
using (var db = Connection)
{
string sql = DatabaseCommon.DeleteSqlById(id).ToString();
return db.Execute(sql);
}
}
#endregion
}
}