From 1eed5ac9c62f313b7954cb93b66c24c91b98e594 Mon Sep 17 00:00:00 2001 From: wenshao Date: Mon, 18 Mar 2013 11:36:45 +0800 Subject: [PATCH] bug fixed for sql parser --- .../sqlserver/parser/SQLServerExprParser.java | 5 + .../sql/sqlserver/SQLServerSelectTest13.java | 95 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 src/test/java/com/alibaba/druid/bvt/sql/sqlserver/SQLServerSelectTest13.java diff --git a/src/main/java/com/alibaba/druid/sql/dialect/sqlserver/parser/SQLServerExprParser.java b/src/main/java/com/alibaba/druid/sql/dialect/sqlserver/parser/SQLServerExprParser.java index d9c661d3e5..04a3fbf0d6 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/sqlserver/parser/SQLServerExprParser.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/sqlserver/parser/SQLServerExprParser.java @@ -22,6 +22,7 @@ import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop; import com.alibaba.druid.sql.dialect.sqlserver.ast.expr.SQLServerObjectReferenceExpr; import com.alibaba.druid.sql.parser.Lexer; import com.alibaba.druid.sql.parser.SQLExprParser; +import com.alibaba.druid.sql.parser.SQLSelectParser; import com.alibaba.druid.sql.parser.Token; public class SQLServerExprParser extends SQLExprParser { @@ -47,6 +48,10 @@ public class SQLServerExprParser extends SQLExprParser { return super.primary(); } + + public SQLServerSelectParser createSelectParser() { + return new SQLServerSelectParser(this); + } public SQLExpr primaryRest(SQLExpr expr) { if (lexer.token() == Token.DOTDOT) { diff --git a/src/test/java/com/alibaba/druid/bvt/sql/sqlserver/SQLServerSelectTest13.java b/src/test/java/com/alibaba/druid/bvt/sql/sqlserver/SQLServerSelectTest13.java new file mode 100644 index 0000000000..7591ecdcfd --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/sqlserver/SQLServerSelectTest13.java @@ -0,0 +1,95 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.bvt.sql.sqlserver; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser; +import com.alibaba.druid.sql.test.TestUtils; + +public class SQLServerSelectTest13 extends TestCase { + + public void test_simple() throws Exception { + String sql = "SELECT" + // + " a.* " + // + "FROM" + // + " (" + // + " SELECT" + // + " row_number () over (ORDER BY a.time_add DESC) ROW," + // + " a.detail_no AS detailNo," + // + " a.ba_id AS baId," + // + " a.ba_name AS baName," + // + " a.tran_no AS tranNo," + // + " a.tran_name AS tranName, " + // + " a.tran_type AS tranType, " + // + " a.balance_type AS balanceType, " + // + " a.detail_income AS detailIncome, " + // + " a.detail_payout AS detailPayout, " + // + " a.before_balance AS beforeBalance, " + // + " a.after_balance AS afterBalance, " + // + " a.time_add AS timeAdd, " + // + " a.user_add AS userAdd, " + // + " a.remark AS remark, " + // + " ( " + // + " SELECT " + // + " top 1 t.param_name " + // + " FROM " + // + " config.sys_params t " + // + " WHERE " + // + " t.param_type = ? " + // + " AND t.param_value = a.tran_type " + // + " ) AS tranTypeName " + // + " FROM " + // + " bussiness.account_detail a " + // + " WHERE " + // + " 1 = 1 " + // + " AND a.time_add >= ? " + // + " AND a.time_add <= ? " + // + " ) a " + // + "WHERE " + // + " a. ROW BETWEEN (?+ 1) " + // + "AND (?+?)"; // + + String expect = "SELECT a.*" + + "\nFROM (SELECT ROW_NUMBER() OVER (ORDER BY a.time_add DESC) AS ROW, a.detail_no AS detailNo, a.ba_id AS baId, a.ba_name AS baName, a.tran_no AS tranNo" + + "\n\t\t, a.tran_name AS tranName, a.tran_type AS tranType, a.balance_type AS balanceType, a.detail_income AS detailIncome, a.detail_payout AS detailPayout" + + "\n\t\t, a.before_balance AS beforeBalance, a.after_balance AS afterBalance, a.time_add AS timeAdd, a.user_add AS userAdd, a.remark AS remark" + + "\n\t\t, (" + + "\n\t\t\tSELECT TOP 1 t.param_name" + + "\n\t\t\tFROM config.sys_params t" + + "\n\t\t\tWHERE t.param_type = ?" + + "\n\t\t\t\tAND t.param_value = a.tran_type" + + "\n\t\t\t) AS tranTypeName" + + "\n\tFROM bussiness.account_detail a" + + "\n\tWHERE 1 = 1" + + "\n\t\tAND a.time_add >= ?" + + "\n\t\tAND a.time_add <= ?" + + "\n\t) a" + + "\nWHERE a.ROW BETWEEN ? + 1 AND ? + ?"; + + SQLServerStatementParser parser = new SQLServerStatementParser(sql); + SQLStatement stmt = parser.parseStatementList().get(0); + + String text = TestUtils.outputSqlServer(stmt); + + Assert.assertEquals(expect, text); + + System.out.println(text); + } +}