c# Implementation defines a set of intermediate SQL Can be executed across libraries SQL sentence
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 query
according 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
group by Inquire about
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_` asc
limit 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_" asc
limit 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();
c# Implementation defines a set of intermediate SQL Can be executed across libraries SQL More related articles on sentence
- laravel Native execution across Libraries sql sentence
Perform a native sql Return result set
- SQL Server Solution to the error of copying table data across libraries
SQL Server The solution of copying table data across Libraries Copy table data across Libraries , There are many ways , The most common is to write programs to batch import data , But this method is not the optimal one , Today we use a very sharp method , Can be perfect in Sql Serv ...
- SQL Server Notes on the method of copying tables across Libraries
insert into tableA (column1,column2.....) SELECT * FROM OPENDATASOURCE('SQLOLEDB', 'Data Source=127. ...
- PCB MS SQL Cross database execution SQL Get the return value
One .SQL Cross database execution SQL Get the return value ) DECLARE @sql nvarchar(MAX) DECLARE @layer INT SET @Dblink = 'P2.fp_db.dbo.' sel ...
- SQL Server Cross library and cross server access
We often encounter a database to access another database , Or one server needs to access the database in another server . So how does this come true ? I believe you will understand after reading this article ! The same server cross library access implementation 1. First, create two databases Cro ...
- SQL Server- focusing sp_executesql Execution dynamics SQL Query performance is really better than exec good ?
Preface We've talked about dynamics before SQL Query? ? Why do we discuss this again ? Because there are still some problems , As the title says , A lot of interview questions or some blogs are talking about the implementation of dynamic SQL When inquiring sp_executesql Performance ratio of ex ...
- SQL Server Cross database query
Mode one : sentence SELECT * FROM database A.dbo. surface A a, database B.dbo. surface B b WHERE a.field=b.field "DBO" It can be omitted Such as SELEC ...
- SQL Server Synchronize data across Libraries
Recently, there is a need for data synchronization across databases , Two databases are distributed on two physical computers , Automatic periodic synchronization can be achieved through SQL Server Agent job to achieve , But the premise is that you need to write a stored procedure to realize synchronous logical processing . The stored procedure here doesn't use op ...
- sql server Across the library operation
SELECT *FROM OPENDATASOURCE('SQLOLEDB','Data Source=sql Server name ;User ID= user name ;Password= password ;').PersonDb.dbo. ...
- SQL Server Cross database query
1. Turn on Ad Hoc Distributed Queries Components , stay sql Execute the following statement in the query editor : reconfigure reconfigure 2. Cross database query operation select * from ...
Random recommendation
- nginx+iis Load balancing
Recently in the research of distributed system architecture knowledge , Including load balancing , Database read write separation , Distributed cache redis etc. . This article starts with the load balancing service architecture , The definition of Baidu Encyclopedia about load balancing is as follows : Load balancing , English name is Load Balance, Its ...
- T-SQL Recipes And Database Backups
The Problem stay DBA and T-SQL In my daily work , For example, routine examination , Service management , Database management , It's one of the most challenging areas . In similar tasks , For example, index fragmentation management , Statistics management , Database backup is extremely important , To any ...
- Swift Developing learning -02 Variables and constants
This course is self-taught IOS/Swift Summary of knowledge , Apply to through to Objictive C The use of programming , And we need to understand that based on iOS programmatic iPhone and iPad The programmer . Make a bigger Of ’ Siege lions ‘, Work with dignity , ...
- [ turn ] Give Way Sublime Text2 Support preview in browser
from http://www.imququ.com/post/view-sublime-text-2-file-in-browser.html 1. Click menu Tools -> New Plugin.. ...
- hdu4771 Stealing Harry Potter&#39;s Precious
Be careful -- You may explode the memory -- Suppose a direct search term -- Queue memory elements are reduced to -- #include<iostream> #include<map> #include<st ...
- Lao Li recommends : The first 5 Chapter 5 section 《MonkeyRunner Source analysis 》Monkey Principle analysis - Startup and operation : Get system service reference
Lao Li recommends : The first 5 Chapter 5 section <MonkeyRunner Source analysis >Monkey Principle analysis - Startup and operation : Get system service reference In the last section we described monkey The command to handle the entry function run How to call optionP ...
- Message queue RabbitMQ And Spring Integrate
1.RabbitMQ brief introduction RabbitMQ It's a popular open source message queuing system , use erlang Language development .RabbitMQ yes AMQP( Advanced message queue protocol ) Standard implementation of . Official website :http://www.rabbitmq.c ...
- Thread Class explanation Multithreading II ( Two )
Java.lang.Thread yes Java Application programmer pair Java The first stop for multithreading ,Thread That's right Java The abstraction of the thread itself So in Java In the concept of thread programming , One Thread example == One thread What are threads ...
- ansible Common commands
One .ansible Common commands One .ansible Common parameters of command ansible Many modules are provided by default for us to use . stay Linux in , We can go through ansible-doc -l Command to view the current ansib ...
- python object-oriented Inherit The derived Combine
Please refer to blog for details :http://www.cnblogs.com/linhaifeng/articles/6182264.html#_label12 One . object-oriented object-oriented : Interaction between objects , Unpredictability ...