c# 实现定义一套中间SQL可以跨库执行的SQL语句

目前数据的种类非常多,每种数据都支持sql语句,但是大家发现没有每种数据的SQL都有自己的语法特性,都是SQL语句都没有一个特定的语法标准,导致开发人员在开发的过程中无法任意选库(如果用自己不熟的库都会要学习一遍SQL语法),主要是学习成本太高

那么有没有一种工具能够统一下sql语法规则,只要学一种sql语句就可以跨库执行,让开发人员不用学习每种库的SQL语句呢?

下面就给大家介绍一款开源工具 hisql源码 hisql介绍

hisql查询样例

单表查询

根据hisql语句通过ToSql()方法生成目标sql的原生sql


var _sql = sqlClient.HiSql("select * from HTest01 where CreateTime>='2022-02-17 09:27:50' and CreateTime<='2022-03-22 09:27:50'").ToSql();

hisql生成的sqlserver 的sql 如下的所示

select  [htest01].[SID],[htest01].[UName],[htest01].[Age],[htest01].[Salary],[htest01].[Descript],[htest01].[CreateTime],[htest01].[CreateName],[htest01].[ModiTime],[htest01].[ModiName] from [HTest01] as [HTest01]
where [htest01].[CreateTime] >= '2022-02-17 09:27:50.000' and [htest01].[CreateTime] <= '2022-03-22 09:27:50.000'

hisql生成mysql的sql如下所示

select   `htest01`.`SID`,`htest01`.`UName`,`htest01`.`Age`,`htest01`.`Salary`,`htest01`.`Descript`,`htest01`.`CreateTime`,`htest01`.`CreateName`,`htest01`.`ModiTime`,`htest01`.`ModiName` from `htest01` as `htest01`
where `htest01`.`CreateTime` >= '2022-02-17 09:27:50.000' and `htest01`.`CreateTime` <= '2022-03-22 09:27:50.000'

hisql生成postgresql 的sql下所示

select  "htest01"."SID","htest01"."UName","htest01"."Age","htest01"."Salary","htest01"."Descript","htest01"."CreateTime","htest01"."CreateName","htest01"."ModiTime","htest01"."ModiName" from "HTest01" as "htest01"
where "htest01"."CreateTime" >= '2022-02-17 09:27:50.000' and "htest01"."CreateTime" <= '2022-03-22 09:27:50.000'

大家可以会问 明明是"select * from HTest01 where CreateTime>='2022-02-17 09:27:50' and CreateTime<='2022-03-22 09:27:50'" 用的*查询 生成的sql语句为什么会直接列出所有字段呢? 熟HiSql的网友都应该知道这里其实隐藏了一个功能就是用户可以自定义字段排序在系统表Hi_FieldModel 中可配置

group by查询

hisql 默认支持的常用函数 max,count,min,sum,avg

string sql = sqlClient.HiSql("select FieldName, count(FieldName) as NAME_count,max(FieldType) as FieldType_max from Hi_FieldModel  group by FieldName").ToSql();

hisql生成的sqlserver 的sql 如下的所示

select  [hi_fieldmodel].[FieldName],count(*) as NAME_count,max([hi_fieldmodel].[FieldType]) as FieldType_max from [Hi_FieldModel] as [Hi_FieldModel]
group by [hi_fieldmodel].[FieldName]

hisql生成mysql的sql如下所示

select   `hi_fieldmodel`.`FieldName`,count(*) as NAME_count,max(`hi_fieldmodel`.`FieldType`) as FieldType_max from `Hi_FieldModel` as `hi_fieldmodel`
group by `hi_fieldmodel`.`FieldName`

hisql生成postgresql 的sql下所示

select  "hi_fieldmodel"."FieldName",count(*) as NAME_count,max("hi_fieldmodel"."FieldType") as FieldType_max from "Hi_FieldModel" as "hi_fieldmodel"
group by "hi_fieldmodel"."FieldName"

having 查询

上次有网友说having 实现起来很麻烦通过正则表达式解析很难实现,我只能说很多网友对正则表达式的理解还不是很深,正则表达式已经应用于种类型的编译器中,下面就来演示一下Having

string sql_having = sqlClient.HiSql("select FieldName, count(FieldName) as NAME_count,max(FieldType) as FieldType_max from Hi_FieldModel  group by FieldName having count(FieldName) > 1").ToSql();

hisql生成的sqlserver 的sql 如下的所示

select  [hi_fieldmodel].[FieldName],count(*) as NAME_count,max([hi_fieldmodel].[FieldType]) as FieldType_max from [Hi_FieldModel] as [Hi_FieldModel]
group by [hi_fieldmodel].[FieldName]
having count(*) > 1

hisql生成mysql的sql如下所示

select   `hi_fieldmodel`.`FieldName`,count(*) as NAME_count,max(`hi_fieldmodel`.`FieldType`) as FieldType_max from `Hi_FieldModel` as `hi_fieldmodel`
group by `hi_fieldmodel`.`FieldName`
having count(*) > 1

hisql生成postgresql 的sql下所示

