Merge pull request #269 from wenshao/master

0.2.12
This commit is contained in:
温高铁
2013-03-12 10:06:58 -07:00
25 changed files with 526 additions and 61 deletions
+4
View File
@@ -0,0 +1,4 @@
druid
========
Already moved to https://github.com/alibaba/druid !
+2 -2
View File
@@ -4,7 +4,7 @@
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>0.2.12-SNAPSHOT</version>
<version>0.2.12</version>
<packaging>jar</packaging>
<name>druid</name>
@@ -16,7 +16,7 @@
<spring.version>3.1.1.RELEASE</spring.version>
<junit.version>4.11</junit.version>
<gpg.skip>true</gpg.skip>
<gpg.skip>false</gpg.skip>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.6</jdk.version>
</properties>
@@ -875,7 +875,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
}
this.discardConnection(holder.getConnection());
pooledConnection.disable();
pooledConnection.disable(t);
}
throw sqlEx;
@@ -53,24 +53,26 @@ import com.alibaba.druid.support.logging.LogFactory;
*/
public class DruidPooledConnection extends PoolableWrapper implements javax.sql.PooledConnection, Connection {
private final static Log LOG = LogFactory.getLog(DruidPooledConnection.class);
private final static Log LOG = LogFactory.getLog(DruidPooledConnection.class);
protected Connection conn;
protected volatile DruidConnectionHolder holder;
protected TransactionInfo transactionInfo;
private final boolean dupCloseLogEnable;
private volatile boolean traceEnable = false;
private boolean disable = false;
private boolean closed = false;
private volatile boolean traceEnable = false;
private boolean disable = false;
private boolean closed = false;
private final Thread ownerThread;
private long connectedTimeNano;
private volatile boolean running = false;
private volatile boolean running = false;
private volatile boolean abandoned = false;
private volatile boolean abandoned = false;
private StackTraceElement[] connectStackTrace;
private Throwable disableError = null;
public DruidPooledConnection(DruidConnectionHolder holder){
super(holder.getConnection());
@@ -148,7 +150,7 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql.
if (rawStatement.getConnection().isClosed()) {
return;
}
LOG.error("clear parameter error", ex);
}
}
@@ -179,10 +181,15 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql.
}
public void disable() {
disable(null);
}
public void disable(Throwable error) {
this.traceEnable = false;
this.holder = null;
this.transactionInfo = null;
this.disable = true;
this.disableError = error;
}
public boolean isDisable() {
@@ -1012,8 +1019,16 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql.
}
public void checkState() throws SQLException {
if (holder == null || closed || disable) {
throw new SQLException("connection is closed");
if (holder == null) {
throw new SQLException("connection holder is null");
}
if (closed) {
throw new SQLException("connection closed");
}
if (disable) {
throw new SQLException("connection disabled", disableError);
}
}
@@ -55,7 +55,7 @@ public class SQLWithSubqueryClause extends SQLObjectImpl {
private static final long serialVersionUID = 1L;
protected SQLIdentifierExpr name;
protected final List<SQLName> columns = new ArrayList<SQLName>();
protected SQLSelectQuery subQuery;
protected SQLSelect subQuery;
@Override
protected void accept0(SQLASTVisitor visitor) {
@@ -75,11 +75,11 @@ public class SQLWithSubqueryClause extends SQLObjectImpl {
this.name = name;
}
public SQLSelectQuery getSubQuery() {
public SQLSelect getSubQuery() {
return subQuery;
}
public void setSubQuery(SQLSelectQuery subQuery) {
public void setSubQuery(SQLSelect subQuery) {
this.subQuery = subQuery;
}
@@ -15,6 +15,7 @@
*/
package com.alibaba.druid.sql.dialect.mysql.visitor;
import java.util.List;
import java.util.Map;
import com.alibaba.druid.sql.ast.SQLCommentHint;
@@ -342,7 +343,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
printAndAccept(x.getArguments(), ", ");
print(")");
}
if (x.getCharSetName() != null) {
print(" CHARACTER SET ");
print(x.getCharSetName());
@@ -521,6 +522,17 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
}
public boolean visit(SQLVariantRefExpr x) {
{
List<Object> parameters = this.getParameters();
int index = x.getIndex();
if (parameters != null && index >= 0 && index < parameters.size()) {
Object param = parameters.get(index);
printParameter(param);
return false;
}
}
if (x.isGlobal()) {
print("@@global.");
} else {
@@ -80,20 +80,58 @@ public class OracleExprParser extends SQLExprParser {
public boolean allowStringAdditive = false;
/**
* @formatter:off
*/
private static final String[] _aggregateFunctions = { //
"AVG", "CORR", "COVAR_POP", "COVAR_SAMP", "COUNT", //
"CUME_DIST", "DENSE_RANK", "FIRST", "FIRST_VALUE", //
"LAG", "LAST", "LAST_VALUE", "LEAD", "MAX", "MIN", //
"NTILE", "PERCENT_RANK", "PERCENTILE_CONT", "PERCENTILE_DISC", "RANK", //
"RATIO_TO_REPORT", "REGR_SLOPE", "REGR_INTERCEPT", "REGR_COUNT", "REGR_R2", //
"REGR_AVGX", "REGR_AVGY", "REGR_SXX", "REGR_SYY", "REGR_SXY", //
"ROW_NUMBER", "STDDEV", "STDDEV_POP", "STDDEV_SAMP", "SUM", //
"VAR_POP", "VAR_SAMP", "VARIANCE" //
private static final String[] AGGREGATE_FUNCTIONS = {
"AVG", //
"CORR", //
"COVAR_POP", //
"COVAR_SAMP", //
"COUNT", //
"CUME_DIST", //
"DENSE_RANK", //
"FIRST", //
"FIRST_VALUE", //
"LAG", //
"LAST", //
"LAST_VALUE", //
"LEAD", //
"MAX", //
"MIN", //
"NTILE", //
"PERCENT_RANK", //
"PERCENTILE_CONT", //
"PERCENTILE_DISC", //
"RANK", //
"RATIO_TO_REPORT", //
"REGR_SLOPE", //
"REGR_INTERCEPT", //
"REGR_COUNT", //
"REGR_R2", //
"REGR_AVGX", //
"REGR_AVGY", //
"REGR_SXX", //
"REGR_SYY", //
"REGR_SXY", //
"ROW_NUMBER", //
"STDDEV", //
"STDDEV_POP", //
"STDDEV_SAMP", //
"SUM", //
"VAR_POP", //
"VAR_SAMP", //
"VARIANCE" //
};
public OracleExprParser(Lexer lexer){
@@ -229,8 +267,8 @@ public class OracleExprParser extends SQLExprParser {
}
public boolean isAggreateFunction(String word) {
for (int i = 0; i < _aggregateFunctions.length; ++i) {
if (_aggregateFunctions[i].compareToIgnoreCase(word) == 0) {
for (int i = 0; i < AGGREGATE_FUNCTIONS.length; ++i) {
if (AGGREGATE_FUNCTIONS[i].compareToIgnoreCase(word) == 0) {
return true;
}
}
@@ -174,7 +174,7 @@ public class OracleSelectParser extends SQLSelectParser {
accept(Token.AS);
accept(Token.LPAREN);
entry.setSubQuery(query());
entry.setSubQuery(select());
accept(Token.RPAREN);
if (identifierEquals("SEARCH")) {
@@ -30,6 +30,18 @@ public class PGExprParser extends SQLExprParser {
public PGExprParser(Lexer lexer){
super(lexer);
}
public boolean isAggreateFunction(String word) {
String[] aggregateFunctions = { "AVG", "COUNT", "MAX", "MIN", "STDDEV", "SUM", "ROW_NUMBER" };
for (int i = 0; i < aggregateFunctions.length; ++i) {
if (aggregateFunctions[i].compareToIgnoreCase(word) == 0) {
return true;
}
}
return false;
}
@Override
public PGOrderBy parseOrderBy() {
@@ -123,7 +123,7 @@ public class SQLParser {
} else {
setErrorEndPos(lexer.pos());
throw new SQLParseException("syntax error, expect " + token + ", actual " + lexer.token() + " "
+ lexer.stringVal());
+ lexer.stringVal() + ", pos " + this.lexer.pos());
}
}
@@ -189,7 +189,7 @@ public class SQLSelectParser extends SQLParser {
accept(Token.AS);
accept(Token.LPAREN);
entry.setSubQuery(query());
entry.setSubQuery(select());
accept(Token.RPAREN);
withQueryClause.getEntries().add(entry);
@@ -700,56 +700,59 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
}
Object param = parameters.get(index);
printParameter(param);
return false;
}
public void printParameter(Object param) {
if (param == null) {
print("NULL");
return false;
return;
}
if (param instanceof Number //
|| param instanceof Boolean) {
print(param.toString());
return false;
return;
}
if (param instanceof String) {
SQLCharExpr charExpr = new SQLCharExpr((String) param);
visit(charExpr);
return false;
return;
}
if (param instanceof Date) {
print((Date) param);
return false;
return;
}
if (param instanceof InputStream) {
print("'<InputStream>");
return false;
return;
}
if (param instanceof Reader) {
print("'<Reader>");
return false;
return;
}
if (param instanceof Blob) {
print("'<Blob>");
return false;
return;
}
if (param instanceof NClob) {
print("'<NClob>");
return false;
return;
}
if (param instanceof Clob) {
print("'<Clob>");
return false;
return;
}
print("'" + param.getClass().getName() + "'");
return false;
}
public boolean visit(SQLDropTableStatement x) {
@@ -58,6 +58,7 @@ import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource;
import com.alibaba.druid.sql.ast.statement.SQLTableElement;
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import com.alibaba.druid.sql.ast.statement.SQLTruncateStatement;
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
import com.alibaba.druid.stat.TableStat;
@@ -153,11 +154,11 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
if (flag0) {
ident = ident.replaceAll("\"", "");
}
if (flag1) {
ident = ident.replaceAll("`", "");
}
if (flag2) {
ident = ident.replaceAll(" ", "");
}
@@ -869,15 +870,22 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
public boolean visit(SQLUpdateStatement x) {
setAliasMap();
setMode(x, Mode.Update);
String ident = x.getTableName().toString();
setCurrentTable(ident);
SQLName identName = x.getTableName();
if (identName != null) {
String ident = identName.toString();
setCurrentTable(ident);
TableStat stat = getTableStat(ident);
stat.incrementUpdateCount();
TableStat stat = getTableStat(ident);
stat.incrementUpdateCount();
Map<String, String> aliasMap = getAliasMap();
aliasMap.put(ident, ident);
Map<String, String> aliasMap = getAliasMap();
aliasMap.put(ident, ident);
} else {
x.getTableSource().accept(this);
}
accept(x.getItems());
accept(x.getWhere());
@@ -1004,7 +1012,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
public void endVisit(SQLAlterTableAddColumn x) {
}
@Override
public boolean visit(SQLRollbackStatement x) {
return false;
@@ -47,11 +47,9 @@ import com.alibaba.druid.support.logging.LogFactory;
*/
public final class JdbcUtils implements JdbcConstants {
private final static Log LOG = LogFactory.getLog(JdbcUtils.class);
private final static Log LOG = LogFactory.getLog(JdbcUtils.class);
private static final Properties driverUrlMapping = new Properties();
private static final Properties driverUrlMapping = new Properties();
static {
try {
@@ -213,13 +211,21 @@ public final class JdbcUtils implements JdbcConstants {
} else {
out.print(String.valueOf(objec));
}
} else if (type == Types.NULL) {
out.print("null");
} else {
Object objec = rs.getObject(columnIndex);
if (rs.wasNull()) {
out.print("null");
} else {
out.print(String.valueOf(objec));
if (objec instanceof byte[]) {
byte[] bytes = (byte[]) objec;
String text = HexBin.encode(bytes);
out.print(text);
} else {
out.print(String.valueOf(objec));
}
}
}
}
@@ -462,7 +468,7 @@ public final class JdbcUtils implements JdbcConstants {
return null;
}
}
public static Driver createDriver(String driverClassName) throws SQLException {
return createDriver(null, driverClassName);
}
@@ -479,7 +485,7 @@ public final class JdbcUtils implements JdbcConstants {
throw new SQLException(e.getMessage(), e);
}
}
try {
return (Driver) Class.forName(driverClassName).newInstance();
} catch (IllegalAccessException e) {
@@ -132,18 +132,21 @@ public class WallVisitorUtils {
checkCondition(visitor, x.getWhere());
if (Boolean.TRUE == getValue(where)) {
boolean isSimpleConstExpr = false;
if (where instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) where;
if (binaryOpExpr.getOperator() == SQLBinaryOperator.Equality
|| binaryOpExpr.getOperator() == SQLBinaryOperator.NotEqual) {
if (binaryOpExpr.getLeft() instanceof SQLIntegerExpr
&& binaryOpExpr.getRight() instanceof SQLIntegerExpr) {
return;
isSimpleConstExpr = true;
}
}
}
addViolation(visitor, x);
if (!isSimpleConstExpr) {
addViolation(visitor, x);
}
}
}
@@ -0,0 +1,39 @@
/*
* 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.filter.wall;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.wall.WallUtils;
/**
* SQLServerWallTest
*
* @author RaymondXiu
* @version 1.0, 2012-3-18
* @see
*/
public class MySqlWallTest3 extends TestCase {
public void test_stuff() throws Exception {
Assert.assertTrue(WallUtils.isValidateMySql(//
"SELECT COUNT(p.id) FROM TB_PRO p" + //
" INNER JOIN TB_Db b ON b.id =p.dbid " + //
" WHERE p.index_status='0' AND p.sc=?"));
}
}
@@ -0,0 +1,57 @@
/*
* 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.filter.wall;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.wall.WallCheckResult;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallProvider;
import com.alibaba.druid.wall.spi.MySqlWallProvider;
public class TenantSelectTest4 extends TestCase {
private String sql = "SELECT a.*,b.name " + //
"FROM vote_info a left join vote_item b on a.item_id=b.id " + //
"where 1=1 limit 1,10";
private WallConfig config = new WallConfig();
protected void setUp() throws Exception {
config.setTenantTablePattern("*");
config.setTenantColumn("tenant");
}
public void testMySql() throws Exception {
WallProvider.setTenantValue(123);
MySqlWallProvider provider = new MySqlWallProvider(config);
WallCheckResult checkResult = provider.check(sql);
Assert.assertEquals(0, checkResult.getViolations().size());
String resultSql = SQLUtils.toSQLString(checkResult.getStatementList(), JdbcConstants.MYSQL);
Assert.assertEquals("SELECT a.*, b.name" + //
"\nFROM vote_info a" + //
"\n\tLEFT JOIN vote_item b ON b.tenant = 123" + //
"\n\t\tAND a.item_id = b.id" + //
"\nWHERE a.tenant = 123" + //
"\n\tAND 1 = 1" + //
"\nLIMIT 1, 10", resultSql);
}
}
@@ -15,9 +15,10 @@
*/
package com.alibaba.druid.bvt.sql;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.util.JdbcUtils;
@@ -0,0 +1,60 @@
/*
* 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.mysql;
import java.util.List;
import junit.framework.Assert;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Column;
public class MySqlUpdateTest_1 extends MysqlTest {
public void test_0() throws Exception {
String sql = "UPDATE t_price, t_basic_store s " + //
"SET purchasePrice = :purchasePrice, operaterId = :operaterId, " + //
" operaterRealName = :operaterRealName, operateDateline = :operateDateline " + //
"WHERE goodsId = :goodsId AND s.id = storeId AND s.areaId = :areaId";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
statemen.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());
Assert.assertEquals(2, visitor.getTables().size());
Assert.assertEquals(8, visitor.getColumns().size());
Assert.assertEquals(4, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_price")));
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store")));
}
}
@@ -0,0 +1,21 @@
package com.alibaba.druid.bvt.sql.mysql;
import java.util.Arrays;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.util.JdbcConstants;
public class SQLUtilsTest extends TestCase {
public void test_format() throws Exception {
String formattedSql = SQLUtils.format("select * from t where id = ?", JdbcConstants.MYSQL,
Arrays.<Object> asList("abc"));
Assert.assertEquals("SELECT *" + //
"\nFROM t" + //
"\nWHERE id = 'abc'", formattedSql);
}
}
@@ -0,0 +1,77 @@
/*
* 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.oracle;
import java.util.List;
import junit.framework.Assert;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
public class OracleSelectTest40 extends OracleTest {
public void test_0() throws Exception {
String sql = //
"WITH a AS (" + //
" SELECT TO_CHAR(csl.create_time,'yyyyMMdd') create_time, cwl.client_key ck, csl.src_id src_id " + //
" FROM xxxx csl, xxxx cwl " + //
" WHERE 1 =1 AND csl.src_id = cwl.src_id AND csl.curr_url = cwl.curr_url " + //
" AND TO_CHAR(csl.create_time,'yyyyMMdd') BETWEEN ? " + //
" AND ? AND cwl.day = TO_CHAR(csl.create_time,'yyyyMMdd') " + //
" GROUP BY cwl.client_key, csl.src_id, csl.create_time ORDER BY csl.src_id )" + //
", b AS (" + //
" SELECT itn.buyerpaytime, itn.esc_orderid, itn.oldck, " + //
" MAX(con.PAYED_AMOUNT) gmv, MAX(con.buyer_onlyid) buyer " + //
" FROM xxxx itn, xxxx don, xxxx con " + //
" WHERE don.esc_orderid = itn.esc_orderid " + //
" AND con.esc_orderid = itn.esc_orderid " + //
" AND don.order_status IN (4,5,6,7,8) AND itn.buyerpaytime BETWEEN ? AND ? " + //
" GROUP BY itn.esc_orderid, itn.oldck, itn.buyerpaytime ) " + //
"SELECT MAX(cos.location) AS position , a.src_id AS srcid , " + //
" COUNT(DISTINCT b.esc_orderid) AS orders , SUM(b.gmv) AS uvGmv, " + //
" COUNT(DISTINCT buyer) AS buyers FROM a, b, cp_operate_statistics cos " + //
"WHERE a.ck = b.oldck(+) AND TO_CHAR(cos.day,'yyyyMMdd') = a.create_time " + //
" AND a.create_time = b.buyerpaytime AND a.src_id = cos.src_id GROUP BY a.src_id ORDER BY a.src_id"; //
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
statemen.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(2, visitor.getTables().size());
Assert.assertEquals(15, visitor.getColumns().size());
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("acduser.vw_acd_info", "xzqh")));
// Assert.assertTrue(visitor.getOrderByColumns().contains(new TableStat.Column("employees", "last_name")));
}
}
@@ -0,0 +1,57 @@
/*
* 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.postgresql;
import java.util.List;
import junit.framework.Assert;
import com.alibaba.druid.sql.PGTest;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
public class PGSelectTest9 extends PGTest {
public void test_0() throws Exception {
String sql = "SELECT res.restaurant_id, row_number() " + //
"over (order by ST_Distance(res.restaurant_point,GeomFromEWKT(?))*111.12 ) as row_id " + //
"FROM restaurant res where res.city_id = ? and res.is_delete =false and res.restaurant_id <1000000 limit 10";
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
statemen.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
Assert.assertEquals(4, visitor.getColumns().size());
Assert.assertEquals(1, visitor.getTables().size());
}
}
// select categoryId , offerIds from cnres.function_select_get_spt_p4p_offer_list (' 1031918 , 1031919 , 1037004 ') as
// a(categoryId numeric,offerIds character varying(4000))
// select memberId , offerIds from cnres.function_select_get_seller_hot_offer_list('\'gzyyd168\'') as a(memberId
// character varying(20),offerIds character varying(4000))
@@ -36,7 +36,7 @@ public class SQLServerRowNumberTest extends TestCase {
"WHERE TerritoryID IS NOT NULL " + //
" AND SalesYTD <> 0;";
String expect = "SELECT c.FirstName, c.LastName, ROW_NUMBER() OVER () ORDER BY SalesYTD DESC AS 'Row Number', s.SalesYTD, a.PostalCode" //
String expect = "SELECT c.FirstName, c.LastName, ROW_NUMBER() OVER (ORDER BY SalesYTD DESC) AS 'Row Number', s.SalesYTD, a.PostalCode" //
+ "\nFROM Sales.SalesPerson s"
+ "\n\tINNER JOIN Person.Contact c ON s.SalesPersonID = c.ContactID"
+ "\n\tINNER JOIN Person.Address a ON a.AddressID = c.ContactID" //
@@ -35,7 +35,7 @@ public class SQLServerRowNumberTest2 extends TestCase {
" WHERE RowNumber BETWEEN ? AND ?";
String expect = "SELECT *" +
"\nFROM (SELECT ROW_NUMBER() OVER () ORDER BY FAlertDate DESC, FAlertLevel, FAlertType AS RowNumber, *" +
"\nFROM (SELECT ROW_NUMBER() OVER (ORDER BY FAlertDate DESC, FAlertLevel, FAlertType) AS RowNumber, *" +
"\n\tFROM monitor_business" +
"\n\tWHERE FRemoveAlert = ?" +
"\n\t) temp_table" +
@@ -0,0 +1,52 @@
/*
* 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 SQLServerSelectTest12 extends TestCase {
public void test_simple() throws Exception {
String sql = "SELECT Row, Name " + //
"FROM(" + //
"SELECT ROW_NUMBER() OVER (ORDER BY ProductID) AS Row, Name " + //
"FROM Product " + //
") AS ProductsWithRowNumbers " + //
"WHERE Row >= 6 AND Row <= 10";
String expect = "SELECT Row, Name" + //
"\nFROM (SELECT ROW_NUMBER() OVER (ORDER BY ProductID) AS Row, Name" + //
"\n\tFROM Product" + //
"\n\t) ProductsWithRowNumbers" + //
"\nWHERE Row >= 6" + //
"\n\tAND Row <= 10";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
String text = TestUtils.outputSqlServer(stmt);
Assert.assertEquals(expect, text);
System.out.println(text);
}
}