mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed for SQLBetweenExpr output. for issue #2593
This commit is contained in:
@@ -496,8 +496,28 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
print0(ucase ? " BETWEEN " : " between ");
|
||||
}
|
||||
|
||||
printExpr(beginExpr);
|
||||
print0(ucase ? " AND " : " and ");
|
||||
int lines = this.lines;
|
||||
if (beginExpr instanceof SQLBinaryOpExpr) {
|
||||
SQLBinaryOpExpr binaryOpBegin = (SQLBinaryOpExpr) beginExpr;
|
||||
incrementIndent();
|
||||
if (binaryOpBegin.isBracket() || binaryOpBegin.getOperator().isLogical()) {
|
||||
print('(');
|
||||
printExpr(beginExpr);
|
||||
print(')');
|
||||
} else {
|
||||
printExpr(beginExpr);
|
||||
}
|
||||
decrementIndent();
|
||||
} else {
|
||||
printExpr(beginExpr);
|
||||
}
|
||||
|
||||
if (lines != this.lines) {
|
||||
println();
|
||||
print0(ucase ? "AND " : "and ");
|
||||
} else {
|
||||
print0(ucase ? " AND " : " and ");
|
||||
}
|
||||
|
||||
printExpr(endExpr);
|
||||
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright 1999-2017 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.mysql.select;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MySqlSelectTest_185 extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "select\n" +
|
||||
"product.brand_id,\n" +
|
||||
"product.sort,\n" +
|
||||
"product.old_product_id,\n" +
|
||||
"product.old_url_key,\n" +
|
||||
"categoryProduct.category_id\n" +
|
||||
"from catalog_product as product, catalog_category_product as categoryProduct\n" +
|
||||
"where product.brand_id = categoryProduct.brand_id and product.lang_id = categoryProduct.lang_id and\n" +
|
||||
"product.product_id = categoryProduct.product_id and product.brand_id = ? and product.lang_id = ? and\n" +
|
||||
"categoryProduct.category_id = ? and product.status = 1 and\n" +
|
||||
"product.more_color between (0 AND 1) and product.master_color = 1\n" +
|
||||
"order by product.sort asc";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
|
||||
|
||||
SQLSelect select = selectStmt.getSelect();
|
||||
assertNotNull(select.getQuery());
|
||||
MySqlSelectQueryBlock queryBlock = (MySqlSelectQueryBlock) select.getQuery();
|
||||
assertNotNull(queryBlock.getOrderBy());
|
||||
|
||||
// print(statementList);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
// System.out.println("coditions : " + visitor.getConditions());
|
||||
// System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
assertEquals(2, visitor.getTables().size());
|
||||
assertEquals(13, visitor.getColumns().size());
|
||||
assertEquals(10, visitor.getConditions().size());
|
||||
assertEquals(1, visitor.getOrderByColumns().size());
|
||||
|
||||
assertTrue(visitor.containsTable("catalog_product"));
|
||||
assertTrue(visitor.containsTable("catalog_category_product"));
|
||||
assertTrue(visitor.containsColumn("catalog_product", "brand_id"));
|
||||
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
assertEquals("SELECT product.brand_id, product.sort, product.old_product_id, product.old_url_key, categoryProduct.category_id\n" +
|
||||
"FROM catalog_product product, catalog_category_product categoryProduct\n" +
|
||||
"WHERE product.brand_id = categoryProduct.brand_id\n" +
|
||||
"\tAND product.lang_id = categoryProduct.lang_id\n" +
|
||||
"\tAND product.product_id = categoryProduct.product_id\n" +
|
||||
"\tAND product.brand_id = ?\n" +
|
||||
"\tAND product.lang_id = ?\n" +
|
||||
"\tAND categoryProduct.category_id = ?\n" +
|
||||
"\tAND product.status = 1\n" +
|
||||
"\tAND product.more_color BETWEEN (0\n" +
|
||||
"\t\tAND 1)\n" +
|
||||
"\tAND product.master_color = 1\n" +
|
||||
"ORDER BY product.sort ASC", //
|
||||
output);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user