select  "hi_fieldmodel"."FieldName",count(*) as NAME_count,max("hi_fieldmodel"."FieldType") as FieldType_max from "Hi_FieldModel" as "hi_fieldmodel"
group by "hi_fieldmodel"."FieldName"
having count(*) > 1

join 多表查询

hisql inner join 和 in 操作语法

var sql = sqlClient.HiSql("select a.tabname from hi_fieldmodel as a inner join Hi_TabModel as  b on a.tabname =b.tabname inner join Hi_TabModel as c on a.tabname=c.tabname where a.tabname='h_test'  and a.FieldType in (11,41,21)  ").ToSql();

hisql生成的sqlserver 的sql 如下的所示

select  [a].[TabName] from [Hi_FieldModel] as [a]
inner join [Hi_TabModel] as [b] on [a].[TabName] = [b].[TabName] inner join [Hi_TabModel] as [c] on [a].[TabName] = [c].[TabName]
where [a].[TabName] = 'h_test' and [a].[FieldType] in (11,41,21)

hisql生成mysql的sql如下所示

select   `a`.`TabName` from `Hi_FieldModel` as `a`
inner join `Hi_TabModel` as `b` on `a`.`TabName` = `b`.`TabName` inner join `Hi_TabModel` as `c` on `a`.`TabName` = `c`.`TabName`
where `a`.`TabName` = 'h_test' and `a`.`FieldType` in (11,41,21)

hisql生成postgresql 的sql下所示

select  "a"."TabName" from "Hi_FieldModel" as "a"
inner join "Hi_TabModel" as "b" on "a"."TabName" = "b"."TabName" inner join "Hi_TabModel" as "c" on "a"."TabName" = "c"."TabName"
where "a"."TabName" = 'h_test' and "a"."FieldType" in (11,41,21)

分页

hisql 分页 只要在日常查询上增加Take() 每页显示多少数据 Skip() 显示第几页

var sql = sqlClient.HiSql("select a.tabname from hi_fieldmodel as a inner join Hi_TabModel as  b on a.tabname =b.tabname inner join Hi_TabModel as c on a.tabname=c.tabname where a.tabname='h_test'  and a.FieldType in (11,41,21)  ").Take(2).Skip(2).ToSql();

hisql生成的sqlserver 的sql 如下的所示

select  [TabName] from (
select ROW_NUMBER() OVER(Order by [a].[FieldType] ASC) AS _hi_rownum_, [a].[TabName] from [Hi_FieldModel] as [a]
inner join [Hi_TabModel] as [b] on [a].[TabName] = [b].[TabName] inner join [Hi_TabModel] as [c] on [a].[TabName] = [c].[TabName]
where [a].[TabName] = 'h_test' and [a].[FieldType] in (11,41,21)
) as hi_sql
where hi_sql._hi_rownum_ BETWEEN (2-1)*2+1 and 2*2 order by _hi_rownum_ asc

hisql生成mysql的sql如下所示

select `TabName` from (
select ROW_NUMBER() OVER(Order by `a`.`FieldType` ASC) AS `_hi_rownum_`, `a`.`TabName` from `Hi_FieldModel` as `a`
inner join `Hi_TabModel` as `b` on `a`.`TabName` = `b`.`TabName` inner join `Hi_TabModel` as `c` on `a`.`TabName` = `c`.`TabName`
where `a`.`TabName` = 'h_test' and `a`.`FieldType` in (11,41,21)
) as hi_sql
order by `_hi_rownum_` asc
limit 2,2

hisql生成postgresql 的sql下所示

select "TabName" from (
select ROW_NUMBER() OVER(Order by "a"."FieldType" ASC) AS "_hi_rownum_", "a"."TabName" from "Hi_FieldModel" as "a"
inner join "Hi_TabModel" as "b" on "a"."TabName" = "b"."TabName" inner join "Hi_TabModel" as "c" on "a"."TabName" = "c"."TabName"
where "a"."TabName" = 'h_test' and "a"."FieldType" in (11,41,21)
) as hi_sql
order by "_hi_rownum_" asc
limit 2 OFFSET 2

hisql 实现参数化

参数化可以有效的防注入,通过前端可以拼接的hisql语句通过参数化的方式传入, 这样就解决了注入问题

    string sql1= sqlClient.HiSql("select * from hi_tabmodel where tabname=@tabname ", new { TabName="H_test" ,FieldName="DID"}).ToSql();
string sql2= sqlClient.HiSql("select * from hi_tabmodel where tabname=@tabname or TabType in( @TabType)", new { TabName="H_test" , TabType =new List<int> { 1,2,3,4} }).ToSql(); string sql3 = sqlClient.HiSql("select * from hi_tabmodel where tabname=@tabname ", new Dictionary<string, object> { { "TabName", "H_test" } }).ToSql();

链式查询

hisql除了提供hisql语法外还提供链式查询方法如下所示

string sql = sqlClient.Query("Hi_FieldModel").As("A").Field("A.FieldType")
.Join("Hi_TabModel").As("B").On(new HiSql.JoinOn() { { "A.TabName", "B.TabName" } })
.Where("A.TabName='GD_UniqueCodeInfo'").Group(new GroupBy { { "A.FieldType" } })
.Sort("A.FieldType asc", "A.TabName asc")
.Take(2).Skip(2)
.ToSql();

