当前位置:网站首页>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 !
边栏推荐
- Build your own website (15)
- Scala basic tutorial -- 14 -- implicit conversion
- Pb extended DLL development (super chapter) (VII)
- What types of Thawte wildcard SSL certificates provide
- 学习路之PHP--phpstudy创建项目时“hosts文件不存在或被阻止打开”
- IBM WebSphere MQ检索邮件
- 6.26CF模拟赛E:价格最大化题解
- 删除字符串中出现次数最少的字符【JS,Map排序,正则】
- Send and receive IBM WebSphere MQ messages
- 小发猫物联网平台搭建与应用模型
猜你喜欢
[2022 Jiangxi graduate mathematical modeling] curling movement idea analysis and code implementation
[go ~ 0 to 1] read, write and create files on the sixth day
Li Kou brush question diary /day1/2022.6.23
Scala基础教程--17--集合
Li Kou brush question diary /day2/2022.6.24
性能优化之关键渲染路径
更安全、更智能、更精致,长安Lumin完虐宏光MINI EV?
输入的查询SQL语句,是如何执行的?
Deleting nodes in binary search tree
奥迪AUDI EDI INVOIC发票报文详解
随机推荐
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
How to open an account is safe,
Interpretation of SIGMOD '22 hiengine paper
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
Scala基础教程--18--集合(二)
1、 Introduction to C language
奥迪AUDI EDI INVOIC发票报文详解
Halcon template matching
技术分享 | 接口测试价值与体系
Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
Build your own website (15)
PB的扩展DLL开发(超级篇)(七)
基于unity的愤怒的小鸟设计
One question per day (2022-07-02) - Minimum refueling times
Go微服务(二)——Protobuf详细入门
信息学奥赛一本通 1336:【例3-1】找树根和孩子
【机器学习的数学基础】(一)线性代数(Linear Algebra)(上+)
6.26CF模拟赛B:数组缩减题解
[mathematical modeling of graduate students in Jiangxi Province in 2022] analysis and code implementation of haze removal by nucleation of water vapor supersaturation
repeat_P1002 [NOIP2002 普及组] 过河卒_dp