www.iocoder.cn/Sharding-JD… “Taro source” welcome to reprint, keep the summary, thank you!

This article is based on Sharding-JDBC 1.5.0 official release

  • 1. An overview of the
  • 2. UpdateStatement
  • 3. #parse()
    • 3.1 # skipBetweenUpdateAndTable ()
    • 3.2 # parseSingleTable ()
    • 3.3 # parseSetItems ()
    • 3.4 # parseWhere ()
  • 666. The eggs

πŸ™‚πŸ™‚πŸ™‚ follow wechat public number:

  1. RocketMQ/MyCAT/Sharding-JDBC all source code analysis article list
  2. RocketMQ/MyCAT/Sharding-JDBC δΈ­ζ–‡ 解 决 source GitHub address
  3. Any questions you may have about the source code will be answered carefully. Even do not know how to read the source can also ask oh.
  4. New source code parsing articles are notified in real time. It’s updated about once a week.
  5. Serious source communication wechat group.

1. An overview of the

Pre-reading:

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

Updating SQL parsing is much less complex than querying SQL parsing. Different databases in the insert SQL syntax is also more uniform. MySQL update SQL parser MySQLUpdateParser

MySQL UPDATE syntax:

  • 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]Copy the code
  • Multiple-table syntax
UPDATE [LOW_PRIORITY] [IGNORE] table_references
    SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
    [WHERE where_condition]Copy the code

Currently, only the first type of SHARding-JDBC is supported. The second is rarely used in business scenarios.

Sharding-jdbc update SQL parsing main process is as follows:

// AbstractUpdateParser.java @Override public UpdateStatement parse() { sqlParser.getLexer().nextToken(); / / skip the UPDATE skipBetweenUpdateAndTable (); / / skip the keywords, such as: MYSQL LOW_PRIORITY and IGNORE sqlParser. ParseSingleTable (updateStatement); ParseSetItems (); // Parse SET sqlParser. SkipUntil (DefaultKeyword.WHERE); sqlParser.setParametersIndex(parametersIndex); sqlParser.parseWhere(updateStatement); return updateStatement; // Parse WHERE}Copy the code

Sharding-JDBC is collecting a list of companies that use: Portals. πŸ™‚ Your registration will allow more people to participate and use Sharding-JDBC. The portal Sharding-JDBC will therefore be able to cover more business scenarios. Sign up for the portal, Slut! portal

2. UpdateStatement

Update the SQL parsing result.

public final class UpdateStatement extends AbstractSQLStatement {
}Copy the code

😈 Yes, no other properties.

UPDATE t_user SET nickname =? , age = ? WHERE user_id = ? The analytical results of

3. #parse()

3.1 # skipBetweenUpdateAndTable ()

There is some morphology between UPDATE and table names, which does not affect SQL routing and rewriting.

// MySQLUpdateParser.java
@Override
protected void skipBetweenUpdateAndTable() {
   getSqlParser().skipAll(MySQLKeyword.LOW_PRIORITY, MySQLKeyword.IGNORE);
}
// OracleUpdateParser.java
@Override
protected void skipBetweenUpdateAndTable() {
   getSqlParser().skipIfEqual(OracleKeyword.ONLY);
}Copy the code

3.2 # parseSingleTable ()

For table parsing, see #parseSingleTable() in SQL Parsing (2).

3.3 # parseSetItems ()

Parse the post-set statement.

/ / AbstractUpdateParser. Java/parsing multiple item SET * * * * / private void parseSetItems () {sqlParser. Accept (DefaultKeyword. SET); do { parseSetItem(); } while (sqlParser.skipIfEqual(Symbol.COMMA)); Private void parseSetItem() {parseSetColumn(); private void parseSetColumn(); sqlParser.skipIfEqual(Symbol.EQ, Symbol.COLON_EQ); parseSetValue(); Private void parseSetColumn() {if (sqlparser.equalany (symbol.left_paren)) {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)) {// Fields have aliases // TableToken if (updateStatement.getTables().getSingleTableName().equalsIgnoreCase(SQLUtil.getExactlyValue(literals))) { updateStatement.getSqlTokens().add(new TableToken(beginPosition - literals.length(), literals)); } sqlParser.getLexer().nextToken(); }} / parse a single SET value * * * * / private void parseSetValue () {sqlParser. ParseExpression (updateStatement); parametersIndex = sqlParser.getParametersIndex(); }Copy the code

3.4 # parseWhere ()

Parse the WHERE condition. Parsing code: #parseWhere() in SQL Parsing (2).

666. The eggs

😝 is not simpler than updating SQL parsing, let alone comparing query SQL parsing. 😳 has a more feeling in the water. Hey hey, the next article (delete SQL parsing) will be easier.

Hey, buddy, how about sharing a wave for me?