• 作者:秋荷雨翔
  • 分类: .Net

写了这么久,代码没有越写越多,代码量的数量级没变,用SourceCounter代码统计工具统计了一下,一共4000多行代码。

ADO.NET换成Dapper后,我迷茫了,因为LiteSql做的工作变少了,它存在的意义就变小了,代码量也变少了。

Dapper支持所有ADO.NET支持的数据库,我用Dapper.LiteSql支持了ClickHouse。

在Dapper的基础上,Dapper.LiteSql提供了哪些功能?

  1. 数据插入、更新、批量插入、批量更新,支持实体类、实体类集合,不用拼SQL了。
  2. 分页查询。
  3. 如果数据库字段名和实体类的属性名不一致,基于Dapper的ITypeMap接口做了映射。
  4. 简单的查询支持Lambda表达式(支持单表条件查询、连表条件查询、排序、查询数量等,不支持分组查询、嵌套查询、子查询等复杂查询)。
  5. 支持对分表进行增删改查。
  6. 统一不同数据库的参数化查询SQL

以上功能,不同数据库实现起来是有区别的,Dapper.LiteSql用最小的代价支持了更多的数据库,只需要实现IProvider接口,就可以支持你想支持的数据库(必须是ADO.NET支持的数据库)。

Dapper.LiteSql满足基本的增删改查是够用了,当初还是DBHelper时,我上家公司就用它来开发ERP、CRM等系统,是够用的;其它的Dapper兜底。

LiteSql和Dapper.LiteSql

基于ADO.NET无其它依赖的版本叫LiteSql,基于Dapper的版本叫Dapper.LiteSql,后面以Dapper.LiteSql为维护重点。
最近为了完善,做了一些破坏性的修改,好在没什么人用,没包袱;后续应该不会再变动接口,暂时也没有要添加的功能了。

插入、更新、删除功能说明

  1. 插入和更新是不需要写SQL的
  2. 删除可以根据ID删,也可写SQL条件。
  3. 如何获取刚插入的数据的ID?提供了一个InsertReturnId接口,鉴于不同数据库有差异,写法也比较多,所以需要传一个查询插入ID的SQL,好吧,我真是机智。

查询功能说明

典型的查询是通过拼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);
}

1. AppendIf是很有用的,在一个查询页面,如果有几个查询条件没有选,那么这几个查询条件就不会添加到SQL中。

2. 当startTime为null时,下面的写法是会报错的:

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);

3. 统一不同数据库的参数化查询SQL

例如ClickHouse的参数化查询是这样的:

t."captured_time" >= {StartTime:DateTime}

使用Dapper.LiteSql,你可以这样写:

t."captured_time" >= @StartTime

不同数据库参数化查询的参数统一使用@前缀

附:ClickHouse测试源码

4. 支持Lambda表达式和原生SQL混写,这是在开发过程中意外实现的功能,我也不知道有什么意义

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);
}

没什么可讲的了,就这些功能。

附:拼SQL经典示例:

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,因为除了我自己没什么人用。

NuGet地址:

https://www.nuget.org/packages/Dapper.LiteSql

Dapper.LiteSql源码地址:

https://gitee.com/s0611163/Dapper.LiteSql

之前出现一个失误,代码注释里含有一个敏感信息,导致我把原来的仓库删了,建了个新的。

转载自: https://www.cnblogs.com/s0611163/archive/2022/08/31/16641136.html