Database middleware Sharding-JDBC source code analysis-SQL analysis (5) update SQL

Database middleware Sharding-JDBC source code analysis-SQL analysis (5) update SQL

"Dubbo Implementation Principle and Source Code Analysis-A Collection of Excellent Works" "Netty Implementation Principle and Source Code Analysis-A Collection of Excellent Products"
"Spring Implementation Principles and Source Code Analysis-Collection of Excellent Works" "MyBatis implementation principle and source code analysis-a collection of excellent products"
"Spring MVC Implementation Principle and Source Code Analysis-A Collection of Excellent Products" "Database Entity Design Collection"
"Spring Boot Implementation Principles and Source Code Analysis-A Collection of Excellent Products" "Java Interview Questions + Java Study Guide"

Abstract: Original source http://www.iocoder.cn/Sharding-JDBC/sql-parse-5/"Taro Source" welcome to reprint, keep the abstract, thank you!

This article is mainly based on the official version of Sharding-JDBC 1.5.0



1 Overview

Pre-reading for this article:

This article shares the source code implementation of updating SQL parsing .

Update SQL parsing is much less complex than querying SQL parsing. Different databases are also much more unified in inserting SQL syntax. This article shares the MySQL update SQL parser MySQLUpdateParser .

There are 2 types of MySQL UPDATE syntax:

  • The first type: Single-table syntax

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]
 

  • The second: Multiple-table syntax

UPDATE [LOW_PRIORITY] [IGNORE] table_references
    SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
    [WHERE where_condition]
 

Sharding-JDBC currently only supports the first type. The second type is rarely used in business scenarios.

The main flow of Sharding-JDBC update SQL parsing is as follows:

//AbstractUpdateParser.java
@Override
public UpdateStatement parse() {
   sqlParser.getLexer().nextToken();//  UPDATE
   skipBetweenUpdateAndTable();// MYSQL   LOW_PRIORITY IGNORE
   sqlParser.parseSingleTable(updateStatement);// 
   parseSetItems();//  SET
   sqlParser.skipUntil(DefaultKeyword.WHERE);
   sqlParser.setParametersIndex(parametersIndex);
   sqlParser.parseWhere(updateStatement);
   return updateStatement;//  WHERE
}
 

Sharding-JDBC is collecting a list of companies using: Portal .
Your registration will allow more people to participate and use Sharding-JDBC. The portal
Sharding-JDBC will also be able to cover more business scenarios as a result. Portal,
register, Sao Nian! Portal

2. UpdateStatement

Update the SQL parsing result.

public final class UpdateStatement extends AbstractSQLStatement {
}
 

Yes, there are no other attributes.

We look at UPDATE t_user SET nickname = ?, age = ? WHERE user_id = ?the result of the analysis :

3. #parse()

3.1 #skipBetweenUpdateAndTable()

In UPDATEbetween some lexical and table name, had no effect on SQL routing and rewriting, were skipped.

//MySQLUpdateParser.java
@Override
protected void skipBetweenUpdateAndTable() {
   getSqlParser().skipAll(MySQLKeyword.LOW_PRIORITY, MySQLKeyword.IGNORE);
}

//OracleUpdateParser.java
@Override
protected void skipBetweenUpdateAndTable() {
   getSqlParser().skipIfEqual(OracleKeyword.ONLY);
}
 

3.2 #parseSingleTable()

Parsing table , see "SQL parsing (b) of the SQL analysis" of #parseSingleTable()the section .

3.3 #parseSetItems()

The SETsentence after parsing .

//AbstractUpdateParser.java
/**
*   SET  
*/
private void parseSetItems() {
   sqlParser.accept(DefaultKeyword.SET);
   do {
       parseSetItem();
   } while (sqlParser.skipIfEqual(Symbol.COMMA));//  ","  
}
/**
*   SET  
*/
private void parseSetItem() {
   parseSetColumn();
   sqlParser.skipIfEqual(Symbol.EQ, Symbol.COLON_EQ);
   parseSetValue();
}
/**
*   SET  
*/
private void parseSetColumn() {
   if (sqlParser.equalAny(Symbol.LEFT_PAREN)) {
       sqlParser.skipParentheses();
       return;
   }
   int beginPosition = sqlParser.getLexer().getCurrentToken().getEndPosition();
   String literals = sqlParser.getLexer().getCurrentToken().getLiterals();
   sqlParser.getLexer().nextToken();
   if (sqlParser.skipIfEqual(Symbol.DOT)) {// 
      //TableToken
       if (updateStatement.getTables().getSingleTableName().equalsIgnoreCase(SQLUtil.getExactlyValue(literals))) {
           updateStatement.getSqlTokens().add(new TableToken(beginPosition - literals.length(), literals));
       }
       sqlParser.getLexer().nextToken();
   }
}
/**
*   SET  
*/
private void parseSetValue() {
   sqlParser.parseExpression(updateStatement);
   parametersIndex = sqlParser.getParametersIndex();
}
 

3.4 #parseWhere()

Parse the WHERE condition. Parsing code: #parseWhere() section of "SQL Parsing (2) SQL Parsing" .

666. Easter eggs

Is it simpler than updating SQL parsing, let alone comparing query SQL parsing. There is a feeling of being more in the water. Hey, the next article ( "Delete SQL Analysis" ) will be easier.

Fellow Daoist, how about sharing a wave for me?