mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -19,7 +19,7 @@ public final class VERSION {
|
||||
|
||||
public final static int MajorVersion = 1;
|
||||
public final static int MinorVersion = 1;
|
||||
public final static int RevisionVersion = 12;
|
||||
public final static int RevisionVersion = 14;
|
||||
|
||||
public static String getVersionNumber() {
|
||||
return VERSION.MajorVersion + "." + VERSION.MinorVersion + "." + VERSION.RevisionVersion;
|
||||
|
||||
@@ -546,7 +546,7 @@ public abstract class LogFilter extends FilterEventAdapter implements LogFilterM
|
||||
}
|
||||
|
||||
private void logExecutableSql(StatementProxy statement, String sql) {
|
||||
if (!isStatementExecutableSqlLogEnable()) {
|
||||
if ((!isStatementExecutableSqlLogEnable()) || !isStatementLogEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -150,6 +150,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
private boolean asyncInit = false;
|
||||
protected boolean killWhenSocketReadTimeout = false;
|
||||
|
||||
private static List<Filter> autoFilters = null;
|
||||
private boolean loadSpifilterSkip = false;
|
||||
|
||||
protected static final AtomicLongFieldUpdater<DruidDataSource> recycleErrorCountUpdater
|
||||
= AtomicLongFieldUpdater.newUpdater(DruidDataSource.class, "recycleErrorCount");
|
||||
protected static final AtomicLongFieldUpdater<DruidDataSource> connectErrorCountUpdater
|
||||
@@ -157,6 +160,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
protected static final AtomicLongFieldUpdater<DruidDataSource> resetCountUpdater
|
||||
= AtomicLongFieldUpdater.newUpdater(DruidDataSource.class, "resetCount");
|
||||
|
||||
|
||||
public DruidDataSource(){
|
||||
this(false);
|
||||
}
|
||||
@@ -316,6 +320,17 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
String property = properties.getProperty("druid.maxWait");
|
||||
if (property != null && property.length() > 0) {
|
||||
try {
|
||||
int value = Integer.parseInt(property);
|
||||
this.setMaxWait(value);
|
||||
} catch (NumberFormatException e) {
|
||||
LOG.error("illegal property 'druid.maxWait'", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
Boolean value = getBoolean(properties, "druid.failFast");
|
||||
if (value != null) {
|
||||
@@ -333,6 +348,17 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
String property = properties.getProperty("druid.phyMaxUseCount");
|
||||
if (property != null && property.length() > 0) {
|
||||
try {
|
||||
long value = Long.parseLong(property);
|
||||
this.setPhyMaxUseCount(value);
|
||||
} catch (NumberFormatException e) {
|
||||
LOG.error("illegal property 'druid.phyMaxUseCount'", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
String property = properties.getProperty("druid.minEvictableIdleTimeMillis");
|
||||
if (property != null && property.length() > 0) {
|
||||
@@ -458,6 +484,12 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
String property = System.getProperty("druid.load.spifilter.skip");
|
||||
if (property != null && !"false".equals(property)) {
|
||||
loadSpifilterSkip = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isKillWhenSocketReadTimeout() {
|
||||
@@ -728,6 +760,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
return;
|
||||
}
|
||||
|
||||
// bug fixed for dead lock, for issue #2980
|
||||
DruidDriver.getInstance();
|
||||
|
||||
final ReentrantLock lock = this.lock;
|
||||
try {
|
||||
lock.lockInterruptibly();
|
||||
@@ -986,22 +1021,28 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
* @see ServiceLoader
|
||||
*/
|
||||
private void initFromSPIServiceLoader() {
|
||||
|
||||
String property = System.getProperty("druid.load.spifilter.skip");
|
||||
if (property != null) {
|
||||
if (loadSpifilterSkip) {
|
||||
return;
|
||||
}
|
||||
|
||||
ServiceLoader<Filter> druidAutoFilterLoader = ServiceLoader.load(Filter.class);
|
||||
if (autoFilters == null) {
|
||||
List<Filter> filters = new ArrayList<Filter>();
|
||||
ServiceLoader<Filter> autoFilterLoader = ServiceLoader.load(Filter.class);
|
||||
|
||||
for (Filter autoFilter : druidAutoFilterLoader) {
|
||||
AutoLoad autoLoad = autoFilter.getClass().getAnnotation(AutoLoad.class);
|
||||
if (autoLoad != null && autoLoad.value()) {
|
||||
if (LOG.isInfoEnabled()) {
|
||||
LOG.info("load filter from spi :" + autoFilter.getClass().getName());
|
||||
for (Filter filter : autoFilterLoader) {
|
||||
AutoLoad autoLoad = filter.getClass().getAnnotation(AutoLoad.class);
|
||||
if (autoLoad != null && autoLoad.value()) {
|
||||
filters.add(filter);
|
||||
}
|
||||
addFilter(autoFilter);
|
||||
}
|
||||
autoFilters = filters;
|
||||
}
|
||||
|
||||
for (Filter filter : autoFilters) {
|
||||
if (LOG.isInfoEnabled()) {
|
||||
LOG.info("load filter from spi :" + filter.getClass().getName());
|
||||
}
|
||||
addFilter(filter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1096,6 +1137,10 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|| JdbcUtils.MYSQL_DRIVER_6.equals(this.dbType)) {
|
||||
isMySql = true;
|
||||
}
|
||||
|
||||
if (removeAbandoned) {
|
||||
LOG.warn("removeAbandoned is true, not use in productiion.");
|
||||
}
|
||||
}
|
||||
|
||||
private void oracleValidationQueryCheck() {
|
||||
@@ -1432,19 +1477,26 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
&& activeCount >= onFatalErrorMaxActive) {
|
||||
connectErrorCountUpdater.incrementAndGet(this);
|
||||
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String errorMsg = "onFatalError, activeCount " + activeCount
|
||||
+ ", onFatalErrorMaxActive " + onFatalErrorMaxActive;
|
||||
StringBuilder errorMsg = new StringBuilder();
|
||||
errorMsg.append("onFatalError, activeCount ")
|
||||
.append(activeCount)
|
||||
.append(", onFatalErrorMaxActive ")
|
||||
.append(onFatalErrorMaxActive);
|
||||
|
||||
if (lastFatalErrorTimeMillis > 0) {
|
||||
errorMsg += ", time '" + format.format(new Date(lastFatalErrorTimeMillis)) + "'";
|
||||
errorMsg.append(", time '")
|
||||
.append(StringUtils.formatDateTime19(
|
||||
lastFatalErrorTimeMillis, TimeZone.getDefault()))
|
||||
.append("'");
|
||||
}
|
||||
|
||||
if (lastFatalErrorSql != null) {
|
||||
errorMsg += ", sql \n" + lastFatalErrorSql;
|
||||
errorMsg.append(", sql \n")
|
||||
.append(lastFatalErrorSql);
|
||||
}
|
||||
|
||||
throw new SQLException(errorMsg, lastFatalError);
|
||||
throw new SQLException(
|
||||
errorMsg.toString(), lastFatalError);
|
||||
}
|
||||
|
||||
connectCount++;
|
||||
@@ -1490,7 +1542,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
if (holder == null) {
|
||||
long waitNanos = waitNanosLocal.get();
|
||||
|
||||
StringBuilder buf = new StringBuilder();
|
||||
StringBuilder buf = new StringBuilder(128);
|
||||
buf.append("wait millis ")//
|
||||
.append(waitNanos / (1000 * 1000))//
|
||||
.append(", active ").append(activeCount)//
|
||||
@@ -1504,6 +1556,10 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
|
||||
if (createErrorCount > 0) {
|
||||
buf.append(", createErrorCount ").append(createErrorCount);
|
||||
}
|
||||
|
||||
List<JdbcSqlStatValue> sqlList = this.getDataSourceStat().getRuningSqlList();
|
||||
for (int i = 0; i < sqlList.size(); ++i) {
|
||||
if (i != 0) {
|
||||
|
||||
@@ -39,53 +39,78 @@ public class DruidDataSourceFactory implements ObjectFactory {
|
||||
|
||||
static final int UNKNOWN_TRANSACTIONISOLATION = -1;
|
||||
|
||||
public final static String PROP_DEFAULTAUTOCOMMIT = "defaultAutoCommit";
|
||||
public final static String PROP_DEFAULTREADONLY = "defaultReadOnly";
|
||||
public final static String PROP_DEFAULTTRANSACTIONISOLATION = "defaultTransactionIsolation";
|
||||
public final static String PROP_DEFAULTCATALOG = "defaultCatalog";
|
||||
public final static String PROP_DRIVERCLASSNAME = "driverClassName";
|
||||
public final static String PROP_MAXACTIVE = "maxActive";
|
||||
public final static String PROP_MAXIDLE = "maxIdle";
|
||||
public final static String PROP_MINIDLE = "minIdle";
|
||||
public final static String PROP_INITIALSIZE = "initialSize";
|
||||
public final static String PROP_MAXWAIT = "maxWait";
|
||||
public final static String PROP_TESTONBORROW = "testOnBorrow";
|
||||
public final static String PROP_TESTONRETURN = "testOnReturn";
|
||||
public final static String PROP_TIMEBETWEENEVICTIONRUNSMILLIS = "timeBetweenEvictionRunsMillis";
|
||||
public final static String PROP_NUMTESTSPEREVICTIONRUN = "numTestsPerEvictionRun";
|
||||
public final static String PROP_MINEVICTABLEIDLETIMEMILLIS = "minEvictableIdleTimeMillis";
|
||||
public final static String PROP_PHY_TIMEOUT_MILLIS = "phyTimeoutMillis";
|
||||
public final static String PROP_TESTWHILEIDLE = "testWhileIdle";
|
||||
public final static String PROP_PASSWORD = "password";
|
||||
public final static String PROP_URL = "url";
|
||||
public final static String PROP_USERNAME = "username";
|
||||
public final static String PROP_VALIDATIONQUERY = "validationQuery";
|
||||
public final static String PROP_VALIDATIONQUERY_TIMEOUT = "validationQueryTimeout";
|
||||
|
||||
public final static String PROP_INITCONNECTIONSQLS = "initConnectionSqls";
|
||||
public final static String PROP_DEFAULTAUTOCOMMIT = "defaultAutoCommit";
|
||||
public final static String PROP_DEFAULTREADONLY = "defaultReadOnly";
|
||||
public final static String PROP_DEFAULTTRANSACTIONISOLATION = "defaultTransactionIsolation";
|
||||
public final static String PROP_DEFAULTCATALOG = "defaultCatalog";
|
||||
public final static String PROP_DRIVERCLASSNAME = "driverClassName";
|
||||
public final static String PROP_MAXACTIVE = "maxActive";
|
||||
public final static String PROP_MAXIDLE = "maxIdle";
|
||||
public final static String PROP_MINIDLE = "minIdle";
|
||||
public final static String PROP_INITIALSIZE = "initialSize";
|
||||
public final static String PROP_MAXWAIT = "maxWait";
|
||||
public final static String PROP_TESTONBORROW = "testOnBorrow";
|
||||
public final static String PROP_TESTONRETURN = "testOnReturn";
|
||||
public final static String PROP_TIMEBETWEENEVICTIONRUNSMILLIS = "timeBetweenEvictionRunsMillis";
|
||||
public final static String PROP_NUMTESTSPEREVICTIONRUN = "numTestsPerEvictionRun";
|
||||
public final static String PROP_MINEVICTABLEIDLETIMEMILLIS = "minEvictableIdleTimeMillis";
|
||||
public final static String PROP_PHY_TIMEOUT_MILLIS = "phyTimeoutMillis";
|
||||
public final static String PROP_TESTWHILEIDLE = "testWhileIdle";
|
||||
public final static String PROP_PASSWORD = "password";
|
||||
public final static String PROP_URL = "url";
|
||||
public final static String PROP_USERNAME = "username";
|
||||
public final static String PROP_VALIDATIONQUERY = "validationQuery";
|
||||
public final static String PROP_VALIDATIONQUERY_TIMEOUT = "validationQueryTimeout";
|
||||
public final static String PROP_INITCONNECTIONSQLS = "initConnectionSqls";
|
||||
public final static String PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED = "accessToUnderlyingConnectionAllowed";
|
||||
public final static String PROP_REMOVEABANDONED = "removeAbandoned";
|
||||
public final static String PROP_REMOVEABANDONEDTIMEOUT = "removeAbandonedTimeout";
|
||||
public final static String PROP_LOGABANDONED = "logAbandoned";
|
||||
public final static String PROP_POOLPREPAREDSTATEMENTS = "poolPreparedStatements";
|
||||
public final static String PROP_MAXOPENPREPAREDSTATEMENTS = "maxOpenPreparedStatements";
|
||||
public final static String PROP_CONNECTIONPROPERTIES = "connectionProperties";
|
||||
public final static String PROP_FILTERS = "filters";
|
||||
public final static String PROP_EXCEPTION_SORTER = "exceptionSorter";
|
||||
public final static String PROP_EXCEPTION_SORTER_CLASS_NAME = "exception-sorter-class-name";
|
||||
public final static String PROP_NAME = "name";
|
||||
public final static String PROP_REMOVEABANDONED = "removeAbandoned";
|
||||
public final static String PROP_REMOVEABANDONEDTIMEOUT = "removeAbandonedTimeout";
|
||||
public final static String PROP_LOGABANDONED = "logAbandoned";
|
||||
public final static String PROP_POOLPREPAREDSTATEMENTS = "poolPreparedStatements";
|
||||
public final static String PROP_MAXOPENPREPAREDSTATEMENTS = "maxOpenPreparedStatements";
|
||||
public final static String PROP_CONNECTIONPROPERTIES = "connectionProperties";
|
||||
public final static String PROP_FILTERS = "filters";
|
||||
public final static String PROP_EXCEPTION_SORTER = "exceptionSorter";
|
||||
public final static String PROP_EXCEPTION_SORTER_CLASS_NAME = "exception-sorter-class-name";
|
||||
public final static String PROP_NAME = "name";
|
||||
public final static String PROP_INIT = "init";
|
||||
|
||||
public final static String PROP_INIT = "init";
|
||||
private final static String[] ALL_PROPERTIES = {
|
||||
PROP_DEFAULTAUTOCOMMIT,
|
||||
PROP_DEFAULTREADONLY,
|
||||
PROP_DEFAULTTRANSACTIONISOLATION,
|
||||
PROP_DEFAULTCATALOG,
|
||||
PROP_DRIVERCLASSNAME,
|
||||
PROP_MAXACTIVE,
|
||||
PROP_MAXIDLE,
|
||||
PROP_MINIDLE,
|
||||
PROP_INITIALSIZE,
|
||||
PROP_MAXWAIT,
|
||||
PROP_TESTONBORROW,
|
||||
PROP_TESTONRETURN,
|
||||
PROP_TIMEBETWEENEVICTIONRUNSMILLIS,
|
||||
PROP_NUMTESTSPEREVICTIONRUN,
|
||||
PROP_MINEVICTABLEIDLETIMEMILLIS,
|
||||
PROP_TESTWHILEIDLE,
|
||||
PROP_PASSWORD,
|
||||
PROP_FILTERS,
|
||||
PROP_URL,
|
||||
PROP_USERNAME,
|
||||
PROP_VALIDATIONQUERY,
|
||||
PROP_VALIDATIONQUERY_TIMEOUT,
|
||||
PROP_INITCONNECTIONSQLS,
|
||||
PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED,
|
||||
PROP_REMOVEABANDONED,
|
||||
PROP_REMOVEABANDONEDTIMEOUT,
|
||||
PROP_LOGABANDONED,
|
||||
PROP_POOLPREPAREDSTATEMENTS,
|
||||
PROP_MAXOPENPREPAREDSTATEMENTS,
|
||||
PROP_CONNECTIONPROPERTIES,
|
||||
PROP_EXCEPTION_SORTER,
|
||||
PROP_EXCEPTION_SORTER_CLASS_NAME,
|
||||
PROP_INIT,
|
||||
PROP_NAME, //
|
||||
|
||||
private final static String[] ALL_PROPERTIES = {PROP_DEFAULTAUTOCOMMIT,
|
||||
PROP_DEFAULTREADONLY, PROP_DEFAULTTRANSACTIONISOLATION, PROP_DEFAULTCATALOG, PROP_DRIVERCLASSNAME,
|
||||
PROP_MAXACTIVE, PROP_MAXIDLE, PROP_MINIDLE, PROP_INITIALSIZE, PROP_MAXWAIT, PROP_TESTONBORROW,
|
||||
PROP_TESTONRETURN, PROP_TIMEBETWEENEVICTIONRUNSMILLIS, PROP_NUMTESTSPEREVICTIONRUN,
|
||||
PROP_MINEVICTABLEIDLETIMEMILLIS, PROP_TESTWHILEIDLE, PROP_PASSWORD, PROP_FILTERS, PROP_URL, PROP_USERNAME,
|
||||
PROP_VALIDATIONQUERY, PROP_VALIDATIONQUERY_TIMEOUT, PROP_INITCONNECTIONSQLS,
|
||||
PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED, PROP_REMOVEABANDONED, PROP_REMOVEABANDONEDTIMEOUT,
|
||||
PROP_LOGABANDONED, PROP_POOLPREPAREDSTATEMENTS, PROP_MAXOPENPREPAREDSTATEMENTS, PROP_CONNECTIONPROPERTIES,
|
||||
PROP_EXCEPTION_SORTER, PROP_EXCEPTION_SORTER_CLASS_NAME, PROP_INIT, PROP_NAME, //
|
||||
"druid.timeBetweenLogStatsMillis", //
|
||||
"druid.stat.sql.MaxSize", //
|
||||
"druid.clearFiltersEnable", //
|
||||
@@ -115,7 +140,7 @@ public class DruidDataSourceFactory implements ObjectFactory {
|
||||
|
||||
if ((!"javax.sql.DataSource".equals(ref.getClassName())) //
|
||||
&& (!"com.alibaba.druid.pool.DruidDataSource".equals(ref.getClassName())) //
|
||||
) {
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,8 @@ public interface DruidDataSourceMBean extends DruidAbstractDataSourceMBean {
|
||||
|
||||
boolean isEnable();
|
||||
|
||||
String getUrl();
|
||||
|
||||
void shrink();
|
||||
|
||||
int removeAbandoned();
|
||||
|
||||
@@ -206,7 +206,7 @@ public class DruidDataSourceStatLoggerImpl extends DruidDataSourceStatLoggerAdap
|
||||
|
||||
if (sqlStat.getFetchRowCount() > 0) {
|
||||
sqlStatMap.put("fetchRowCount", sqlStat.getFetchRowCount());
|
||||
sqlStatMap.put("fetchRowCount", sqlStat.getFetchRowCountMax());
|
||||
sqlStatMap.put("fetchRowCountMax", sqlStat.getFetchRowCountMax());
|
||||
sqlStatMap.put("fetchRowHistogram", rtrim(sqlStat.getFetchRowHistogram()));
|
||||
}
|
||||
|
||||
|
||||
@@ -1150,14 +1150,6 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql.
|
||||
}
|
||||
|
||||
private void checkStateInternal() throws SQLException {
|
||||
if (holder == null) {
|
||||
if (disableError != null) {
|
||||
throw new SQLException("connection holder is null", disableError);
|
||||
} else {
|
||||
throw new SQLException("connection holder is null");
|
||||
}
|
||||
}
|
||||
|
||||
if (closed) {
|
||||
if (disableError != null) {
|
||||
throw new SQLException("connection closed", disableError);
|
||||
@@ -1173,6 +1165,14 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql.
|
||||
throw new SQLException("connection disabled");
|
||||
}
|
||||
}
|
||||
|
||||
if (holder == null) {
|
||||
if (disableError != null) {
|
||||
throw new SQLException("connection holder is null", disableError);
|
||||
} else {
|
||||
throw new SQLException("connection holder is null");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
|
||||
@@ -485,16 +485,19 @@ public class DruidPooledStatement extends PoolableWrapper implements Statement {
|
||||
|
||||
@Override
|
||||
public void close() throws SQLException {
|
||||
if (!this.closed) {
|
||||
clearResultSet();
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
}
|
||||
this.closed = true;
|
||||
if (this.closed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (conn.getConnectionHolder() != null) {
|
||||
conn.getConnectionHolder().removeTrace(this);
|
||||
}
|
||||
clearResultSet();
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
}
|
||||
this.closed = true;
|
||||
|
||||
DruidConnectionHolder connHolder = conn.getConnectionHolder();
|
||||
if (connHolder != null) {
|
||||
connHolder.removeTrace(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -477,6 +477,24 @@ public class SQLUtils {
|
||||
return stmtList;
|
||||
}
|
||||
|
||||
public static SQLStatement parseSingleStatement(String sql, String dbType, SQLParserFeature... features) {
|
||||
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType, features);
|
||||
List<SQLStatement> stmtList = parser.parseStatementList();
|
||||
|
||||
if (stmtList.size() > 1) {
|
||||
throw new ParserException(" Mutil-Statment be found.");
|
||||
}
|
||||
|
||||
if (parser.getLexer().token() != Token.EOF) {
|
||||
throw new ParserException("syntax error. " + sql);
|
||||
}
|
||||
return stmtList.get(0);
|
||||
}
|
||||
|
||||
public static SQLStatement parseSingleMysqlStatement(String sql) {
|
||||
return parseSingleStatement(sql, JdbcConstants.MYSQL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @author owenludong.lud
|
||||
* @param columnName
|
||||
|
||||
@@ -24,4 +24,7 @@ public interface SQLStatement extends SQLObject {
|
||||
SQLStatement clone();
|
||||
List<SQLObject> getChildren();
|
||||
String toLowerCaseString();
|
||||
|
||||
List<SQLCommentHint> getHeadHintsDirect();
|
||||
void setHeadHints(List<SQLCommentHint> headHints);
|
||||
}
|
||||
|
||||
@@ -19,10 +19,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLObjectImpl;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.*;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLInsertStatement extends SQLInsertInto implements SQLStatement {
|
||||
@@ -34,6 +31,8 @@ public class SQLInsertStatement extends SQLInsertInto implements SQLStatement {
|
||||
|
||||
private boolean afterSemi;
|
||||
|
||||
protected List<SQLCommentHint> headHints;
|
||||
|
||||
public SQLInsertStatement(){
|
||||
|
||||
}
|
||||
@@ -198,4 +197,12 @@ public class SQLInsertStatement extends SQLInsertInto implements SQLStatement {
|
||||
public String toLowerCaseString() {
|
||||
return SQLUtils.toSQLString(this, dbType, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
}
|
||||
|
||||
public List<SQLCommentHint> getHeadHintsDirect() {
|
||||
return headHints;
|
||||
}
|
||||
|
||||
public void setHeadHints(List<SQLCommentHint> headHints) {
|
||||
this.headHints = headHints;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package com.alibaba.druid.sql.dialect.hive.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLArrayExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
@@ -64,6 +65,13 @@ public class HiveExprParser extends SQLExprParser {
|
||||
String propertyName = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
expr = new SQLPropertyExpr(expr, propertyName);
|
||||
} else if (lexer.token() == Token.LBRACKET) {
|
||||
SQLArrayExpr array = new SQLArrayExpr();
|
||||
array.setExpr(expr);
|
||||
lexer.nextToken();
|
||||
this.exprList(array.getValues(), array);
|
||||
accept(Token.RBRACKET);
|
||||
expr = array;
|
||||
}
|
||||
return super.primaryRest(expr);
|
||||
}
|
||||
|
||||
+15
-1
@@ -15,14 +15,18 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
|
||||
public class MySqlShowProcessListStatement extends MySqlStatementImpl implements MySqlShowStatement {
|
||||
|
||||
private boolean full = false;
|
||||
private SQLExpr where;
|
||||
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
visitor.visit(this);
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, where);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
@@ -34,4 +38,14 @@ public class MySqlShowProcessListStatement extends MySqlStatementImpl implements
|
||||
this.full = full;
|
||||
}
|
||||
|
||||
public SQLExpr getWhere() {
|
||||
return where;
|
||||
}
|
||||
|
||||
public void setWhere(SQLExpr x) {
|
||||
if (x != null) {
|
||||
x.setParent(this);
|
||||
}
|
||||
this.where = x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -516,6 +516,16 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
column.setName(name());
|
||||
column.setDataType(parseDataType());
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.GENERATED)) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("ALWAYS");
|
||||
accept(Token.AS);
|
||||
accept(Token.LPAREN);
|
||||
SQLExpr expr = this.expr();
|
||||
accept(Token.RPAREN);
|
||||
column.setGeneratedAlawsAs(expr);
|
||||
}
|
||||
|
||||
return parseColumnRest(column);
|
||||
}
|
||||
|
||||
|
||||
@@ -289,7 +289,7 @@ public class MySqlLexer extends Lexer {
|
||||
|
||||
final char first = ch;
|
||||
|
||||
if (ch == 'b'
|
||||
if ((ch == 'b' || ch == 'B' )
|
||||
&& charAt(pos + 1) == '\'') {
|
||||
int i = 2;
|
||||
int mark = pos + 2;
|
||||
|
||||
@@ -934,12 +934,12 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
}
|
||||
|
||||
if (tddlHints) {
|
||||
SQLStatementImpl stmt = (SQLStatementImpl)this.parseStatement();
|
||||
SQLStatement stmt = this.parseStatement();
|
||||
stmt.setHeadHints(hints);
|
||||
statementList.add(stmt);
|
||||
return true;
|
||||
} else if (accept) {
|
||||
SQLStatementImpl stmt = (SQLStatementImpl) this.parseStatement();
|
||||
SQLStatement stmt = this.parseStatement();
|
||||
stmt.setHeadHints(hints);
|
||||
statementList.add(stmt);
|
||||
return true;
|
||||
@@ -1712,6 +1712,14 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
if (lexer.identifierEquals("PROCESSLIST")) {
|
||||
lexer.nextToken();
|
||||
MySqlShowProcessListStatement stmt = new MySqlShowProcessListStatement();
|
||||
|
||||
if (lexer.token() == Token.WHERE) {
|
||||
lexer.nextToken();
|
||||
stmt.setWhere(
|
||||
this.exprParser.expr()
|
||||
);
|
||||
}
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
|
||||
@@ -269,6 +269,13 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
dataType.accept(this);
|
||||
}
|
||||
|
||||
SQLExpr generatedAlawsAs = x.getGeneratedAlawsAs();
|
||||
if (generatedAlawsAs != null) {
|
||||
print0(ucase ? " GENERATED ALWAYS AS (" : " generated always as (");
|
||||
printExpr(generatedAlawsAs);
|
||||
print(')');
|
||||
}
|
||||
|
||||
final SQLExpr charsetExpr = x.getCharsetExpr();
|
||||
if (charsetExpr != null) {
|
||||
print0(ucase ? " CHARSET " : " charset ");
|
||||
@@ -881,6 +888,14 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlInsertStatement x) {
|
||||
List<SQLCommentHint> headHints = x.getHeadHintsDirect();
|
||||
if (headHints != null) {
|
||||
for (SQLCommentHint hint : headHints) {
|
||||
hint.accept(this);
|
||||
println();
|
||||
}
|
||||
}
|
||||
|
||||
print0(ucase ? "INSERT " : "insert ");
|
||||
|
||||
if (x.isLowPriority()) {
|
||||
|
||||
@@ -73,7 +73,8 @@ public class OdpsExprParser extends SQLExprParser {
|
||||
}
|
||||
|
||||
protected SQLExpr parseAliasExpr(String alias) {
|
||||
return new SQLCharExpr(alias);
|
||||
String chars = alias.substring(1, alias.length() - 1);
|
||||
return new SQLCharExpr(chars);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -270,7 +270,7 @@ public class Lexer {
|
||||
}
|
||||
|
||||
this.posLine = line;
|
||||
this.posColumn = posColumn;
|
||||
this.posColumn = column;
|
||||
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf
|
||||
@@ -1358,8 +1358,9 @@ public class Lexer {
|
||||
stringVal = new String(buf, 0, bufPos);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected final void scanAlias() {
|
||||
final char quote = ch;
|
||||
{
|
||||
boolean hasSpecial = false;
|
||||
int startIndex = pos + 1;
|
||||
@@ -1370,22 +1371,16 @@ public class Lexer {
|
||||
hasSpecial = true;
|
||||
continue;
|
||||
}
|
||||
if (ch == '"') {
|
||||
if (ch == quote) {
|
||||
if (i + 1 < text.length()) {
|
||||
char ch_next = charAt(i + 1);
|
||||
if (ch_next == '"' || ch_next == '\'') {
|
||||
if (ch_next == quote) {
|
||||
hasSpecial = true;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (i > 0) {
|
||||
char ch_last = charAt(i - 1);
|
||||
if (ch_last == '\'') {
|
||||
hasSpecial = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
endIndex = i;
|
||||
break;
|
||||
}
|
||||
@@ -1415,6 +1410,7 @@ public class Lexer {
|
||||
initBuff(bufPos);
|
||||
//putChar(ch);
|
||||
|
||||
putChar(ch);
|
||||
for (;;) {
|
||||
if (isEOF()) {
|
||||
lexError("unclosed.str.lit");
|
||||
@@ -1431,9 +1427,15 @@ public class Lexer {
|
||||
putChar('\0');
|
||||
break;
|
||||
case '\'':
|
||||
if (ch == quote) {
|
||||
putChar('\\');
|
||||
}
|
||||
putChar('\'');
|
||||
break;
|
||||
case '"':
|
||||
if (ch == quote) {
|
||||
putChar('\\');
|
||||
}
|
||||
putChar('"');
|
||||
break;
|
||||
case 'b':
|
||||
@@ -1449,6 +1451,7 @@ public class Lexer {
|
||||
putChar('\t');
|
||||
break;
|
||||
case '\\':
|
||||
putChar('\\');
|
||||
putChar('\\');
|
||||
break;
|
||||
case 'Z':
|
||||
@@ -1462,23 +1465,19 @@ public class Lexer {
|
||||
continue;
|
||||
}
|
||||
|
||||
// if (ch == '\'') {
|
||||
// char ch_next = charAt(pos + 1);
|
||||
// if (ch_next == '"') {
|
||||
// scanChar();
|
||||
// continue;
|
||||
// }
|
||||
// } else
|
||||
if (ch == '\"') {
|
||||
if (ch == quote) {
|
||||
char ch_next = charAt(pos + 1);
|
||||
if (ch_next == '"' || ch_next == '\'') {
|
||||
|
||||
if (ch_next == quote) {
|
||||
putChar('\\');
|
||||
putChar(ch);
|
||||
scanChar();
|
||||
continue;
|
||||
}
|
||||
|
||||
//putChar(ch);
|
||||
putChar(ch);
|
||||
scanChar();
|
||||
token = LITERAL_CHARS;
|
||||
token = LITERAL_ALIAS;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -436,7 +436,6 @@ public class SQLExprParser extends SQLParser {
|
||||
break;
|
||||
case DUAL:
|
||||
case KEY:
|
||||
case DISTINCT:
|
||||
case LIMIT:
|
||||
case SCHEMA:
|
||||
case COLUMN:
|
||||
@@ -629,6 +628,8 @@ public class SQLExprParser extends SQLParser {
|
||||
lexer.nextToken();
|
||||
break;
|
||||
case IDENTIFIER: // 当负号后面为字段的情况
|
||||
case LITERAL_CHARS:
|
||||
case LITERAL_ALIAS:
|
||||
sqlExpr = new SQLIdentifierExpr(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -671,6 +672,8 @@ public class SQLExprParser extends SQLParser {
|
||||
lexer.nextToken();
|
||||
break;
|
||||
case IDENTIFIER: // 当+号后面为字段的情况
|
||||
case LITERAL_CHARS:
|
||||
case LITERAL_ALIAS:
|
||||
sqlExpr = new SQLIdentifierExpr(lexer.stringVal());
|
||||
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Plus, sqlExpr);
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.alibaba.druid.sql.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.hive.stmt.HiveCreateTableStatement;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
|
||||
public class SQLParser {
|
||||
protected final Lexer lexer;
|
||||
@@ -79,8 +80,11 @@ public class SQLParser {
|
||||
}
|
||||
|
||||
if (token == Token.IDENTIFIER) {
|
||||
String ident = lexer.stringVal;
|
||||
if (ident.equalsIgnoreCase("START") || ident.equalsIgnoreCase("CONNECT")) {
|
||||
long hash = lexer.hash_lower;
|
||||
if (hash == FnvHash.Constants.START
|
||||
|| hash == FnvHash.Constants.CONNECT
|
||||
|| hash == FnvHash.Constants.OFFSET
|
||||
|| hash == FnvHash.Constants.LIMIT) {
|
||||
if (must) {
|
||||
throw new ParserException("illegal alias. " + lexer.info());
|
||||
}
|
||||
|
||||
@@ -2548,6 +2548,14 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
}
|
||||
|
||||
public boolean visit(SQLInsertStatement x) {
|
||||
List<SQLCommentHint> headHints = x.getHeadHintsDirect();
|
||||
if (headHints != null) {
|
||||
for (SQLCommentHint hint : headHints) {
|
||||
hint.accept(this);
|
||||
println();
|
||||
}
|
||||
}
|
||||
|
||||
if (x.isUpsert()) {
|
||||
print0(ucase ? "UPSERT INTO " : "upsert into ");
|
||||
} else {
|
||||
|
||||
@@ -444,6 +444,7 @@ public final class FnvHash {
|
||||
long LESS = fnv1a_64_lower("LESS");
|
||||
long MAXVALUE = fnv1a_64_lower("MAXVALUE");
|
||||
long OFFSET = fnv1a_64_lower("OFFSET");
|
||||
long LIMIT = fnv1a_64_lower("LIMIT");
|
||||
long RAW = fnv1a_64_lower("RAW");
|
||||
long LONG_RAW = fnv1a_64_lower("LONG RAW");
|
||||
long LONG = fnv1a_64_lower("LONG");
|
||||
|
||||
@@ -109,6 +109,7 @@ public interface JdbcConstants {
|
||||
String ALIYUN_DRDS = "aliyun_drds";
|
||||
|
||||
String PRESTO = "presto";
|
||||
String PRESTO_DRIVER = "com.facebook.presto.jdbc.PrestoDriver";
|
||||
|
||||
String ELASTIC_SEARCH = "elastic_search";
|
||||
|
||||
|
||||
@@ -470,7 +470,9 @@ public final class JdbcUtils implements JdbcConstants {
|
||||
return JdbcConstants.ELASTIC_SEARCH_DRIVER;
|
||||
} else if (rawUrl.startsWith("jdbc:clickhouse:")) {
|
||||
return JdbcConstants.CLICKHOUSE_DRIVER;
|
||||
} else {
|
||||
} else if(rawUrl.startsWith("jdbc:presto:")) {
|
||||
return JdbcConstants.PRESTO_DRIVER;
|
||||
}else {
|
||||
throw new SQLException("unkow jdbc driver : " + rawUrl);
|
||||
}
|
||||
}
|
||||
@@ -563,6 +565,8 @@ public final class JdbcUtils implements JdbcConstants {
|
||||
return ELASTIC_SEARCH;
|
||||
} else if (rawUrl.startsWith("jdbc:clickhouse:")) {
|
||||
return CLICKHOUSE;
|
||||
}else if (rawUrl.startsWith("jdbc:presto:")) {
|
||||
return PRESTO;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -132,14 +132,14 @@ public class MySqlUtils {
|
||||
|
||||
try {
|
||||
// pinGlobalTxToPhysicalConnection
|
||||
boolean pinGlobTx = (Boolean) method_6_getValue.invoke(
|
||||
Boolean pinGlobTx = (Boolean) method_6_getValue.invoke(
|
||||
method_6_getBooleanReadableProperty.invoke(
|
||||
method_6_getPropertySet.invoke(physicalConn)
|
||||
, "pinGlobalTxToPhysicalConnection"
|
||||
)
|
||||
);
|
||||
|
||||
if (pinGlobTx) {
|
||||
if (pinGlobTx != null && pinGlobTx) {
|
||||
try {
|
||||
if (method_6_getInstance == null && !method_6_getInstance_error) {
|
||||
class_6_suspendableXAConnection = Class.forName("com.mysql.cj.jdbc.SuspendableXAConnection");
|
||||
@@ -349,14 +349,14 @@ public class MySqlUtils {
|
||||
return SQLUtils.toSQLString(stmtList, JdbcConstants.MYSQL);
|
||||
}
|
||||
|
||||
private static Class class_connectionImpl = null;
|
||||
private static boolean class_connectionImpl_Error = false;
|
||||
private static Method method_getIO = null;
|
||||
private static boolean method_getIO_error = false;
|
||||
private static Class class_MysqlIO = null;
|
||||
private static boolean class_MysqlIO_Error = false;
|
||||
private static Method method_getLastPacketReceivedTimeMs = null;
|
||||
private static boolean method_getLastPacketReceivedTimeMs_error = false;
|
||||
private static transient Class class_connectionImpl = null;
|
||||
private static transient boolean class_connectionImpl_Error = false;
|
||||
private static transient Method method_getIO = null;
|
||||
private static transient boolean method_getIO_error = false;
|
||||
private static transient Class class_MysqlIO = null;
|
||||
private static transient boolean class_MysqlIO_Error = false;
|
||||
private static transient Method method_getLastPacketReceivedTimeMs = null;
|
||||
private static transient boolean method_getLastPacketReceivedTimeMs_error = false;
|
||||
|
||||
public static long getLastPacketReceivedTimeMs(Connection conn) throws SQLException {
|
||||
if (class_connectionImpl == null && !class_connectionImpl_Error) {
|
||||
|
||||
@@ -18,6 +18,9 @@ package com.alibaba.druid.util;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* @author sandzhang[sandzhangtoo@gmail.com]
|
||||
*/
|
||||
@@ -347,4 +350,40 @@ public class StringUtils {
|
||||
// found digit it to make sure weird stuff like '.' and '1E-' doesn't pass
|
||||
return !allowSigns && foundDigit;
|
||||
}
|
||||
|
||||
public static String formatDateTime19(long millis, TimeZone timeZone) {
|
||||
Calendar cale = timeZone == null
|
||||
? Calendar.getInstance()
|
||||
: Calendar.getInstance(timeZone);
|
||||
cale.setTimeInMillis(millis);
|
||||
|
||||
int year = cale.get(Calendar.YEAR);
|
||||
int month = cale.get(Calendar.MONTH) + 1;
|
||||
int dayOfMonth = cale.get(Calendar.DAY_OF_MONTH);
|
||||
int hour = cale.get(Calendar.HOUR_OF_DAY);
|
||||
int minute = cale.get(Calendar.MINUTE);
|
||||
int second = cale.get(Calendar.SECOND);
|
||||
|
||||
char[] chars = new char[19];
|
||||
chars[0] = (char) (year/1000 + '0');
|
||||
chars[1] = (char) ((year/100)%10 + '0');
|
||||
chars[2] = (char) ((year/10)%10 + '0');
|
||||
chars[3] = (char) (year%10 + '0');
|
||||
chars[4] = '-';
|
||||
chars[5] = (char) (month/10 + '0');
|
||||
chars[6] = (char) (month%10 + '0');
|
||||
chars[7] = '-';
|
||||
chars[8] = (char) (dayOfMonth/10 + '0');
|
||||
chars[9] = (char) (dayOfMonth%10 + '0');
|
||||
chars[10] = ' ';
|
||||
chars[11] = (char) (hour/10 + '0');
|
||||
chars[12] = (char) (hour%10 + '0');
|
||||
chars[13] = ':';
|
||||
chars[14] = (char) (minute/10 + '0');
|
||||
chars[15] = (char) (minute%10 + '0');
|
||||
chars[16] = ':';
|
||||
chars[17] = (char) (second/10 + '0');
|
||||
chars[18] = (char) (second%10 + '0');
|
||||
return new String(chars);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,7 +124,8 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
|
||||
|
||||
if (JdbcUtils.MYSQL.equals(dbType) || //
|
||||
JdbcUtils.MARIADB.equals(dbType) || //
|
||||
JdbcUtils.H2.equals(dbType)) {
|
||||
JdbcUtils.H2.equals(dbType)||
|
||||
JdbcUtils.PRESTO.equals(dbType)) {
|
||||
if (config == null) {
|
||||
config = new WallConfig(MySqlWallProvider.DEFAULT_CONFIG_DIR);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user