当前位置:网站首页>C # implementation defines a set of SQL statements that can be executed across databases in the middle of SQL (detailed explanation of the case)
C # implementation defines a set of SQL statements that can be executed across databases in the middle of SQL (detailed explanation of the case)
2022-07-04 19:18:00 【1024 questions】
hisql Query sample
Single table query
group by Inquire about
having Inquire about
join Multi-table query
Pagination
hisql Realize parameterization
Chain query
hisql Statements and chained queries are mixed
At present, there are many kinds of data , Every kind of data supports sql sentence , But we found that there is no data of every kind SQL Each has its own grammatical characteristics , All are SQL Statements do not have a specific grammatical standard , As a result, developers cannot choose libraries arbitrarily in the process of development ( If you use a library you are not familiar with, you will have to learn it again SQL grammar ), The main reason is that the learning cost is too high
Is there a tool that can unify sql Rule of grammar , Just learn one sql Statement can be executed across libraries , So that developers do not have to learn every kind of Library SQL Sentence? ?
Now let's introduce an open source tool hisql Source code hisql Introduce
hisql Query sample Single table queryaccording to hisql Statement passing ToSql() Method to generate the target sql The native 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 Generated sqlserver Of sql As shown below
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 Generate mysql Of sql As shown below
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 Generate postgresql Of sql Shown below
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'
You can ask Obviously "select * from HTest01 where CreateTime>='2022-02-17 09:27:50' and CreateTime<='2022-03-22 09:27:50'"
With *
Inquire about Generated sql Why does the statement list all fields directly ? Ripe HiSql Netizens should know that a function hidden here is that users can customize the sorting of fields in the system table Hi_FieldModel
Can be configured in
hisql Common functions supported by default 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 Generated sqlserver Of sql As shown below
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 Generate mysql Of sql As shown below
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 Generate postgresql Of sql Shown below
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 Inquire about Last time, some netizens said having It's troublesome to implement, and it's difficult to implement regular expression parsing , I can only say that many netizens don't understand regular expressions very well , Regular expressions have been applied to various types of compilers , Let's demonstrate 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 Generated sqlserver Of sql As shown below
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 Generate mysql Of sql As shown below
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 Generate postgresql Of sql Shown below
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 Multi-table query hisql inner join and in Operation grammar
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 Generated sqlserver Of sql As shown below
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 Generate mysql Of sql As shown below
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 Generate postgresql Of sql Shown below
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)
Pagination hisql Pagination Just add Take()
How much data is displayed per page Skip()
Show the page
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 Generated sqlserver Of sql As shown below
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 Generate mysql Of sql As shown below
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_` asclimit 2,2
hisql Generate postgresql Of sql Shown below
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_" asclimit 2 OFFSET 2
hisql Realize parameterization Parameterization can effectively prevent injection , It can be spliced through the front end hisql The statement is passed into , This solves the injection problem
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();
Chain query hisql In addition to providing hisql In addition to the syntax, the chain query method is also provided as follows
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 Statements and chained queries are mixed 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();
This is about C# Implementation defines a set of intermediate SQL Can be executed across libraries SQL That's all for the statement article , More about C# Cross database execution SQL Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !
边栏推荐
- Scala基础教程--16--泛型
- Scala basic tutorial -- 17 -- Collection
- 基于lex和yacc的词法分析器+语法分析器
- Uni app and uviewui realize the imitation of Xiaomi mall app (with source code)
- 英特尔集成光电研究最新进展推动共封装光学和光互连技术进步
- Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
- 《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
- TorchDrug教程
- Is the securities account opened by qiniu safe?
- 神经网络物联网是什么意思通俗的解释
猜你喜欢
Scala基础教程--17--集合
How is the entered query SQL statement executed?
Principle and application of ThreadLocal
力扣刷题日记/day3/2022.6.25
Wireshark packet capturing TLS protocol bar displays version inconsistency
英特尔集成光电研究最新进展推动共封装光学和光互连技术进步
基于unity的愤怒的小鸟设计
神经网络物联网平台搭建(物联网平台搭建实战教程)
[release] a tool for testing WebService and database connection - dbtest v1.0
LeetCode第300场周赛(20220703)
随机推荐
How to open an account is safe,
Li Kou brush question diary /day3/2022.6.25
My colleagues quietly told me that flying Book notification can still play like this
What types of Thawte wildcard SSL certificates provide
Is Guoyuan futures a regular platform? Is it safe to open an account in Guoyuan futures?
sqlserver的CDC第一次查询的能读取到数据,但后面增删改读取不到,是什么原因
Unity给自己的脚本添加类似编辑器扩展的功能案例ContextMenu的使用
How to download files using WGet and curl
Other InterSystems%net tools
【机器学习的数学基础】(一)线性代数(Linear Algebra)(上+)
2022年字节跳动日常实习面经(抖音)
输入的查询SQL语句,是如何执行的?
Send and receive IBM WebSphere MQ messages
[cloud voice suggestion collection] cloud store renewal and upgrading: provide effective suggestions, win a large number of code beans, Huawei AI speaker 2!
神经网络物联网应用技术学什么
力扣刷题日记/day3/2022.6.25
Scala基础教程--17--集合
Is the securities account opened by qiniu safe?
ByteDance dev better technology salon was successfully held, and we joined hands with Huatai to share our experience in improving the efficiency of web research and development
[mathematical modeling of graduate students in Jiangxi Province in 2022] analysis and code implementation of haze removal by nucleation of water vapor supersaturation