hisql语句和链式查询混用

string sql = sqlClient.HiSql("select A.FieldType from Hi_FieldModel as A ")
.Where("A.TabName='GD_UniqueCodeInfo'").Group(new GroupBy { { "A.FieldType" } })
.Sort("A.FieldType asc", "A.TabName asc")
.Take(2).Skip(2)
.ToSql();

c# 实现定义一套中间SQL可以跨库执行的SQL语句的更多相关文章

  1. laravel 跨库执行原生 sql 语句

    执行原生 sql 返回结果集

  2. SQL Server跨库复制表数据错误的解决办法

    SQL Server跨库复制表数据的解决办法   跨库复制表数据,有很多种方法,最常见的是写程序来批量导入数据了,但是这种方法并不是最优方法,今天就用到了一个很犀利的方法,可以完美在 Sql Serv ...

  3. SQL Server 跨库复制表方法小笔记

    insert into tableA (column1,column2.....) SELECT * FROM OPENDATASOURCE('SQLOLEDB', 'Data Source=127. ...

  4. PCB MS SQL跨库执行SQL 获取返回值

    一.SQL跨库执行SQL 获取返回值 ) DECLARE @sql nvarchar(MAX) DECLARE @layer INT SET @Dblink = 'P2.fp_db.dbo.' sel ...

  5. SQL Server跨库跨服务器访问实现

    我们经常会遇到一个数据库要访问另一个数据库,或者一台服务器要访问另一台服务器里面的数据库. 那么这个如何实现的呢? 相信看完这篇文章你就懂了! 同一台服务器跨库访问实现 1. 首先创建两个数据库Cro ...

  6. SQL Server-聚焦sp_executesql执行动态SQL查询性能真的比exec好?

    前言 之前我们已经讨论过动态SQL查询呢?这里为何再来探讨一番呢?因为其中还是存在一定问题,如标题所言,很多面试题也好或者有些博客也好都在说在执行动态SQL查询时sp_executesql的性能比ex ...

  7. SQL Server跨库查询

    方式一: 语句 SELECT * FROM 数据库A.dbo.表A a, 数据库B.dbo.表B b WHERE a.field=b.field "DBO"可以省略 如 SELEC ...

  8. SQL Server 跨库同步数据

    最近有个需求是要跨库进行数据同步,两个数据库分布在两台物理计算机上,自动定期同步可以通过SQL Server代理作业来实现,但是前提是需要编写一个存储过程来实现同步逻辑处理.这里的存储过程用的不是op ...

  9. sql server 跨库操作

    SELECT *FROM OPENDATASOURCE('SQLOLEDB','Data Source=sql服务器名;User ID=用户名;Password=密码;').PersonDb.dbo. ...

随机推荐

  1. RecyclerView + SQLite 简易备忘录-----下

    最后就是添加备忘录的界面了.同时也是显示备忘录内容的界面. 1.activity_add_info.xml 也是比较简陋的一个页面设计. 顶部是一个自定义的Toolbar,剩下的部分都是ScrollV ...

  2. ElasticSearch7.3学习(二十)----采用restful风格查询详解

    1.Query DSL入门 1.1 DSL DSL:Domain Specified Language,特定领域的语言.es特有的搜索语言,可在请求体中携带搜索条件,功能强大. 查询全部 GET /b ...

  3. python跑机器学习时报错:Process finished with exit code -1073740791 (0xC0000409)

    emmm...第二次遇到这个错误了,好好的好好的卷积神经网络突然就跑不起了.就弹出一堆信息也不报那行代码错了... 记录一下: 两次解决方法相同,删h5py包 Process finished wit ...

  4. 从0开始用webpack开发antd,react组件库npm包并发布

    一.初始化一个npm包 1.新建一个文件夹(名称随意,建议和报名一致),输入命令 :npm init -y 会自动生成一个包的说明文件 package.json如下(本文以scroll-antd-ta ...

  5. golang /js index 转换excel字母表头

    Golang 1 package main 2 3 import "fmt" 4 5 func main() { 6 var Letters = []string{"A& ...

  6. linux下redis开机自启动

    将/usr/local/app/redis-4.0.8/redis.conf文件中daemonize no改为daemonize yes 在/etc目录下新建redis目录:mkdir /etc/re ...

  7. 团队Arpha2

    队名:观光队 组长博客 作业博客 组员实践情况 王耀鑫 **过去两天完成了哪些任务 ** 文字/口头描述 完成服务器连接数据库部分代码 展示GitHub当日代码/文档签入记录 接下来的计划 服务器网络 ...

  8. 打造一款高逼格的Vim神器

    点击上方"开源Linux",选择"设为星标" 回复"学习"获取独家整理的学习资料! 作者:枫上雾棋 链接:https://segmentfa ...

  9. 《你不知道的JS》上

  10. Vue报错: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'protocol')

    Vue报错: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'protocol') 报错信 ...