c# 實現定義一套中間SQL可以跨庫執行的SQL語句

tansar 發表於 2022-07-04
C# SQL

c# 實現定義一套中間SQL可以跨庫執行的SQL語句

目前資料的種類非常多,每種資料都支援sql語句,但是大家發現沒有每種資料的SQL都有自己的語法特性,都是SQL語句都沒有一個特定的語法標準,導致開發人員在開發的過程中無法任意選庫(如果用自己不熟的庫都會要學習一遍SQL語法),主要是學習成本太高

那麼有沒有一種工具能夠統一下sql語法規則,只要學一種sql語句就可以跨庫執行,讓開發人員不用學習每種庫的SQL語句呢?

下面就給大家介紹一款開源工具 hisql原始碼 hisql介紹

hisql 官方群

c# 實現定義一套中間SQL可以跨庫執行的SQL語句

為了更好的服務於真正使用hisql的使用者,進群的夥伴必須是在github或gitee 上star了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 [email protected] ", new { TabName="H_test" ,FieldName="DID"}).ToSql();
    string sql2= sqlClient.HiSql("select * from hi_tabmodel where [email protected] 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 [email protected] ", 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();