写了这么久,代码没有越写越多,代码量的数量级没变,用SourceCounter代码统计工具统计了一下,一共4000多行代码。
ADO.NET换成Dapper后,我迷茫了,因为LiteSql做的工作变少了,它存在的意义就变小了,代码量也变少了。
Dapper支持所有ADO.NET支持的数据库,我用Dapper.LiteSql支持了ClickHouse。
以上功能,不同数据库实现起来是有区别的,Dapper.LiteSql用最小的代价支持了更多的数据库,只需要实现IProvider接口,就可以支持你想支持的数据库(必须是ADO.NET支持的数据库)。
Dapper.LiteSql满足基本的增删改查是够用了,当初还是DBHelper时,我上家公司就用它来开发ERP、CRM等系统,是够用的;其它的Dapper兜底。
基于ADO.NET无其它依赖的版本叫LiteSql,基于Dapper的版本叫Dapper.LiteSql,后面以Dapper.LiteSql为维护重点。
最近为了完善,做了一些破坏性的修改,好在没什么人用,没包袱;后续应该不会再变动接口,暂时也没有要添加的功能了。
典型的查询是通过拼SQL。
int? status = 0;
string remark = "测试";
DateTime? startTime = null;
DateTime? endTime = DateTime.Now;
using (var session = LiteSqlFactory.GetSession())
{
session.OnExecuting = (s, p) => Console.WriteLine(s);
ISqlString sql = session.CreateSql(@"
select t.*, u.real_name as OrderUserRealName
from bs_order t
left join sys_user u on t.order_userid=u.id
where 1=1")
.Append(" and t.status=@status", status);
.AppendIf(!string.IsNullOrWhiteSpace(remark), " and t.remark like @remark", "%" + remark + "%");
.AppendIf(startTime.HasValue, " and t.order_time >= @startTime ", startTime);
.AppendIf(endTime.HasValue, " and t.order_time <= @endTime", endTime);
.Append(" order by t.order_time desc, t.id asc ");
long total = sql.QueryCount();
List<BsOrder> list = sql.QueryPage<BsOrder>(null, pageModel.PageSize, pageModel.CurrentPage);
}
sql.AppendIf(startTime.HasValue, " and t.order_time >= @startTime ", startTime.Value);
可以使用下面的写法:
sql.AppendIf(startTime.HasValue, " and t.order_time >= @startTime ", () => startTime.Value);
或:
sql.AppendIf(startTime.HasValue, " and t.order_time>= @startTime ", startTime);
例如ClickHouse的参数化查询是这样的:
t."captured_time" >= {StartTime:DateTime}
使用Dapper.LiteSql,你可以这样写:
t."captured_time" >= @StartTime
不同数据库参数化查询的参数统一使用@前缀
DateTime? startTime = null;
using (var session = LiteSqlFactory.GetSession())
{
session.OnExecuting = (s, p) => Console.WriteLine(s); //打印SQL
List<SysUser> list = session.Queryable<SysUser>() //Lambda写法
//拼SQL写法
.Append<SysUser>(@" where t.create_userid = @CreateUserId
and t.password like @Password
and t.id in @Ids",
new
{
CreateUserId = "1",
Password = "%345%",
Ids = session.CreateSql().ForList(new List<int> { 1, 2, 9, 10, 11 })
})
.Where(t => !t.UserName.Contains("管理员")) //Lambda写法
.Append<SysUser>(@" and t.create_time >= @StartTime", new { StartTime = new DateTime(2020, 1, 1) }) //拼SQL写法
.Where<SysUser>(t => t.Id <= 20) //Lambda写法
.AppendIf(startTime.HasValue, " and t.create_time >= @StartTime ", new { StartTime = startTime }) //拼SQL写法
.Append(" and t.create_time <= @EndTime ", new { EndTime = new DateTime(2022, 8, 1) }) //拼SQL写法
.QueryList<SysUser>(); //如果上一句是拼SQL写法,就用QueryList
//.ToList(); //如果上一句是Lambda写法,就用ToList
long id = session.Queryable<SysUser>().Where(t => t.Id == 1).First().Id;
Assert.IsTrue(id == 1);
foreach (SysUser item in list)
{
Console.WriteLine(ModelToStringUtil.ToString(item));
}
Assert.IsTrue(list.Count > 0);
}
DateTime? startTime = null;
using (var session = LiteSqlFactory.GetSession())
{
session.OnExecuting = (s, p) => Console.WriteLine(s); //打印SQL
List<SysUser> list = session.CreateSql(@"
select * from sys_user t where t.id <= @Id", new { Id = 20 })
.Append(@" and t.create_userid = @CreateUserId
and t.password like @Password
and t.id in @Ids",
new
{
CreateUserId = "1",
Password = "%345%",
Ids = session.CreateSql().ForList(new List<int> { 1, 2, 9, 10, 11 })
})
.AppendIf(startTime.HasValue, " and t.create_time >= @StartTime ", new { StartTime = startTime })
.Append(" and t.create_time <= @EndTime ", new { EndTime = new DateTime(2022, 8, 1) })
.QueryList<SysUser>();
long id = session.CreateSql("select id from sys_user where id=@Id", new { Id = 1 })
.QuerySingle<long>();
Assert.IsTrue(id == 1);
foreach (SysUser item in list)
{
Console.WriteLine(ModelToStringUtil.ToString(item));
}
Assert.IsTrue(list.Count > 0);
}
只要代码写的足够少,就...没有BUG?嗯...暂时没有发现BUG,因为除了我自己没什么人用。
https://www.nuget.org/packages/Dapper.LiteSql
https://gitee.com/s0611163/Dapper.LiteSql
之前出现一个失误,代码注释里含有一个敏感信息,导致我把原来的仓库删了,建了个新的。
转载自: https://www.cnblogs.com/s0611163/archive/2022/08/31/16641136.html