当前位置:网站首页>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(*) > 1hisql 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(*) > 1hisql 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(*) > 1join 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_ aschisql 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,2hisql 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 2hisql 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 !
边栏推荐
- Using FTP
- File processing examples of fopen, FREAD, fwrite, fseek
- 使用SSH
- Is the securities account opened by qiniu safe?
- How is the entered query SQL statement executed?
- 【机器学习的数学基础】(一)线性代数(Linear Algebra)(上+)
- Go microservice (II) - detailed introduction to protobuf
- redis分布式锁的8大坑总结梳理
- The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology
- What if the self incrementing ID of online MySQL is exhausted?
猜你喜欢

Nature Microbiology | 可感染阿斯加德古菌的六种深海沉积物中的病毒基因组

2022CoCa: Contrastive Captioners are Image-Text Fountion Models

The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology

Halcon template matching

Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii

Li Kou brush question diary /day4/6.26

Process of manually encrypt the mass-producing firmware and programming ESP devices

Scala基础教程--15--递归

从实时应用角度谈通信总线仲裁机制和网络流控

LeetCode第300场周赛(20220703)
随机推荐
Uni app and uviewui realize the imitation of Xiaomi mall app (with source code)
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
[mathematical basis of machine learning] (I) linear algebra (Part 1 +)
Perfect JS event delegation
Angry bird design based on unity
[go ~ 0 to 1] read, write and create files on the sixth day
奥迪AUDI EDI INVOIC发票报文详解
小发猫物联网平台搭建与应用模型
6.26CF模拟赛E:价格最大化题解
Li Kou brush question diary /day2/2022.6.24
A method of using tree LSTM reinforcement learning for connection sequence selection
6.26cf simulation match B: solution to array reduction problem
Technology sharing | interface testing value and system
使用FTP
DeFi生态NFT流动性挖矿系统开发搭建
How is the entered query SQL statement executed?
Go microservice (II) - detailed introduction to protobuf
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
Scala basic tutorial -- 19 -- actor
ThreadLocal原理与使用