This commit is contained in:
melin
2013-07-29 11:33:58 +08:00
16 changed files with 495 additions and 173 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
Copyright 1999-2011 Alibaba Group Holding Ltd.
Copyright 1999-2013 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.
@@ -48,7 +48,7 @@ public class StatementProxyImpl extends WrapperProxyImpl implements StatementPro
protected StatementExecuteType lastExecuteType;
protected Integer updateCount = null;
protected Integer updateCount = null;
private FilterChainImpl filterChain = null;
@@ -319,9 +319,11 @@ public class StatementProxyImpl extends WrapperProxyImpl implements StatementPro
@Override
public boolean getMoreResults() throws SQLException {
updateCount = null;
FilterChainImpl chain = createChain();
boolean value = chain.statement_getMoreResults(this);
if (value) {
updateCount = null;
}
recycleFilterChain(chain);
return value;
}
@@ -549,21 +551,21 @@ public class StatementProxyImpl extends WrapperProxyImpl implements StatementPro
public boolean isFirstResultSet() {
return firstResultSet;
}
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface == StatementProxy.class) {
return (T) this;
}
return super.unwrap(iface);
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
if (iface == StatementProxy.class) {
return true;
}
return super.isWrapperFor(iface);
}
@@ -92,7 +92,7 @@ public class PagerUtils {
if (JdbcConstants.DB2.equals(dbType)) {
return limitDB2(select, dbType, offset, count);
}
if (JdbcConstants.SQL_SERVER.equals(dbType)) {
return limitSQLServer(select, dbType, offset, count);
}
@@ -118,6 +118,14 @@ public class PagerUtils {
private static String limitDB2(SQLSelect select, String dbType, int offset, int count) {
SQLSelectQuery query = select.getQuery();
SQLBinaryOpExpr gt = new SQLBinaryOpExpr(new SQLIdentifierExpr("ROWNUM"), //
SQLBinaryOperator.GreaterThan, //
new SQLNumberExpr(offset));
SQLBinaryOpExpr lteq = new SQLBinaryOpExpr(new SQLIdentifierExpr("ROWNUM"), //
SQLBinaryOperator.LessThanOrEqual, //
new SQLNumberExpr(count + offset));
SQLBinaryOpExpr pageCondition = new SQLBinaryOpExpr(gt, SQLBinaryOperator.BooleanAnd, lteq);
if (query instanceof SQLSelectQueryBlock) {
DB2SelectQueryBlock queryBlock = (DB2SelectQueryBlock) query;
if (offset <= 0) {
@@ -137,25 +145,44 @@ public class PagerUtils {
countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
SQLBinaryOpExpr gt = new SQLBinaryOpExpr(new SQLIdentifierExpr("ROWNUM"), //
SQLBinaryOperator.GreaterThan, //
new SQLNumberExpr(offset));
SQLBinaryOpExpr lteq = new SQLBinaryOpExpr(new SQLIdentifierExpr("ROWNUM"), //
SQLBinaryOperator.LessThanOrEqual, //
new SQLNumberExpr(count + offset));
SQLBinaryOpExpr pageCondition = new SQLBinaryOpExpr(gt, SQLBinaryOperator.BooleanAnd, lteq);
countQueryBlock.setWhere(pageCondition);
return SQLUtils.toSQLString(countQueryBlock, dbType);
}
return SQLUtils.toSQLString(select, dbType);
DB2SelectQueryBlock countQueryBlock = new DB2SelectQueryBlock();
countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*")));
SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
SQLOrderBy orderBy = select.getOrderBy();
aggregateExpr.setOver(new SQLOver(orderBy));
select.setOrderBy(null);
countQueryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
if (offset <= 0) {
return SQLUtils.toSQLString(countQueryBlock, dbType);
}
DB2SelectQueryBlock offsetQueryBlock = new DB2SelectQueryBlock();
offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
offsetQueryBlock.setWhere(pageCondition);
return SQLUtils.toSQLString(offsetQueryBlock, dbType);
}
private static String limitSQLServer(SQLSelect select, String dbType, int offset, int count) {
SQLSelectQuery query = select.getQuery();
SQLBinaryOpExpr gt = new SQLBinaryOpExpr(new SQLIdentifierExpr("ROWNUM"), //
SQLBinaryOperator.GreaterThan, //
new SQLNumberExpr(offset));
SQLBinaryOpExpr lteq = new SQLBinaryOpExpr(new SQLIdentifierExpr("ROWNUM"), //
SQLBinaryOperator.LessThanOrEqual, //
new SQLNumberExpr(count + offset));
SQLBinaryOpExpr pageCondition = new SQLBinaryOpExpr(gt, SQLBinaryOperator.BooleanAnd, lteq);
if (query instanceof SQLSelectQueryBlock) {
SQLServerSelectQueryBlock queryBlock = (SQLServerSelectQueryBlock) query;
if (offset <= 0) {
@@ -165,32 +192,44 @@ public class PagerUtils {
SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
SQLOrderBy orderBy = select.getOrderBy();
if (orderBy != null) {
aggregateExpr.setOver(new SQLOver(orderBy));
select.setOrderBy(null);
}
aggregateExpr.setOver(new SQLOver(orderBy));
select.setOrderBy(null);
queryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
DB2SelectQueryBlock countQueryBlock = new DB2SelectQueryBlock();
SQLServerSelectQueryBlock countQueryBlock = new SQLServerSelectQueryBlock();
countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
SQLBinaryOpExpr gt = new SQLBinaryOpExpr(new SQLIdentifierExpr("ROWNUM"), //
SQLBinaryOperator.GreaterThan, //
new SQLNumberExpr(offset));
SQLBinaryOpExpr lteq = new SQLBinaryOpExpr(new SQLIdentifierExpr("ROWNUM"), //
SQLBinaryOperator.LessThanOrEqual, //
new SQLNumberExpr(count + offset));
SQLBinaryOpExpr pageCondition = new SQLBinaryOpExpr(gt, SQLBinaryOperator.BooleanAnd, lteq);
countQueryBlock.setWhere(pageCondition);
return SQLUtils.toSQLString(countQueryBlock, dbType);
}
return SQLUtils.toSQLString(select, dbType);
SQLServerSelectQueryBlock countQueryBlock = new SQLServerSelectQueryBlock();
countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*")));
countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
if (offset <= 0) {
countQueryBlock.setTop(new SQLServerTop(new SQLNumberExpr(count)));
return SQLUtils.toSQLString(countQueryBlock, dbType);
}
SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
SQLOrderBy orderBy = select.getOrderBy();
aggregateExpr.setOver(new SQLOver(orderBy));
select.setOrderBy(null);
countQueryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
SQLServerSelectQueryBlock offsetQueryBlock = new SQLServerSelectQueryBlock();
offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
offsetQueryBlock.setWhere(pageCondition);
return SQLUtils.toSQLString(offsetQueryBlock, dbType);
}
private static String limitOracle(SQLSelect select, String dbType, int offset, int count) {
@@ -1,36 +1,37 @@
/*
* 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.sql.ast;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public abstract class SQLStatementImpl extends SQLObjectImpl implements SQLStatement {
private static final long serialVersionUID = 1L;
public SQLStatementImpl(){
}
public void output(StringBuffer buf) {
buf.append(super.toString());
}
@Override
protected void accept0(SQLASTVisitor visitor) {
throw new UnsupportedOperationException(this.getClass().getName());
}
}
/*
* 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.sql.ast;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public abstract class SQLStatementImpl extends SQLObjectImpl implements SQLStatement {
private static final long serialVersionUID = 1L;
public SQLStatementImpl(){
}
public String toString() {
return SQLUtils.toSQLString(this);
}
@Override
protected void accept0(SQLASTVisitor visitor) {
throw new UnsupportedOperationException(this.getClass().getName());
}
}
@@ -15,6 +15,7 @@
*/
package com.alibaba.druid.sql.dialect.oracle.ast.stmt;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatementImpl;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
@@ -28,4 +29,8 @@ public abstract class OracleStatementImpl extends SQLStatementImpl implements Or
}
public abstract void accept0(OracleASTVisitor visitor);
public String toString() {
return SQLUtils.toOracleString(this);
}
}
@@ -102,6 +102,32 @@ public class WebURIStat {
private final static ThreadLocal<WebURIStat> currentLocal = new ThreadLocal<WebURIStat>();
private volatile long histogram_0_1;
private volatile long histogram_1_10;
private volatile long histogram_10_100;
private volatile long histogram_100_1000;
private volatile int histogram_1000_10000;
private volatile int histogram_10000_100000;
private volatile int histogram_100000_1000000;
private volatile int histogram_1000000_more;
final static AtomicLongFieldUpdater<WebURIStat> histogram_0_1_Updater = AtomicLongFieldUpdater.newUpdater(WebURIStat.class,
"histogram_0_1");
final static AtomicLongFieldUpdater<WebURIStat> histogram_1_10_Updater = AtomicLongFieldUpdater.newUpdater(WebURIStat.class,
"histogram_1_10");
final static AtomicLongFieldUpdater<WebURIStat> histogram_10_100_Updater = AtomicLongFieldUpdater.newUpdater(WebURIStat.class,
"histogram_10_100");
final static AtomicLongFieldUpdater<WebURIStat> histogram_100_1000_Updater = AtomicLongFieldUpdater.newUpdater(WebURIStat.class,
"histogram_100_1000");
final static AtomicIntegerFieldUpdater<WebURIStat> histogram_1000_10000_Updater = AtomicIntegerFieldUpdater.newUpdater(WebURIStat.class,
"histogram_1000_10000");
final static AtomicIntegerFieldUpdater<WebURIStat> histogram_10000_100000_Updater = AtomicIntegerFieldUpdater.newUpdater(WebURIStat.class,
"histogram_10000_100000");
final static AtomicIntegerFieldUpdater<WebURIStat> histogram_100000_1000000_Updater = AtomicIntegerFieldUpdater.newUpdater(WebURIStat.class,
"histogram_100000_1000000");
final static AtomicIntegerFieldUpdater<WebURIStat> histogram_1000000_more_Updater = AtomicIntegerFieldUpdater.newUpdater(WebURIStat.class,
"histogram_1000000_more");
public WebURIStat(String uri){
super();
this.uri = uri;
@@ -145,6 +171,8 @@ public class WebURIStat {
runningCountUpdater.decrementAndGet(this);
requestTimeNanoUpdater.addAndGet(this, nanos);
histogramRecord(nanos);
if (error != null) {
errorCountUpdater.incrementAndGet(this);
}
@@ -212,6 +240,28 @@ public class WebURIStat {
currentLocal.set(null);
}
private void histogramRecord(long nanos) {
final long millis = nanos / 1000 / 1000;
if (millis < 1) {
histogram_0_1_Updater.incrementAndGet(this);
} else if (millis < 10) {
histogram_1_10_Updater.incrementAndGet(this);
} else if (millis < 100) {
histogram_10_100_Updater.incrementAndGet(this);
} else if (millis < 1000) {
histogram_100_1000_Updater.incrementAndGet(this);
} else if (millis < 10000) {
histogram_1000_10000_Updater.incrementAndGet(this);
} else if (millis < 100000) {
histogram_10000_100000_Updater.incrementAndGet(this);
} else if (millis < 1000000) {
histogram_100000_1000000_Updater.incrementAndGet(this);
} else {
histogram_1000000_more_Updater.incrementAndGet(this);
}
}
public int getRunningCount() {
return this.runningCount;
}
@@ -364,6 +414,20 @@ public class WebURIStat {
return profiletat;
}
public long[] getHistogramValues() {
return new long[] {
//
histogram_0_1, //
histogram_1_10, //
histogram_10_100, //
histogram_100_1000, //
histogram_1000_10000, //
histogram_10000_100000, //
histogram_100000_1000000, //
histogram_1000000_more //
};
}
public WebURIStatValue getValue(boolean reset) {
WebURIStatValue val = new WebURIStatValue();
@@ -399,11 +463,20 @@ public class WebURIStat {
val.setLastAccessTimeMillis(get(this, lastAccessTimeMillisUpdater, reset));
val.setProfileEntryStatValueList(this.getProfiletat().getStatValue(reset));
val.histogram_0_1 = get(this, histogram_0_1_Updater, reset);
val.histogram_1_10 = get(this, histogram_1_10_Updater, reset);
val.histogram_10_100 = get(this, histogram_10_100_Updater, reset);
val.histogram_100_1000 = get(this, histogram_100_1000_Updater, reset);
val.histogram_1000_10000 = get(this, histogram_1000_10000_Updater, reset);
val.histogram_10000_100000 = get(this, histogram_10000_100000_Updater, reset);
val.histogram_100000_1000000 = get(this, histogram_100000_1000000_Updater, reset);
val.histogram_1000000_more = get(this, histogram_1000000_more_Updater, reset);
return val;
}
public Map<String, Object> getStatData() {
return getValue(false).getStatData();
return getValue(false).getStatData();
}
}
@@ -58,6 +58,27 @@ public class WebURIStatValue {
private List<ProfileEntryStatValue> profileEntryStatValueList;
protected long histogram_0_1;
protected long histogram_1_10;
protected long histogram_10_100;
protected long histogram_100_1000;
protected int histogram_1000_10000;
protected int histogram_10000_100000;
protected int histogram_100000_1000000;
protected int histogram_1000000_more;
public long[] getHistogram() {
return new long[] { histogram_0_1, //
histogram_1_10, //
histogram_10_100, //
histogram_100_1000, //
histogram_1000_10000, //
histogram_10000_100000, //
histogram_100000_1000000, //
histogram_1000000_more, //
};
}
public List<ProfileEntryStatValue> getProfileEntryStatValueList() {
return profileEntryStatValueList;
}
@@ -280,6 +301,7 @@ public class WebURIStatValue {
data.put("JdbcResultSetOpenCount", this.getJdbcResultSetOpenCount());
data.put("JdbcResultSetCloseCount", this.getJdbcResultSetCloseCount());
data.put("Histogram", this.getHistogram());
if (this.profileEntryStatValueList != null) {
int size = profileEntryStatValueList.size();
@@ -1,101 +1,101 @@
/*
* 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.support.logging;
import java.lang.reflect.Constructor;
@SuppressWarnings("rawtypes")
public class LogFactory {
private static Constructor logConstructor;
static {
// TODO add slf4j logging
// 优先选择log4j,而非Apache Common Logging. 因为后者无法设置真实Log调用者的信息
tryImplementation("org.slf4j.Logger", "com.alibaba.druid.support.logging.SLF4JImpl");
tryImplementation("org.apache.log4j.Logger", "com.alibaba.druid.support.logging.Log4jImpl");
tryImplementation("java.util.logging.Logger", "com.alibaba.druid.support.logging.Jdk14LoggingImpl");
tryImplementation("org.apache.commons.logging.LogFactory",
"com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl");
if (logConstructor == null) {
try {
logConstructor = NoLoggingImpl.class.getConstructor(String.class);
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
}
@SuppressWarnings("unchecked")
private static void tryImplementation(String testClassName, String implClassName) {
if (logConstructor != null) {
return;
}
try {
Resources.classForName(testClassName);
Class implClass = Resources.classForName(implClassName);
logConstructor = implClass.getConstructor(new Class[] { String.class });
Class<?> declareClass = logConstructor.getDeclaringClass();
if (!Log.class.isAssignableFrom(declareClass)) {
logConstructor = null;
}
try {
logConstructor.newInstance(LogFactory.class.getName());
} catch (Throwable t) {
logConstructor = null;
}
} catch (Throwable t) {
// skip
}
}
public static Log getLog(Class clazz) {
return getLog(clazz.getName());
}
public static Log getLog(String loggerName) {
try {
return (Log) logConstructor.newInstance(new Object[] { loggerName });
} catch (Throwable t) {
throw new RuntimeException("Error creating logger for logger '" + loggerName + "'. Cause: " + t, t);
}
}
@SuppressWarnings("unchecked")
public static synchronized void selectLog4JLogging() {
try {
Resources.classForName("org.apache.log4j.Logger");
Class implClass = Resources.classForName("com.alibaba.druid.support.logging.Log4jImpl");
logConstructor = implClass.getConstructor(new Class[] { String.class });
} catch (Throwable t) {
}
}
@SuppressWarnings("unchecked")
public static synchronized void selectJavaLogging() {
try {
Resources.classForName("java.util.logging.Logger");
Class implClass = Resources.classForName("com.alibaba.druid.support.logging.Jdk14LoggingImpl");
logConstructor = implClass.getConstructor(new Class[] { String.class });
} catch (Throwable t) {
}
}
}
/*
* 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.support.logging;
import java.lang.reflect.Constructor;
@SuppressWarnings("rawtypes")
public class LogFactory {
private static Constructor logConstructor;
static {
// TODO add slf4j logging
// 优先选择log4j,而非Apache Common Logging. 因为后者无法设置真实Log调用者的信息
tryImplementation("org.apache.log4j.Logger", "com.alibaba.druid.support.logging.Log4jImpl");
tryImplementation("org.slf4j.Logger", "com.alibaba.druid.support.logging.SLF4JImpl");
tryImplementation("org.apache.commons.logging.LogFactory",
"com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl");
tryImplementation("java.util.logging.Logger", "com.alibaba.druid.support.logging.Jdk14LoggingImpl");
if (logConstructor == null) {
try {
logConstructor = NoLoggingImpl.class.getConstructor(String.class);
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
}
@SuppressWarnings("unchecked")
private static void tryImplementation(String testClassName, String implClassName) {
if (logConstructor != null) {
return;
}
try {
Resources.classForName(testClassName);
Class implClass = Resources.classForName(implClassName);
logConstructor = implClass.getConstructor(new Class[] { String.class });
Class<?> declareClass = logConstructor.getDeclaringClass();
if (!Log.class.isAssignableFrom(declareClass)) {
logConstructor = null;
}
try {
logConstructor.newInstance(LogFactory.class.getName());
} catch (Throwable t) {
logConstructor = null;
}
} catch (Throwable t) {
// skip
}
}
public static Log getLog(Class clazz) {
return getLog(clazz.getName());
}
public static Log getLog(String loggerName) {
try {
return (Log) logConstructor.newInstance(new Object[] { loggerName });
} catch (Throwable t) {
throw new RuntimeException("Error creating logger for logger '" + loggerName + "'. Cause: " + t, t);
}
}
@SuppressWarnings("unchecked")
public static synchronized void selectLog4JLogging() {
try {
Resources.classForName("org.apache.log4j.Logger");
Class implClass = Resources.classForName("com.alibaba.druid.support.logging.Log4jImpl");
logConstructor = implClass.getConstructor(new Class[] { String.class });
} catch (Throwable t) {
}
}
@SuppressWarnings("unchecked")
public static synchronized void selectJavaLogging() {
try {
Resources.classForName("java.util.logging.Logger");
Class implClass = Resources.classForName("com.alibaba.druid.support.logging.Jdk14LoggingImpl");
logConstructor = implClass.getConstructor(new Class[] { String.class });
} catch (Throwable t) {
}
}
}
@@ -59,7 +59,7 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
public final static String ATTR_SQL_STAT = "wall.sqlStat";
@Override
public void init(DataSourceProxy dataSource) {
public synchronized void init(DataSourceProxy dataSource) {
if (this.dbType == null || this.dbType.trim().length() == 0) {
if (dataSource != null && dataSource.getDbType() != null) {
this.dbType = dataSource.getDbType();
@@ -0,0 +1,27 @@
package com.alibaba.druid.bvt.sql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.util.JdbcConstants;
public class PagerUtilsTest_Limit_SQLServer_1 extends TestCase {
public void test_db2_union() throws Exception {
String sql = "select * from t1 union select * from t2";
String result = PagerUtils.limit(sql, JdbcConstants.SQL_SERVER, 20, 10);
Assert.assertEquals("SELECT *" //
+ "\nFROM (SELECT XX.*, ROW_NUMBER() OVER () AS ROWNUM" //
+ "\n\tFROM (SELECT *" //
+ "\n\t\tFROM t1" //
+ "\n\t\tUNION" //
+ "\n\t\tSELECT *" //
+ "\n\t\tFROM t2" //
+ "\n\t\t) XX" //
+ "\n\t) XXX" //
+ "\nWHERE ROWNUM > 20" //
+ "\n\tAND ROWNUM <= 30", result);
}
}
@@ -0,0 +1,23 @@
package com.alibaba.druid.bvt.sql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.util.JdbcConstants;
public class PagerUtilsTest_Limit_SQLServer_2 extends TestCase {
public void test_db2_union() throws Exception {
String sql = "select * from t1 union select * from t2";
String result = PagerUtils.limit(sql, JdbcConstants.SQL_SERVER, 0, 10);
Assert.assertEquals("SELECT TOP 10 XX.*" //
+ "\nFROM (SELECT *" //
+ "\n\tFROM t1" //
+ "\n\tUNION" //
+ "\n\tSELECT *" //
+ "\n\tFROM t2" //
+ "\n\t) XX", result);
}
}
@@ -0,0 +1,19 @@
package com.alibaba.druid.bvt.sql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.util.JdbcConstants;
public class PagerUtilsTest_Limit_SQLServer_3 extends TestCase {
public void test_db2_union() throws Exception {
String sql = "select * from t1 where id > 1";
String result = PagerUtils.limit(sql, JdbcConstants.SQL_SERVER, 0, 10);
Assert.assertEquals("SELECT TOP 10 *" //
+ "\nFROM t1" //
+ "\nWHERE id > 1", result);
}
}
@@ -0,0 +1,23 @@
package com.alibaba.druid.bvt.sql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.util.JdbcConstants;
public class PagerUtilsTest_Limit_SQLServer_4 extends TestCase {
public void test_db2_union() throws Exception {
String sql = "select * from t1 where id > 1";
String result = PagerUtils.limit(sql, JdbcConstants.SQL_SERVER, 100, 10);
Assert.assertEquals("SELECT *" //
+ "\nFROM (SELECT *, ROW_NUMBER() OVER () AS ROWNUM" //
+ "\n\tFROM t1" //
+ "\n\tWHERE id > 1" //
+ "\n\t) XX" //
+ "\nWHERE ROWNUM > 100" //
+ "\n\tAND ROWNUM <= 110", result);
}
}
@@ -21,7 +21,7 @@ public class PagerUtilsTest_Limit_db2_0 extends TestCase {
String sql = "select * from t";
String result = PagerUtils.limit(sql, JdbcConstants.DB2, 10, 10);
Assert.assertEquals("SELECT *"//
+ "\nFROM (SELECT *, ROW_NUMBER() AS ROWNUM"//
+ "\nFROM (SELECT *, ROW_NUMBER() OVER () AS ROWNUM"//
+ "\n\tFROM t"//
+ "\n\t) XX"//
+ "\nWHERE ROWNUM > 10"//
@@ -32,7 +32,7 @@ public class PagerUtilsTest_Limit_db2_0 extends TestCase {
String sql = "select * from t where age > 100";
String result = PagerUtils.limit(sql, JdbcConstants.DB2, 20, 10);
Assert.assertEquals("SELECT *"//
+ "\nFROM (SELECT *, ROW_NUMBER() AS ROWNUM"//
+ "\nFROM (SELECT *, ROW_NUMBER() OVER () AS ROWNUM"//
+ "\n\tFROM t"//
+ "\n\tWHERE age > 100"//
+ "\n\t) XX"//
@@ -0,0 +1,27 @@
package com.alibaba.druid.bvt.sql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.util.JdbcConstants;
public class PagerUtilsTest_Limit_db2_1 extends TestCase {
public void test_db2_union() throws Exception {
String sql = "select * from t1 union select * from t2";
String result = PagerUtils.limit(sql, JdbcConstants.DB2, 20, 10);
Assert.assertEquals("SELECT *" //
+ "\nFROM (SELECT XX.*, ROW_NUMBER() OVER () AS ROWNUM" //
+ "\n\tFROM (SELECT *" //
+ "\n\t\tFROM t1" //
+ "\n\t\tUNION" //
+ "\n\t\tSELECT *" //
+ "\n\t\tFROM t2" //
+ "\n\t\t) XX" //
+ "\n\t) XXX" //
+ "\nWHERE ROWNUM > 20" //
+ "\n\tAND ROWNUM <= 30", result);
}
}
@@ -0,0 +1,61 @@
/*
* 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 org.junit.Assert;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleMergeStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.parser.SQLStatementParser;
public class OracleMergeTest7 extends OracleTest {
public void test_0() throws Exception {
String sql = "MERGE INTO copy_emp c " + //
"USING employees e " + //
"ON (c.employee_id=e.employee_id) " + //
"WHEN MATCHED THEN " + //
"UPDATE SET " + //
"c.first_name=e.first_name, " + //
"c.last_name=e.last_name, " + //
"c.department_id=e.department_id " + //
"WHEN NOT MATCHED THEN " + //
"INSERT VALUES(e.employee_id,e.first_name,e.last_name," + //
"e.email,e.phone_number,e.hire_date,e.job_id, " + //
"e.salary,e.commission_pct,e.manager_id,e.department_id)";
SQLStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> stmtList = parser.parseStatementList();
OracleMergeStatement mergeStatement = (OracleMergeStatement) stmtList.get(0);
String result = SQLUtils.toOracleString(mergeStatement);
Assert.assertEquals("MERGE INTO copy_emp c" //
+ "\nUSING employees e ON c.employee_id = e.employee_id" //
+ "\nWHEN MATCHED THEN UPDATE SET c.first_name = e.first_name, c.last_name = e.last_name, c.department_id = e.department_id" //
+ "\nWHEN NOT MATCHED THEN INSERT VALUES (e.employee_id, e.first_name, e.last_name, e.email, e.phone_number, e.hire_date, e.job_id, e.salary, e.commission_pct, e.manager_id, e.department_id)",
result);
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "employee_id")));
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "salary")));
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "department_id")));
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("bonuses", "employee_id")));
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("bonuses", "bonus")));
}
}