当前位置:网站首页>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 !
边栏推荐
- 神经网络物联网应用技术就业前景【欢迎补充】
- Uni app and uviewui realize the imitation of Xiaomi mall app (with source code)
- Perfect JS event delegation
- 基于C语言的菜鸟驿站管理系统
- 问下各位大佬有用过cdc直接mysql to clickhouse的么
- 完善的js事件委托
- 使用SSH
- 《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
- Other InterSystems%net tools
- 6.26cf simulation match B: solution to array reduction problem
猜你喜欢
自由小兵儿
Li Kou brush question diary /day3/2022.6.25
[mathematical modeling of graduate students in Jiangxi Province in 2022] analysis and code implementation of haze removal by nucleation of water vapor supersaturation
物联网应用技术的就业前景和现状
One question per day (2022-07-02) - Minimum refueling times
MXNet对GoogLeNet的实现(并行连结网络)
Mxnet implementation of googlenet (parallel connection network)
LeetCode第300场周赛(20220703)
基于lex和yacc的词法分析器+语法分析器
一、C语言入门基础
随机推荐
ESP32-C3入门教程 问题篇⑫——undefined reference to rom_temp_to_power, in function phy_get_romfunc_addr
C#实现定义一套中间SQL可以跨库执行的SQL语句(案例详解)
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
Download the first Tencent technology open day course essence!
Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg
Process of manually encrypt the mass-producing firmware and programming ESP devices
Scala basic tutorial -- 19 -- actor
Uni app and uviewui realize the imitation of Xiaomi mall app (with source code)
ThreadLocal原理与使用
2022年字节跳动日常实习面经(抖音)
Cache é JSON uses JSON adapters
删除字符串中出现次数最少的字符【JS,Map排序,正则】
Using SSH
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
一、C语言入门基础
[opencv introduction to mastery 9] opencv video capture, image and video conversion
Halcon模板匹配
性能优化之关键渲染路径
What types of Thawte wildcard SSL certificates provide