Structured SQL writing logic, writing and reading SQL as if writing an article outline

Structured SQL writing logic, writing and reading SQL as if writing an article outline

2022-10-28 0 644
Resource Number 47066 Last Updated 2025-02-21
¥ 0USD Upgrade VIP
Download Now Matters needing attention
Can't download? Please contact customer service to submit a link error!
Value-added Service: Installation Guide Environment Configuration Secondary Development Template Modification Source Code Installation

This issue recommends an open source structured sql generator called SQl-GeneratorPublic.

Structured SQL writing logic, writing and reading SQL as if writing an article outline插图

If you want to write a complex SQL, and a lot of SQL code is similar but not the same. Then it is recommended to use this tool, you do not have to write SQL repeatedly, more conducive to modification, maintenance and understanding. Especially in big data analysis scenarios, there is often a need to write complex SQL, which you can use as a repeat code generator.

Function

  • Structured SQL writing logic, writing and reading SQL as if writing an article outline
  • The repeated SQL needs to be written only once, and one place can be modified when the SQL changes
  • Can pass parameters and debug some SQL
  • View the reference tree and replacement process of SQL statements for easy analysis and understanding of SQL

Structured SQL writing logic, writing and reading SQL as if writing an article outline插图1

Advantages

  • Support online editing JSON and SQL, support code highlighting, syntax verification, one-click formatting, find and replace, code block folding, etc., good experience
  • Support one-click SQL
  • Support transparent transmission of parameters, such as @a(xx = #{yy}), yy variable can be passed to @a formula
  • Supports nested parameter passing (taking subqueries as parameters), such as @a(xx = @b(yy = 1))
  • does not limit what the user can write in JSON, so the tool can also be used as a repeat code generator
  • Support to view the call tree and replacement details of SQL statements, easy to analyze reference relationships

Implementation

Using the JavaScript that works best with JSON, write a logical JS file that can be applied to both browser and server side.

The function is relatively lightweight, so it is preferred to implement the pure browser side.

The front-end uses Vue3 + Vite + Ant Design Vue development interface, and chooses Monaco Editor to achieve code editing, highlighting, formatting and other functions. Ensure code specification with TypeScript + ESLint.

The SQL generation logic is as follows:

    1. JSON string to object
    2. Starting from the entry, replace the params static parameter first to get the current layer analysis
    3. Performs recursive parsing of @xxx syntax. During recursive parsing, static parameters are replaced first, and then call parameters from the outer layer are replaced

.

  1. Get the final SQL

The

parser was originally implemented as a regular non-greedy substitution, but could not implement nested calls, such as @a(xx = @b()), which would be recognized as @a(xx = @b(), matching the nearest closing parenthesis. Therefore, the subquery replacement algorithm is optimized for the case of parenthesis nesting, and the nested calls containing parenthesis statements are supported.

Parameter Description

    • Object key: defines the name of the SQL generation rule. main indicates the entry SQL, which is generated from the SQL statement.
    • Object value: Defines specific generation rules. It can be an SQL string or an object.
    • sql: defines a template SQL statement, which can be any string, such as a group of fields, a query condition, a calculation logic, and a complete SQL.
    • params: a static parameter that the parser preferentially replaces with

in the #{variable name} of the current statement

  • #{xxx} : defines variables that can be replaced with the current level params first, otherwise passed by the outer layer
  • @xxx(yy = 1 ||| zz = #{variable}) : references other SQL, can be passed parameters, parameters can be represented by variables, use ||| (three vertical bars) to separate the parameters.
{
"main": " Required, code is generated from here, using @ rule name () to reference other statements ",
" Rule name ": " can write any SQL statement @ rule name 2() @ dynamic parameter passing (a = to ||| b = star)",
" Rule name 2": {
"sql": " Specify replaceable values with #{parameter name} ",
"params": {
" Parameter name ": " Specifies static parameters in params, which are replaced preferentially "
}
},
< span class = "HLJS - attr" > "dynamic mass participation" : < / span > < span class = "HLJS - string" > "# {a} fishskin # {b}" < / span >
}

Example

Requirement: Calculate the height difference between two students with id = 1 and id = 2

The SQL looks something like this:

select (s1.height - s2.height) as Height difference
from
(select * from student where id = 1) s1,
(select * from student where id = 2)  s2

Obviously, the above SQL secondary table query twice, and except for the query id is different, the query logic is completely the same!

If the logic of querying students is modified, the above two sub-queries must be modified at the same time, which is not conducive to maintenance.

With this tool, you can automatically generate a complete SQL by writing the following JSON:

{
"main":  < span class = "HLJS - string" > "select @ height difference () from (@ students table (id = 1)), s1 (@ students table (id = 2)) s2" < / span >,
" Height difference ": "(s1.height-s2.height) as height difference ",
" student table ": "select * from student where id = #{id}"
}

With a method like function call + parameter passing, we do not need to write SQL repeatedly, and the logic of the entire SQL is clearer! Of course, the above is just an example, the real big data offline analysis scenario, SQL is N times more complex than this!

Complex example

Demand: Use a SQL to query the following table

Structured SQL writing logic, writing and reading SQL as if writing an article outline插图2

What is the difficulty in this table?

    1. The difference between checking the summary and finding out the fine particle size can not be distinguished by group by union (red)
    2. data in different rows in the classification column are crossed, which can only be distinguished by union

instead of group by.

    1. Each column is joined by multiple tables, and different tables can be associated with different categories, so it is necessary to distinguish (gray indicates that it cannot be associated), and complete the missing fields (otherwise, it cannot be union)
    2. The calculation formula for the same column in different rows may be different (blue)
    3. Different columns have different filtering conditions (for example, the last two dark green columns are to check the whole school, and the remaining columns are only to check Grade 1)
    4. To query the same chain, only two complete data can be joined and then misaligned to get

.

Clearly, much of the query logic in this table is repetitive but different.

So, how many base table select will be included in the final SQL? How many times should each base table query be rewritten?

However, this table is only obtained after simplifying the actual demand of the fish skin, the actual demand is several times more complex!

Can you imagine how disgusting manual writing is? !

But with this tool, just write structured JSON like this:

{
"main":  "select (a/b-1) from (@check whole (date = today)) a, (@check whole (date = yesterday)) b",
" Check the whole ": " @Check grade () union @ Check Class 1 () union @ Check Class 2 () where date = #{date}",
" Check grade ": " @check summary _ Gender summary _ Gender group _ union @ Check summary _ Hobby summary (union @ Check summary _ Hobby group () union @ check summary _ Computer category summary () union @Search Summary _ Computer Category Group ()",
< span class = "HLJS - attr" > "check summary _ gender summary" < / span > < span class = "HLJS - string" > "@ check in addition to the computer associated table ()" < / span >,
" Search summary _ Gender grouping ": "@ Search computer association table () group by gender ",
< span class = "HLJS - attr" > "check summary _ hobby summary" < / span > < span class = "HLJS - string" > "@ check in addition to the computer associated table ()" < / span >,
" Search summary _ Hobby groups ": "@ Search computer association table () where hobbies in (xx) group by hobbies ",
< span class = "HLJS - attr" > "check summary _ computer category summary" < / span > < span class = "HLJS - string" > "@ check table in addition to the three even and learning ()" < / span >,
" Check summary _ Computer Category group ": "@ Check group by Computer Category () ",

	" Check Class 1 ": " @check class 1 _ Gender summary () union @check class 1 _ Gender grouping () union @check class 1 _ Hobby summary () union @check class 1 _ Hobby grouping () union @check class 1 _ Computer category summary () union @Search Summary _ Computer Category Group ()",
" Check class 1 _ Gender summary ": "@ Check computer association table () where Class 1 ",
" Check class 1 _ Gender grouping ": "@ Check computer association table () where Class 1 group by gender ",
" Check class 1 _ Hobby summary ": "@ Check computer association table () where Class 1 ",
" Check class 1 _ Hobby Group ": "@ Search computer association table () where 1 class and hobbies in (xx) group by hobbies ",
" Check class 1 _ Computer Category Summary ": "@ Check triple and learning table () where Class 1 ",
" Check class 1 _ Computer Category group ": "@ Check triple and learning table () where Class 1 group by Computer category ",

" Check Class 2 ": "@ Check Class 2 _ Gender summary () union @ Check Class 2 _ Gender grouping () union @ Check Class 2 _ Computer Category Summary () union @ Check Class 2 _ Computer Category grouping ()",
" Check class 2 _ Gender summary ": "@ Check computer association table () where Class 2 ",
" Check class 2 _ Gender grouping ": "@ Check computer association table () where Class 2 group by gender ",
" Check class 2 _ Computer Category Summary ": "@ Check triple and learning table () where Class 2 ",
" Check class 2 _ Computer Category group ": "@ Check triple and learning table () where Class 2 group by Computer category ",

" Look up all associated tables ": " @Check information table () left join (@check three-link table ()) left join (@check learning table ()) left join (@check computer table ()) left join (@Check the whole school information ())",
" Delete computer association table ": " @check information table () left join (@check study table ()) left join (@check whole school information ())",
" Check the triple sum learning table ": " @check information table () left join (@check computer table ()) left join (@check school information ())",
" Look up information table ": "select field from information table where grade = 1",
" Check the triple table ": "select field from the triple table where grade = 1",
" Look up study table ": "select field from study table where grade = 1",
" Check the computer table ": "select field from the computer table where grade = 1",
" Check the whole school information ": "select field from Information table "
}

can automatically generate SQL, you can also view the call relationship, very clear:

Structured SQL writing logic, writing and reading SQL as if writing an article outline插图3

—END—

Open Source protocol: Apache2.0

Author: liyupi

资源下载此资源为免费资源立即下载
Telegram:@John_Software

Disclaimer: This article is published by a third party and represents the views of the author only and has nothing to do with this website. This site does not make any guarantee or commitment to the authenticity, completeness and timeliness of this article and all or part of its content, please readers for reference only, and please verify the relevant content. The publication or republication of articles by this website for the purpose of conveying more information does not mean that it endorses its views or confirms its description, nor does it mean that this website is responsible for its authenticity.

Ictcoder Free source code Structured SQL writing logic, writing and reading SQL as if writing an article outline https://ictcoder.com/kyym/structured-sql-writing-logic-writing-and-reading-sql-as-if-writing-an-article-outline.html

Share free open-source source code

Q&A
  • 1, automatic: after taking the photo, click the (download) link to download; 2. Manual: After taking the photo, contact the seller to issue it or contact the official to find the developer to ship.
View details
  • 1, the default transaction cycle of the source code: manual delivery of goods for 1-3 days, and the user payment amount will enter the platform guarantee until the completion of the transaction or 3-7 days can be issued, in case of disputes indefinitely extend the collection amount until the dispute is resolved or refunded!
View details
  • 1. Heptalon will permanently archive the process of trading between the two parties and the snapshots of the traded goods to ensure that the transaction is true, effective and safe! 2, Seven PAWS can not guarantee such as "permanent package update", "permanent technical support" and other similar transactions after the merchant commitment, please identify the buyer; 3, in the source code at the same time there is a website demonstration and picture demonstration, and the site is inconsistent with the diagram, the default according to the diagram as the dispute evaluation basis (except for special statements or agreement); 4, in the absence of "no legitimate basis for refund", the commodity written "once sold, no support for refund" and other similar statements, shall be deemed invalid; 5, before the shooting, the transaction content agreed by the two parties on QQ can also be the basis for dispute judgment (agreement and description of the conflict, the agreement shall prevail); 6, because the chat record can be used as the basis for dispute judgment, so when the two sides contact, only communicate with the other party on the QQ and mobile phone number left on the systemhere, in case the other party does not recognize self-commitment. 7, although the probability of disputes is very small, but be sure to retain such important information as chat records, mobile phone messages, etc., in case of disputes, it is convenient for seven PAWS to intervene in rapid processing.
View details
  • 1. As a third-party intermediary platform, Qichou protects the security of the transaction and the rights and interests of both buyers and sellers according to the transaction contract (commodity description, content agreed before the transaction); 2, non-platform online trading projects, any consequences have nothing to do with mutual site; No matter the seller for any reason to require offline transactions, please contact the management report.
View details

Related Article

make a comment
No comments available at the moment
Official customer service team

To solve your worries - 24 hours online professional service