From 28c26f5340a7b4b55024b1fdc6b0f9699492b504 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 1 Sep 2017 14:52:42 +0800 Subject: [PATCH] refactor WhereClauseParser 2nd version --- .../parser/clause/WhereClauseParser.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/clause/WhereClauseParser.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/clause/WhereClauseParser.java index 3e4364928893f..25de2dc796fb1 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/clause/WhereClauseParser.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/clause/WhereClauseParser.java @@ -85,27 +85,17 @@ private void parseComparisonCondition(final ShardingRule shardingRule, final SQL lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN); return; } - if (lexerEngine.skipIfEqual(Symbol.LT, Symbol.LT_EQ)) { - if (left instanceof SQLIdentifierExpression && sqlStatement instanceof SelectStatement - && isRowNumberCondition(items, ((SQLIdentifierExpression) left).getName())) { + if (sqlStatement instanceof SelectStatement && isRowNumberCondition(items, left)) { + if (lexerEngine.skipIfEqual(Symbol.LT, Symbol.LT_EQ)) { parseRowCountCondition((SelectStatement) sqlStatement); - } else if (left instanceof SQLPropertyExpression && sqlStatement instanceof SelectStatement - && isRowNumberCondition(items, ((SQLPropertyExpression) left).getName())) { - parseRowCountCondition((SelectStatement) sqlStatement); - } else { - parseOtherCondition(sqlStatement); + return; } - } else if (lexerEngine.skipIfEqual(Symbol.GT, Symbol.GT_EQ)) { - if (left instanceof SQLIdentifierExpression && sqlStatement instanceof SelectStatement - && isRowNumberCondition(items, ((SQLIdentifierExpression) left).getName())) { - parseOffsetCondition((SelectStatement) sqlStatement); - } else if (left instanceof SQLPropertyExpression && sqlStatement instanceof SelectStatement - && isRowNumberCondition(items, ((SQLPropertyExpression) left).getName())) { + if (lexerEngine.skipIfEqual(Symbol.GT, Symbol.GT_EQ)) { parseOffsetCondition((SelectStatement) sqlStatement); - } else { - parseOtherCondition(sqlStatement); + return; } - } else if (lexerEngine.skipIfEqual(Symbol.LT_GT, Symbol.BANG_EQ, Symbol.BANG_GT, Symbol.BANG_LT, DefaultKeyword.LIKE)) { + } + if (lexerEngine.skipIfEqual(Symbol.LT, Symbol.LT_EQ, Symbol.GT, Symbol.GT_EQ, Symbol.LT_GT, Symbol.BANG_EQ, Symbol.BANG_GT, Symbol.BANG_LT, DefaultKeyword.LIKE)) { parseOtherCondition(sqlStatement); } lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN); @@ -148,6 +138,16 @@ private void parseBetweenCondition(final ShardingRule shardingRule, final SQLSta } } + private boolean isRowNumberCondition(final List items, final SQLExpression sqlExpression) { + String columnLabel = null; + if (sqlExpression instanceof SQLIdentifierExpression) { + columnLabel = ((SQLIdentifierExpression) sqlExpression).getName(); + } else if (sqlExpression instanceof SQLPropertyExpression) { + columnLabel = ((SQLPropertyExpression) sqlExpression).getName(); + } + return null != columnLabel && isRowNumberCondition(items, columnLabel); + } + protected boolean isRowNumberCondition(final List items, final String columnLabel) { return false; }