SQL中间件源码分析:SQL 改写(2)

  • 时间:2018-06-20 22:25 作者:JAVA编程学习 来源:JAVA编程学习 阅读:175
  • 扫一扫,手机访问
摘要:1 // SQLRewriteEngine.java2 /**3 * 拼接 TableToken4 *5 * @param sqlBuilder SQL构建器6 * @param tableToken tableToken7 * @param count tableToken 在 sqlTokens

1 // SQLRewriteEngine.java

2 /**

3 * 拼接 TableToken

4 *

5 * @param sqlBuilder SQL构建器

6 * @param tableToken tableToken

7 * @param count tableToken 在 sqlTokens 的顺序

8 * @param sqlTokens sqlTokens

9 */

10 private void appendTableToken(final SQLBuilder sqlBuilder, final TableToken tableToken, final int count, final List sqlTokens) {

11 // 拼接 TableToken

12 String tableName = sqlStatement.getTables().getTableNames().contains(tableToken.getTableName()) ? tableToken.getTableName() : tableToken.getOriginalLiterals();

13 sqlBuilder.appendTable(tableName);

14 // 拼接 SQLToken 后面的字符串

15 int beginPosition = tableToken.getBeginPosition() + tableToken.getOriginalLiterals().length();

16 int endPosition = sqlTokens.size() - 1 == count ? originalSQL.length() : sqlTokens.get(count + 1).getBeginPosition();

17 sqlBuilder.appendLiterals(originalSQL.substring(beginPosition, endPosition));

18 }

SQL中间件源码分析:SQL 改写(2)

java

1 // TableToken.java

2 /**

3 * 获取表名称.

4 */

5 public String getTableName() {

6 return SQLUtil.getExactlyValue(originalLiterals);

7 }

8 // SQLUtil.java

9 public static String getExactlyValue(final String value) {

10 return null == value ? null : CharMatcher.anyOf("[]`'\"").removeFrom(value);

11 }

SQL中间件源码分析:SQL 改写(2)

java

1 // SQLRewriteEngine.java

2 /**

3 * 拼接 TableToken

4 *

5 * @param sqlBuilder SQL构建器

6 * @param itemsToken itemsToken

7 * @param count itemsToken 在 sqlTokens 的顺序

8 * @param sqlTokens sqlTokens

9 */

10 private void appendItemsToken(final SQLBuilder sqlBuilder, final ItemsToken itemsToken, final int count, final List sqlTokens) {

11 // 拼接 ItemsToken

12 for (String item : itemsToken.getItems()) {

13 sqlBuilder.appendLiterals(", ");

14 sqlBuilder.appendLiterals(item);

15 }

16 // SQLToken 后面的字符串

17 int beginPosition = itemsToken.getBeginPosition();

18 int endPosition = sqlTokens.size() - 1 == count ? originalSQL.length() : sqlTokens.get(count + 1).getBeginPosition();

19 sqlBuilder.appendLiterals(originalSQL.substring(beginPosition, endPosition));

20 }

SQL中间件源码分析:SQL 改写(2)

java

1 // SQLRewriteEngine.java

2 /**

3 * 拼接 OffsetToken

4 *

5 * @param sqlBuilder SQL构建器

6 * @param offsetToken offsetToken

7 * @param count offsetToken 在 sqlTokens 的顺序

8 * @param sqlTokens sqlTokens

9 * @param isRewrite 能否重写。当路由结果为单分片时无需重写

10 */

11 private void appendLimitOffsetToken(final SQLBuilder sqlBuilder, final OffsetToken offsetToken, final int count, final List sqlTokens, final boolean isRewrite) {

12 // 拼接 OffsetToken

13 sqlBuilder.appendLiterals(isRewrite ? "0" : String.valueOf(offsetToken.getOffset()));

14 // SQLToken 后面的字符串

15 int beginPosition = offsetToken.getBeginPosition() + String.valueOf(offsetToken.getOffset()).length();

16 int endPosition = sqlTokens.size() - 1 == count ? originalSQL.length() : sqlTokens.get(count + 1).getBeginPosition();

17 sqlBuilder.appendLiterals(originalSQL.substring(beginPosition, endPosition));

18 } }

视频资料链接:

data:text/html;charset=UTF-8;base64,

5oGt5Zac5L2g77yM5p625p6E5biI5a2m5Lmg576k5Y+35pivNTc1NzUxODU0Cg==

复制粘贴在网站打开就可

  • 全部评论(0)
手机二维码手机访问领取大礼包
返回顶部