From 0609ce5d26f2b6d341aa6f226bab9ae47160ce94 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sun, 9 Jun 2013 22:51:38 +0800 Subject: [PATCH] decrease memory used --- .../pool/DruidPooledCallableStatement.java | 2178 +++--- .../pool/DruidPooledPreparedStatement.java | 2142 +++--- .../druid/pool/DruidPooledStatement.java | 1293 ++-- .../sql/ast/statement/SQLTableSourceImpl.java | 124 +- .../ast/statement/MySqlSelectQueryBlock.java | 518 +- .../mysql/visitor/MySqlOutputVisitor.java | 6127 +++++++++-------- .../sql/visitor/SQLASTOutputVisitor.java | 2972 ++++---- .../pool/basic/PoolableStatementTest3.java | 90 +- 8 files changed, 7744 insertions(+), 7700 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/DruidPooledCallableStatement.java b/src/main/java/com/alibaba/druid/pool/DruidPooledCallableStatement.java index ef106e769f..90e68419bb 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidPooledCallableStatement.java +++ b/src/main/java/com/alibaba/druid/pool/DruidPooledCallableStatement.java @@ -1,1089 +1,1089 @@ -/* - * 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.pool; - -import java.io.InputStream; -import java.io.Reader; -import java.math.BigDecimal; -import java.sql.Array; -import java.sql.Blob; -import java.sql.CallableStatement; -import java.sql.Clob; -import java.sql.NClob; -import java.sql.PreparedStatement; -import java.sql.Ref; -import java.sql.ResultSet; -import java.sql.RowId; -import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; -import java.sql.SQLXML; -import java.util.Calendar; - -import com.alibaba.druid.proxy.jdbc.CallableStatementProxy; - -/** - * @author wenshao - */ -public class DruidPooledCallableStatement extends DruidPooledPreparedStatement implements CallableStatement { - - private CallableStatement stmt; - - public DruidPooledCallableStatement(DruidPooledConnection conn, PreparedStatementHolder holder) throws SQLException{ - super(conn, holder); - this.stmt = (CallableStatement) holder.getStatement(); - } - - public CallableStatement getCallableStatementRaw() { - return stmt; - } - - @Override - public void registerOutParameter(int parameterIndex, int sqlType) throws SQLException { - try { - stmt.registerOutParameter(parameterIndex, sqlType); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void registerOutParameter(int parameterIndex, int sqlType, int scale) throws SQLException { - try { - stmt.registerOutParameter(parameterIndex, sqlType, scale); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public boolean wasNull() throws SQLException { - try { - return stmt.wasNull(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public String getString(int parameterIndex) throws SQLException { - try { - return stmt.getString(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public boolean getBoolean(int parameterIndex) throws SQLException { - try { - return stmt.getBoolean(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public byte getByte(int parameterIndex) throws SQLException { - try { - return stmt.getByte(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public short getShort(int parameterIndex) throws SQLException { - try { - return stmt.getShort(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public int getInt(int parameterIndex) throws SQLException { - try { - return stmt.getInt(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public long getLong(int parameterIndex) throws SQLException { - try { - return stmt.getLong(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public float getFloat(int parameterIndex) throws SQLException { - try { - return stmt.getFloat(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public double getDouble(int parameterIndex) throws SQLException { - try { - return stmt.getDouble(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - @Deprecated - public BigDecimal getBigDecimal(int parameterIndex, int scale) throws SQLException { - try { - return stmt.getBigDecimal(parameterIndex, scale); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public byte[] getBytes(int parameterIndex) throws SQLException { - try { - return stmt.getBytes(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.sql.Date getDate(int parameterIndex) throws SQLException { - try { - return stmt.getDate(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.sql.Time getTime(int parameterIndex) throws SQLException { - try { - return stmt.getTime(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.sql.Timestamp getTimestamp(int parameterIndex) throws SQLException { - try { - return stmt.getTimestamp(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public Object getObject(int parameterIndex) throws SQLException { - try { - Object obj = stmt.getObject(parameterIndex); - return wrapObject(obj); - } catch (Throwable t) { - throw checkException(t); - } - } - - private Object wrapObject(Object obj) { - if (obj instanceof ResultSet) { - ResultSet rs = (ResultSet) obj; - - DruidPooledResultSet poolableResultSet = new DruidPooledResultSet(this, rs); - resultSetTrace.add(poolableResultSet); - - obj = poolableResultSet; - } - - return obj; - } - - @Override - public BigDecimal getBigDecimal(int parameterIndex) throws SQLException { - try { - return stmt.getBigDecimal(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public Object getObject(int parameterIndex, java.util.Map> map) throws SQLException { - try { - Object obj = stmt.getObject(parameterIndex, map); - return wrapObject(obj); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public Ref getRef(int parameterIndex) throws SQLException { - try { - return stmt.getRef(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public Blob getBlob(int parameterIndex) throws SQLException { - try { - return stmt.getBlob(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public Clob getClob(int parameterIndex) throws SQLException { - try { - return stmt.getClob(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public Array getArray(int parameterIndex) throws SQLException { - try { - return stmt.getArray(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.sql.Date getDate(int parameterIndex, Calendar cal) throws SQLException { - try { - return stmt.getDate(parameterIndex, cal); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.sql.Time getTime(int parameterIndex, Calendar cal) throws SQLException { - try { - return stmt.getTime(parameterIndex, cal); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.sql.Timestamp getTimestamp(int parameterIndex, Calendar cal) throws SQLException { - try { - return stmt.getTimestamp(parameterIndex, cal); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void registerOutParameter(int parameterIndex, int sqlType, String typeName) throws SQLException { - try { - stmt.registerOutParameter(parameterIndex, sqlType, typeName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void registerOutParameter(String parameterName, int sqlType) throws SQLException { - try { - stmt.registerOutParameter(parameterName, sqlType); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void registerOutParameter(String parameterName, int sqlType, int scale) throws SQLException { - try { - stmt.registerOutParameter(parameterName, sqlType, scale); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void registerOutParameter(String parameterName, int sqlType, String typeName) throws SQLException { - try { - stmt.registerOutParameter(parameterName, sqlType, typeName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.net.URL getURL(int parameterIndex) throws SQLException { - try { - return stmt.getURL(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setURL(String parameterName, java.net.URL val) throws SQLException { - try { - stmt.setURL(parameterName, val); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setNull(String parameterName, int sqlType) throws SQLException { - try { - stmt.setNull(parameterName, sqlType); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBoolean(String parameterName, boolean x) throws SQLException { - try { - stmt.setBoolean(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setByte(String parameterName, byte x) throws SQLException { - try { - stmt.setByte(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setShort(String parameterName, short x) throws SQLException { - try { - stmt.setShort(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setInt(String parameterName, int x) throws SQLException { - try { - stmt.setInt(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setLong(String parameterName, long x) throws SQLException { - try { - stmt.setLong(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setFloat(String parameterName, float x) throws SQLException { - try { - stmt.setFloat(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setDouble(String parameterName, double x) throws SQLException { - try { - stmt.setDouble(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBigDecimal(String parameterName, BigDecimal x) throws SQLException { - try { - stmt.setBigDecimal(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setString(String parameterName, String x) throws SQLException { - try { - stmt.setString(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBytes(String parameterName, byte x[]) throws SQLException { - try { - stmt.setBytes(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setDate(String parameterName, java.sql.Date x) throws SQLException { - try { - stmt.setDate(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setTime(String parameterName, java.sql.Time x) throws SQLException { - try { - stmt.setTime(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setTimestamp(String parameterName, java.sql.Timestamp x) throws SQLException { - try { - stmt.setTimestamp(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setAsciiStream(String parameterName, java.io.InputStream x, int length) throws SQLException { - try { - stmt.setAsciiStream(parameterName, x, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBinaryStream(String parameterName, java.io.InputStream x, int length) throws SQLException { - try { - stmt.setBinaryStream(parameterName, x, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setObject(String parameterName, Object x, int targetSqlType, int scale) throws SQLException { - try { - stmt.setObject(parameterName, x, targetSqlType, scale); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setObject(String parameterName, Object x, int targetSqlType) throws SQLException { - try { - stmt.setObject(parameterName, x, targetSqlType); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setObject(String parameterName, Object x) throws SQLException { - try { - stmt.setObject(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setCharacterStream(String parameterName, java.io.Reader reader, int length) throws SQLException { - try { - stmt.setCharacterStream(parameterName, reader, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setDate(String parameterName, java.sql.Date x, Calendar cal) throws SQLException { - try { - stmt.setDate(parameterName, x, cal); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setTime(String parameterName, java.sql.Time x, Calendar cal) throws SQLException { - try { - stmt.setTime(parameterName, x, cal); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setTimestamp(String parameterName, java.sql.Timestamp x, Calendar cal) throws SQLException { - try { - stmt.setTimestamp(parameterName, x, cal); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setNull(String parameterName, int sqlType, String typeName) throws SQLException { - try { - stmt.setNull(parameterName, sqlType, typeName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public String getString(String parameterName) throws SQLException { - try { - return stmt.getString(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public boolean getBoolean(String parameterName) throws SQLException { - try { - return stmt.getBoolean(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public byte getByte(String parameterName) throws SQLException { - try { - return stmt.getByte(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public short getShort(String parameterName) throws SQLException { - try { - return stmt.getShort(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public int getInt(String parameterName) throws SQLException { - try { - return stmt.getInt(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public long getLong(String parameterName) throws SQLException { - try { - return stmt.getLong(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public float getFloat(String parameterName) throws SQLException { - try { - return stmt.getFloat(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public double getDouble(String parameterName) throws SQLException { - try { - return stmt.getDouble(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public byte[] getBytes(String parameterName) throws SQLException { - try { - return stmt.getBytes(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.sql.Date getDate(String parameterName) throws SQLException { - try { - return stmt.getDate(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.sql.Time getTime(String parameterName) throws SQLException { - try { - return stmt.getTime(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.sql.Timestamp getTimestamp(String parameterName) throws SQLException { - try { - return stmt.getTimestamp(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public Object getObject(String parameterName) throws SQLException { - try { - Object obj = stmt.getObject(parameterName); - return wrapObject(obj); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public BigDecimal getBigDecimal(String parameterName) throws SQLException { - try { - return stmt.getBigDecimal(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public Object getObject(String parameterName, java.util.Map> map) throws SQLException { - try { - Object obj = stmt.getObject(parameterName, map); - return wrapObject(obj); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public Ref getRef(String parameterName) throws SQLException { - try { - return stmt.getRef(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public Blob getBlob(String parameterName) throws SQLException { - try { - return stmt.getBlob(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public Clob getClob(String parameterName) throws SQLException { - try { - return stmt.getClob(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public Array getArray(String parameterName) throws SQLException { - try { - return stmt.getArray(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.sql.Date getDate(String parameterName, Calendar cal) throws SQLException { - try { - return stmt.getDate(parameterName, cal); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.sql.Time getTime(String parameterName, Calendar cal) throws SQLException { - try { - return stmt.getTime(parameterName, cal); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.sql.Timestamp getTimestamp(String parameterName, Calendar cal) throws SQLException { - try { - return stmt.getTimestamp(parameterName, cal); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.net.URL getURL(String parameterName) throws SQLException { - try { - return stmt.getURL(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public RowId getRowId(int parameterIndex) throws SQLException { - try { - return stmt.getRowId(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public RowId getRowId(String parameterName) throws SQLException { - try { - return stmt.getRowId(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setRowId(String parameterName, RowId x) throws SQLException { - try { - stmt.setRowId(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setNString(String parameterName, String value) throws SQLException { - try { - stmt.setNString(parameterName, value); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setNCharacterStream(String parameterName, Reader value, long length) throws SQLException { - try { - stmt.setNCharacterStream(parameterName, value, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setNClob(String parameterName, NClob value) throws SQLException { - try { - stmt.setNClob(parameterName, value); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setClob(String parameterName, Reader reader, long length) throws SQLException { - try { - stmt.setClob(parameterName, reader, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBlob(String parameterName, InputStream inputStream, long length) throws SQLException { - try { - stmt.setBlob(parameterName, inputStream, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setNClob(String parameterName, Reader reader, long length) throws SQLException { - try { - stmt.setNClob(parameterName, reader, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public NClob getNClob(int parameterIndex) throws SQLException { - try { - return stmt.getNClob(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public NClob getNClob(String parameterName) throws SQLException { - try { - return stmt.getNClob(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setSQLXML(String parameterName, SQLXML xmlObject) throws SQLException { - try { - stmt.setSQLXML(parameterName, xmlObject); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public SQLXML getSQLXML(int parameterIndex) throws SQLException { - try { - return stmt.getSQLXML(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public SQLXML getSQLXML(String parameterName) throws SQLException { - try { - return stmt.getSQLXML(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public String getNString(int parameterIndex) throws SQLException { - try { - return stmt.getNString(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public String getNString(String parameterName) throws SQLException { - try { - return stmt.getNString(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.io.Reader getNCharacterStream(int parameterIndex) throws SQLException { - try { - return stmt.getNCharacterStream(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.io.Reader getNCharacterStream(String parameterName) throws SQLException { - try { - return stmt.getNCharacterStream(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.io.Reader getCharacterStream(int parameterIndex) throws SQLException { - try { - return stmt.getCharacterStream(parameterIndex); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public java.io.Reader getCharacterStream(String parameterName) throws SQLException { - try { - return stmt.getCharacterStream(parameterName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBlob(String parameterName, Blob x) throws SQLException { - try { - stmt.setBlob(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setClob(String parameterName, Clob x) throws SQLException { - try { - stmt.setClob(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setAsciiStream(String parameterName, java.io.InputStream x, long length) throws SQLException { - try { - stmt.setAsciiStream(parameterName, x, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBinaryStream(String parameterName, java.io.InputStream x, long length) throws SQLException { - try { - stmt.setBinaryStream(parameterName, x, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setCharacterStream(String parameterName, java.io.Reader reader, long length) throws SQLException { - try { - stmt.setCharacterStream(parameterName, reader, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setAsciiStream(String parameterName, java.io.InputStream x) throws SQLException { - try { - stmt.setAsciiStream(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBinaryStream(String parameterName, java.io.InputStream x) throws SQLException { - try { - stmt.setBinaryStream(parameterName, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setCharacterStream(String parameterName, java.io.Reader reader) throws SQLException { - try { - stmt.setCharacterStream(parameterName, reader); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setNCharacterStream(String parameterName, Reader value) throws SQLException { - try { - stmt.setNCharacterStream(parameterName, value); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setClob(String parameterName, Reader reader) throws SQLException { - try { - stmt.setClob(parameterName, reader); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBlob(String parameterName, InputStream inputStream) throws SQLException { - try { - stmt.setBlob(parameterName, inputStream); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setNClob(String parameterName, Reader reader) throws SQLException { - try { - stmt.setNClob(parameterName, reader); - } catch (Throwable t) { - throw checkException(t); - } - } - - public T getObject(int parameterIndex, Class type) throws SQLException { - throw new SQLFeatureNotSupportedException(); - } - - public T getObject(String parameterName, Class type) throws SQLException { - throw new SQLFeatureNotSupportedException(); - } - - @SuppressWarnings("unchecked") - public T unwrap(Class iface) throws SQLException { - if (iface == CallableStatement.class || iface == PreparedStatement.class) { - if (stmt instanceof CallableStatementProxy) { - return stmt.unwrap(iface); - } - return (T) stmt; - } - - return super.unwrap(iface); - } -} +/* + * 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.pool; + +import java.io.InputStream; +import java.io.Reader; +import java.math.BigDecimal; +import java.sql.Array; +import java.sql.Blob; +import java.sql.CallableStatement; +import java.sql.Clob; +import java.sql.NClob; +import java.sql.PreparedStatement; +import java.sql.Ref; +import java.sql.ResultSet; +import java.sql.RowId; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLXML; +import java.util.Calendar; + +import com.alibaba.druid.proxy.jdbc.CallableStatementProxy; + +/** + * @author wenshao + */ +public class DruidPooledCallableStatement extends DruidPooledPreparedStatement implements CallableStatement { + + private CallableStatement stmt; + + public DruidPooledCallableStatement(DruidPooledConnection conn, PreparedStatementHolder holder) throws SQLException{ + super(conn, holder); + this.stmt = (CallableStatement) holder.getStatement(); + } + + public CallableStatement getCallableStatementRaw() { + return stmt; + } + + @Override + public void registerOutParameter(int parameterIndex, int sqlType) throws SQLException { + try { + stmt.registerOutParameter(parameterIndex, sqlType); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void registerOutParameter(int parameterIndex, int sqlType, int scale) throws SQLException { + try { + stmt.registerOutParameter(parameterIndex, sqlType, scale); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public boolean wasNull() throws SQLException { + try { + return stmt.wasNull(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public String getString(int parameterIndex) throws SQLException { + try { + return stmt.getString(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public boolean getBoolean(int parameterIndex) throws SQLException { + try { + return stmt.getBoolean(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public byte getByte(int parameterIndex) throws SQLException { + try { + return stmt.getByte(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public short getShort(int parameterIndex) throws SQLException { + try { + return stmt.getShort(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public int getInt(int parameterIndex) throws SQLException { + try { + return stmt.getInt(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public long getLong(int parameterIndex) throws SQLException { + try { + return stmt.getLong(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public float getFloat(int parameterIndex) throws SQLException { + try { + return stmt.getFloat(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public double getDouble(int parameterIndex) throws SQLException { + try { + return stmt.getDouble(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + @Deprecated + public BigDecimal getBigDecimal(int parameterIndex, int scale) throws SQLException { + try { + return stmt.getBigDecimal(parameterIndex, scale); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public byte[] getBytes(int parameterIndex) throws SQLException { + try { + return stmt.getBytes(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.sql.Date getDate(int parameterIndex) throws SQLException { + try { + return stmt.getDate(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.sql.Time getTime(int parameterIndex) throws SQLException { + try { + return stmt.getTime(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.sql.Timestamp getTimestamp(int parameterIndex) throws SQLException { + try { + return stmt.getTimestamp(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public Object getObject(int parameterIndex) throws SQLException { + try { + Object obj = stmt.getObject(parameterIndex); + return wrapObject(obj); + } catch (Throwable t) { + throw checkException(t); + } + } + + private Object wrapObject(Object obj) { + if (obj instanceof ResultSet) { + ResultSet rs = (ResultSet) obj; + + DruidPooledResultSet poolableResultSet = new DruidPooledResultSet(this, rs); + addResultSetTrace(poolableResultSet); + + obj = poolableResultSet; + } + + return obj; + } + + @Override + public BigDecimal getBigDecimal(int parameterIndex) throws SQLException { + try { + return stmt.getBigDecimal(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public Object getObject(int parameterIndex, java.util.Map> map) throws SQLException { + try { + Object obj = stmt.getObject(parameterIndex, map); + return wrapObject(obj); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public Ref getRef(int parameterIndex) throws SQLException { + try { + return stmt.getRef(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public Blob getBlob(int parameterIndex) throws SQLException { + try { + return stmt.getBlob(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public Clob getClob(int parameterIndex) throws SQLException { + try { + return stmt.getClob(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public Array getArray(int parameterIndex) throws SQLException { + try { + return stmt.getArray(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.sql.Date getDate(int parameterIndex, Calendar cal) throws SQLException { + try { + return stmt.getDate(parameterIndex, cal); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.sql.Time getTime(int parameterIndex, Calendar cal) throws SQLException { + try { + return stmt.getTime(parameterIndex, cal); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.sql.Timestamp getTimestamp(int parameterIndex, Calendar cal) throws SQLException { + try { + return stmt.getTimestamp(parameterIndex, cal); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void registerOutParameter(int parameterIndex, int sqlType, String typeName) throws SQLException { + try { + stmt.registerOutParameter(parameterIndex, sqlType, typeName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void registerOutParameter(String parameterName, int sqlType) throws SQLException { + try { + stmt.registerOutParameter(parameterName, sqlType); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void registerOutParameter(String parameterName, int sqlType, int scale) throws SQLException { + try { + stmt.registerOutParameter(parameterName, sqlType, scale); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void registerOutParameter(String parameterName, int sqlType, String typeName) throws SQLException { + try { + stmt.registerOutParameter(parameterName, sqlType, typeName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.net.URL getURL(int parameterIndex) throws SQLException { + try { + return stmt.getURL(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setURL(String parameterName, java.net.URL val) throws SQLException { + try { + stmt.setURL(parameterName, val); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setNull(String parameterName, int sqlType) throws SQLException { + try { + stmt.setNull(parameterName, sqlType); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBoolean(String parameterName, boolean x) throws SQLException { + try { + stmt.setBoolean(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setByte(String parameterName, byte x) throws SQLException { + try { + stmt.setByte(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setShort(String parameterName, short x) throws SQLException { + try { + stmt.setShort(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setInt(String parameterName, int x) throws SQLException { + try { + stmt.setInt(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setLong(String parameterName, long x) throws SQLException { + try { + stmt.setLong(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setFloat(String parameterName, float x) throws SQLException { + try { + stmt.setFloat(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setDouble(String parameterName, double x) throws SQLException { + try { + stmt.setDouble(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBigDecimal(String parameterName, BigDecimal x) throws SQLException { + try { + stmt.setBigDecimal(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setString(String parameterName, String x) throws SQLException { + try { + stmt.setString(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBytes(String parameterName, byte x[]) throws SQLException { + try { + stmt.setBytes(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setDate(String parameterName, java.sql.Date x) throws SQLException { + try { + stmt.setDate(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setTime(String parameterName, java.sql.Time x) throws SQLException { + try { + stmt.setTime(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setTimestamp(String parameterName, java.sql.Timestamp x) throws SQLException { + try { + stmt.setTimestamp(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setAsciiStream(String parameterName, java.io.InputStream x, int length) throws SQLException { + try { + stmt.setAsciiStream(parameterName, x, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBinaryStream(String parameterName, java.io.InputStream x, int length) throws SQLException { + try { + stmt.setBinaryStream(parameterName, x, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setObject(String parameterName, Object x, int targetSqlType, int scale) throws SQLException { + try { + stmt.setObject(parameterName, x, targetSqlType, scale); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setObject(String parameterName, Object x, int targetSqlType) throws SQLException { + try { + stmt.setObject(parameterName, x, targetSqlType); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setObject(String parameterName, Object x) throws SQLException { + try { + stmt.setObject(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setCharacterStream(String parameterName, java.io.Reader reader, int length) throws SQLException { + try { + stmt.setCharacterStream(parameterName, reader, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setDate(String parameterName, java.sql.Date x, Calendar cal) throws SQLException { + try { + stmt.setDate(parameterName, x, cal); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setTime(String parameterName, java.sql.Time x, Calendar cal) throws SQLException { + try { + stmt.setTime(parameterName, x, cal); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setTimestamp(String parameterName, java.sql.Timestamp x, Calendar cal) throws SQLException { + try { + stmt.setTimestamp(parameterName, x, cal); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setNull(String parameterName, int sqlType, String typeName) throws SQLException { + try { + stmt.setNull(parameterName, sqlType, typeName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public String getString(String parameterName) throws SQLException { + try { + return stmt.getString(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public boolean getBoolean(String parameterName) throws SQLException { + try { + return stmt.getBoolean(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public byte getByte(String parameterName) throws SQLException { + try { + return stmt.getByte(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public short getShort(String parameterName) throws SQLException { + try { + return stmt.getShort(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public int getInt(String parameterName) throws SQLException { + try { + return stmt.getInt(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public long getLong(String parameterName) throws SQLException { + try { + return stmt.getLong(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public float getFloat(String parameterName) throws SQLException { + try { + return stmt.getFloat(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public double getDouble(String parameterName) throws SQLException { + try { + return stmt.getDouble(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public byte[] getBytes(String parameterName) throws SQLException { + try { + return stmt.getBytes(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.sql.Date getDate(String parameterName) throws SQLException { + try { + return stmt.getDate(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.sql.Time getTime(String parameterName) throws SQLException { + try { + return stmt.getTime(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.sql.Timestamp getTimestamp(String parameterName) throws SQLException { + try { + return stmt.getTimestamp(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public Object getObject(String parameterName) throws SQLException { + try { + Object obj = stmt.getObject(parameterName); + return wrapObject(obj); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public BigDecimal getBigDecimal(String parameterName) throws SQLException { + try { + return stmt.getBigDecimal(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public Object getObject(String parameterName, java.util.Map> map) throws SQLException { + try { + Object obj = stmt.getObject(parameterName, map); + return wrapObject(obj); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public Ref getRef(String parameterName) throws SQLException { + try { + return stmt.getRef(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public Blob getBlob(String parameterName) throws SQLException { + try { + return stmt.getBlob(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public Clob getClob(String parameterName) throws SQLException { + try { + return stmt.getClob(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public Array getArray(String parameterName) throws SQLException { + try { + return stmt.getArray(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.sql.Date getDate(String parameterName, Calendar cal) throws SQLException { + try { + return stmt.getDate(parameterName, cal); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.sql.Time getTime(String parameterName, Calendar cal) throws SQLException { + try { + return stmt.getTime(parameterName, cal); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.sql.Timestamp getTimestamp(String parameterName, Calendar cal) throws SQLException { + try { + return stmt.getTimestamp(parameterName, cal); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.net.URL getURL(String parameterName) throws SQLException { + try { + return stmt.getURL(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public RowId getRowId(int parameterIndex) throws SQLException { + try { + return stmt.getRowId(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public RowId getRowId(String parameterName) throws SQLException { + try { + return stmt.getRowId(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setRowId(String parameterName, RowId x) throws SQLException { + try { + stmt.setRowId(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setNString(String parameterName, String value) throws SQLException { + try { + stmt.setNString(parameterName, value); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setNCharacterStream(String parameterName, Reader value, long length) throws SQLException { + try { + stmt.setNCharacterStream(parameterName, value, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setNClob(String parameterName, NClob value) throws SQLException { + try { + stmt.setNClob(parameterName, value); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setClob(String parameterName, Reader reader, long length) throws SQLException { + try { + stmt.setClob(parameterName, reader, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBlob(String parameterName, InputStream inputStream, long length) throws SQLException { + try { + stmt.setBlob(parameterName, inputStream, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setNClob(String parameterName, Reader reader, long length) throws SQLException { + try { + stmt.setNClob(parameterName, reader, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public NClob getNClob(int parameterIndex) throws SQLException { + try { + return stmt.getNClob(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public NClob getNClob(String parameterName) throws SQLException { + try { + return stmt.getNClob(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setSQLXML(String parameterName, SQLXML xmlObject) throws SQLException { + try { + stmt.setSQLXML(parameterName, xmlObject); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public SQLXML getSQLXML(int parameterIndex) throws SQLException { + try { + return stmt.getSQLXML(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public SQLXML getSQLXML(String parameterName) throws SQLException { + try { + return stmt.getSQLXML(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public String getNString(int parameterIndex) throws SQLException { + try { + return stmt.getNString(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public String getNString(String parameterName) throws SQLException { + try { + return stmt.getNString(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.io.Reader getNCharacterStream(int parameterIndex) throws SQLException { + try { + return stmt.getNCharacterStream(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.io.Reader getNCharacterStream(String parameterName) throws SQLException { + try { + return stmt.getNCharacterStream(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.io.Reader getCharacterStream(int parameterIndex) throws SQLException { + try { + return stmt.getCharacterStream(parameterIndex); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public java.io.Reader getCharacterStream(String parameterName) throws SQLException { + try { + return stmt.getCharacterStream(parameterName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBlob(String parameterName, Blob x) throws SQLException { + try { + stmt.setBlob(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setClob(String parameterName, Clob x) throws SQLException { + try { + stmt.setClob(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setAsciiStream(String parameterName, java.io.InputStream x, long length) throws SQLException { + try { + stmt.setAsciiStream(parameterName, x, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBinaryStream(String parameterName, java.io.InputStream x, long length) throws SQLException { + try { + stmt.setBinaryStream(parameterName, x, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setCharacterStream(String parameterName, java.io.Reader reader, long length) throws SQLException { + try { + stmt.setCharacterStream(parameterName, reader, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setAsciiStream(String parameterName, java.io.InputStream x) throws SQLException { + try { + stmt.setAsciiStream(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBinaryStream(String parameterName, java.io.InputStream x) throws SQLException { + try { + stmt.setBinaryStream(parameterName, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setCharacterStream(String parameterName, java.io.Reader reader) throws SQLException { + try { + stmt.setCharacterStream(parameterName, reader); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setNCharacterStream(String parameterName, Reader value) throws SQLException { + try { + stmt.setNCharacterStream(parameterName, value); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setClob(String parameterName, Reader reader) throws SQLException { + try { + stmt.setClob(parameterName, reader); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBlob(String parameterName, InputStream inputStream) throws SQLException { + try { + stmt.setBlob(parameterName, inputStream); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setNClob(String parameterName, Reader reader) throws SQLException { + try { + stmt.setNClob(parameterName, reader); + } catch (Throwable t) { + throw checkException(t); + } + } + + public T getObject(int parameterIndex, Class type) throws SQLException { + throw new SQLFeatureNotSupportedException(); + } + + public T getObject(String parameterName, Class type) throws SQLException { + throw new SQLFeatureNotSupportedException(); + } + + @SuppressWarnings("unchecked") + public T unwrap(Class iface) throws SQLException { + if (iface == CallableStatement.class || iface == PreparedStatement.class) { + if (stmt instanceof CallableStatementProxy) { + return stmt.unwrap(iface); + } + return (T) stmt; + } + + return super.unwrap(iface); + } +} diff --git a/src/main/java/com/alibaba/druid/pool/DruidPooledPreparedStatement.java b/src/main/java/com/alibaba/druid/pool/DruidPooledPreparedStatement.java index 285e14c569..257bb3055d 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidPooledPreparedStatement.java +++ b/src/main/java/com/alibaba/druid/pool/DruidPooledPreparedStatement.java @@ -1,1071 +1,1071 @@ -/* - * 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.pool; - -import java.io.InputStream; -import java.io.Reader; -import java.math.BigDecimal; -import java.net.URL; -import java.sql.Array; -import java.sql.Blob; -import java.sql.Clob; -import java.sql.Date; -import java.sql.NClob; -import java.sql.ParameterMetaData; -import java.sql.PreparedStatement; -import java.sql.Ref; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.RowId; -import java.sql.SQLException; -import java.sql.SQLXML; -import java.sql.Time; -import java.sql.Timestamp; -import java.util.Arrays; -import java.util.Calendar; - -import com.alibaba.druid.pool.PreparedStatementPool.MethodType; -import com.alibaba.druid.support.logging.Log; -import com.alibaba.druid.support.logging.LogFactory; -import com.alibaba.druid.util.OracleUtils; - -/** - * @author wenshao - */ -public class DruidPooledPreparedStatement extends DruidPooledStatement implements PreparedStatement { - - private final static Log LOG = LogFactory.getLog(DruidPooledPreparedStatement.class); - - private final PreparedStatementHolder holder; - private final PreparedStatement stmt; - private final String sql; - - private int defaultMaxFieldSize; - private int defaultMaxRows; - private int defaultQueryTimeout; - private int defaultFetchDirection; - private int defaultFetchSize; - private int currentMaxFieldSize; - private int currentMaxRows; - private int currentQueryTimeout; - private int currentFetchDirection; - private int currentFetchSize; - - private boolean pooled = false; - - public DruidPooledPreparedStatement(DruidPooledConnection conn, PreparedStatementHolder holder) throws SQLException{ - super(conn, holder.getStatement()); - this.stmt = holder.getStatement(); - this.holder = holder; - this.sql = holder.getKey().sql; - - pooled = conn.getConnectionHolder().isPoolPreparedStatements(); - // Remember the defaults - - if (pooled) { - try { - defaultMaxFieldSize = stmt.getMaxFieldSize(); - } catch (SQLException e) { - LOG.error("getMaxFieldSize error", e); - } - - try { - defaultMaxRows = stmt.getMaxRows(); - } catch (SQLException e) { - LOG.error("getMaxRows error", e); - } - - try { - defaultQueryTimeout = stmt.getQueryTimeout(); - } catch (SQLException e) { - LOG.error("getMaxRows error", e); - } - - try { - defaultFetchDirection = stmt.getFetchDirection(); - } catch (SQLException e) { - LOG.error("getFetchDirection error", e); - } - - try { - defaultFetchSize = stmt.getFetchSize(); - } catch (SQLException e) { - LOG.error("getFetchSize error", e); - } - } - - currentMaxFieldSize = defaultMaxFieldSize; - currentMaxRows = defaultMaxRows; - currentQueryTimeout = defaultQueryTimeout; - currentFetchDirection = defaultFetchDirection; - currentFetchSize = defaultFetchSize; - } - - public PreparedStatementHolder getPreparedStatementHolder() { - return holder; - } - - public int getHitCount() { - return holder.getHitCount(); - } - - public void setFetchSize(int rows) throws SQLException { - currentFetchSize = rows; - super.setFetchSize(rows); - } - - public void setFetchDirection(int direction) throws SQLException { - currentFetchDirection = direction; - super.setFetchDirection(direction); - } - - public void setMaxFieldSize(int max) throws SQLException { - currentMaxFieldSize = max; - super.setMaxFieldSize(max); - } - - public void setMaxRows(int max) throws SQLException { - currentMaxRows = max; - super.setMaxRows(max); - } - - public void setQueryTimeout(int seconds) throws SQLException { - currentQueryTimeout = seconds; - super.setQueryTimeout(seconds); - } - - public String getSql() { - return sql; - } - - public PreparedStatementKey getKey() { - return holder.getKey(); - } - - public PreparedStatement getRawPreparedStatement() { - return stmt; - } - - public PreparedStatement getRawStatement() { - return stmt; - } - - @Override - public void close() throws SQLException { - if (isClosed()) { - return; - } - - boolean connectionClosed = this.conn.isClosed(); - // Reset the defaults - if (pooled && !connectionClosed) { - try { - if (defaultMaxFieldSize != currentMaxFieldSize) { - stmt.setMaxFieldSize(defaultMaxFieldSize); - currentMaxFieldSize = defaultMaxFieldSize; - } - if (defaultMaxRows != currentMaxRows) { - stmt.setMaxRows(defaultMaxRows); - currentMaxRows = defaultMaxRows; - } - if (defaultQueryTimeout != currentQueryTimeout) { - stmt.setQueryTimeout(defaultQueryTimeout); - currentQueryTimeout = defaultQueryTimeout; - } - if (defaultFetchDirection != currentFetchDirection) { - stmt.setFetchDirection(defaultFetchDirection); - currentFetchDirection = defaultFetchDirection; - } - if (defaultFetchSize != currentFetchSize) { - stmt.setFetchSize(defaultFetchSize); - currentFetchSize = defaultFetchSize; - } - } catch (Exception e) { - this.conn.handleException(e); - } - } - - conn.closePoolableStatement(this); - } - - public boolean isPooled() { - return pooled; - } - - void closeInternal() throws SQLException { - super.close(); - } - - void setClosed(boolean value) { - this.closed = value; - } - - @Override - public ResultSet executeQuery() throws SQLException { - checkOpen(); - - incrementExecuteCount(); - transactionRecord(sql); - - oracleSetRowPrefetch(); - - conn.beforeExecute(); - try { - ResultSet rs = stmt.executeQuery(); - - if (rs == null) { - return null; - } - - DruidPooledResultSet poolableResultSet = new DruidPooledResultSet(this, rs); - resultSetTrace.add(poolableResultSet); - - return poolableResultSet; - } catch (Throwable t) { - throw checkException(t); - } finally { - conn.afterExecute(); - } - } - - @Override - public int executeUpdate() throws SQLException { - checkOpen(); - - incrementExecuteCount(); - transactionRecord(sql); - - conn.beforeExecute(); - try { - return stmt.executeUpdate(); - } catch (Throwable t) { - throw checkException(t); - } finally { - conn.afterExecute(); - } - } - - @Override - public void setNull(int parameterIndex, int sqlType) throws SQLException { - checkOpen(); - - try { - stmt.setNull(parameterIndex, sqlType); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBoolean(int parameterIndex, boolean x) throws SQLException { - checkOpen(); - - try { - stmt.setBoolean(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setByte(int parameterIndex, byte x) throws SQLException { - checkOpen(); - - try { - stmt.setByte(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setShort(int parameterIndex, short x) throws SQLException { - checkOpen(); - - try { - stmt.setShort(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setInt(int parameterIndex, int x) throws SQLException { - checkOpen(); - - try { - stmt.setInt(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setLong(int parameterIndex, long x) throws SQLException { - checkOpen(); - - try { - stmt.setLong(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setFloat(int parameterIndex, float x) throws SQLException { - checkOpen(); - - try { - stmt.setFloat(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setDouble(int parameterIndex, double x) throws SQLException { - checkOpen(); - - try { - stmt.setDouble(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { - checkOpen(); - - try { - stmt.setBigDecimal(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setString(int parameterIndex, String x) throws SQLException { - checkOpen(); - - try { - stmt.setString(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBytes(int parameterIndex, byte[] x) throws SQLException { - checkOpen(); - - try { - stmt.setBytes(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setDate(int parameterIndex, Date x) throws SQLException { - checkOpen(); - - try { - stmt.setDate(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setTime(int parameterIndex, Time x) throws SQLException { - checkOpen(); - - try { - stmt.setTime(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException { - checkOpen(); - - try { - stmt.setTimestamp(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException { - checkOpen(); - - try { - stmt.setAsciiStream(parameterIndex, x, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - @Deprecated - public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException { - checkOpen(); - - try { - stmt.setUnicodeStream(parameterIndex, x, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { - checkOpen(); - - try { - stmt.setBinaryStream(parameterIndex, x, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void clearParameters() throws SQLException { - checkOpen(); - - try { - stmt.clearParameters(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { - checkOpen(); - - try { - stmt.setObject(parameterIndex, x, targetSqlType); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setObject(int parameterIndex, Object x) throws SQLException { - checkOpen(); - - try { - stmt.setObject(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public boolean execute() throws SQLException { - checkOpen(); - - incrementExecuteCount(); - transactionRecord(sql); - - // oracleSetRowPrefetch(); - - conn.beforeExecute(); - try { - return stmt.execute(); - } catch (Throwable t) { - throw checkException(t); - } finally { - conn.afterExecute(); - } - } - - protected void oracleSetRowPrefetch() throws SQLException { - if (!conn.isOracle()) { - return; - } - - if (holder.getHitCount() == 0) { - return; - } - - int fetchRowPeak = holder.getFetchRowPeak(); - - if (fetchRowPeak < 0) { - return; - } - - if (holder.getDefaultRowPretch() == -1) { - int defaultRowPretch = OracleUtils.getRowPrefetch(this); - if (defaultRowPretch != holder.getDefaultRowPretch()) { - holder.setDefaultRowPretch(defaultRowPretch); - holder.setRowPrefetch(defaultRowPretch); - } - } - - int rowPrefetch; - - if (fetchRowPeak <= 1) { - rowPrefetch = 2; - } else if (fetchRowPeak > holder.getDefaultRowPretch()) { - rowPrefetch = holder.getDefaultRowPretch(); - } else { - rowPrefetch = fetchRowPeak + 1; - } - - if (rowPrefetch != holder.getRowPrefetch()) { - OracleUtils.setRowPrefetch(this, rowPrefetch); - holder.setRowPrefetch(rowPrefetch); - } - } - - @Override - public void addBatch() throws SQLException { - checkOpen(); - - try { - stmt.addBatch(); - } catch (Throwable t) { - throw checkException(t); - } - } - - public int[] executeBatch() throws SQLException { - checkOpen(); - - incrementExecuteCount(); - transactionRecord(sql); - - conn.beforeExecute(); - try { - return stmt.executeBatch(); - } catch (Throwable t) { - throw checkException(t); - } finally { - conn.afterExecute(); - } - } - - @Override - public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException { - checkOpen(); - - try { - stmt.setCharacterStream(parameterIndex, reader, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setRef(int parameterIndex, Ref x) throws SQLException { - checkOpen(); - - try { - stmt.setRef(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBlob(int parameterIndex, Blob x) throws SQLException { - checkOpen(); - - try { - stmt.setBlob(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setClob(int parameterIndex, Clob x) throws SQLException { - checkOpen(); - - try { - stmt.setClob(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setArray(int parameterIndex, Array x) throws SQLException { - checkOpen(); - - try { - stmt.setArray(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public ResultSetMetaData getMetaData() throws SQLException { - checkOpen(); - - try { - return stmt.getMetaData(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException { - checkOpen(); - - try { - stmt.setDate(parameterIndex, x, cal); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException { - checkOpen(); - - try { - stmt.setTime(parameterIndex, x, cal); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException { - checkOpen(); - - try { - stmt.setTimestamp(parameterIndex, x, cal); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException { - checkOpen(); - - try { - stmt.setNull(parameterIndex, sqlType, typeName); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setURL(int parameterIndex, URL x) throws SQLException { - checkOpen(); - - try { - stmt.setURL(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public ParameterMetaData getParameterMetaData() throws SQLException { - checkOpen(); - - try { - return stmt.getParameterMetaData(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setRowId(int parameterIndex, RowId x) throws SQLException { - checkOpen(); - - try { - stmt.setRowId(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setNString(int parameterIndex, String value) throws SQLException { - checkOpen(); - - try { - stmt.setNString(parameterIndex, value); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException { - checkOpen(); - - try { - stmt.setNCharacterStream(parameterIndex, value, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setNClob(int parameterIndex, NClob value) throws SQLException { - checkOpen(); - - try { - stmt.setNClob(parameterIndex, value); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setClob(int parameterIndex, Reader reader, long length) throws SQLException { - checkOpen(); - - try { - stmt.setClob(parameterIndex, reader, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException { - checkOpen(); - - try { - stmt.setBlob(parameterIndex, inputStream, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException { - checkOpen(); - - try { - stmt.setNClob(parameterIndex, reader, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException { - checkOpen(); - - try { - stmt.setSQLXML(parameterIndex, xmlObject); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength) throws SQLException { - checkOpen(); - - try { - stmt.setObject(parameterIndex, x, targetSqlType, scaleOrLength); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException { - checkOpen(); - - try { - stmt.setAsciiStream(parameterIndex, x, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException { - checkOpen(); - - try { - stmt.setBinaryStream(parameterIndex, x, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException { - checkOpen(); - - try { - stmt.setCharacterStream(parameterIndex, reader, length); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException { - checkOpen(); - - try { - stmt.setAsciiStream(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException { - checkOpen(); - - try { - stmt.setBinaryStream(parameterIndex, x); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException { - checkOpen(); - - try { - stmt.setCharacterStream(parameterIndex, reader); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException { - checkOpen(); - - try { - stmt.setNCharacterStream(parameterIndex, value); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setClob(int parameterIndex, Reader reader) throws SQLException { - checkOpen(); - - try { - stmt.setClob(parameterIndex, reader); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException { - checkOpen(); - - try { - stmt.setBlob(parameterIndex, inputStream); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setNClob(int parameterIndex, Reader reader) throws SQLException { - checkOpen(); - - try { - stmt.setNClob(parameterIndex, reader); - } catch (Throwable t) { - throw checkException(t); - } - } - - public static class PreparedStatementKey { - - private final String sql; - private final String catalog; - - protected final MethodType methodType; - - private final int resultSetType; - private final int resultSetConcurrency; - private final int resultSetHoldability; - private final int autoGeneratedKeys; - private final int[] columnIndexes; - private final String[] columnNames; - - public PreparedStatementKey(String sql, String catalog, MethodType methodType) throws SQLException{ - this(sql, catalog, methodType, 0, 0, 0, 0, null, null); - } - - public PreparedStatementKey(String sql, String catalog, MethodType methodType, int resultSetType, - int resultSetConcurrency) throws SQLException{ - this(sql, catalog, methodType, resultSetType, resultSetConcurrency, 0, 0, null, null); - } - - public PreparedStatementKey(String sql, String catalog, MethodType methodType, int resultSetType, - int resultSetConcurrency, int resultSetHoldability) throws SQLException{ - this(sql, catalog, methodType, resultSetType, resultSetConcurrency, resultSetHoldability, 0, null, null); - } - - public PreparedStatementKey(String sql, String catalog, MethodType methodType, int autoGeneratedKeys) - throws SQLException{ - this(sql, catalog, methodType, 0, 0, 0, autoGeneratedKeys, null, null); - } - - public PreparedStatementKey(String sql, String catalog, MethodType methodType, int[] columnIndexes) - throws SQLException{ - this(sql, catalog, methodType, 0, 0, 0, 0, columnIndexes, null); - } - - public PreparedStatementKey(String sql, String catalog, MethodType methodType, String[] columnNames) - throws SQLException{ - this(sql, catalog, methodType, 0, 0, 0, 0, null, columnNames); - } - - public PreparedStatementKey(String sql, String catalog, MethodType methodType, int resultSetType, - int resultSetConcurrency, int resultSetHoldability, int autoGeneratedKeys, - int[] columnIndexes, String[] columnNames) throws SQLException{ - if (sql == null) { - throw new SQLException("sql is null"); - } - - this.sql = sql; - this.catalog = catalog; - this.methodType = methodType; - this.resultSetType = resultSetType; - this.resultSetConcurrency = resultSetConcurrency; - this.resultSetHoldability = resultSetHoldability; - this.autoGeneratedKeys = autoGeneratedKeys; - this.columnIndexes = columnIndexes; - this.columnNames = columnNames; - } - - public int getResultSetType() { - return resultSetType; - } - - public int getResultSetConcurrency() { - return resultSetConcurrency; - } - - public int getResultSetHoldability() { - return resultSetHoldability; - } - - public boolean equals(Object object) { - PreparedStatementKey that = (PreparedStatementKey) object; - - if (!this.sql.equals(that.sql)) { - return false; - } - - if (this.catalog == null) { - if (that.catalog != null) { - return false; - } - } else { - if (!this.catalog.equals(that.catalog)) { - return false; - } - } - - if (this.methodType != that.methodType) { - return false; - } - - if (this.resultSetType != that.resultSetType) { - return false; - } - - if (this.resultSetConcurrency != that.resultSetConcurrency) { - return false; - } - - if (this.resultSetHoldability != that.resultSetHoldability) { - return false; - } - - if (this.autoGeneratedKeys != that.autoGeneratedKeys) { - return false; - } - - if (!Arrays.equals(columnIndexes, that.columnIndexes)) { - return false; - } - - if (!Arrays.equals(columnNames, that.columnNames)) { - return false; - } - - return true; - } - - public int hashCode() { - final int prime = 31; - int result = 1; - - result = prime * result + ((sql == null) ? 0 : sql.hashCode()); - result = prime * result + ((catalog == null) ? 0 : catalog.hashCode()); - result = prime * result + ((methodType == null) ? 0 : methodType.hashCode()); - - result = prime * result + resultSetConcurrency; - result = prime * result + resultSetHoldability; - result = prime * result + resultSetType; - - result = prime * result + autoGeneratedKeys; - - result = prime * result + Arrays.hashCode(columnIndexes); - result = prime * result + Arrays.hashCode(columnNames); - - return result; - } - - public String getSql() { - return sql; - } - - } - - @Override - public boolean isWrapperFor(Class iface) throws SQLException { - if (iface == PreparedStatementHolder.class) { - return true; - } - - return super.isWrapperFor(iface); - } - - @SuppressWarnings("unchecked") - public T unwrap(Class iface) throws SQLException { - if (iface == PreparedStatementHolder.class) { - return (T) holder; - } - return super.unwrap(iface); - } -} +/* + * 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.pool; + +import java.io.InputStream; +import java.io.Reader; +import java.math.BigDecimal; +import java.net.URL; +import java.sql.Array; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.Date; +import java.sql.NClob; +import java.sql.ParameterMetaData; +import java.sql.PreparedStatement; +import java.sql.Ref; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.RowId; +import java.sql.SQLException; +import java.sql.SQLXML; +import java.sql.Time; +import java.sql.Timestamp; +import java.util.Arrays; +import java.util.Calendar; + +import com.alibaba.druid.pool.PreparedStatementPool.MethodType; +import com.alibaba.druid.support.logging.Log; +import com.alibaba.druid.support.logging.LogFactory; +import com.alibaba.druid.util.OracleUtils; + +/** + * @author wenshao + */ +public class DruidPooledPreparedStatement extends DruidPooledStatement implements PreparedStatement { + + private final static Log LOG = LogFactory.getLog(DruidPooledPreparedStatement.class); + + private final PreparedStatementHolder holder; + private final PreparedStatement stmt; + private final String sql; + + private int defaultMaxFieldSize; + private int defaultMaxRows; + private int defaultQueryTimeout; + private int defaultFetchDirection; + private int defaultFetchSize; + private int currentMaxFieldSize; + private int currentMaxRows; + private int currentQueryTimeout; + private int currentFetchDirection; + private int currentFetchSize; + + private boolean pooled = false; + + public DruidPooledPreparedStatement(DruidPooledConnection conn, PreparedStatementHolder holder) throws SQLException{ + super(conn, holder.getStatement()); + this.stmt = holder.getStatement(); + this.holder = holder; + this.sql = holder.getKey().sql; + + pooled = conn.getConnectionHolder().isPoolPreparedStatements(); + // Remember the defaults + + if (pooled) { + try { + defaultMaxFieldSize = stmt.getMaxFieldSize(); + } catch (SQLException e) { + LOG.error("getMaxFieldSize error", e); + } + + try { + defaultMaxRows = stmt.getMaxRows(); + } catch (SQLException e) { + LOG.error("getMaxRows error", e); + } + + try { + defaultQueryTimeout = stmt.getQueryTimeout(); + } catch (SQLException e) { + LOG.error("getMaxRows error", e); + } + + try { + defaultFetchDirection = stmt.getFetchDirection(); + } catch (SQLException e) { + LOG.error("getFetchDirection error", e); + } + + try { + defaultFetchSize = stmt.getFetchSize(); + } catch (SQLException e) { + LOG.error("getFetchSize error", e); + } + } + + currentMaxFieldSize = defaultMaxFieldSize; + currentMaxRows = defaultMaxRows; + currentQueryTimeout = defaultQueryTimeout; + currentFetchDirection = defaultFetchDirection; + currentFetchSize = defaultFetchSize; + } + + public PreparedStatementHolder getPreparedStatementHolder() { + return holder; + } + + public int getHitCount() { + return holder.getHitCount(); + } + + public void setFetchSize(int rows) throws SQLException { + currentFetchSize = rows; + super.setFetchSize(rows); + } + + public void setFetchDirection(int direction) throws SQLException { + currentFetchDirection = direction; + super.setFetchDirection(direction); + } + + public void setMaxFieldSize(int max) throws SQLException { + currentMaxFieldSize = max; + super.setMaxFieldSize(max); + } + + public void setMaxRows(int max) throws SQLException { + currentMaxRows = max; + super.setMaxRows(max); + } + + public void setQueryTimeout(int seconds) throws SQLException { + currentQueryTimeout = seconds; + super.setQueryTimeout(seconds); + } + + public String getSql() { + return sql; + } + + public PreparedStatementKey getKey() { + return holder.getKey(); + } + + public PreparedStatement getRawPreparedStatement() { + return stmt; + } + + public PreparedStatement getRawStatement() { + return stmt; + } + + @Override + public void close() throws SQLException { + if (isClosed()) { + return; + } + + boolean connectionClosed = this.conn.isClosed(); + // Reset the defaults + if (pooled && !connectionClosed) { + try { + if (defaultMaxFieldSize != currentMaxFieldSize) { + stmt.setMaxFieldSize(defaultMaxFieldSize); + currentMaxFieldSize = defaultMaxFieldSize; + } + if (defaultMaxRows != currentMaxRows) { + stmt.setMaxRows(defaultMaxRows); + currentMaxRows = defaultMaxRows; + } + if (defaultQueryTimeout != currentQueryTimeout) { + stmt.setQueryTimeout(defaultQueryTimeout); + currentQueryTimeout = defaultQueryTimeout; + } + if (defaultFetchDirection != currentFetchDirection) { + stmt.setFetchDirection(defaultFetchDirection); + currentFetchDirection = defaultFetchDirection; + } + if (defaultFetchSize != currentFetchSize) { + stmt.setFetchSize(defaultFetchSize); + currentFetchSize = defaultFetchSize; + } + } catch (Exception e) { + this.conn.handleException(e); + } + } + + conn.closePoolableStatement(this); + } + + public boolean isPooled() { + return pooled; + } + + void closeInternal() throws SQLException { + super.close(); + } + + void setClosed(boolean value) { + this.closed = value; + } + + @Override + public ResultSet executeQuery() throws SQLException { + checkOpen(); + + incrementExecuteCount(); + transactionRecord(sql); + + oracleSetRowPrefetch(); + + conn.beforeExecute(); + try { + ResultSet rs = stmt.executeQuery(); + + if (rs == null) { + return null; + } + + DruidPooledResultSet poolableResultSet = new DruidPooledResultSet(this, rs); + addResultSetTrace(poolableResultSet); + + return poolableResultSet; + } catch (Throwable t) { + throw checkException(t); + } finally { + conn.afterExecute(); + } + } + + @Override + public int executeUpdate() throws SQLException { + checkOpen(); + + incrementExecuteCount(); + transactionRecord(sql); + + conn.beforeExecute(); + try { + return stmt.executeUpdate(); + } catch (Throwable t) { + throw checkException(t); + } finally { + conn.afterExecute(); + } + } + + @Override + public void setNull(int parameterIndex, int sqlType) throws SQLException { + checkOpen(); + + try { + stmt.setNull(parameterIndex, sqlType); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBoolean(int parameterIndex, boolean x) throws SQLException { + checkOpen(); + + try { + stmt.setBoolean(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setByte(int parameterIndex, byte x) throws SQLException { + checkOpen(); + + try { + stmt.setByte(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setShort(int parameterIndex, short x) throws SQLException { + checkOpen(); + + try { + stmt.setShort(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setInt(int parameterIndex, int x) throws SQLException { + checkOpen(); + + try { + stmt.setInt(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setLong(int parameterIndex, long x) throws SQLException { + checkOpen(); + + try { + stmt.setLong(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setFloat(int parameterIndex, float x) throws SQLException { + checkOpen(); + + try { + stmt.setFloat(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setDouble(int parameterIndex, double x) throws SQLException { + checkOpen(); + + try { + stmt.setDouble(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { + checkOpen(); + + try { + stmt.setBigDecimal(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setString(int parameterIndex, String x) throws SQLException { + checkOpen(); + + try { + stmt.setString(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBytes(int parameterIndex, byte[] x) throws SQLException { + checkOpen(); + + try { + stmt.setBytes(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setDate(int parameterIndex, Date x) throws SQLException { + checkOpen(); + + try { + stmt.setDate(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setTime(int parameterIndex, Time x) throws SQLException { + checkOpen(); + + try { + stmt.setTime(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException { + checkOpen(); + + try { + stmt.setTimestamp(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException { + checkOpen(); + + try { + stmt.setAsciiStream(parameterIndex, x, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + @Deprecated + public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException { + checkOpen(); + + try { + stmt.setUnicodeStream(parameterIndex, x, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { + checkOpen(); + + try { + stmt.setBinaryStream(parameterIndex, x, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void clearParameters() throws SQLException { + checkOpen(); + + try { + stmt.clearParameters(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { + checkOpen(); + + try { + stmt.setObject(parameterIndex, x, targetSqlType); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setObject(int parameterIndex, Object x) throws SQLException { + checkOpen(); + + try { + stmt.setObject(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public boolean execute() throws SQLException { + checkOpen(); + + incrementExecuteCount(); + transactionRecord(sql); + + // oracleSetRowPrefetch(); + + conn.beforeExecute(); + try { + return stmt.execute(); + } catch (Throwable t) { + throw checkException(t); + } finally { + conn.afterExecute(); + } + } + + protected void oracleSetRowPrefetch() throws SQLException { + if (!conn.isOracle()) { + return; + } + + if (holder.getHitCount() == 0) { + return; + } + + int fetchRowPeak = holder.getFetchRowPeak(); + + if (fetchRowPeak < 0) { + return; + } + + if (holder.getDefaultRowPretch() == -1) { + int defaultRowPretch = OracleUtils.getRowPrefetch(this); + if (defaultRowPretch != holder.getDefaultRowPretch()) { + holder.setDefaultRowPretch(defaultRowPretch); + holder.setRowPrefetch(defaultRowPretch); + } + } + + int rowPrefetch; + + if (fetchRowPeak <= 1) { + rowPrefetch = 2; + } else if (fetchRowPeak > holder.getDefaultRowPretch()) { + rowPrefetch = holder.getDefaultRowPretch(); + } else { + rowPrefetch = fetchRowPeak + 1; + } + + if (rowPrefetch != holder.getRowPrefetch()) { + OracleUtils.setRowPrefetch(this, rowPrefetch); + holder.setRowPrefetch(rowPrefetch); + } + } + + @Override + public void addBatch() throws SQLException { + checkOpen(); + + try { + stmt.addBatch(); + } catch (Throwable t) { + throw checkException(t); + } + } + + public int[] executeBatch() throws SQLException { + checkOpen(); + + incrementExecuteCount(); + transactionRecord(sql); + + conn.beforeExecute(); + try { + return stmt.executeBatch(); + } catch (Throwable t) { + throw checkException(t); + } finally { + conn.afterExecute(); + } + } + + @Override + public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException { + checkOpen(); + + try { + stmt.setCharacterStream(parameterIndex, reader, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setRef(int parameterIndex, Ref x) throws SQLException { + checkOpen(); + + try { + stmt.setRef(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBlob(int parameterIndex, Blob x) throws SQLException { + checkOpen(); + + try { + stmt.setBlob(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setClob(int parameterIndex, Clob x) throws SQLException { + checkOpen(); + + try { + stmt.setClob(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setArray(int parameterIndex, Array x) throws SQLException { + checkOpen(); + + try { + stmt.setArray(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public ResultSetMetaData getMetaData() throws SQLException { + checkOpen(); + + try { + return stmt.getMetaData(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException { + checkOpen(); + + try { + stmt.setDate(parameterIndex, x, cal); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException { + checkOpen(); + + try { + stmt.setTime(parameterIndex, x, cal); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException { + checkOpen(); + + try { + stmt.setTimestamp(parameterIndex, x, cal); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException { + checkOpen(); + + try { + stmt.setNull(parameterIndex, sqlType, typeName); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setURL(int parameterIndex, URL x) throws SQLException { + checkOpen(); + + try { + stmt.setURL(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public ParameterMetaData getParameterMetaData() throws SQLException { + checkOpen(); + + try { + return stmt.getParameterMetaData(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setRowId(int parameterIndex, RowId x) throws SQLException { + checkOpen(); + + try { + stmt.setRowId(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setNString(int parameterIndex, String value) throws SQLException { + checkOpen(); + + try { + stmt.setNString(parameterIndex, value); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException { + checkOpen(); + + try { + stmt.setNCharacterStream(parameterIndex, value, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setNClob(int parameterIndex, NClob value) throws SQLException { + checkOpen(); + + try { + stmt.setNClob(parameterIndex, value); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setClob(int parameterIndex, Reader reader, long length) throws SQLException { + checkOpen(); + + try { + stmt.setClob(parameterIndex, reader, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException { + checkOpen(); + + try { + stmt.setBlob(parameterIndex, inputStream, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException { + checkOpen(); + + try { + stmt.setNClob(parameterIndex, reader, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException { + checkOpen(); + + try { + stmt.setSQLXML(parameterIndex, xmlObject); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength) throws SQLException { + checkOpen(); + + try { + stmt.setObject(parameterIndex, x, targetSqlType, scaleOrLength); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException { + checkOpen(); + + try { + stmt.setAsciiStream(parameterIndex, x, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException { + checkOpen(); + + try { + stmt.setBinaryStream(parameterIndex, x, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException { + checkOpen(); + + try { + stmt.setCharacterStream(parameterIndex, reader, length); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException { + checkOpen(); + + try { + stmt.setAsciiStream(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException { + checkOpen(); + + try { + stmt.setBinaryStream(parameterIndex, x); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException { + checkOpen(); + + try { + stmt.setCharacterStream(parameterIndex, reader); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException { + checkOpen(); + + try { + stmt.setNCharacterStream(parameterIndex, value); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setClob(int parameterIndex, Reader reader) throws SQLException { + checkOpen(); + + try { + stmt.setClob(parameterIndex, reader); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException { + checkOpen(); + + try { + stmt.setBlob(parameterIndex, inputStream); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setNClob(int parameterIndex, Reader reader) throws SQLException { + checkOpen(); + + try { + stmt.setNClob(parameterIndex, reader); + } catch (Throwable t) { + throw checkException(t); + } + } + + public static class PreparedStatementKey { + + private final String sql; + private final String catalog; + + protected final MethodType methodType; + + private final int resultSetType; + private final int resultSetConcurrency; + private final int resultSetHoldability; + private final int autoGeneratedKeys; + private final int[] columnIndexes; + private final String[] columnNames; + + public PreparedStatementKey(String sql, String catalog, MethodType methodType) throws SQLException{ + this(sql, catalog, methodType, 0, 0, 0, 0, null, null); + } + + public PreparedStatementKey(String sql, String catalog, MethodType methodType, int resultSetType, + int resultSetConcurrency) throws SQLException{ + this(sql, catalog, methodType, resultSetType, resultSetConcurrency, 0, 0, null, null); + } + + public PreparedStatementKey(String sql, String catalog, MethodType methodType, int resultSetType, + int resultSetConcurrency, int resultSetHoldability) throws SQLException{ + this(sql, catalog, methodType, resultSetType, resultSetConcurrency, resultSetHoldability, 0, null, null); + } + + public PreparedStatementKey(String sql, String catalog, MethodType methodType, int autoGeneratedKeys) + throws SQLException{ + this(sql, catalog, methodType, 0, 0, 0, autoGeneratedKeys, null, null); + } + + public PreparedStatementKey(String sql, String catalog, MethodType methodType, int[] columnIndexes) + throws SQLException{ + this(sql, catalog, methodType, 0, 0, 0, 0, columnIndexes, null); + } + + public PreparedStatementKey(String sql, String catalog, MethodType methodType, String[] columnNames) + throws SQLException{ + this(sql, catalog, methodType, 0, 0, 0, 0, null, columnNames); + } + + public PreparedStatementKey(String sql, String catalog, MethodType methodType, int resultSetType, + int resultSetConcurrency, int resultSetHoldability, int autoGeneratedKeys, + int[] columnIndexes, String[] columnNames) throws SQLException{ + if (sql == null) { + throw new SQLException("sql is null"); + } + + this.sql = sql; + this.catalog = catalog; + this.methodType = methodType; + this.resultSetType = resultSetType; + this.resultSetConcurrency = resultSetConcurrency; + this.resultSetHoldability = resultSetHoldability; + this.autoGeneratedKeys = autoGeneratedKeys; + this.columnIndexes = columnIndexes; + this.columnNames = columnNames; + } + + public int getResultSetType() { + return resultSetType; + } + + public int getResultSetConcurrency() { + return resultSetConcurrency; + } + + public int getResultSetHoldability() { + return resultSetHoldability; + } + + public boolean equals(Object object) { + PreparedStatementKey that = (PreparedStatementKey) object; + + if (!this.sql.equals(that.sql)) { + return false; + } + + if (this.catalog == null) { + if (that.catalog != null) { + return false; + } + } else { + if (!this.catalog.equals(that.catalog)) { + return false; + } + } + + if (this.methodType != that.methodType) { + return false; + } + + if (this.resultSetType != that.resultSetType) { + return false; + } + + if (this.resultSetConcurrency != that.resultSetConcurrency) { + return false; + } + + if (this.resultSetHoldability != that.resultSetHoldability) { + return false; + } + + if (this.autoGeneratedKeys != that.autoGeneratedKeys) { + return false; + } + + if (!Arrays.equals(columnIndexes, that.columnIndexes)) { + return false; + } + + if (!Arrays.equals(columnNames, that.columnNames)) { + return false; + } + + return true; + } + + public int hashCode() { + final int prime = 31; + int result = 1; + + result = prime * result + ((sql == null) ? 0 : sql.hashCode()); + result = prime * result + ((catalog == null) ? 0 : catalog.hashCode()); + result = prime * result + ((methodType == null) ? 0 : methodType.hashCode()); + + result = prime * result + resultSetConcurrency; + result = prime * result + resultSetHoldability; + result = prime * result + resultSetType; + + result = prime * result + autoGeneratedKeys; + + result = prime * result + Arrays.hashCode(columnIndexes); + result = prime * result + Arrays.hashCode(columnNames); + + return result; + } + + public String getSql() { + return sql; + } + + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + if (iface == PreparedStatementHolder.class) { + return true; + } + + return super.isWrapperFor(iface); + } + + @SuppressWarnings("unchecked") + public T unwrap(Class iface) throws SQLException { + if (iface == PreparedStatementHolder.class) { + return (T) holder; + } + return super.unwrap(iface); + } +} diff --git a/src/main/java/com/alibaba/druid/pool/DruidPooledStatement.java b/src/main/java/com/alibaba/druid/pool/DruidPooledStatement.java index 4085f821f7..7b13e79296 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidPooledStatement.java +++ b/src/main/java/com/alibaba/druid/pool/DruidPooledStatement.java @@ -1,641 +1,652 @@ -/* - * 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.pool; - -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; -import java.sql.SQLWarning; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.List; - -import com.alibaba.druid.support.logging.Log; -import com.alibaba.druid.support.logging.LogFactory; - -/** - * @author wenshao - */ -public class DruidPooledStatement extends PoolableWrapper implements Statement { - - private final static Log LOG = LogFactory.getLog(DruidPooledStatement.class); - - private final Statement stmt; - protected DruidPooledConnection conn; - protected final List resultSetTrace = new ArrayList(); - protected boolean closed = false; - protected int fetchRowPeak = -1; - - public DruidPooledStatement(DruidPooledConnection conn, Statement stmt){ - super(stmt); - - this.conn = conn; - this.stmt = stmt; - } - - protected void recordFetchRowCount(int fetchRowCount) { - if (fetchRowPeak < fetchRowCount) { - fetchRowPeak = fetchRowCount; - } - } - - public int getFetchRowPeak() { - return fetchRowPeak; - } - - protected SQLException checkException(Throwable error) throws SQLException { - return conn.handleException(error); - } - - public DruidPooledConnection getPoolableConnection() { - return conn; - } - - public Statement getStatement() { - return stmt; - } - - protected void checkOpen() throws SQLException { - if (closed) { - Throwable disableError = null; - if (this.conn != null) { - disableError = this.conn.getDisableError(); - } - - if (disableError != null) { - throw new SQLException("statement is closed", disableError); - } else { - throw new SQLException("statement is closed"); - } - } - } - - protected void clearResultSet() { - for (ResultSet rs : resultSetTrace) { - try { - if (!rs.isClosed()) { - rs.close(); - } - } catch (SQLException ex) { - LOG.error("clearResultSet error", ex); - } - } - resultSetTrace.clear(); - } - - public void incrementExecuteCount() { - DruidPooledConnection conn = this.getPoolableConnection(); - - if (conn == null) { - return; - } - - DruidConnectionHolder holder = conn.getConnectionHolder(); - if (holder == null) { - return; - } - - if (holder.getDataSource() == null) { - return; - } - - holder.getDataSource().incrementExecuteCount(); - } - - protected void transactionRecord(String sql) throws SQLException { - conn.transactionRecord(sql); - } - - @Override - public final ResultSet executeQuery(String sql) throws SQLException { - checkOpen(); - - incrementExecuteCount(); - transactionRecord(sql); - - conn.beforeExecute(); - try { - ResultSet rs = stmt.executeQuery(sql); - - if (rs == null) { - return rs; - } - - DruidPooledResultSet poolableResultSet = new DruidPooledResultSet(this, rs); - resultSetTrace.add(poolableResultSet); - - return poolableResultSet; - } catch (Throwable t) { - throw checkException(t); - } finally { - conn.afterExecute(); - } - } - - @Override - public final int executeUpdate(String sql) throws SQLException { - checkOpen(); - - incrementExecuteCount(); - transactionRecord(sql); - - conn.beforeExecute(); - try { - return stmt.executeUpdate(sql); - } catch (Throwable t) { - throw checkException(t); - } finally { - conn.afterExecute(); - } - } - - @Override - public final int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { - checkOpen(); - - incrementExecuteCount(); - transactionRecord(sql); - - conn.beforeExecute(); - try { - return stmt.executeUpdate(sql, autoGeneratedKeys); - } catch (Throwable t) { - throw checkException(t); - } finally { - conn.afterExecute(); - } - } - - @Override - public final int executeUpdate(String sql, int columnIndexes[]) throws SQLException { - checkOpen(); - - incrementExecuteCount(); - transactionRecord(sql); - - conn.beforeExecute(); - try { - return stmt.executeUpdate(sql, columnIndexes); - } catch (Throwable t) { - throw checkException(t); - } finally { - conn.afterExecute(); - } - } - - @Override - public final int executeUpdate(String sql, String columnNames[]) throws SQLException { - checkOpen(); - - incrementExecuteCount(); - transactionRecord(sql); - - conn.beforeExecute(); - try { - return stmt.executeUpdate(sql, columnNames); - } catch (Throwable t) { - throw checkException(t); - } finally { - conn.afterExecute(); - } - } - - @Override - public final boolean execute(String sql, int autoGeneratedKeys) throws SQLException { - checkOpen(); - - incrementExecuteCount(); - transactionRecord(sql); - - conn.beforeExecute(); - try { - return stmt.execute(sql, autoGeneratedKeys); - } catch (Throwable t) { - throw checkException(t); - } finally { - conn.afterExecute(); - } - } - - @Override - public final boolean execute(String sql, int columnIndexes[]) throws SQLException { - checkOpen(); - - incrementExecuteCount(); - transactionRecord(sql); - - conn.beforeExecute(); - try { - return stmt.execute(sql, columnIndexes); - } catch (Throwable t) { - throw checkException(t); - } finally { - conn.afterExecute(); - } - } - - @Override - public final boolean execute(String sql, String columnNames[]) throws SQLException { - checkOpen(); - - incrementExecuteCount(); - transactionRecord(sql); - - conn.beforeExecute(); - try { - return stmt.execute(sql, columnNames); - } catch (Throwable t) { - throw checkException(t); - } finally { - conn.afterExecute(); - } - } - - @Override - public int getMaxFieldSize() throws SQLException { - checkOpen(); - - try { - return stmt.getMaxFieldSize(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void close() throws SQLException { - if (!this.closed) { - clearResultSet(); - stmt.close(); - this.closed = true; - - if (conn.getConnectionHolder() != null) { - conn.getConnectionHolder().removeTrace(this); - } - } - } - - @Override - public void setMaxFieldSize(int max) throws SQLException { - checkOpen(); - - try { - stmt.setMaxFieldSize(max); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final int getMaxRows() throws SQLException { - checkOpen(); - - try { - return stmt.getMaxRows(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setMaxRows(int max) throws SQLException { - checkOpen(); - - try { - stmt.setMaxRows(max); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final void setEscapeProcessing(boolean enable) throws SQLException { - checkOpen(); - - try { - stmt.setEscapeProcessing(enable); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final int getQueryTimeout() throws SQLException { - checkOpen(); - - try { - return stmt.getQueryTimeout(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setQueryTimeout(int seconds) throws SQLException { - checkOpen(); - - try { - stmt.setQueryTimeout(seconds); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final void cancel() throws SQLException { - checkOpen(); - - try { - stmt.cancel(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final SQLWarning getWarnings() throws SQLException { - checkOpen(); - - try { - return stmt.getWarnings(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final void clearWarnings() throws SQLException { - checkOpen(); - - try { - stmt.clearWarnings(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final void setCursorName(String name) throws SQLException { - checkOpen(); - - try { - stmt.setCursorName(name); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final boolean execute(String sql) throws SQLException { - checkOpen(); - - incrementExecuteCount(); - transactionRecord(sql); - - try { - return stmt.execute(sql); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final ResultSet getResultSet() throws SQLException { - checkOpen(); - - try { - ResultSet rs = stmt.getResultSet(); - if (rs == null) { - return null; - } - - DruidPooledResultSet poolableResultSet = new DruidPooledResultSet(this, rs); - resultSetTrace.add(poolableResultSet); - - return poolableResultSet; - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final int getUpdateCount() throws SQLException { - checkOpen(); - - try { - return stmt.getUpdateCount(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final boolean getMoreResults() throws SQLException { - checkOpen(); - - try { - return stmt.getMoreResults(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setFetchDirection(int direction) throws SQLException { - checkOpen(); - - try { - stmt.setFetchDirection(direction); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final int getFetchDirection() throws SQLException { - checkOpen(); - - try { - return stmt.getFetchDirection(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public void setFetchSize(int rows) throws SQLException { - checkOpen(); - - try { - stmt.setFetchSize(rows); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final int getFetchSize() throws SQLException { - checkOpen(); - - try { - return stmt.getFetchSize(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final int getResultSetConcurrency() throws SQLException { - checkOpen(); - - try { - return stmt.getResultSetConcurrency(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final int getResultSetType() throws SQLException { - checkOpen(); - - try { - return stmt.getResultSetType(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final void addBatch(String sql) throws SQLException { - checkOpen(); - - transactionRecord(sql); - - try { - stmt.addBatch(sql); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final void clearBatch() throws SQLException { - if (closed) { - return; - } - - try { - stmt.clearBatch(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public int[] executeBatch() throws SQLException { - checkOpen(); - - incrementExecuteCount(); - - conn.beforeExecute(); - try { - return stmt.executeBatch(); - } catch (Throwable t) { - throw checkException(t); - } finally { - conn.afterExecute(); - } - } - - @Override - public final Connection getConnection() throws SQLException { - checkOpen(); - - return conn; - } - - @Override - public final boolean getMoreResults(int current) throws SQLException { - checkOpen(); - - try { - return stmt.getMoreResults(current); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final ResultSet getGeneratedKeys() throws SQLException { - checkOpen(); - - try { - ResultSet rs = stmt.getGeneratedKeys(); - - DruidPooledResultSet poolableResultSet = new DruidPooledResultSet(this, rs); - - resultSetTrace.add(poolableResultSet); - - return poolableResultSet; - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final int getResultSetHoldability() throws SQLException { - checkOpen(); - - try { - return stmt.getResultSetHoldability(); - } catch (Throwable t) { - throw checkException(t); - } - } - - @Override - public final boolean isClosed() throws SQLException { - return closed; - } - - @Override - public final void setPoolable(boolean poolable) throws SQLException { - if (poolable) { - return; - } - - throw new SQLException("not support"); - } - - @Override - public final boolean isPoolable() throws SQLException { - return false; - } - - public String toString() { - return stmt.toString(); - } - - public void closeOnCompletion() throws SQLException { - throw new SQLFeatureNotSupportedException(); - } - - public boolean isCloseOnCompletion() throws SQLException { - throw new SQLFeatureNotSupportedException(); - } -} +/* + * 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.pool; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLWarning; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + +import com.alibaba.druid.support.logging.Log; +import com.alibaba.druid.support.logging.LogFactory; + +/** + * @author wenshao + */ +public class DruidPooledStatement extends PoolableWrapper implements Statement { + + private final static Log LOG = LogFactory.getLog(DruidPooledStatement.class); + + private final Statement stmt; + protected DruidPooledConnection conn; + protected List resultSetTrace; + protected boolean closed = false; + protected int fetchRowPeak = -1; + + public DruidPooledStatement(DruidPooledConnection conn, Statement stmt){ + super(stmt); + + this.conn = conn; + this.stmt = stmt; + } + + protected void addResultSetTrace(ResultSet resultSet) { + if (resultSetTrace == null) { + resultSetTrace = new ArrayList(1); + } + resultSetTrace.add(resultSet); + } + + protected void recordFetchRowCount(int fetchRowCount) { + if (fetchRowPeak < fetchRowCount) { + fetchRowPeak = fetchRowCount; + } + } + + public int getFetchRowPeak() { + return fetchRowPeak; + } + + protected SQLException checkException(Throwable error) throws SQLException { + return conn.handleException(error); + } + + public DruidPooledConnection getPoolableConnection() { + return conn; + } + + public Statement getStatement() { + return stmt; + } + + protected void checkOpen() throws SQLException { + if (closed) { + Throwable disableError = null; + if (this.conn != null) { + disableError = this.conn.getDisableError(); + } + + if (disableError != null) { + throw new SQLException("statement is closed", disableError); + } else { + throw new SQLException("statement is closed"); + } + } + } + + protected void clearResultSet() { + if (resultSetTrace == null) { + return; + } + + for (ResultSet rs : resultSetTrace) { + try { + if (!rs.isClosed()) { + rs.close(); + } + } catch (SQLException ex) { + LOG.error("clearResultSet error", ex); + } + } + resultSetTrace.clear(); + } + + public void incrementExecuteCount() { + DruidPooledConnection conn = this.getPoolableConnection(); + + if (conn == null) { + return; + } + + DruidConnectionHolder holder = conn.getConnectionHolder(); + if (holder == null) { + return; + } + + if (holder.getDataSource() == null) { + return; + } + + holder.getDataSource().incrementExecuteCount(); + } + + protected void transactionRecord(String sql) throws SQLException { + conn.transactionRecord(sql); + } + + @Override + public final ResultSet executeQuery(String sql) throws SQLException { + checkOpen(); + + incrementExecuteCount(); + transactionRecord(sql); + + conn.beforeExecute(); + try { + ResultSet rs = stmt.executeQuery(sql); + + if (rs == null) { + return rs; + } + + DruidPooledResultSet poolableResultSet = new DruidPooledResultSet(this, rs); + addResultSetTrace(poolableResultSet); + + return poolableResultSet; + } catch (Throwable t) { + throw checkException(t); + } finally { + conn.afterExecute(); + } + } + + @Override + public final int executeUpdate(String sql) throws SQLException { + checkOpen(); + + incrementExecuteCount(); + transactionRecord(sql); + + conn.beforeExecute(); + try { + return stmt.executeUpdate(sql); + } catch (Throwable t) { + throw checkException(t); + } finally { + conn.afterExecute(); + } + } + + @Override + public final int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { + checkOpen(); + + incrementExecuteCount(); + transactionRecord(sql); + + conn.beforeExecute(); + try { + return stmt.executeUpdate(sql, autoGeneratedKeys); + } catch (Throwable t) { + throw checkException(t); + } finally { + conn.afterExecute(); + } + } + + @Override + public final int executeUpdate(String sql, int columnIndexes[]) throws SQLException { + checkOpen(); + + incrementExecuteCount(); + transactionRecord(sql); + + conn.beforeExecute(); + try { + return stmt.executeUpdate(sql, columnIndexes); + } catch (Throwable t) { + throw checkException(t); + } finally { + conn.afterExecute(); + } + } + + @Override + public final int executeUpdate(String sql, String columnNames[]) throws SQLException { + checkOpen(); + + incrementExecuteCount(); + transactionRecord(sql); + + conn.beforeExecute(); + try { + return stmt.executeUpdate(sql, columnNames); + } catch (Throwable t) { + throw checkException(t); + } finally { + conn.afterExecute(); + } + } + + @Override + public final boolean execute(String sql, int autoGeneratedKeys) throws SQLException { + checkOpen(); + + incrementExecuteCount(); + transactionRecord(sql); + + conn.beforeExecute(); + try { + return stmt.execute(sql, autoGeneratedKeys); + } catch (Throwable t) { + throw checkException(t); + } finally { + conn.afterExecute(); + } + } + + @Override + public final boolean execute(String sql, int columnIndexes[]) throws SQLException { + checkOpen(); + + incrementExecuteCount(); + transactionRecord(sql); + + conn.beforeExecute(); + try { + return stmt.execute(sql, columnIndexes); + } catch (Throwable t) { + throw checkException(t); + } finally { + conn.afterExecute(); + } + } + + @Override + public final boolean execute(String sql, String columnNames[]) throws SQLException { + checkOpen(); + + incrementExecuteCount(); + transactionRecord(sql); + + conn.beforeExecute(); + try { + return stmt.execute(sql, columnNames); + } catch (Throwable t) { + throw checkException(t); + } finally { + conn.afterExecute(); + } + } + + @Override + public int getMaxFieldSize() throws SQLException { + checkOpen(); + + try { + return stmt.getMaxFieldSize(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void close() throws SQLException { + if (!this.closed) { + clearResultSet(); + stmt.close(); + this.closed = true; + + if (conn.getConnectionHolder() != null) { + conn.getConnectionHolder().removeTrace(this); + } + } + } + + @Override + public void setMaxFieldSize(int max) throws SQLException { + checkOpen(); + + try { + stmt.setMaxFieldSize(max); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final int getMaxRows() throws SQLException { + checkOpen(); + + try { + return stmt.getMaxRows(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setMaxRows(int max) throws SQLException { + checkOpen(); + + try { + stmt.setMaxRows(max); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final void setEscapeProcessing(boolean enable) throws SQLException { + checkOpen(); + + try { + stmt.setEscapeProcessing(enable); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final int getQueryTimeout() throws SQLException { + checkOpen(); + + try { + return stmt.getQueryTimeout(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setQueryTimeout(int seconds) throws SQLException { + checkOpen(); + + try { + stmt.setQueryTimeout(seconds); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final void cancel() throws SQLException { + checkOpen(); + + try { + stmt.cancel(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final SQLWarning getWarnings() throws SQLException { + checkOpen(); + + try { + return stmt.getWarnings(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final void clearWarnings() throws SQLException { + checkOpen(); + + try { + stmt.clearWarnings(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final void setCursorName(String name) throws SQLException { + checkOpen(); + + try { + stmt.setCursorName(name); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final boolean execute(String sql) throws SQLException { + checkOpen(); + + incrementExecuteCount(); + transactionRecord(sql); + + try { + return stmt.execute(sql); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final ResultSet getResultSet() throws SQLException { + checkOpen(); + + try { + ResultSet rs = stmt.getResultSet(); + if (rs == null) { + return null; + } + + DruidPooledResultSet poolableResultSet = new DruidPooledResultSet(this, rs); + addResultSetTrace(poolableResultSet); + + return poolableResultSet; + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final int getUpdateCount() throws SQLException { + checkOpen(); + + try { + return stmt.getUpdateCount(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final boolean getMoreResults() throws SQLException { + checkOpen(); + + try { + return stmt.getMoreResults(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setFetchDirection(int direction) throws SQLException { + checkOpen(); + + try { + stmt.setFetchDirection(direction); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final int getFetchDirection() throws SQLException { + checkOpen(); + + try { + return stmt.getFetchDirection(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public void setFetchSize(int rows) throws SQLException { + checkOpen(); + + try { + stmt.setFetchSize(rows); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final int getFetchSize() throws SQLException { + checkOpen(); + + try { + return stmt.getFetchSize(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final int getResultSetConcurrency() throws SQLException { + checkOpen(); + + try { + return stmt.getResultSetConcurrency(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final int getResultSetType() throws SQLException { + checkOpen(); + + try { + return stmt.getResultSetType(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final void addBatch(String sql) throws SQLException { + checkOpen(); + + transactionRecord(sql); + + try { + stmt.addBatch(sql); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final void clearBatch() throws SQLException { + if (closed) { + return; + } + + try { + stmt.clearBatch(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public int[] executeBatch() throws SQLException { + checkOpen(); + + incrementExecuteCount(); + + conn.beforeExecute(); + try { + return stmt.executeBatch(); + } catch (Throwable t) { + throw checkException(t); + } finally { + conn.afterExecute(); + } + } + + @Override + public final Connection getConnection() throws SQLException { + checkOpen(); + + return conn; + } + + @Override + public final boolean getMoreResults(int current) throws SQLException { + checkOpen(); + + try { + return stmt.getMoreResults(current); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final ResultSet getGeneratedKeys() throws SQLException { + checkOpen(); + + try { + ResultSet rs = stmt.getGeneratedKeys(); + + DruidPooledResultSet poolableResultSet = new DruidPooledResultSet(this, rs); + + addResultSetTrace(poolableResultSet); + + return poolableResultSet; + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final int getResultSetHoldability() throws SQLException { + checkOpen(); + + try { + return stmt.getResultSetHoldability(); + } catch (Throwable t) { + throw checkException(t); + } + } + + @Override + public final boolean isClosed() throws SQLException { + return closed; + } + + @Override + public final void setPoolable(boolean poolable) throws SQLException { + if (poolable) { + return; + } + + throw new SQLException("not support"); + } + + @Override + public final boolean isPoolable() throws SQLException { + return false; + } + + public String toString() { + return stmt.toString(); + } + + public void closeOnCompletion() throws SQLException { + throw new SQLFeatureNotSupportedException(); + } + + public boolean isCloseOnCompletion() throws SQLException { + throw new SQLFeatureNotSupportedException(); + } +} diff --git a/src/main/java/com/alibaba/druid/sql/ast/statement/SQLTableSourceImpl.java b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLTableSourceImpl.java index cf700cff3d..70844c3707 100644 --- a/src/main/java/com/alibaba/druid/sql/ast/statement/SQLTableSourceImpl.java +++ b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLTableSourceImpl.java @@ -1,57 +1,67 @@ -/* - * 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.statement; - -import java.util.ArrayList; -import java.util.List; - -import com.alibaba.druid.sql.ast.SQLHint; -import com.alibaba.druid.sql.ast.SQLObjectImpl; - -public abstract class SQLTableSourceImpl extends SQLObjectImpl implements SQLTableSource { - - private static final long serialVersionUID = 1L; - - protected String alias; - - protected List hints = new ArrayList(2); - - public SQLTableSourceImpl(){ - - } - - public SQLTableSourceImpl(String alias){ - - this.alias = alias; - } - - public String getAlias() { - return this.alias; - } - - public void setAlias(String alias) { - this.alias = alias; - } - - public List getHints() { - return hints; - } - - public void setHints(List hints) { - this.hints = hints; - } - -} +/* + * 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.statement; + +import java.util.ArrayList; +import java.util.List; + +import com.alibaba.druid.sql.ast.SQLHint; +import com.alibaba.druid.sql.ast.SQLObjectImpl; + +public abstract class SQLTableSourceImpl extends SQLObjectImpl implements SQLTableSource { + + private static final long serialVersionUID = 1L; + + protected String alias; + + protected List hints; + + public SQLTableSourceImpl(){ + + } + + public SQLTableSourceImpl(String alias){ + + this.alias = alias; + } + + public String getAlias() { + return this.alias; + } + + public void setAlias(String alias) { + this.alias = alias; + } + + public int getHintsSize() { + if (hints == null) { + return 0; + } + + return hints.size(); + } + + public List getHints() { + if (hints == null) { + hints = new ArrayList(2); + } + return hints; + } + + public void setHints(List hints) { + this.hints = hints; + } +} diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlSelectQueryBlock.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlSelectQueryBlock.java index 45139561ae..74138433d5 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlSelectQueryBlock.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlSelectQueryBlock.java @@ -1,252 +1,266 @@ -/* - * 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.dialect.mysql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import com.alibaba.druid.sql.ast.SQLCommentHint; -import com.alibaba.druid.sql.ast.SQLExpr; -import com.alibaba.druid.sql.ast.SQLName; -import com.alibaba.druid.sql.ast.SQLObjectImpl; -import com.alibaba.druid.sql.ast.SQLOrderBy; -import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; -import com.alibaba.druid.sql.dialect.mysql.ast.MySqlObject; -import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import com.alibaba.druid.sql.visitor.SQLASTVisitor; - -@SuppressWarnings("serial") -public class MySqlSelectQueryBlock extends SQLSelectQueryBlock implements MySqlObject { - - private boolean hignPriority; - private boolean straightJoin; - - private boolean smallResult; - private boolean bigResult; - private boolean bufferResult; - private Boolean cache; - private boolean calcFoundRows; - - private SQLOrderBy orderBy; - - private Limit limit; - - private SQLName procedureName; - private List procedureArgumentList = new ArrayList(); - - private boolean forUpdate = false; - private boolean lockInShareMode = false; - - private List hints = new ArrayList(); - - public MySqlSelectQueryBlock(){ - - } - - public List getHints() { - return hints; - } - - public void setHints(List hints) { - this.hints = hints; - } - - public boolean isForUpdate() { - return forUpdate; - } - - public void setForUpdate(boolean forUpdate) { - this.forUpdate = forUpdate; - } - - public boolean isLockInShareMode() { - return lockInShareMode; - } - - public void setLockInShareMode(boolean lockInShareMode) { - this.lockInShareMode = lockInShareMode; - } - - public SQLName getProcedureName() { - return procedureName; - } - - public void setProcedureName(SQLName procedureName) { - this.procedureName = procedureName; - } - - public List getProcedureArgumentList() { - return procedureArgumentList; - } - - public void setProcedureArgumentList(List procedureArgumentList) { - this.procedureArgumentList = procedureArgumentList; - } - - public boolean isHignPriority() { - return hignPriority; - } - - public void setHignPriority(boolean hignPriority) { - this.hignPriority = hignPriority; - } - - public boolean isStraightJoin() { - return straightJoin; - } - - public void setStraightJoin(boolean straightJoin) { - this.straightJoin = straightJoin; - } - - public boolean isSmallResult() { - return smallResult; - } - - public void setSmallResult(boolean smallResult) { - this.smallResult = smallResult; - } - - public boolean isBigResult() { - return bigResult; - } - - public void setBigResult(boolean bigResult) { - this.bigResult = bigResult; - } - - public boolean isBufferResult() { - return bufferResult; - } - - public void setBufferResult(boolean bufferResult) { - this.bufferResult = bufferResult; - } - - public Boolean getCache() { - return cache; - } - - public void setCache(Boolean cache) { - this.cache = cache; - } - - public boolean isCalcFoundRows() { - return calcFoundRows; - } - - public void setCalcFoundRows(boolean calcFoundRows) { - this.calcFoundRows = calcFoundRows; - } - - public SQLOrderBy getOrderBy() { - return orderBy; - } - - public void setOrderBy(SQLOrderBy orderBy) { - this.orderBy = orderBy; - } - - public Limit getLimit() { - return limit; - } - - public void setLimit(Limit limit) { - if (limit != null) { - limit.setParent(this); - } - this.limit = limit; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - return; - } - - if (visitor.visit(this)) { - acceptChild(visitor, this.selectList); - acceptChild(visitor, this.from); - acceptChild(visitor, this.where); - acceptChild(visitor, this.groupBy); - acceptChild(visitor, this.orderBy); - acceptChild(visitor, this.limit); - acceptChild(visitor, this.procedureName); - acceptChild(visitor, this.procedureArgumentList); - acceptChild(visitor, this.into); - } - - visitor.endVisit(this); - } - - @Override - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.selectList); - acceptChild(visitor, this.from); - acceptChild(visitor, this.where); - acceptChild(visitor, this.groupBy); - acceptChild(visitor, this.orderBy); - acceptChild(visitor, this.limit); - acceptChild(visitor, this.procedureName); - acceptChild(visitor, this.procedureArgumentList); - acceptChild(visitor, this.into); - } - - visitor.endVisit(this); - } - - public static class Limit extends SQLObjectImpl { - - public Limit(){ - - } - - private SQLExpr rowCount; - private SQLExpr offset; - - public SQLExpr getRowCount() { - return rowCount; - } - - public void setRowCount(SQLExpr rowCount) { - this.rowCount = rowCount; - } - - public SQLExpr getOffset() { - return offset; - } - - public void setOffset(SQLExpr offset) { - this.offset = offset; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - MySqlASTVisitor mysqlVisitor = (MySqlASTVisitor) visitor; - - if (mysqlVisitor.visit(this)) { - acceptChild(visitor, offset); - acceptChild(visitor, rowCount); - } - } - } - - } - -} +/* + * 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.dialect.mysql.ast.statement; + +import java.util.ArrayList; +import java.util.List; + +import com.alibaba.druid.sql.ast.SQLCommentHint; +import com.alibaba.druid.sql.ast.SQLExpr; +import com.alibaba.druid.sql.ast.SQLName; +import com.alibaba.druid.sql.ast.SQLObjectImpl; +import com.alibaba.druid.sql.ast.SQLOrderBy; +import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; +import com.alibaba.druid.sql.dialect.mysql.ast.MySqlObject; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor; +import com.alibaba.druid.sql.visitor.SQLASTVisitor; + +@SuppressWarnings("serial") +public class MySqlSelectQueryBlock extends SQLSelectQueryBlock implements MySqlObject { + + private boolean hignPriority; + private boolean straightJoin; + + private boolean smallResult; + private boolean bigResult; + private boolean bufferResult; + private Boolean cache; + private boolean calcFoundRows; + + private SQLOrderBy orderBy; + + private Limit limit; + + private SQLName procedureName; + private List procedureArgumentList; + + private boolean forUpdate = false; + private boolean lockInShareMode = false; + + private List hints; + + public MySqlSelectQueryBlock(){ + + } + + public int getHintsSize() { + if (hints == null) { + return 0; + } + + return hints.size(); + } + + public List getHints() { + if (hints == null) { + hints = new ArrayList(2); + } + return hints; + } + + public void setHints(List hints) { + this.hints = hints; + } + + public boolean isForUpdate() { + return forUpdate; + } + + public void setForUpdate(boolean forUpdate) { + this.forUpdate = forUpdate; + } + + public boolean isLockInShareMode() { + return lockInShareMode; + } + + public void setLockInShareMode(boolean lockInShareMode) { + this.lockInShareMode = lockInShareMode; + } + + public SQLName getProcedureName() { + return procedureName; + } + + public void setProcedureName(SQLName procedureName) { + this.procedureName = procedureName; + } + + public List getProcedureArgumentList() { + if (procedureArgumentList == null) { + procedureArgumentList = new ArrayList(2); + } + return procedureArgumentList; + } + + public void setProcedureArgumentList(List procedureArgumentList) { + this.procedureArgumentList = procedureArgumentList; + } + + public boolean isHignPriority() { + return hignPriority; + } + + public void setHignPriority(boolean hignPriority) { + this.hignPriority = hignPriority; + } + + public boolean isStraightJoin() { + return straightJoin; + } + + public void setStraightJoin(boolean straightJoin) { + this.straightJoin = straightJoin; + } + + public boolean isSmallResult() { + return smallResult; + } + + public void setSmallResult(boolean smallResult) { + this.smallResult = smallResult; + } + + public boolean isBigResult() { + return bigResult; + } + + public void setBigResult(boolean bigResult) { + this.bigResult = bigResult; + } + + public boolean isBufferResult() { + return bufferResult; + } + + public void setBufferResult(boolean bufferResult) { + this.bufferResult = bufferResult; + } + + public Boolean getCache() { + return cache; + } + + public void setCache(Boolean cache) { + this.cache = cache; + } + + public boolean isCalcFoundRows() { + return calcFoundRows; + } + + public void setCalcFoundRows(boolean calcFoundRows) { + this.calcFoundRows = calcFoundRows; + } + + public SQLOrderBy getOrderBy() { + return orderBy; + } + + public void setOrderBy(SQLOrderBy orderBy) { + this.orderBy = orderBy; + } + + public Limit getLimit() { + return limit; + } + + public void setLimit(Limit limit) { + if (limit != null) { + limit.setParent(this); + } + this.limit = limit; + } + + @Override + protected void accept0(SQLASTVisitor visitor) { + if (visitor instanceof MySqlASTVisitor) { + accept0((MySqlASTVisitor) visitor); + return; + } + + if (visitor.visit(this)) { + acceptChild(visitor, this.selectList); + acceptChild(visitor, this.from); + acceptChild(visitor, this.where); + acceptChild(visitor, this.groupBy); + acceptChild(visitor, this.orderBy); + acceptChild(visitor, this.limit); + acceptChild(visitor, this.procedureName); + acceptChild(visitor, this.procedureArgumentList); + acceptChild(visitor, this.into); + } + + visitor.endVisit(this); + } + + @Override + public void accept0(MySqlASTVisitor visitor) { + if (visitor.visit(this)) { + acceptChild(visitor, this.selectList); + acceptChild(visitor, this.from); + acceptChild(visitor, this.where); + acceptChild(visitor, this.groupBy); + acceptChild(visitor, this.orderBy); + acceptChild(visitor, this.limit); + acceptChild(visitor, this.procedureName); + acceptChild(visitor, this.procedureArgumentList); + acceptChild(visitor, this.into); + } + + visitor.endVisit(this); + } + + public static class Limit extends SQLObjectImpl { + + public Limit(){ + + } + + private SQLExpr rowCount; + private SQLExpr offset; + + public SQLExpr getRowCount() { + return rowCount; + } + + public void setRowCount(SQLExpr rowCount) { + this.rowCount = rowCount; + } + + public SQLExpr getOffset() { + return offset; + } + + public void setOffset(SQLExpr offset) { + this.offset = offset; + } + + @Override + protected void accept0(SQLASTVisitor visitor) { + if (visitor instanceof MySqlASTVisitor) { + MySqlASTVisitor mysqlVisitor = (MySqlASTVisitor) visitor; + + if (mysqlVisitor.visit(this)) { + acceptChild(visitor, offset); + acceptChild(visitor, rowCount); + } + } + } + + } + +} diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java index f6651c53a9..e1efa7860e 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java @@ -1,3063 +1,3064 @@ -/* - * 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.dialect.mysql.visitor; - -import java.util.List; -import java.util.Map; - -import com.alibaba.druid.sql.ast.SQLCommentHint; -import com.alibaba.druid.sql.ast.SQLDataType; -import com.alibaba.druid.sql.ast.SQLExpr; -import com.alibaba.druid.sql.ast.SQLObject; -import com.alibaba.druid.sql.ast.SQLSetQuantifier; -import com.alibaba.druid.sql.ast.expr.SQLCharExpr; -import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr; -import com.alibaba.druid.sql.ast.expr.SQLNullExpr; -import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr; -import com.alibaba.druid.sql.ast.statement.SQLAlterTableItem; -import com.alibaba.druid.sql.ast.statement.SQLAssignItem; -import com.alibaba.druid.sql.ast.statement.SQLCharactorDataType; -import com.alibaba.druid.sql.ast.statement.SQLColumnConstraint; -import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition; -import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement; -import com.alibaba.druid.sql.ast.statement.SQLExprTableSource; -import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; -import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint; -import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForeignKey; -import com.alibaba.druid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint; -import com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey; -import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey; -import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique; -import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint; -import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBinaryExpr; -import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBooleanExpr; -import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr; -import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr; -import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr; -import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr; -import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr; -import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.CobarShowStatus; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddColumn; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddIndex; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddUnique; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableCharacter; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableDiscardTablespace; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableImportTablespace; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCommitStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateIndexStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement.TableSpaceOption; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement.UserSpecification; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDescribeStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDropTableStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDropUser; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDropViewStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlExecuteStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlHelpStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlKillStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByRange; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef.InValues; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef.LessThanValues; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlResetStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRollbackStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSQLColumnDefinition; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectGroupBy; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinLogEventsStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCharacterSetStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCollationStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowContributorsStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateDatabaseStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateEventStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateFunctionStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateProcedureStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEngineStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEnginesStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowErrorsStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEventsStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowFunctionCodeStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowFunctionStatusStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowGrantsStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterStatusStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowOpenTablesStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPluginsStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureCodeStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatusStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfileStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowRelayLogEventsStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowSlaveHostsStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowSlaveStatusStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTableStatusStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTriggersStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowVariantsStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlStartTransactionStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlTableIndex; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnionQuery; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnlockTablesStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement; -import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor; - -public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTVisitor { - - public MySqlOutputVisitor(Appendable appender){ - super(appender); - } - - public boolean visit(MySqlBooleanExpr x) { - print(x.getValue() ? "true" : "false"); - - return false; - } - - public void endVisit(MySqlBooleanExpr x) { - } - - @Override - public boolean visit(SQLSelectQueryBlock select) { - if (select instanceof MySqlSelectQueryBlock) { - return visit((MySqlSelectQueryBlock) select); - } - - return false; - } - - public boolean visit(MySqlSelectQueryBlock x) { - if (x.getOrderBy() != null) { - x.getOrderBy().setParent(x); - } - - print("SELECT "); - - for (SQLCommentHint hint : x.getHints()) { - hint.accept(this); - print(' '); - } - - if (SQLSetQuantifier.ALL == x.getDistionOption()) { - print("ALL "); - } else if (SQLSetQuantifier.DISTINCT == x.getDistionOption()) { - print("DISTINCT "); - } else if (SQLSetQuantifier.DISTINCTROW == x.getDistionOption()) { - print("DISTINCTROW "); - } - - if (x.isHignPriority()) { - print("HIGH_PRIORITY "); - } - - if (x.isStraightJoin()) { - print("STRAIGHT_JOIN "); - } - - if (x.isSmallResult()) { - print("SQL_SMALL_RESULT "); - } - - if (x.isBigResult()) { - print("SQL_BIG_RESULT "); - } - - if (x.isBufferResult()) { - print("SQL_BUFFER_RESULT "); - } - - if (x.getCache() != null) { - if (x.getCache().booleanValue()) { - print("SQL_CACHE "); - } else { - print("SQL_NO_CACHE "); - } - } - - if (x.isCalcFoundRows()) { - print("SQL_CALC_FOUND_ROWS "); - } - - printSelectList(x.getSelectList()); - - if (x.getInto() != null) { - println(); - print("INTO "); - x.getInto().accept(this); - } - - if (x.getFrom() != null) { - println(); - print("FROM "); - x.getFrom().accept(this); - } - - if (x.getWhere() != null) { - println(); - print("WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - if (x.getGroupBy() != null) { - println(); - x.getGroupBy().accept(this); - } - - if (x.getOrderBy() != null) { - println(); - x.getOrderBy().accept(this); - } - - if (x.getLimit() != null) { - println(); - x.getLimit().accept(this); - } - - if (x.getProcedureName() != null) { - print(" PROCEDURE "); - x.getProcedureName().accept(this); - if (x.getProcedureArgumentList().size() > 0) { - print("("); - printAndAccept(x.getProcedureArgumentList(), ", "); - print(")"); - } - } - - if (x.isForUpdate()) { - println(); - print("FOR UPDATE"); - } - - if (x.isLockInShareMode()) { - println(); - print("LOCK IN SHARE MODE"); - } - - return false; - } - - public boolean visit(SQLColumnDefinition x) { - MySqlSQLColumnDefinition mysqlColumn = null; - - if (x instanceof MySqlSQLColumnDefinition) { - mysqlColumn = (MySqlSQLColumnDefinition) x; - } - - x.getName().accept(this); - print(' '); - x.getDataType().accept(this); - - if (x.getDefaultExpr() != null) { - if (x.getDefaultExpr() instanceof SQLNullExpr) { - print(" NULL"); - } else { - print(" DEFAULT"); - x.getDefaultExpr().accept(this); - } - } - - if (mysqlColumn != null && mysqlColumn.getStorage() != null) { - print(" STORAGE "); - mysqlColumn.getStorage().accept(this); - } - - if (mysqlColumn != null && mysqlColumn.getOnUpdate() != null) { - print(" ON UPDATE "); - - mysqlColumn.getOnUpdate().accept(this); - } - - if (mysqlColumn != null && mysqlColumn.isAutoIncrement()) { - print(" AUTO_INCREMENT"); - } - - for (SQLColumnConstraint item : x.getConstaints()) { - print(' '); - item.accept(this); - } - - return false; - } - - public boolean visit(MySqlSelectQueryBlock.Limit x) { - print("LIMIT "); - if (x.getOffset() != null) { - x.getOffset().accept(this); - print(", "); - } - x.getRowCount().accept(this); - - return false; - } - - public boolean visit(SQLDataType x) { - print(x.getName()); - if (x.getArguments().size() > 0) { - print("("); - printAndAccept(x.getArguments(), ", "); - print(")"); - } - - if (Boolean.TRUE == x.getAttribute("unsigned")) { - print(" unsigned"); - } - - if (x instanceof SQLCharactorDataType) { - SQLCharactorDataType charType = (SQLCharactorDataType) x; - if (charType.getCharSetName() != null) { - print(" CHARACTER SET "); - print(charType.getCharSetName()); - - if (charType.getCollate() != null) { - print(" COLLATE "); - print(charType.getCollate()); - } - } - } - return false; - } - - public boolean visit(SQLCharactorDataType x) { - print(x.getName()); - if (x.getArguments().size() > 0) { - print("("); - printAndAccept(x.getArguments(), ", "); - print(")"); - } - - if (x.getCharSetName() != null) { - print(" CHARACTER SET "); - print(x.getCharSetName()); - - if (x.getCollate() != null) { - print(" COLLATE "); - print(x.getCollate()); - } - } - return false; - } - - @Override - public void endVisit(Limit x) { - - } - - @Override - public void endVisit(MySqlTableIndex x) { - - } - - @Override - public boolean visit(MySqlTableIndex x) { - print("INDEX"); - if (x.getName() != null) { - print(" "); - x.getName().accept(this); - } - - if (x.getIndexType() != null) { - print(" USING "); - print(x.getIndexType()); - } - - print("("); - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - return false; - } - - public boolean visit(MySqlCreateTableStatement x) { - - print("CREATE "); - - for (SQLCommentHint hint : x.getHints()) { - hint.accept(this); - print(' '); - } - - if (SQLCreateTableStatement.Type.GLOBAL_TEMPORARY.equals(x.getType())) { - print("TEMPORARY TABLE "); - } else { - print("TABLE "); - } - - if (x.isIfNotExiists()) { - print("IF NOT EXISTS "); - } - - x.getName().accept(this); - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - int size = x.getTableElementList().size(); - if (size > 0) { - print(" ("); - incrementIndent(); - println(); - for (int i = 0; i < size; ++i) { - if (i != 0) { - print(", "); - println(); - } - x.getTableElementList().get(i).accept(this); - } - decrementIndent(); - println(); - print(")"); - } - - for (Map.Entry option : x.getTableOptions().entrySet()) { - String key = option.getKey(); - - print(' '); - print(key); - - if ("TABLESPACE".equals(key)) { - print(' '); - option.getValue().accept(this); - continue; - } - - print(" = "); - - option.getValue().accept(this); - } - - if (x.getPartitioning() != null) { - print(' '); - x.getPartitioning().accept(this); - } - - if (x.getQuery() != null) { - incrementIndent(); - println(); - x.getQuery().accept(this); - decrementIndent(); - } - - return false; - } - - @Override - public void endVisit(MySqlKey x) { - - } - - @Override - public void endVisit(MySqlPrimaryKey x) { - - } - - @Override - public boolean visit(MySqlKey x) { - if (x.getName() != null) { - print("CONSTRAINT "); - x.getName().accept(this); - print(' '); - } - - print("KEY"); - - if (x.getIndexType() != null) { - print(" USING "); - print(x.getIndexType()); - } - - print(" ("); - - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - - return false; - } - - @Override - public boolean visit(MySqlPrimaryKey x) { - if (x.getName() != null) { - print("CONSTRAINT "); - x.getName().accept(this); - print(' '); - } - - print("PRIAMRY KEY"); - - if (x.getIndexType() != null) { - print(" USING "); - print(x.getIndexType()); - } - - print(" ("); - - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - - return false; - } - - public boolean visit(SQLCharExpr x) { - print('\''); - - String text = x.getText(); - text = text.replaceAll("'", "''"); - text = text.replaceAll("\\\\", "\\\\"); - - print(text); - - print('\''); - return false; - } - - public boolean visit(SQLVariantRefExpr x) { - { - List parameters = this.getParameters(); - int index = x.getIndex(); - - if (parameters != null && index >= 0 && index < parameters.size()) { - Object param = parameters.get(index); - printParameter(param); - return false; - } - } - - String varName = x.getName(); - if (x.isGlobal()) { - print("@@global."); - } else { - if ((!varName.startsWith("@")) // / - && (!varName.equals("?")) // - && (!varName.startsWith("#")) // - && (!varName.startsWith("$")) // - && (!varName.startsWith(":")) - ) { - print("@@"); - } - } - - for (int i = 0; i < x.getName().length(); ++i) { - char ch = x.getName().charAt(i); - if (ch == '\'') { - if (x.getName().startsWith("@@") && i == 2) { - print(ch); - } else if (x.getName().startsWith("@") && i == 1) { - print(ch); - } else if (i != 0 && i != x.getName().length() - 1) { - print("\\'"); - } else { - print(ch); - } - } else { - print(ch); - } - } - - String collate = (String) x.getAttribute("COLLATE"); - if (collate != null) { - print(" COLLATE "); - print(collate); - } - - return false; - } - - public boolean visit(SQLMethodInvokeExpr x) { - if ("SUBSTRING".equalsIgnoreCase(x.getMethodName())) { - if (x.getOwner() != null) { - x.getOwner().accept(this); - print("."); - } - print(x.getMethodName()); - print("("); - printAndAccept(x.getParameters(), ", "); - SQLExpr from = (SQLExpr) x.getAttribute("FROM"); - if (from != null) { - print(" FROM "); - from.accept(this); - } - - SQLExpr forExpr = (SQLExpr) x.getAttribute("FOR"); - if (forExpr != null) { - print(" FOR "); - forExpr.accept(this); - } - print(")"); - - return false; - } - - if ("TRIM".equalsIgnoreCase(x.getMethodName())) { - if (x.getOwner() != null) { - x.getOwner().accept(this); - print("."); - } - print(x.getMethodName()); - print("("); - - String trimType = (String) x.getAttribute("TRIM_TYPE"); - if (trimType != null) { - print(trimType); - print(' '); - } - - printAndAccept(x.getParameters(), ", "); - - SQLExpr from = (SQLExpr) x.getAttribute("FROM"); - if (from != null) { - print(" FROM "); - from.accept(this); - } - - print(")"); - - return false; - } - - if ("CONVERT".equalsIgnoreCase(x.getMethodName())) { - if (x.getOwner() != null) { - x.getOwner().accept(this); - print("."); - } - print(x.getMethodName()); - print("("); - printAndAccept(x.getParameters(), ", "); - - String charset = (String) x.getAttribute("USING"); - if (charset != null) { - print(" USING "); - print(charset); - } - print(")"); - return false; - } - - return super.visit(x); - } - - @Override - public void endVisit(MySqlIntervalExpr x) { - - } - - @Override - public boolean visit(MySqlIntervalExpr x) { - print("INTERVAL "); - x.getValue().accept(this); - print(' '); - print(x.getUnit().name()); - return false; - } - - @Override - public boolean visit(MySqlExtractExpr x) { - print("EXTRACT("); - print(x.getUnit().name()); - print(" FROM "); - x.getValue().accept(this); - print(')'); - return false; - } - - @Override - public void endVisit(MySqlExtractExpr x) { - - } - - @Override - public void endVisit(MySqlMatchAgainstExpr x) { - - } - - @Override - public boolean visit(MySqlMatchAgainstExpr x) { - print("MATCH ("); - printAndAccept(x.getColumns(), ", "); - print(")"); - - print(" AGAINST ("); - x.getAgainst().accept(this); - if (x.getSearchModifier() != null) { - print(' '); - print(x.getSearchModifier().name); - } - print(')'); - - return false; - } - - @Override - public void endVisit(MySqlBinaryExpr x) { - - } - - @Override - public boolean visit(MySqlBinaryExpr x) { - print("b'"); - print(x.getValue()); - print('\''); - - return false; - } - - @Override - public void endVisit(MySqlPrepareStatement x) { - } - - @Override - public boolean visit(MySqlPrepareStatement x) { - print("PREPARE "); - x.getName().accept(this); - print(" FROM "); - x.getFrom().accept(this); - return false; - } - - @Override - public void endVisit(MySqlExecuteStatement x) { - - } - - @Override - public boolean visit(MySqlExecuteStatement x) { - print("EXECUTE "); - x.getStatementName().accept(this); - if (x.getParameters().size() > 0) { - print(" USING "); - printAndAccept(x.getParameters(), ", "); - } - return false; - } - - @Override - public void endVisit(MySqlDeleteStatement x) { - - } - - @Override - public boolean visit(MySqlDeleteStatement x) { - print("DELETE "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isQuick()) { - print("QUICK "); - } - - if (x.isIgnore()) { - print("IGNORE "); - } - - if (x.getFrom() == null) { - print("FROM "); - x.getTableSource().accept(this); - } else { - x.getTableSource().accept(this); - println(); - print("FROM "); - x.getFrom().accept(this); - } - - if (x.getUsing() != null) { - println(); - print("USING "); - x.getUsing().accept(this); - } - - if (x.getWhere() != null) { - println(); - incrementIndent(); - print("WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - decrementIndent(); - } - - if (x.getOrderBy() != null) { - println(); - x.getOrderBy().accept(this); - } - - if (x.getLimit() != null) { - println(); - x.getLimit().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlInsertStatement x) { - - } - - @Override - public boolean visit(MySqlInsertStatement x) { - print("INSERT "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isDelayed()) { - print("DELAYED "); - } - - if (x.isHighPriority()) { - print("HIGH_PRIORITY "); - } - - if (x.isIgnore()) { - print("IGNORE "); - } - - print("INTO "); - - x.getTableSource().accept(this); - - if (x.getColumns().size() > 0) { - incrementIndent(); - print(" ("); - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - if (i % 5 == 0) { - println(); - } - print(", "); - } - - x.getColumns().get(i).accept(this); - } - print(")"); - decrementIndent(); - } - - if (x.getValuesList().size() != 0) { - println(); - print("VALUES "); - if (x.getValuesList().size() > 1) { - incrementIndent(); - } - for (int i = 0, size = x.getValuesList().size(); i < size; ++i) { - if (i != 0) { - print(","); - println(); - } - x.getValuesList().get(i).accept(this); - } - if (x.getValuesList().size() > 1) { - decrementIndent(); - } - } - - if (x.getQuery() != null) { - println(); - x.getQuery().accept(this); - } - - if (x.getDuplicateKeyUpdate().size() != 0) { - println(); - print("ON DUPLICATE KEY UPDATE "); - for (int i = 0, size = x.getDuplicateKeyUpdate().size(); i < size; ++i) { - if (i != 0) { - if (i % 5 == 0) { - println(); - } - print(", "); - } - x.getDuplicateKeyUpdate().get(i).accept(this); - } - } - - return false; - } - - @Override - public void endVisit(MySqlLoadDataInFileStatement x) { - - } - - @Override - public boolean visit(MySqlLoadDataInFileStatement x) { - print("LOAD DATA "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isConcurrent()) { - print("CONCURRENT "); - } - - if (x.isLocal()) { - print("LOCAL "); - } - - print("INFILE "); - - x.getFileName().accept(this); - - if (x.isReplicate()) { - print(" REPLACE "); - } - - if (x.isIgnore()) { - print(" IGNORE "); - } - - print(" INTO TABLE "); - x.getTableName().accept(this); - - if (x.getColumnsTerminatedBy() != null || x.getColumnsEnclosedBy() != null || x.getColumnsEscaped() != null) { - print(" COLUMNS"); - if (x.getColumnsTerminatedBy() != null) { - print(" TERMINATED BY "); - x.getColumnsTerminatedBy().accept(this); - } - - if (x.getColumnsEnclosedBy() != null) { - if (x.isColumnsEnclosedOptionally()) { - print(" OPTIONALLY"); - } - print(" ENCLOSED BY "); - x.getColumnsEnclosedBy().accept(this); - } - - if (x.getColumnsEscaped() != null) { - print(" ESCAPED BY "); - x.getColumnsEscaped().accept(this); - } - } - - if (x.getLinesStartingBy() != null || x.getLinesTerminatedBy() != null) { - print(" LINES"); - if (x.getLinesStartingBy() != null) { - print(" STARTING BY "); - x.getLinesStartingBy().accept(this); - } - - if (x.getLinesTerminatedBy() != null) { - print(" TERMINATED BY "); - x.getLinesTerminatedBy().accept(this); - } - } - - if (x.getSetList().size() != 0) { - print(" SET "); - printAndAccept(x.getSetList(), ", "); - } - - return false; - } - - @Override - public void endVisit(MySqlReplaceStatement x) { - - } - - @Override - public boolean visit(MySqlReplaceStatement x) { - print("REPLACE "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isDelayed()) { - print("DELAYED "); - } - - print("INTO "); - - x.getTableName().accept(this); - - if (x.getColumns().size() > 0) { - print(" ("); - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - } - - if (x.getValuesList().size() != 0) { - println(); - print("VALUES "); - int size = x.getValuesList().size(); - if (size == 0) { - print("()"); - } else { - for (int i = 0; i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getValuesList().get(i).accept(this); - } - } - } - - if (x.getQuery() != null) { - x.getQuery().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlSelectGroupBy x) { - - } - - @Override - public boolean visit(MySqlSelectGroupBy x) { - super.visit(x); - - if (x.isRollUp()) { - print(" WITH ROLLUP"); - } - - return false; - } - - @Override - public void endVisit(MySqlStartTransactionStatement x) { - - } - - @Override - public boolean visit(MySqlStartTransactionStatement x) { - print("START TRANSACTION"); - if (x.isConsistentSnapshot()) { - print(" WITH CONSISTENT SNAPSHOT"); - } - - if (x.isBegin()) { - print(" BEGIN"); - } - - if (x.isWork()) { - print(" WORK"); - } - - return false; - } - - @Override - public void endVisit(MySqlCommitStatement x) { - - } - - @Override - public boolean visit(MySqlCommitStatement x) { - print("COMMIT"); - - if (x.isWork()) { - print(" WORK"); - } - - if (x.getChain() != null) { - if (x.getChain().booleanValue()) { - print(" AND CHAIN"); - } else { - print(" AND NO CHAIN"); - } - } - - if (x.getRelease() != null) { - if (x.getRelease().booleanValue()) { - print(" AND RELEASE"); - } else { - print(" AND NO RELEASE"); - } - } - - return false; - } - - @Override - public void endVisit(MySqlRollbackStatement x) { - - } - - @Override - public boolean visit(MySqlRollbackStatement x) { - print("ROLLBACK"); - - if (x.getChain() != null) { - if (x.getChain().booleanValue()) { - print(" AND CHAIN"); - } else { - print(" AND NO CHAIN"); - } - } - - if (x.getRelease() != null) { - if (x.getRelease().booleanValue()) { - print(" AND RELEASE"); - } else { - print(" AND NO RELEASE"); - } - } - - if (x.getTo() != null) { - print(" TO "); - x.getTo().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowColumnsStatement x) { - - } - - @Override - public boolean visit(MySqlShowColumnsStatement x) { - if (x.isFull()) { - print("SHOW FULL COLUMNS"); - } else { - print("SHOW COLUMNS"); - } - - if (x.getTable() != null) { - print(" FROM "); - if (x.getDatabase() != null) { - x.getDatabase().accept(this); - print('.'); - } - x.getTable().accept(this); - } - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowTablesStatement x) { - - } - - @Override - public boolean visit(MySqlShowTablesStatement x) { - if (x.isFull()) { - print("SHOW FULL TABLES"); - } else { - print("SHOW TABLES"); - } - - if (x.getDatabase() != null) { - print(" FROM "); - x.getDatabase().accept(this); - } - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowDatabasesStatement x) { - - } - - @Override - public boolean visit(MySqlShowDatabasesStatement x) { - print("SHOW DATABASES"); - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowWarningsStatement x) { - - } - - @Override - public boolean visit(MySqlShowWarningsStatement x) { - if (x.isCount()) { - print("SHOW COUNT(*) WARNINGS"); - } else { - print("SHOW WARNINGS"); - if (x.getLimit() != null) { - print(' '); - x.getLimit().accept(this); - } - } - - return false; - } - - @Override - public void endVisit(MySqlShowStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowStatusStatement x) { - print("SHOW "); - - if (x.isGlobal()) { - print("GLOBAL "); - } - - if (x.isSession()) { - print("SESSION "); - } - - print("STATUS"); - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlLoadXmlStatement x) { - - } - - @Override - public boolean visit(MySqlLoadXmlStatement x) { - print("LOAD XML "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isConcurrent()) { - print("CONCURRENT "); - } - - if (x.isLocal()) { - print("LOCAL "); - } - - print("INFILE "); - - x.getFileName().accept(this); - - if (x.isReplicate()) { - print(" REPLACE "); - } - - if (x.isIgnore()) { - print(" IGNORE "); - } - - print(" INTO TABLE "); - x.getTableName().accept(this); - - if (x.getCharset() != null) { - print(" CHARSET "); - print(x.getCharset()); - } - - if (x.getRowsIdentifiedBy() != null) { - print(" ROWS IDENTIFIED BY "); - x.getRowsIdentifiedBy().accept(this); - } - - if (x.getSetList().size() != 0) { - print(" SET "); - printAndAccept(x.getSetList(), ", "); - } - - return false; - } - - @Override - public void endVisit(CobarShowStatus x) { - - } - - @Override - public boolean visit(CobarShowStatus x) { - print("SHOW COBAR_STATUS"); - return false; - } - - @Override - public void endVisit(MySqlKillStatement x) { - - } - - @Override - public boolean visit(MySqlKillStatement x) { - if (MySqlKillStatement.Type.CONNECTION.equals(x.getType())) { - print("KILL CONNECTION "); - } else if (MySqlKillStatement.Type.QUERY.equals(x.getType())) { - print("KILL QUERY "); - } - x.getThreadId().accept(this); - return false; - } - - @Override - public void endVisit(MySqlBinlogStatement x) { - - } - - @Override - public boolean visit(MySqlBinlogStatement x) { - print("BINLOG "); - x.getExpr().accept(this); - return false; - } - - @Override - public void endVisit(MySqlResetStatement x) { - - } - - @Override - public boolean visit(MySqlResetStatement x) { - print("RESET "); - for (int i = 0; i < x.getOptions().size(); ++i) { - if (i != 0) { - print(", "); - } - print(x.getOptions().get(i)); - } - return false; - } - - @Override - public void endVisit(MySqlCreateUserStatement x) { - - } - - @Override - public boolean visit(MySqlCreateUserStatement x) { - print("CREATE USER "); - printAndAccept(x.getUsers(), ", "); - return false; - } - - @Override - public void endVisit(UserSpecification x) { - - } - - @Override - public boolean visit(UserSpecification x) { - x.getUser().accept(this); - - if (x.getPassword() != null) { - print(" IDENTIFIED BY "); - x.getPassword().accept(this); - } - - if (x.getAuthPlugin() != null) { - print(" IDENTIFIED WITH "); - x.getAuthPlugin().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlDropUser x) { - - } - - @Override - public boolean visit(MySqlDropUser x) { - print("DROP USER "); - printAndAccept(x.getUsers(), ", "); - return false; - } - - @Override - public void endVisit(MySqlDropTableStatement x) { - - } - - @Override - public boolean visit(MySqlDropTableStatement x) { - if (x.isTemporary()) { - print("DROP TEMPORARY TABLE "); - } else { - print("DROP TABLE "); - } - if (x.isIfExists()) { - print("IF EXISTS "); - } - - printAndAccept(x.getTableSources(), ", "); - - if (x.getOption() != null) { - print(' '); - print(x.getOption()); - } - return false; - } - - @Override - public void endVisit(MySqlPartitionByKey x) { - - } - - @Override - public boolean visit(MySqlPartitionByKey x) { - if (x.isLinear()) { - print("PARTITION BY LINEAR KEY ("); - } else { - print("PARTITION BY KEY ("); - } - printAndAccept(x.getColumns(), ", "); - print(")"); - - if (x.getPartitionCount() != null) { - print(" PARTITIONS "); - x.getPartitionCount().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlPartitionByRange x) { - - } - - @Override - public boolean visit(MySqlPartitionByRange x) { - print("PARTITION BY RANGE"); - if (x.getExpr() != null) { - print(" ("); - x.getExpr().accept(this); - print(")"); - } else { - print(" COLUMNS ("); - printAndAccept(x.getColumns(), ", "); - print(")"); - } - - if (x.getPartitionCount() != null) { - print(" PARTITIONS "); - x.getPartitionCount().accept(this); - } - - List partitions = x.getPartitions(); - int partitionsSize = partitions.size(); - if (partitionsSize > 0) { - print("("); - incrementIndent(); - for (int i = 0; i < partitionsSize; ++i) { - println(); - partitions.get(i).accept(this); - if (i != partitionsSize - 1) { - print(", "); - } - } - decrementIndent(); - println(); - print(")"); - } - return false; - } - - @Override - public void endVisit(MySqlPartitionByList x) { - - } - - @Override - public boolean visit(MySqlPartitionByList x) { - print("PARTITION BY LIST "); - if (x.getExpr() != null) { - print("("); - x.getExpr().accept(this); - print(") "); - } else { - print("COLUMNS ("); - printAndAccept(x.getColumns(), ", "); - print(") "); - } - - if (x.getPartitionCount() != null) { - print(" PARTITIONS "); - x.getPartitionCount().accept(this); - } - - List partitions = x.getPartitions(); - int partitionsSize = partitions.size(); - if (partitionsSize > 0) { - print("("); - incrementIndent(); - for (int i = 0; i < partitionsSize; ++i) { - println(); - partitions.get(i).accept(this); - if (i != partitionsSize - 1) { - print(", "); - } - } - decrementIndent(); - println(); - print(")"); - } - return false; - } - - // - - @Override - public void endVisit(MySqlPartitionByHash x) { - - } - - @Override - public boolean visit(MySqlPartitionByHash x) { - if (x.isLinear()) { - print("PARTITION BY LINEAR HASH ("); - } else { - print("PARTITION BY HASH ("); - } - // - x.getExpr().accept(this); - print(")"); - - if (x.getPartitionCount() != null) { - print(" PARTITIONS "); - x.getPartitionCount().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlSelectQueryBlock x) { - - } - - @Override - public boolean visit(MySqlOutFileExpr x) { - print("OUTFILE "); - x.getFile().accept(this); - - if (x.getCharset() != null) { - print(" CHARACTER SET "); - print(x.getCharset()); - } - - if (x.getColumnsTerminatedBy() != null || x.getColumnsEnclosedBy() != null || x.getColumnsEscaped() != null) { - print(" COLUMNS"); - if (x.getColumnsTerminatedBy() != null) { - print(" TERMINATED BY "); - x.getColumnsTerminatedBy().accept(this); - } - - if (x.getColumnsEnclosedBy() != null) { - if (x.isColumnsEnclosedOptionally()) { - print(" OPTIONALLY"); - } - print(" ENCLOSED BY "); - x.getColumnsEnclosedBy().accept(this); - } - - if (x.getColumnsEscaped() != null) { - print(" ESCAPED BY "); - x.getColumnsEscaped().accept(this); - } - } - - if (x.getLinesStartingBy() != null || x.getLinesTerminatedBy() != null) { - print(" LINES"); - if (x.getLinesStartingBy() != null) { - print(" STARTING BY "); - x.getLinesStartingBy().accept(this); - } - - if (x.getLinesTerminatedBy() != null) { - print(" TERMINATED BY "); - x.getLinesTerminatedBy().accept(this); - } - } - - return false; - } - - @Override - public void endVisit(MySqlOutFileExpr x) { - - } - - @Override - public boolean visit(MySqlDescribeStatement x) { - print("DESC "); - x.getObject().accept(this); - return false; - } - - @Override - public void endVisit(MySqlDescribeStatement x) { - - } - - @Override - public boolean visit(MySqlUpdateStatement x) { - print("UPDATE "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isIgnore()) { - print("IGNORE "); - } - - x.getTableSource().accept(this); - - println(); - print("SET "); - for (int i = 0, size = x.getItems().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getItems().get(i).accept(this); - } - - if (x.getWhere() != null) { - println(); - incrementIndent(); - print("WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - decrementIndent(); - } - - if (x.getOrderBy() != null) { - println(); - x.getOrderBy().accept(this); - } - - if (x.getLimit() != null) { - println(); - x.getLimit().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlUpdateStatement x) { - - } - - @Override - public boolean visit(MySqlSetTransactionIsolationLevelStatement x) { - return false; - } - - @Override - public void endVisit(MySqlSetTransactionIsolationLevelStatement x) { - if (x.getGlobal() == null) { - print("SET TRANSACTION ISOLATION LEVEL "); - } else if (x.getGlobal().booleanValue()) { - print("SET GLOBAL TRANSACTION ISOLATION LEVEL "); - } else { - print("SET SESSION TRANSACTION ISOLATION LEVEL "); - } - print(x.getLevel()); - } - - @Override - public boolean visit(MySqlSetNamesStatement x) { - print("SET NAMES "); - if (x.isDefault()) { - print("DEFAULT"); - } else { - print(x.getCharSet()); - if (x.getCollate() != null) { - print(" COLLATE "); - print(x.getCollate()); - } - } - return false; - } - - @Override - public void endVisit(MySqlSetNamesStatement x) { - - } - - @Override - public boolean visit(MySqlSetCharSetStatement x) { - print("SET CHARACTER SET "); - if (x.isDefault()) { - print("DEFAULT"); - } else { - print(x.getCharSet()); - if (x.getCollate() != null) { - print(" COLLATE "); - print(x.getCollate()); - } - } - return false; - } - - @Override - public void endVisit(MySqlSetCharSetStatement x) { - - } - - @Override - public void endVisit(MySqlShowAuthorsStatement x) { - - } - - @Override - public boolean visit(MySqlShowAuthorsStatement x) { - print("SHOW AUTHORS"); - return false; - } - - @Override - public void endVisit(MySqlShowBinaryLogsStatement x) { - - } - - @Override - public boolean visit(MySqlShowBinaryLogsStatement x) { - print("SHOW BINARY LOGS"); - return false; - } - - @Override - public boolean visit(MySqlShowMasterLogsStatement x) { - print("SHOW MASTER LOGS"); - return false; - } - - @Override - public void endVisit(MySqlShowMasterLogsStatement x) { - - } - - @Override - public boolean visit(MySqlShowCollationStatement x) { - print("SHOW COLLATION"); - if (x.getPattern() != null) { - print(" LIKE "); - x.getPattern().accept(this); - } - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlShowCollationStatement x) { - - } - - @Override - public boolean visit(MySqlShowBinLogEventsStatement x) { - print("SHOW BINLOG EVENTS"); - if (x.getIn() != null) { - print(" IN "); - x.getIn().accept(this); - } - if (x.getFrom() != null) { - print(" FROM "); - x.getFrom().accept(this); - } - if (x.getLimit() != null) { - print(" "); - x.getLimit().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlShowBinLogEventsStatement x) { - - } - - @Override - public boolean visit(MySqlShowCharacterSetStatement x) { - print("SHOW CHARACTER SET"); - if (x.getPattern() != null) { - print(" LIKE "); - x.getPattern().accept(this); - } - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlShowCharacterSetStatement x) { - - } - - @Override - public boolean visit(MySqlShowContributorsStatement x) { - print("SHOW CONTRIBUTORS"); - return false; - } - - @Override - public void endVisit(MySqlShowContributorsStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateDatabaseStatement x) { - print("SHOW CREATE DATABASE "); - x.getDatabase().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowCreateDatabaseStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateEventStatement x) { - print("SHOW CREATE EVENT "); - x.getEventName().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowCreateEventStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateFunctionStatement x) { - print("SHOW CREATE FUNCTION "); - x.getName().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowCreateFunctionStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateProcedureStatement x) { - print("SHOW CREATE PROCEDURE "); - x.getName().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowCreateProcedureStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateTableStatement x) { - print("SHOW CREATE TABLE "); - x.getName().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowCreateTableStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateTriggerStatement x) { - print("SHOW CREATE TRIGGER "); - x.getName().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowCreateTriggerStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateViewStatement x) { - print("SHOW CREATE VIEW "); - x.getName().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowCreateViewStatement x) { - - } - - @Override - public boolean visit(MySqlShowEngineStatement x) { - print("SHOW ENGINE "); - x.getName().accept(this); - print(' '); - print(x.getOption().name()); - return false; - } - - @Override - public void endVisit(MySqlShowEngineStatement x) { - - } - - @Override - public boolean visit(MySqlShowEventsStatement x) { - print("SHOW EVENTS"); - if (x.getSchema() != null) { - print(" FROM "); - x.getSchema().accept(this); - } - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowEventsStatement x) { - - } - - @Override - public boolean visit(MySqlShowFunctionCodeStatement x) { - print("SHOW FUNCTION CODE "); - x.getName().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowFunctionCodeStatement x) { - - } - - @Override - public boolean visit(MySqlShowFunctionStatusStatement x) { - print("SHOW FUNCTION STATUS"); - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowFunctionStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowEnginesStatement x) { - if (x.isStorage()) { - print("SHOW STORAGE ENGINES"); - } else { - print("SHOW ENGINES"); - } - return false; - } - - @Override - public void endVisit(MySqlShowEnginesStatement x) { - - } - - @Override - public boolean visit(MySqlShowErrorsStatement x) { - if (x.isCount()) { - print("SHOW COUNT(*) ERRORS"); - } else { - print("SHOW ERRORS"); - if (x.getLimit() != null) { - print(' '); - x.getLimit().accept(this); - } - } - return false; - } - - @Override - public void endVisit(MySqlShowErrorsStatement x) { - - } - - @Override - public boolean visit(MySqlShowGrantsStatement x) { - print("SHOW GRANTS"); - if (x.getUser() != null) { - print(" FOR "); - x.getUser().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlShowGrantsStatement x) { - - } - - @Override - public boolean visit(MySqlUserName x) { - print(x.getUserName()); - if (x.getHost() != null) { - print('@'); - print(x.getHost()); - } - return false; - } - - @Override - public void endVisit(MySqlUserName x) { - - } - - @Override - public boolean visit(MySqlShowIndexesStatement x) { - print("SHOW INDEX"); - - if (x.getTable() != null) { - print(" FROM "); - if (x.getDatabase() != null) { - x.getDatabase().accept(this); - print('.'); - } - x.getTable().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowIndexesStatement x) { - - } - - @Override - public boolean visit(MySqlShowKeysStatement x) { - print("SHOW KEYS"); - - if (x.getTable() != null) { - print(" FROM "); - if (x.getDatabase() != null) { - x.getDatabase().accept(this); - print('.'); - } - x.getTable().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlShowKeysStatement x) { - - } - - @Override - public boolean visit(MySqlShowMasterStatusStatement x) { - print("SHOW MASTER STATUS"); - return false; - } - - @Override - public void endVisit(MySqlShowMasterStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowOpenTablesStatement x) { - print("SHOW OPEN TABLES"); - - if (x.getDatabase() != null) { - print(" FROM "); - x.getDatabase().accept(this); - } - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowOpenTablesStatement x) { - - } - - @Override - public boolean visit(MySqlShowPluginsStatement x) { - print("SHOW PLUGINS"); - return false; - } - - @Override - public void endVisit(MySqlShowPluginsStatement x) { - - } - - @Override - public boolean visit(MySqlShowPrivilegesStatement x) { - print("SHOW PRIVILEGES"); - return false; - } - - @Override - public void endVisit(MySqlShowPrivilegesStatement x) { - - } - - @Override - public boolean visit(MySqlShowProcedureCodeStatement x) { - print("SHOW PROCEDURE CODE "); - x.getName().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowProcedureCodeStatement x) { - - } - - @Override - public boolean visit(MySqlShowProcedureStatusStatement x) { - print("SHOW PROCEDURE STATUS"); - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlShowProcedureStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowProcessListStatement x) { - if (x.isFull()) { - print("SHOW FULL PROCESSLIST"); - } else { - print("SHOW PROCESSLIST"); - } - return false; - } - - @Override - public void endVisit(MySqlShowProcessListStatement x) { - - } - - @Override - public boolean visit(MySqlShowProfileStatement x) { - print("SHOW PROFILE"); - for (int i = 0; i < x.getTypes().size(); ++i) { - if (i == 0) { - print(' '); - } else { - print(", "); - } - print(x.getTypes().get(i).name); - } - - if (x.getForQuery() != null) { - print(" FOR QUERY "); - x.getForQuery().accept(this); - } - - if (x.getLimit() != null) { - print(' '); - x.getLimit().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlShowProfileStatement x) { - - } - - @Override - public boolean visit(MySqlShowProfilesStatement x) { - print("SHOW PROFILES"); - return false; - } - - @Override - public void endVisit(MySqlShowProfilesStatement x) { - - } - - @Override - public boolean visit(MySqlShowRelayLogEventsStatement x) { - print("SHOW RELAYLOG EVENTS"); - - if (x.getLogName() != null) { - print(" IN "); - x.getLogName().accept(this); - } - - if (x.getFrom() != null) { - print(" FROM "); - x.getFrom().accept(this); - } - - if (x.getLimit() != null) { - print(' '); - x.getLimit().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowRelayLogEventsStatement x) { - - } - - @Override - public boolean visit(MySqlShowSlaveHostsStatement x) { - print("SHOW SLAVE HOSTS"); - return false; - } - - @Override - public void endVisit(MySqlShowSlaveHostsStatement x) { - - } - - @Override - public boolean visit(MySqlShowSlaveStatusStatement x) { - print("SHOW SLAVE STATUS"); - return false; - } - - @Override - public void endVisit(MySqlShowSlaveStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowTableStatusStatement x) { - print("SHOW TABLE STATUS"); - if (x.getDatabase() != null) { - print(" FROM "); - x.getDatabase().accept(this); - } - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowTableStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowTriggersStatement x) { - print("SHOW TRIGGERS"); - - if (x.getDatabase() != null) { - print(" FROM "); - x.getDatabase().accept(this); - } - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowTriggersStatement x) { - - } - - @Override - public boolean visit(MySqlShowVariantsStatement x) { - print("SHOW "); - - if (x.isGlobal()) { - print("GLOBAL "); - } - - if (x.isSession()) { - print("SESSION "); - } - - print("VARIABLES"); - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowVariantsStatement x) { - - } - - @Override - public boolean visit(MySqlAlterTableStatement x) { - if (x.isIgnore()) { - print("ALTER IGNORE TABLE "); - } else { - print("ALTER TABLE "); - } - x.getName().accept(this); - incrementIndent(); - for (int i = 0; i < x.getItems().size(); ++i) { - SQLAlterTableItem item = x.getItems().get(i); - if (i != 0) { - print(','); - } - println(); - item.accept(this); - } - decrementIndent(); - return false; - } - - @Override - public void endVisit(MySqlAlterTableStatement x) { - - } - - @Override - public boolean visit(MySqlAlterTableAddColumn x) { - print("ADD COLUMN "); - printAndAccept(x.getColumns(), ", "); - if (x.getFirstColumn() != null) { - print(" FIRST "); - x.getFirstColumn().accept(this); - } else if (x.getAfterColumn() != null) { - print(" AFTER "); - x.getAfterColumn().accept(this); - } else if (x.isFirst()) { - print(" FIRST"); - } - return false; - } - - @Override - public void endVisit(MySqlAlterTableAddColumn x) { - - } - - @Override - public boolean visit(MySqlCreateIndexStatement x) { - print("CREATE "); - if (x.getType() != null) { - print(x.getType()); - print(" "); - } - - print("INDEX "); - - x.getName().accept(this); - print(" ON "); - x.getTable().accept(this); - print(" ("); - printAndAccept(x.getItems(), ", "); - print(")"); - - if (x.getUsing() != null) { - print(" USING "); - print(x.getUsing()); - } - return false; - } - - @Override - public void endVisit(MySqlCreateIndexStatement x) { - - } - - @Override - public boolean visit(MySqlRenameTableStatement.Item x) { - x.getName().accept(this); - print(" TO "); - x.getTo().accept(this); - return false; - } - - @Override - public void endVisit(MySqlRenameTableStatement.Item x) { - - } - - @Override - public boolean visit(MySqlRenameTableStatement x) { - print("RENAME TABLE "); - printAndAccept(x.getItems(), ", "); - return false; - } - - @Override - public void endVisit(MySqlRenameTableStatement x) { - - } - - @Override - public boolean visit(MySqlDropViewStatement x) { - print("DROP VIEW "); - if (x.isIfExists()) { - print("IF EXISTS "); - } - - printAndAccept(x.getTableSources(), ", "); - - if (x.getOption() != null) { - print(' '); - print(x.getOption()); - } - return false; - } - - @Override - public void endVisit(MySqlDropViewStatement x) { - - } - - @Override - public boolean visit(MySqlUnionQuery x) { - { - boolean needParen = false; - if (x.getLeft() instanceof MySqlSelectQueryBlock) { - MySqlSelectQueryBlock right = (MySqlSelectQueryBlock) x.getLeft(); - if (right.getOrderBy() != null || right.getLimit() != null) { - needParen = true; - } - } - if (needParen) { - print('('); - x.getLeft().accept(this); - print(')'); - } else { - x.getLeft().accept(this); - } - } - println(); - print(x.getOperator().name); - println(); - - boolean needParen = false; - - if (x.getOrderBy() != null || x.getLimit() != null) { - needParen = true; - } else if (x.getRight() instanceof MySqlSelectQueryBlock) { - MySqlSelectQueryBlock right = (MySqlSelectQueryBlock) x.getRight(); - if (right.getOrderBy() != null || right.getLimit() != null) { - needParen = true; - } - } - - if (needParen) { - print('('); - x.getRight().accept(this); - print(')'); - } else { - x.getRight().accept(this); - } - - if (x.getOrderBy() != null) { - println(); - x.getOrderBy().accept(this); - } - - if (x.getLimit() != null) { - println(); - x.getLimit().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlUnionQuery x) { - - } - - @Override - public boolean visit(MySqlUseIndexHint x) { - print("USE INDEX "); - if (x.getOption() != null) { - print("FOR "); - print(x.getOption().name); - print(' '); - } - print('('); - printAndAccept(x.getIndexList(), ", "); - print(')'); - return false; - } - - @Override - public void endVisit(MySqlUseIndexHint x) { - - } - - @Override - public boolean visit(MySqlIgnoreIndexHint x) { - print("IGNORE INDEX "); - if (x.getOption() != null) { - print("FOR "); - print(x.getOption().name); - print(' '); - } - print('('); - printAndAccept(x.getIndexList(), ", "); - print(')'); - return false; - } - - @Override - public void endVisit(MySqlIgnoreIndexHint x) { - - } - - public boolean visit(SQLExprTableSource x) { - x.getExpr().accept(this); - - if (x.getAlias() != null) { - print(' '); - print(x.getAlias()); - } - - for (int i = 0; i < x.getHints().size(); ++i) { - print(' '); - x.getHints().get(i).accept(this); - } - - return false; - } - - @Override - public boolean visit(MySqlLockTableStatement x) { - print("LOCK TABLES "); - x.getTableSource().accept(this); - if (x.getLockType() != null) { - print(' '); - print(x.getLockType().name); - } - return false; - } - - @Override - public void endVisit(MySqlLockTableStatement x) { - - } - - @Override - public boolean visit(MySqlUnlockTablesStatement x) { - print("UNLOCK TABLES"); - return false; - } - - @Override - public void endVisit(MySqlUnlockTablesStatement x) { - - } - - @Override - public boolean visit(MySqlForceIndexHint x) { - print("FORCE INDEX "); - if (x.getOption() != null) { - print("FOR "); - print(x.getOption().name); - print(' '); - } - print('('); - printAndAccept(x.getIndexList(), ", "); - print(')'); - return false; - } - - @Override - public void endVisit(MySqlForceIndexHint x) { - - } - - @Override - public boolean visit(MySqlAlterTableChangeColumn x) { - print("CHANGE COLUMN "); - x.getColumnName().accept(this); - print(' '); - x.getNewColumnDefinition().accept(this); - if (x.getFirstColumn() != null) { - print(" FIRST "); - x.getFirstColumn().accept(this); - } else if (x.getAfterColumn() != null) { - print(" AFTER "); - x.getAfterColumn().accept(this); - } else if (x.isFirst()) { - print(" FIRST"); - } - - return false; - } - - @Override - public void endVisit(MySqlAlterTableChangeColumn x) { - - } - - @Override - public boolean visit(MySqlAlterTableModifyColumn x) { - print("MODIFY COLUMN "); - x.getNewColumnDefinition().accept(this); - if (x.getFirstColumn() != null) { - print(" FIRST "); - x.getFirstColumn().accept(this); - } else if (x.getAfterColumn() != null) { - print(" AFTER "); - x.getAfterColumn().accept(this); - } else if (x.isFirst()) { - print(" FIRST"); - } - - return false; - } - - @Override - public void endVisit(MySqlAlterTableModifyColumn x) { - - } - - @Override - public boolean visit(MySqlAlterTableCharacter x) { - print("CHARACTER SET = "); - x.getCharacterSet().accept(this); - - if (x.getCollate() != null) { - print(", COLLATE = "); - x.getCollate().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlAlterTableCharacter x) { - - } - - @Override - public boolean visit(MySqlAlterTableAddIndex x) { - print("ADD "); - if (x.getType() != null) { - print(x.getType()); - print(" "); - } - - print("INDEX "); - - if (x.getName() != null) { - x.getName().accept(this); - print(' '); - } - print("("); - printAndAccept(x.getItems(), ", "); - print(")"); - - if (x.getUsing() != null) { - print(" USING "); - print(x.getUsing()); - } - return false; - } - - @Override - public void endVisit(MySqlAlterTableAddIndex x) { - - } - - @Override - public boolean visit(MySqlAlterTableAddUnique x) { - print("ADD "); - if (x.getType() != null) { - print(x.getType()); - print(" "); - } - - print("UNIQUE "); - - if (x.getName() != null) { - x.getName().accept(this); - print(' '); - } - print("("); - printAndAccept(x.getItems(), ", "); - print(")"); - - if (x.getUsing() != null) { - print(" USING "); - print(x.getUsing()); - } - return false; - } - - @Override - public void endVisit(MySqlAlterTableAddUnique x) { - - } - - @Override - public boolean visit(MySqlAlterTableOption x) { - print(x.getName()); - print(" = "); - print(x.getValue().toString()); - return false; - } - - @Override - public void endVisit(MySqlAlterTableOption x) { - - } - - @Override - public void endVisit(MySqlCreateTableStatement x) { - - } - - @Override - public boolean visit(MySqlHelpStatement x) { - print("HELP "); - x.getContent().accept(this); - return false; - } - - @Override - public void endVisit(MySqlHelpStatement x) { - - } - - @Override - public boolean visit(MySqlCharExpr x) { - print(x.toString()); - return false; - } - - @Override - public void endVisit(MySqlCharExpr x) { - - } - - @Override - public boolean visit(MySqlUnique x) { - if (x.getName() != null) { - print("CONSTRAINT "); - x.getName().accept(this); - print(' '); - } - - print("UNIQUE"); - - if (x.getIndexType() != null) { - print(" USING "); - print(x.getIndexType()); - } - - print(" ("); - printAndAccept(x.getColumns(), ", "); - print(")"); - - return false; - } - - @Override - public void endVisit(MySqlUnique x) { - - } - - @Override - public boolean visit(MySqlForeignKey x) { - if (x.getName() != null) { - print("CONSTRAINT "); - x.getName().accept(this); - print(' '); - } - - print("FOREIGN KEY ("); - printAndAccept(x.getReferencedColumns(), ", "); - print(")"); - - print(" REFERENCES "); - x.getReferencedTableName().accept(this); - - print(" ("); - printAndAccept(x.getReferencedColumns(), ", "); - print(")"); - return false; - } - - @Override - public void endVisit(MySqlForeignKey x) { - - } - - @Override - public boolean visit(MySqlAlterTableDiscardTablespace x) { - print("DISCARD TABLESPACE"); - return false; - } - - @Override - public void endVisit(MySqlAlterTableDiscardTablespace x) { - - } - - @Override - public boolean visit(MySqlAlterTableImportTablespace x) { - print("IMPORT TABLESPACE"); - return false; - } - - @Override - public void endVisit(MySqlAlterTableImportTablespace x) { - - } - - @Override - public boolean visit(SQLAssignItem x) { - x.getTarget().accept(this); - if (!"NAMES".equalsIgnoreCase(x.getTarget().toString())) { - print(" = "); - } - x.getValue().accept(this); - return false; - } - - @Override - public boolean visit(TableSpaceOption x) { - x.getName().accept(this); - - if (x.getStorage() != null) { - print(' '); - x.getStorage().accept(this); - } - return false; - } - - @Override - public void endVisit(TableSpaceOption x) { - - } - - @Override - public boolean visit(MySqlPartitioningDef x) { - print("PARTITION "); - x.getName().accept(this); - if (x.getValues() != null) { - print(' '); - x.getValues().accept(this); - } - - if (x.getDataDirectory() != null) { - incrementIndent(); - println(); - print("DATA DIRECTORY "); - x.getDataDirectory().accept(this); - decrementIndent(); - } - if (x.getIndexDirectory() != null) { - incrementIndent(); - println(); - print("INDEX DIRECTORY "); - x.getIndexDirectory().accept(this); - decrementIndent(); - } - - return false; - } - - @Override - public void endVisit(MySqlPartitioningDef x) { - - } - - @Override - public boolean visit(LessThanValues x) { - print("VALUES LESS THAN ("); - printAndAccept(x.getItems(), ", "); - print(')'); - return false; - } - - @Override - public void endVisit(LessThanValues x) { - - } - - @Override - public boolean visit(InValues x) { - print("VALUES IN ("); - printAndAccept(x.getItems(), ", "); - print(')'); - return false; - } - - @Override - public void endVisit(InValues x) { - - } - -} // +/* + * 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.dialect.mysql.visitor; + +import java.util.List; +import java.util.Map; + +import com.alibaba.druid.sql.ast.SQLCommentHint; +import com.alibaba.druid.sql.ast.SQLDataType; +import com.alibaba.druid.sql.ast.SQLExpr; +import com.alibaba.druid.sql.ast.SQLObject; +import com.alibaba.druid.sql.ast.SQLSetQuantifier; +import com.alibaba.druid.sql.ast.expr.SQLCharExpr; +import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr; +import com.alibaba.druid.sql.ast.expr.SQLNullExpr; +import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableItem; +import com.alibaba.druid.sql.ast.statement.SQLAssignItem; +import com.alibaba.druid.sql.ast.statement.SQLCharactorDataType; +import com.alibaba.druid.sql.ast.statement.SQLColumnConstraint; +import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition; +import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement; +import com.alibaba.druid.sql.ast.statement.SQLExprTableSource; +import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; +import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint; +import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForeignKey; +import com.alibaba.druid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint; +import com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey; +import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey; +import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique; +import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint; +import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBinaryExpr; +import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBooleanExpr; +import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr; +import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr; +import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr; +import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr; +import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr; +import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.CobarShowStatus; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddColumn; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddIndex; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddUnique; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableCharacter; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableDiscardTablespace; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableImportTablespace; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCommitStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateIndexStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement.TableSpaceOption; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement.UserSpecification; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDescribeStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDropTableStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDropUser; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDropViewStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlExecuteStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlHelpStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlKillStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByRange; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef.InValues; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef.LessThanValues; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlResetStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRollbackStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSQLColumnDefinition; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectGroupBy; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinLogEventsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCharacterSetStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCollationStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowContributorsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateDatabaseStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateEventStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateFunctionStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateProcedureStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEngineStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEnginesStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowErrorsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEventsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowFunctionCodeStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowFunctionStatusStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowGrantsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterStatusStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowOpenTablesStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPluginsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureCodeStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatusStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfileStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowRelayLogEventsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowSlaveHostsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowSlaveStatusStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTableStatusStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTriggersStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowVariantsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlStartTransactionStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlTableIndex; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnionQuery; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnlockTablesStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement; +import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor; + +public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTVisitor { + + public MySqlOutputVisitor(Appendable appender){ + super(appender); + } + + public boolean visit(MySqlBooleanExpr x) { + print(x.getValue() ? "true" : "false"); + + return false; + } + + public void endVisit(MySqlBooleanExpr x) { + } + + @Override + public boolean visit(SQLSelectQueryBlock select) { + if (select instanceof MySqlSelectQueryBlock) { + return visit((MySqlSelectQueryBlock) select); + } + + return false; + } + + public boolean visit(MySqlSelectQueryBlock x) { + if (x.getOrderBy() != null) { + x.getOrderBy().setParent(x); + } + + print("SELECT "); + + for (int i = 0, size = x.getHintsSize(); i < size; ++i) { + SQLCommentHint hint = x.getHints().get(i); + hint.accept(this); + print(' '); + } + + if (SQLSetQuantifier.ALL == x.getDistionOption()) { + print("ALL "); + } else if (SQLSetQuantifier.DISTINCT == x.getDistionOption()) { + print("DISTINCT "); + } else if (SQLSetQuantifier.DISTINCTROW == x.getDistionOption()) { + print("DISTINCTROW "); + } + + if (x.isHignPriority()) { + print("HIGH_PRIORITY "); + } + + if (x.isStraightJoin()) { + print("STRAIGHT_JOIN "); + } + + if (x.isSmallResult()) { + print("SQL_SMALL_RESULT "); + } + + if (x.isBigResult()) { + print("SQL_BIG_RESULT "); + } + + if (x.isBufferResult()) { + print("SQL_BUFFER_RESULT "); + } + + if (x.getCache() != null) { + if (x.getCache().booleanValue()) { + print("SQL_CACHE "); + } else { + print("SQL_NO_CACHE "); + } + } + + if (x.isCalcFoundRows()) { + print("SQL_CALC_FOUND_ROWS "); + } + + printSelectList(x.getSelectList()); + + if (x.getInto() != null) { + println(); + print("INTO "); + x.getInto().accept(this); + } + + if (x.getFrom() != null) { + println(); + print("FROM "); + x.getFrom().accept(this); + } + + if (x.getWhere() != null) { + println(); + print("WHERE "); + x.getWhere().setParent(x); + x.getWhere().accept(this); + } + + if (x.getGroupBy() != null) { + println(); + x.getGroupBy().accept(this); + } + + if (x.getOrderBy() != null) { + println(); + x.getOrderBy().accept(this); + } + + if (x.getLimit() != null) { + println(); + x.getLimit().accept(this); + } + + if (x.getProcedureName() != null) { + print(" PROCEDURE "); + x.getProcedureName().accept(this); + if (x.getProcedureArgumentList().size() > 0) { + print("("); + printAndAccept(x.getProcedureArgumentList(), ", "); + print(")"); + } + } + + if (x.isForUpdate()) { + println(); + print("FOR UPDATE"); + } + + if (x.isLockInShareMode()) { + println(); + print("LOCK IN SHARE MODE"); + } + + return false; + } + + public boolean visit(SQLColumnDefinition x) { + MySqlSQLColumnDefinition mysqlColumn = null; + + if (x instanceof MySqlSQLColumnDefinition) { + mysqlColumn = (MySqlSQLColumnDefinition) x; + } + + x.getName().accept(this); + print(' '); + x.getDataType().accept(this); + + if (x.getDefaultExpr() != null) { + if (x.getDefaultExpr() instanceof SQLNullExpr) { + print(" NULL"); + } else { + print(" DEFAULT"); + x.getDefaultExpr().accept(this); + } + } + + if (mysqlColumn != null && mysqlColumn.getStorage() != null) { + print(" STORAGE "); + mysqlColumn.getStorage().accept(this); + } + + if (mysqlColumn != null && mysqlColumn.getOnUpdate() != null) { + print(" ON UPDATE "); + + mysqlColumn.getOnUpdate().accept(this); + } + + if (mysqlColumn != null && mysqlColumn.isAutoIncrement()) { + print(" AUTO_INCREMENT"); + } + + for (SQLColumnConstraint item : x.getConstaints()) { + print(' '); + item.accept(this); + } + + return false; + } + + public boolean visit(MySqlSelectQueryBlock.Limit x) { + print("LIMIT "); + if (x.getOffset() != null) { + x.getOffset().accept(this); + print(", "); + } + x.getRowCount().accept(this); + + return false; + } + + public boolean visit(SQLDataType x) { + print(x.getName()); + if (x.getArguments().size() > 0) { + print("("); + printAndAccept(x.getArguments(), ", "); + print(")"); + } + + if (Boolean.TRUE == x.getAttribute("unsigned")) { + print(" unsigned"); + } + + if (x instanceof SQLCharactorDataType) { + SQLCharactorDataType charType = (SQLCharactorDataType) x; + if (charType.getCharSetName() != null) { + print(" CHARACTER SET "); + print(charType.getCharSetName()); + + if (charType.getCollate() != null) { + print(" COLLATE "); + print(charType.getCollate()); + } + } + } + return false; + } + + public boolean visit(SQLCharactorDataType x) { + print(x.getName()); + if (x.getArguments().size() > 0) { + print("("); + printAndAccept(x.getArguments(), ", "); + print(")"); + } + + if (x.getCharSetName() != null) { + print(" CHARACTER SET "); + print(x.getCharSetName()); + + if (x.getCollate() != null) { + print(" COLLATE "); + print(x.getCollate()); + } + } + return false; + } + + @Override + public void endVisit(Limit x) { + + } + + @Override + public void endVisit(MySqlTableIndex x) { + + } + + @Override + public boolean visit(MySqlTableIndex x) { + print("INDEX"); + if (x.getName() != null) { + print(" "); + x.getName().accept(this); + } + + if (x.getIndexType() != null) { + print(" USING "); + print(x.getIndexType()); + } + + print("("); + for (int i = 0, size = x.getColumns().size(); i < size; ++i) { + if (i != 0) { + print(", "); + } + x.getColumns().get(i).accept(this); + } + print(")"); + return false; + } + + public boolean visit(MySqlCreateTableStatement x) { + + print("CREATE "); + + for (SQLCommentHint hint : x.getHints()) { + hint.accept(this); + print(' '); + } + + if (SQLCreateTableStatement.Type.GLOBAL_TEMPORARY.equals(x.getType())) { + print("TEMPORARY TABLE "); + } else { + print("TABLE "); + } + + if (x.isIfNotExiists()) { + print("IF NOT EXISTS "); + } + + x.getName().accept(this); + + if (x.getLike() != null) { + print(" LIKE "); + x.getLike().accept(this); + } + + int size = x.getTableElementList().size(); + if (size > 0) { + print(" ("); + incrementIndent(); + println(); + for (int i = 0; i < size; ++i) { + if (i != 0) { + print(", "); + println(); + } + x.getTableElementList().get(i).accept(this); + } + decrementIndent(); + println(); + print(")"); + } + + for (Map.Entry option : x.getTableOptions().entrySet()) { + String key = option.getKey(); + + print(' '); + print(key); + + if ("TABLESPACE".equals(key)) { + print(' '); + option.getValue().accept(this); + continue; + } + + print(" = "); + + option.getValue().accept(this); + } + + if (x.getPartitioning() != null) { + print(' '); + x.getPartitioning().accept(this); + } + + if (x.getQuery() != null) { + incrementIndent(); + println(); + x.getQuery().accept(this); + decrementIndent(); + } + + return false; + } + + @Override + public void endVisit(MySqlKey x) { + + } + + @Override + public void endVisit(MySqlPrimaryKey x) { + + } + + @Override + public boolean visit(MySqlKey x) { + if (x.getName() != null) { + print("CONSTRAINT "); + x.getName().accept(this); + print(' '); + } + + print("KEY"); + + if (x.getIndexType() != null) { + print(" USING "); + print(x.getIndexType()); + } + + print(" ("); + + for (int i = 0, size = x.getColumns().size(); i < size; ++i) { + if (i != 0) { + print(", "); + } + x.getColumns().get(i).accept(this); + } + print(")"); + + return false; + } + + @Override + public boolean visit(MySqlPrimaryKey x) { + if (x.getName() != null) { + print("CONSTRAINT "); + x.getName().accept(this); + print(' '); + } + + print("PRIAMRY KEY"); + + if (x.getIndexType() != null) { + print(" USING "); + print(x.getIndexType()); + } + + print(" ("); + + for (int i = 0, size = x.getColumns().size(); i < size; ++i) { + if (i != 0) { + print(", "); + } + x.getColumns().get(i).accept(this); + } + print(")"); + + return false; + } + + public boolean visit(SQLCharExpr x) { + print('\''); + + String text = x.getText(); + text = text.replaceAll("'", "''"); + text = text.replaceAll("\\\\", "\\\\"); + + print(text); + + print('\''); + return false; + } + + public boolean visit(SQLVariantRefExpr x) { + { + int parametersSize = this.getParametersSize(); + int index = x.getIndex(); + + if (index >= 0 && index < parametersSize) { + Object param = this.getParameters().get(index); + printParameter(param); + return false; + } + } + + String varName = x.getName(); + if (x.isGlobal()) { + print("@@global."); + } else { + if ((!varName.startsWith("@")) // / + && (!varName.equals("?")) // + && (!varName.startsWith("#")) // + && (!varName.startsWith("$")) // + && (!varName.startsWith(":")) + ) { + print("@@"); + } + } + + for (int i = 0; i < x.getName().length(); ++i) { + char ch = x.getName().charAt(i); + if (ch == '\'') { + if (x.getName().startsWith("@@") && i == 2) { + print(ch); + } else if (x.getName().startsWith("@") && i == 1) { + print(ch); + } else if (i != 0 && i != x.getName().length() - 1) { + print("\\'"); + } else { + print(ch); + } + } else { + print(ch); + } + } + + String collate = (String) x.getAttribute("COLLATE"); + if (collate != null) { + print(" COLLATE "); + print(collate); + } + + return false; + } + + public boolean visit(SQLMethodInvokeExpr x) { + if ("SUBSTRING".equalsIgnoreCase(x.getMethodName())) { + if (x.getOwner() != null) { + x.getOwner().accept(this); + print("."); + } + print(x.getMethodName()); + print("("); + printAndAccept(x.getParameters(), ", "); + SQLExpr from = (SQLExpr) x.getAttribute("FROM"); + if (from != null) { + print(" FROM "); + from.accept(this); + } + + SQLExpr forExpr = (SQLExpr) x.getAttribute("FOR"); + if (forExpr != null) { + print(" FOR "); + forExpr.accept(this); + } + print(")"); + + return false; + } + + if ("TRIM".equalsIgnoreCase(x.getMethodName())) { + if (x.getOwner() != null) { + x.getOwner().accept(this); + print("."); + } + print(x.getMethodName()); + print("("); + + String trimType = (String) x.getAttribute("TRIM_TYPE"); + if (trimType != null) { + print(trimType); + print(' '); + } + + printAndAccept(x.getParameters(), ", "); + + SQLExpr from = (SQLExpr) x.getAttribute("FROM"); + if (from != null) { + print(" FROM "); + from.accept(this); + } + + print(")"); + + return false; + } + + if ("CONVERT".equalsIgnoreCase(x.getMethodName())) { + if (x.getOwner() != null) { + x.getOwner().accept(this); + print("."); + } + print(x.getMethodName()); + print("("); + printAndAccept(x.getParameters(), ", "); + + String charset = (String) x.getAttribute("USING"); + if (charset != null) { + print(" USING "); + print(charset); + } + print(")"); + return false; + } + + return super.visit(x); + } + + @Override + public void endVisit(MySqlIntervalExpr x) { + + } + + @Override + public boolean visit(MySqlIntervalExpr x) { + print("INTERVAL "); + x.getValue().accept(this); + print(' '); + print(x.getUnit().name()); + return false; + } + + @Override + public boolean visit(MySqlExtractExpr x) { + print("EXTRACT("); + print(x.getUnit().name()); + print(" FROM "); + x.getValue().accept(this); + print(')'); + return false; + } + + @Override + public void endVisit(MySqlExtractExpr x) { + + } + + @Override + public void endVisit(MySqlMatchAgainstExpr x) { + + } + + @Override + public boolean visit(MySqlMatchAgainstExpr x) { + print("MATCH ("); + printAndAccept(x.getColumns(), ", "); + print(")"); + + print(" AGAINST ("); + x.getAgainst().accept(this); + if (x.getSearchModifier() != null) { + print(' '); + print(x.getSearchModifier().name); + } + print(')'); + + return false; + } + + @Override + public void endVisit(MySqlBinaryExpr x) { + + } + + @Override + public boolean visit(MySqlBinaryExpr x) { + print("b'"); + print(x.getValue()); + print('\''); + + return false; + } + + @Override + public void endVisit(MySqlPrepareStatement x) { + } + + @Override + public boolean visit(MySqlPrepareStatement x) { + print("PREPARE "); + x.getName().accept(this); + print(" FROM "); + x.getFrom().accept(this); + return false; + } + + @Override + public void endVisit(MySqlExecuteStatement x) { + + } + + @Override + public boolean visit(MySqlExecuteStatement x) { + print("EXECUTE "); + x.getStatementName().accept(this); + if (x.getParameters().size() > 0) { + print(" USING "); + printAndAccept(x.getParameters(), ", "); + } + return false; + } + + @Override + public void endVisit(MySqlDeleteStatement x) { + + } + + @Override + public boolean visit(MySqlDeleteStatement x) { + print("DELETE "); + + if (x.isLowPriority()) { + print("LOW_PRIORITY "); + } + + if (x.isQuick()) { + print("QUICK "); + } + + if (x.isIgnore()) { + print("IGNORE "); + } + + if (x.getFrom() == null) { + print("FROM "); + x.getTableSource().accept(this); + } else { + x.getTableSource().accept(this); + println(); + print("FROM "); + x.getFrom().accept(this); + } + + if (x.getUsing() != null) { + println(); + print("USING "); + x.getUsing().accept(this); + } + + if (x.getWhere() != null) { + println(); + incrementIndent(); + print("WHERE "); + x.getWhere().setParent(x); + x.getWhere().accept(this); + decrementIndent(); + } + + if (x.getOrderBy() != null) { + println(); + x.getOrderBy().accept(this); + } + + if (x.getLimit() != null) { + println(); + x.getLimit().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlInsertStatement x) { + + } + + @Override + public boolean visit(MySqlInsertStatement x) { + print("INSERT "); + + if (x.isLowPriority()) { + print("LOW_PRIORITY "); + } + + if (x.isDelayed()) { + print("DELAYED "); + } + + if (x.isHighPriority()) { + print("HIGH_PRIORITY "); + } + + if (x.isIgnore()) { + print("IGNORE "); + } + + print("INTO "); + + x.getTableSource().accept(this); + + if (x.getColumns().size() > 0) { + incrementIndent(); + print(" ("); + for (int i = 0, size = x.getColumns().size(); i < size; ++i) { + if (i != 0) { + if (i % 5 == 0) { + println(); + } + print(", "); + } + + x.getColumns().get(i).accept(this); + } + print(")"); + decrementIndent(); + } + + if (x.getValuesList().size() != 0) { + println(); + print("VALUES "); + if (x.getValuesList().size() > 1) { + incrementIndent(); + } + for (int i = 0, size = x.getValuesList().size(); i < size; ++i) { + if (i != 0) { + print(","); + println(); + } + x.getValuesList().get(i).accept(this); + } + if (x.getValuesList().size() > 1) { + decrementIndent(); + } + } + + if (x.getQuery() != null) { + println(); + x.getQuery().accept(this); + } + + if (x.getDuplicateKeyUpdate().size() != 0) { + println(); + print("ON DUPLICATE KEY UPDATE "); + for (int i = 0, size = x.getDuplicateKeyUpdate().size(); i < size; ++i) { + if (i != 0) { + if (i % 5 == 0) { + println(); + } + print(", "); + } + x.getDuplicateKeyUpdate().get(i).accept(this); + } + } + + return false; + } + + @Override + public void endVisit(MySqlLoadDataInFileStatement x) { + + } + + @Override + public boolean visit(MySqlLoadDataInFileStatement x) { + print("LOAD DATA "); + + if (x.isLowPriority()) { + print("LOW_PRIORITY "); + } + + if (x.isConcurrent()) { + print("CONCURRENT "); + } + + if (x.isLocal()) { + print("LOCAL "); + } + + print("INFILE "); + + x.getFileName().accept(this); + + if (x.isReplicate()) { + print(" REPLACE "); + } + + if (x.isIgnore()) { + print(" IGNORE "); + } + + print(" INTO TABLE "); + x.getTableName().accept(this); + + if (x.getColumnsTerminatedBy() != null || x.getColumnsEnclosedBy() != null || x.getColumnsEscaped() != null) { + print(" COLUMNS"); + if (x.getColumnsTerminatedBy() != null) { + print(" TERMINATED BY "); + x.getColumnsTerminatedBy().accept(this); + } + + if (x.getColumnsEnclosedBy() != null) { + if (x.isColumnsEnclosedOptionally()) { + print(" OPTIONALLY"); + } + print(" ENCLOSED BY "); + x.getColumnsEnclosedBy().accept(this); + } + + if (x.getColumnsEscaped() != null) { + print(" ESCAPED BY "); + x.getColumnsEscaped().accept(this); + } + } + + if (x.getLinesStartingBy() != null || x.getLinesTerminatedBy() != null) { + print(" LINES"); + if (x.getLinesStartingBy() != null) { + print(" STARTING BY "); + x.getLinesStartingBy().accept(this); + } + + if (x.getLinesTerminatedBy() != null) { + print(" TERMINATED BY "); + x.getLinesTerminatedBy().accept(this); + } + } + + if (x.getSetList().size() != 0) { + print(" SET "); + printAndAccept(x.getSetList(), ", "); + } + + return false; + } + + @Override + public void endVisit(MySqlReplaceStatement x) { + + } + + @Override + public boolean visit(MySqlReplaceStatement x) { + print("REPLACE "); + + if (x.isLowPriority()) { + print("LOW_PRIORITY "); + } + + if (x.isDelayed()) { + print("DELAYED "); + } + + print("INTO "); + + x.getTableName().accept(this); + + if (x.getColumns().size() > 0) { + print(" ("); + for (int i = 0, size = x.getColumns().size(); i < size; ++i) { + if (i != 0) { + print(", "); + } + x.getColumns().get(i).accept(this); + } + print(")"); + } + + if (x.getValuesList().size() != 0) { + println(); + print("VALUES "); + int size = x.getValuesList().size(); + if (size == 0) { + print("()"); + } else { + for (int i = 0; i < size; ++i) { + if (i != 0) { + print(", "); + } + x.getValuesList().get(i).accept(this); + } + } + } + + if (x.getQuery() != null) { + x.getQuery().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlSelectGroupBy x) { + + } + + @Override + public boolean visit(MySqlSelectGroupBy x) { + super.visit(x); + + if (x.isRollUp()) { + print(" WITH ROLLUP"); + } + + return false; + } + + @Override + public void endVisit(MySqlStartTransactionStatement x) { + + } + + @Override + public boolean visit(MySqlStartTransactionStatement x) { + print("START TRANSACTION"); + if (x.isConsistentSnapshot()) { + print(" WITH CONSISTENT SNAPSHOT"); + } + + if (x.isBegin()) { + print(" BEGIN"); + } + + if (x.isWork()) { + print(" WORK"); + } + + return false; + } + + @Override + public void endVisit(MySqlCommitStatement x) { + + } + + @Override + public boolean visit(MySqlCommitStatement x) { + print("COMMIT"); + + if (x.isWork()) { + print(" WORK"); + } + + if (x.getChain() != null) { + if (x.getChain().booleanValue()) { + print(" AND CHAIN"); + } else { + print(" AND NO CHAIN"); + } + } + + if (x.getRelease() != null) { + if (x.getRelease().booleanValue()) { + print(" AND RELEASE"); + } else { + print(" AND NO RELEASE"); + } + } + + return false; + } + + @Override + public void endVisit(MySqlRollbackStatement x) { + + } + + @Override + public boolean visit(MySqlRollbackStatement x) { + print("ROLLBACK"); + + if (x.getChain() != null) { + if (x.getChain().booleanValue()) { + print(" AND CHAIN"); + } else { + print(" AND NO CHAIN"); + } + } + + if (x.getRelease() != null) { + if (x.getRelease().booleanValue()) { + print(" AND RELEASE"); + } else { + print(" AND NO RELEASE"); + } + } + + if (x.getTo() != null) { + print(" TO "); + x.getTo().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlShowColumnsStatement x) { + + } + + @Override + public boolean visit(MySqlShowColumnsStatement x) { + if (x.isFull()) { + print("SHOW FULL COLUMNS"); + } else { + print("SHOW COLUMNS"); + } + + if (x.getTable() != null) { + print(" FROM "); + if (x.getDatabase() != null) { + x.getDatabase().accept(this); + print('.'); + } + x.getTable().accept(this); + } + + if (x.getLike() != null) { + print(" LIKE "); + x.getLike().accept(this); + } + + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().setParent(x); + x.getWhere().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlShowTablesStatement x) { + + } + + @Override + public boolean visit(MySqlShowTablesStatement x) { + if (x.isFull()) { + print("SHOW FULL TABLES"); + } else { + print("SHOW TABLES"); + } + + if (x.getDatabase() != null) { + print(" FROM "); + x.getDatabase().accept(this); + } + + if (x.getLike() != null) { + print(" LIKE "); + x.getLike().accept(this); + } + + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().setParent(x); + x.getWhere().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlShowDatabasesStatement x) { + + } + + @Override + public boolean visit(MySqlShowDatabasesStatement x) { + print("SHOW DATABASES"); + + if (x.getLike() != null) { + print(" LIKE "); + x.getLike().accept(this); + } + + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().setParent(x); + x.getWhere().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlShowWarningsStatement x) { + + } + + @Override + public boolean visit(MySqlShowWarningsStatement x) { + if (x.isCount()) { + print("SHOW COUNT(*) WARNINGS"); + } else { + print("SHOW WARNINGS"); + if (x.getLimit() != null) { + print(' '); + x.getLimit().accept(this); + } + } + + return false; + } + + @Override + public void endVisit(MySqlShowStatusStatement x) { + + } + + @Override + public boolean visit(MySqlShowStatusStatement x) { + print("SHOW "); + + if (x.isGlobal()) { + print("GLOBAL "); + } + + if (x.isSession()) { + print("SESSION "); + } + + print("STATUS"); + + if (x.getLike() != null) { + print(" LIKE "); + x.getLike().accept(this); + } + + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().setParent(x); + x.getWhere().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlLoadXmlStatement x) { + + } + + @Override + public boolean visit(MySqlLoadXmlStatement x) { + print("LOAD XML "); + + if (x.isLowPriority()) { + print("LOW_PRIORITY "); + } + + if (x.isConcurrent()) { + print("CONCURRENT "); + } + + if (x.isLocal()) { + print("LOCAL "); + } + + print("INFILE "); + + x.getFileName().accept(this); + + if (x.isReplicate()) { + print(" REPLACE "); + } + + if (x.isIgnore()) { + print(" IGNORE "); + } + + print(" INTO TABLE "); + x.getTableName().accept(this); + + if (x.getCharset() != null) { + print(" CHARSET "); + print(x.getCharset()); + } + + if (x.getRowsIdentifiedBy() != null) { + print(" ROWS IDENTIFIED BY "); + x.getRowsIdentifiedBy().accept(this); + } + + if (x.getSetList().size() != 0) { + print(" SET "); + printAndAccept(x.getSetList(), ", "); + } + + return false; + } + + @Override + public void endVisit(CobarShowStatus x) { + + } + + @Override + public boolean visit(CobarShowStatus x) { + print("SHOW COBAR_STATUS"); + return false; + } + + @Override + public void endVisit(MySqlKillStatement x) { + + } + + @Override + public boolean visit(MySqlKillStatement x) { + if (MySqlKillStatement.Type.CONNECTION.equals(x.getType())) { + print("KILL CONNECTION "); + } else if (MySqlKillStatement.Type.QUERY.equals(x.getType())) { + print("KILL QUERY "); + } + x.getThreadId().accept(this); + return false; + } + + @Override + public void endVisit(MySqlBinlogStatement x) { + + } + + @Override + public boolean visit(MySqlBinlogStatement x) { + print("BINLOG "); + x.getExpr().accept(this); + return false; + } + + @Override + public void endVisit(MySqlResetStatement x) { + + } + + @Override + public boolean visit(MySqlResetStatement x) { + print("RESET "); + for (int i = 0; i < x.getOptions().size(); ++i) { + if (i != 0) { + print(", "); + } + print(x.getOptions().get(i)); + } + return false; + } + + @Override + public void endVisit(MySqlCreateUserStatement x) { + + } + + @Override + public boolean visit(MySqlCreateUserStatement x) { + print("CREATE USER "); + printAndAccept(x.getUsers(), ", "); + return false; + } + + @Override + public void endVisit(UserSpecification x) { + + } + + @Override + public boolean visit(UserSpecification x) { + x.getUser().accept(this); + + if (x.getPassword() != null) { + print(" IDENTIFIED BY "); + x.getPassword().accept(this); + } + + if (x.getAuthPlugin() != null) { + print(" IDENTIFIED WITH "); + x.getAuthPlugin().accept(this); + } + return false; + } + + @Override + public void endVisit(MySqlDropUser x) { + + } + + @Override + public boolean visit(MySqlDropUser x) { + print("DROP USER "); + printAndAccept(x.getUsers(), ", "); + return false; + } + + @Override + public void endVisit(MySqlDropTableStatement x) { + + } + + @Override + public boolean visit(MySqlDropTableStatement x) { + if (x.isTemporary()) { + print("DROP TEMPORARY TABLE "); + } else { + print("DROP TABLE "); + } + if (x.isIfExists()) { + print("IF EXISTS "); + } + + printAndAccept(x.getTableSources(), ", "); + + if (x.getOption() != null) { + print(' '); + print(x.getOption()); + } + return false; + } + + @Override + public void endVisit(MySqlPartitionByKey x) { + + } + + @Override + public boolean visit(MySqlPartitionByKey x) { + if (x.isLinear()) { + print("PARTITION BY LINEAR KEY ("); + } else { + print("PARTITION BY KEY ("); + } + printAndAccept(x.getColumns(), ", "); + print(")"); + + if (x.getPartitionCount() != null) { + print(" PARTITIONS "); + x.getPartitionCount().accept(this); + } + return false; + } + + @Override + public void endVisit(MySqlPartitionByRange x) { + + } + + @Override + public boolean visit(MySqlPartitionByRange x) { + print("PARTITION BY RANGE"); + if (x.getExpr() != null) { + print(" ("); + x.getExpr().accept(this); + print(")"); + } else { + print(" COLUMNS ("); + printAndAccept(x.getColumns(), ", "); + print(")"); + } + + if (x.getPartitionCount() != null) { + print(" PARTITIONS "); + x.getPartitionCount().accept(this); + } + + List partitions = x.getPartitions(); + int partitionsSize = partitions.size(); + if (partitionsSize > 0) { + print("("); + incrementIndent(); + for (int i = 0; i < partitionsSize; ++i) { + println(); + partitions.get(i).accept(this); + if (i != partitionsSize - 1) { + print(", "); + } + } + decrementIndent(); + println(); + print(")"); + } + return false; + } + + @Override + public void endVisit(MySqlPartitionByList x) { + + } + + @Override + public boolean visit(MySqlPartitionByList x) { + print("PARTITION BY LIST "); + if (x.getExpr() != null) { + print("("); + x.getExpr().accept(this); + print(") "); + } else { + print("COLUMNS ("); + printAndAccept(x.getColumns(), ", "); + print(") "); + } + + if (x.getPartitionCount() != null) { + print(" PARTITIONS "); + x.getPartitionCount().accept(this); + } + + List partitions = x.getPartitions(); + int partitionsSize = partitions.size(); + if (partitionsSize > 0) { + print("("); + incrementIndent(); + for (int i = 0; i < partitionsSize; ++i) { + println(); + partitions.get(i).accept(this); + if (i != partitionsSize - 1) { + print(", "); + } + } + decrementIndent(); + println(); + print(")"); + } + return false; + } + + // + + @Override + public void endVisit(MySqlPartitionByHash x) { + + } + + @Override + public boolean visit(MySqlPartitionByHash x) { + if (x.isLinear()) { + print("PARTITION BY LINEAR HASH ("); + } else { + print("PARTITION BY HASH ("); + } + // + x.getExpr().accept(this); + print(")"); + + if (x.getPartitionCount() != null) { + print(" PARTITIONS "); + x.getPartitionCount().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlSelectQueryBlock x) { + + } + + @Override + public boolean visit(MySqlOutFileExpr x) { + print("OUTFILE "); + x.getFile().accept(this); + + if (x.getCharset() != null) { + print(" CHARACTER SET "); + print(x.getCharset()); + } + + if (x.getColumnsTerminatedBy() != null || x.getColumnsEnclosedBy() != null || x.getColumnsEscaped() != null) { + print(" COLUMNS"); + if (x.getColumnsTerminatedBy() != null) { + print(" TERMINATED BY "); + x.getColumnsTerminatedBy().accept(this); + } + + if (x.getColumnsEnclosedBy() != null) { + if (x.isColumnsEnclosedOptionally()) { + print(" OPTIONALLY"); + } + print(" ENCLOSED BY "); + x.getColumnsEnclosedBy().accept(this); + } + + if (x.getColumnsEscaped() != null) { + print(" ESCAPED BY "); + x.getColumnsEscaped().accept(this); + } + } + + if (x.getLinesStartingBy() != null || x.getLinesTerminatedBy() != null) { + print(" LINES"); + if (x.getLinesStartingBy() != null) { + print(" STARTING BY "); + x.getLinesStartingBy().accept(this); + } + + if (x.getLinesTerminatedBy() != null) { + print(" TERMINATED BY "); + x.getLinesTerminatedBy().accept(this); + } + } + + return false; + } + + @Override + public void endVisit(MySqlOutFileExpr x) { + + } + + @Override + public boolean visit(MySqlDescribeStatement x) { + print("DESC "); + x.getObject().accept(this); + return false; + } + + @Override + public void endVisit(MySqlDescribeStatement x) { + + } + + @Override + public boolean visit(MySqlUpdateStatement x) { + print("UPDATE "); + + if (x.isLowPriority()) { + print("LOW_PRIORITY "); + } + + if (x.isIgnore()) { + print("IGNORE "); + } + + x.getTableSource().accept(this); + + println(); + print("SET "); + for (int i = 0, size = x.getItems().size(); i < size; ++i) { + if (i != 0) { + print(", "); + } + x.getItems().get(i).accept(this); + } + + if (x.getWhere() != null) { + println(); + incrementIndent(); + print("WHERE "); + x.getWhere().setParent(x); + x.getWhere().accept(this); + decrementIndent(); + } + + if (x.getOrderBy() != null) { + println(); + x.getOrderBy().accept(this); + } + + if (x.getLimit() != null) { + println(); + x.getLimit().accept(this); + } + return false; + } + + @Override + public void endVisit(MySqlUpdateStatement x) { + + } + + @Override + public boolean visit(MySqlSetTransactionIsolationLevelStatement x) { + return false; + } + + @Override + public void endVisit(MySqlSetTransactionIsolationLevelStatement x) { + if (x.getGlobal() == null) { + print("SET TRANSACTION ISOLATION LEVEL "); + } else if (x.getGlobal().booleanValue()) { + print("SET GLOBAL TRANSACTION ISOLATION LEVEL "); + } else { + print("SET SESSION TRANSACTION ISOLATION LEVEL "); + } + print(x.getLevel()); + } + + @Override + public boolean visit(MySqlSetNamesStatement x) { + print("SET NAMES "); + if (x.isDefault()) { + print("DEFAULT"); + } else { + print(x.getCharSet()); + if (x.getCollate() != null) { + print(" COLLATE "); + print(x.getCollate()); + } + } + return false; + } + + @Override + public void endVisit(MySqlSetNamesStatement x) { + + } + + @Override + public boolean visit(MySqlSetCharSetStatement x) { + print("SET CHARACTER SET "); + if (x.isDefault()) { + print("DEFAULT"); + } else { + print(x.getCharSet()); + if (x.getCollate() != null) { + print(" COLLATE "); + print(x.getCollate()); + } + } + return false; + } + + @Override + public void endVisit(MySqlSetCharSetStatement x) { + + } + + @Override + public void endVisit(MySqlShowAuthorsStatement x) { + + } + + @Override + public boolean visit(MySqlShowAuthorsStatement x) { + print("SHOW AUTHORS"); + return false; + } + + @Override + public void endVisit(MySqlShowBinaryLogsStatement x) { + + } + + @Override + public boolean visit(MySqlShowBinaryLogsStatement x) { + print("SHOW BINARY LOGS"); + return false; + } + + @Override + public boolean visit(MySqlShowMasterLogsStatement x) { + print("SHOW MASTER LOGS"); + return false; + } + + @Override + public void endVisit(MySqlShowMasterLogsStatement x) { + + } + + @Override + public boolean visit(MySqlShowCollationStatement x) { + print("SHOW COLLATION"); + if (x.getPattern() != null) { + print(" LIKE "); + x.getPattern().accept(this); + } + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().accept(this); + } + return false; + } + + @Override + public void endVisit(MySqlShowCollationStatement x) { + + } + + @Override + public boolean visit(MySqlShowBinLogEventsStatement x) { + print("SHOW BINLOG EVENTS"); + if (x.getIn() != null) { + print(" IN "); + x.getIn().accept(this); + } + if (x.getFrom() != null) { + print(" FROM "); + x.getFrom().accept(this); + } + if (x.getLimit() != null) { + print(" "); + x.getLimit().accept(this); + } + return false; + } + + @Override + public void endVisit(MySqlShowBinLogEventsStatement x) { + + } + + @Override + public boolean visit(MySqlShowCharacterSetStatement x) { + print("SHOW CHARACTER SET"); + if (x.getPattern() != null) { + print(" LIKE "); + x.getPattern().accept(this); + } + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().accept(this); + } + return false; + } + + @Override + public void endVisit(MySqlShowCharacterSetStatement x) { + + } + + @Override + public boolean visit(MySqlShowContributorsStatement x) { + print("SHOW CONTRIBUTORS"); + return false; + } + + @Override + public void endVisit(MySqlShowContributorsStatement x) { + + } + + @Override + public boolean visit(MySqlShowCreateDatabaseStatement x) { + print("SHOW CREATE DATABASE "); + x.getDatabase().accept(this); + return false; + } + + @Override + public void endVisit(MySqlShowCreateDatabaseStatement x) { + + } + + @Override + public boolean visit(MySqlShowCreateEventStatement x) { + print("SHOW CREATE EVENT "); + x.getEventName().accept(this); + return false; + } + + @Override + public void endVisit(MySqlShowCreateEventStatement x) { + + } + + @Override + public boolean visit(MySqlShowCreateFunctionStatement x) { + print("SHOW CREATE FUNCTION "); + x.getName().accept(this); + return false; + } + + @Override + public void endVisit(MySqlShowCreateFunctionStatement x) { + + } + + @Override + public boolean visit(MySqlShowCreateProcedureStatement x) { + print("SHOW CREATE PROCEDURE "); + x.getName().accept(this); + return false; + } + + @Override + public void endVisit(MySqlShowCreateProcedureStatement x) { + + } + + @Override + public boolean visit(MySqlShowCreateTableStatement x) { + print("SHOW CREATE TABLE "); + x.getName().accept(this); + return false; + } + + @Override + public void endVisit(MySqlShowCreateTableStatement x) { + + } + + @Override + public boolean visit(MySqlShowCreateTriggerStatement x) { + print("SHOW CREATE TRIGGER "); + x.getName().accept(this); + return false; + } + + @Override + public void endVisit(MySqlShowCreateTriggerStatement x) { + + } + + @Override + public boolean visit(MySqlShowCreateViewStatement x) { + print("SHOW CREATE VIEW "); + x.getName().accept(this); + return false; + } + + @Override + public void endVisit(MySqlShowCreateViewStatement x) { + + } + + @Override + public boolean visit(MySqlShowEngineStatement x) { + print("SHOW ENGINE "); + x.getName().accept(this); + print(' '); + print(x.getOption().name()); + return false; + } + + @Override + public void endVisit(MySqlShowEngineStatement x) { + + } + + @Override + public boolean visit(MySqlShowEventsStatement x) { + print("SHOW EVENTS"); + if (x.getSchema() != null) { + print(" FROM "); + x.getSchema().accept(this); + } + + if (x.getLike() != null) { + print(" LIKE "); + x.getLike().accept(this); + } + + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlShowEventsStatement x) { + + } + + @Override + public boolean visit(MySqlShowFunctionCodeStatement x) { + print("SHOW FUNCTION CODE "); + x.getName().accept(this); + return false; + } + + @Override + public void endVisit(MySqlShowFunctionCodeStatement x) { + + } + + @Override + public boolean visit(MySqlShowFunctionStatusStatement x) { + print("SHOW FUNCTION STATUS"); + if (x.getLike() != null) { + print(" LIKE "); + x.getLike().accept(this); + } + + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlShowFunctionStatusStatement x) { + + } + + @Override + public boolean visit(MySqlShowEnginesStatement x) { + if (x.isStorage()) { + print("SHOW STORAGE ENGINES"); + } else { + print("SHOW ENGINES"); + } + return false; + } + + @Override + public void endVisit(MySqlShowEnginesStatement x) { + + } + + @Override + public boolean visit(MySqlShowErrorsStatement x) { + if (x.isCount()) { + print("SHOW COUNT(*) ERRORS"); + } else { + print("SHOW ERRORS"); + if (x.getLimit() != null) { + print(' '); + x.getLimit().accept(this); + } + } + return false; + } + + @Override + public void endVisit(MySqlShowErrorsStatement x) { + + } + + @Override + public boolean visit(MySqlShowGrantsStatement x) { + print("SHOW GRANTS"); + if (x.getUser() != null) { + print(" FOR "); + x.getUser().accept(this); + } + return false; + } + + @Override + public void endVisit(MySqlShowGrantsStatement x) { + + } + + @Override + public boolean visit(MySqlUserName x) { + print(x.getUserName()); + if (x.getHost() != null) { + print('@'); + print(x.getHost()); + } + return false; + } + + @Override + public void endVisit(MySqlUserName x) { + + } + + @Override + public boolean visit(MySqlShowIndexesStatement x) { + print("SHOW INDEX"); + + if (x.getTable() != null) { + print(" FROM "); + if (x.getDatabase() != null) { + x.getDatabase().accept(this); + print('.'); + } + x.getTable().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlShowIndexesStatement x) { + + } + + @Override + public boolean visit(MySqlShowKeysStatement x) { + print("SHOW KEYS"); + + if (x.getTable() != null) { + print(" FROM "); + if (x.getDatabase() != null) { + x.getDatabase().accept(this); + print('.'); + } + x.getTable().accept(this); + } + return false; + } + + @Override + public void endVisit(MySqlShowKeysStatement x) { + + } + + @Override + public boolean visit(MySqlShowMasterStatusStatement x) { + print("SHOW MASTER STATUS"); + return false; + } + + @Override + public void endVisit(MySqlShowMasterStatusStatement x) { + + } + + @Override + public boolean visit(MySqlShowOpenTablesStatement x) { + print("SHOW OPEN TABLES"); + + if (x.getDatabase() != null) { + print(" FROM "); + x.getDatabase().accept(this); + } + + if (x.getLike() != null) { + print(" LIKE "); + x.getLike().accept(this); + } + + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlShowOpenTablesStatement x) { + + } + + @Override + public boolean visit(MySqlShowPluginsStatement x) { + print("SHOW PLUGINS"); + return false; + } + + @Override + public void endVisit(MySqlShowPluginsStatement x) { + + } + + @Override + public boolean visit(MySqlShowPrivilegesStatement x) { + print("SHOW PRIVILEGES"); + return false; + } + + @Override + public void endVisit(MySqlShowPrivilegesStatement x) { + + } + + @Override + public boolean visit(MySqlShowProcedureCodeStatement x) { + print("SHOW PROCEDURE CODE "); + x.getName().accept(this); + return false; + } + + @Override + public void endVisit(MySqlShowProcedureCodeStatement x) { + + } + + @Override + public boolean visit(MySqlShowProcedureStatusStatement x) { + print("SHOW PROCEDURE STATUS"); + if (x.getLike() != null) { + print(" LIKE "); + x.getLike().accept(this); + } + + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().accept(this); + } + return false; + } + + @Override + public void endVisit(MySqlShowProcedureStatusStatement x) { + + } + + @Override + public boolean visit(MySqlShowProcessListStatement x) { + if (x.isFull()) { + print("SHOW FULL PROCESSLIST"); + } else { + print("SHOW PROCESSLIST"); + } + return false; + } + + @Override + public void endVisit(MySqlShowProcessListStatement x) { + + } + + @Override + public boolean visit(MySqlShowProfileStatement x) { + print("SHOW PROFILE"); + for (int i = 0; i < x.getTypes().size(); ++i) { + if (i == 0) { + print(' '); + } else { + print(", "); + } + print(x.getTypes().get(i).name); + } + + if (x.getForQuery() != null) { + print(" FOR QUERY "); + x.getForQuery().accept(this); + } + + if (x.getLimit() != null) { + print(' '); + x.getLimit().accept(this); + } + return false; + } + + @Override + public void endVisit(MySqlShowProfileStatement x) { + + } + + @Override + public boolean visit(MySqlShowProfilesStatement x) { + print("SHOW PROFILES"); + return false; + } + + @Override + public void endVisit(MySqlShowProfilesStatement x) { + + } + + @Override + public boolean visit(MySqlShowRelayLogEventsStatement x) { + print("SHOW RELAYLOG EVENTS"); + + if (x.getLogName() != null) { + print(" IN "); + x.getLogName().accept(this); + } + + if (x.getFrom() != null) { + print(" FROM "); + x.getFrom().accept(this); + } + + if (x.getLimit() != null) { + print(' '); + x.getLimit().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlShowRelayLogEventsStatement x) { + + } + + @Override + public boolean visit(MySqlShowSlaveHostsStatement x) { + print("SHOW SLAVE HOSTS"); + return false; + } + + @Override + public void endVisit(MySqlShowSlaveHostsStatement x) { + + } + + @Override + public boolean visit(MySqlShowSlaveStatusStatement x) { + print("SHOW SLAVE STATUS"); + return false; + } + + @Override + public void endVisit(MySqlShowSlaveStatusStatement x) { + + } + + @Override + public boolean visit(MySqlShowTableStatusStatement x) { + print("SHOW TABLE STATUS"); + if (x.getDatabase() != null) { + print(" FROM "); + x.getDatabase().accept(this); + } + + if (x.getLike() != null) { + print(" LIKE "); + x.getLike().accept(this); + } + + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlShowTableStatusStatement x) { + + } + + @Override + public boolean visit(MySqlShowTriggersStatement x) { + print("SHOW TRIGGERS"); + + if (x.getDatabase() != null) { + print(" FROM "); + x.getDatabase().accept(this); + } + + if (x.getLike() != null) { + print(" LIKE "); + x.getLike().accept(this); + } + + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().setParent(x); + x.getWhere().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlShowTriggersStatement x) { + + } + + @Override + public boolean visit(MySqlShowVariantsStatement x) { + print("SHOW "); + + if (x.isGlobal()) { + print("GLOBAL "); + } + + if (x.isSession()) { + print("SESSION "); + } + + print("VARIABLES"); + + if (x.getLike() != null) { + print(" LIKE "); + x.getLike().accept(this); + } + + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().setParent(x); + x.getWhere().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlShowVariantsStatement x) { + + } + + @Override + public boolean visit(MySqlAlterTableStatement x) { + if (x.isIgnore()) { + print("ALTER IGNORE TABLE "); + } else { + print("ALTER TABLE "); + } + x.getName().accept(this); + incrementIndent(); + for (int i = 0; i < x.getItems().size(); ++i) { + SQLAlterTableItem item = x.getItems().get(i); + if (i != 0) { + print(','); + } + println(); + item.accept(this); + } + decrementIndent(); + return false; + } + + @Override + public void endVisit(MySqlAlterTableStatement x) { + + } + + @Override + public boolean visit(MySqlAlterTableAddColumn x) { + print("ADD COLUMN "); + printAndAccept(x.getColumns(), ", "); + if (x.getFirstColumn() != null) { + print(" FIRST "); + x.getFirstColumn().accept(this); + } else if (x.getAfterColumn() != null) { + print(" AFTER "); + x.getAfterColumn().accept(this); + } else if (x.isFirst()) { + print(" FIRST"); + } + return false; + } + + @Override + public void endVisit(MySqlAlterTableAddColumn x) { + + } + + @Override + public boolean visit(MySqlCreateIndexStatement x) { + print("CREATE "); + if (x.getType() != null) { + print(x.getType()); + print(" "); + } + + print("INDEX "); + + x.getName().accept(this); + print(" ON "); + x.getTable().accept(this); + print(" ("); + printAndAccept(x.getItems(), ", "); + print(")"); + + if (x.getUsing() != null) { + print(" USING "); + print(x.getUsing()); + } + return false; + } + + @Override + public void endVisit(MySqlCreateIndexStatement x) { + + } + + @Override + public boolean visit(MySqlRenameTableStatement.Item x) { + x.getName().accept(this); + print(" TO "); + x.getTo().accept(this); + return false; + } + + @Override + public void endVisit(MySqlRenameTableStatement.Item x) { + + } + + @Override + public boolean visit(MySqlRenameTableStatement x) { + print("RENAME TABLE "); + printAndAccept(x.getItems(), ", "); + return false; + } + + @Override + public void endVisit(MySqlRenameTableStatement x) { + + } + + @Override + public boolean visit(MySqlDropViewStatement x) { + print("DROP VIEW "); + if (x.isIfExists()) { + print("IF EXISTS "); + } + + printAndAccept(x.getTableSources(), ", "); + + if (x.getOption() != null) { + print(' '); + print(x.getOption()); + } + return false; + } + + @Override + public void endVisit(MySqlDropViewStatement x) { + + } + + @Override + public boolean visit(MySqlUnionQuery x) { + { + boolean needParen = false; + if (x.getLeft() instanceof MySqlSelectQueryBlock) { + MySqlSelectQueryBlock right = (MySqlSelectQueryBlock) x.getLeft(); + if (right.getOrderBy() != null || right.getLimit() != null) { + needParen = true; + } + } + if (needParen) { + print('('); + x.getLeft().accept(this); + print(')'); + } else { + x.getLeft().accept(this); + } + } + println(); + print(x.getOperator().name); + println(); + + boolean needParen = false; + + if (x.getOrderBy() != null || x.getLimit() != null) { + needParen = true; + } else if (x.getRight() instanceof MySqlSelectQueryBlock) { + MySqlSelectQueryBlock right = (MySqlSelectQueryBlock) x.getRight(); + if (right.getOrderBy() != null || right.getLimit() != null) { + needParen = true; + } + } + + if (needParen) { + print('('); + x.getRight().accept(this); + print(')'); + } else { + x.getRight().accept(this); + } + + if (x.getOrderBy() != null) { + println(); + x.getOrderBy().accept(this); + } + + if (x.getLimit() != null) { + println(); + x.getLimit().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlUnionQuery x) { + + } + + @Override + public boolean visit(MySqlUseIndexHint x) { + print("USE INDEX "); + if (x.getOption() != null) { + print("FOR "); + print(x.getOption().name); + print(' '); + } + print('('); + printAndAccept(x.getIndexList(), ", "); + print(')'); + return false; + } + + @Override + public void endVisit(MySqlUseIndexHint x) { + + } + + @Override + public boolean visit(MySqlIgnoreIndexHint x) { + print("IGNORE INDEX "); + if (x.getOption() != null) { + print("FOR "); + print(x.getOption().name); + print(' '); + } + print('('); + printAndAccept(x.getIndexList(), ", "); + print(')'); + return false; + } + + @Override + public void endVisit(MySqlIgnoreIndexHint x) { + + } + + public boolean visit(SQLExprTableSource x) { + x.getExpr().accept(this); + + if (x.getAlias() != null) { + print(' '); + print(x.getAlias()); + } + + for (int i = 0; i < x.getHintsSize(); ++i) { + print(' '); + x.getHints().get(i).accept(this); + } + + return false; + } + + @Override + public boolean visit(MySqlLockTableStatement x) { + print("LOCK TABLES "); + x.getTableSource().accept(this); + if (x.getLockType() != null) { + print(' '); + print(x.getLockType().name); + } + return false; + } + + @Override + public void endVisit(MySqlLockTableStatement x) { + + } + + @Override + public boolean visit(MySqlUnlockTablesStatement x) { + print("UNLOCK TABLES"); + return false; + } + + @Override + public void endVisit(MySqlUnlockTablesStatement x) { + + } + + @Override + public boolean visit(MySqlForceIndexHint x) { + print("FORCE INDEX "); + if (x.getOption() != null) { + print("FOR "); + print(x.getOption().name); + print(' '); + } + print('('); + printAndAccept(x.getIndexList(), ", "); + print(')'); + return false; + } + + @Override + public void endVisit(MySqlForceIndexHint x) { + + } + + @Override + public boolean visit(MySqlAlterTableChangeColumn x) { + print("CHANGE COLUMN "); + x.getColumnName().accept(this); + print(' '); + x.getNewColumnDefinition().accept(this); + if (x.getFirstColumn() != null) { + print(" FIRST "); + x.getFirstColumn().accept(this); + } else if (x.getAfterColumn() != null) { + print(" AFTER "); + x.getAfterColumn().accept(this); + } else if (x.isFirst()) { + print(" FIRST"); + } + + return false; + } + + @Override + public void endVisit(MySqlAlterTableChangeColumn x) { + + } + + @Override + public boolean visit(MySqlAlterTableModifyColumn x) { + print("MODIFY COLUMN "); + x.getNewColumnDefinition().accept(this); + if (x.getFirstColumn() != null) { + print(" FIRST "); + x.getFirstColumn().accept(this); + } else if (x.getAfterColumn() != null) { + print(" AFTER "); + x.getAfterColumn().accept(this); + } else if (x.isFirst()) { + print(" FIRST"); + } + + return false; + } + + @Override + public void endVisit(MySqlAlterTableModifyColumn x) { + + } + + @Override + public boolean visit(MySqlAlterTableCharacter x) { + print("CHARACTER SET = "); + x.getCharacterSet().accept(this); + + if (x.getCollate() != null) { + print(", COLLATE = "); + x.getCollate().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlAlterTableCharacter x) { + + } + + @Override + public boolean visit(MySqlAlterTableAddIndex x) { + print("ADD "); + if (x.getType() != null) { + print(x.getType()); + print(" "); + } + + print("INDEX "); + + if (x.getName() != null) { + x.getName().accept(this); + print(' '); + } + print("("); + printAndAccept(x.getItems(), ", "); + print(")"); + + if (x.getUsing() != null) { + print(" USING "); + print(x.getUsing()); + } + return false; + } + + @Override + public void endVisit(MySqlAlterTableAddIndex x) { + + } + + @Override + public boolean visit(MySqlAlterTableAddUnique x) { + print("ADD "); + if (x.getType() != null) { + print(x.getType()); + print(" "); + } + + print("UNIQUE "); + + if (x.getName() != null) { + x.getName().accept(this); + print(' '); + } + print("("); + printAndAccept(x.getItems(), ", "); + print(")"); + + if (x.getUsing() != null) { + print(" USING "); + print(x.getUsing()); + } + return false; + } + + @Override + public void endVisit(MySqlAlterTableAddUnique x) { + + } + + @Override + public boolean visit(MySqlAlterTableOption x) { + print(x.getName()); + print(" = "); + print(x.getValue().toString()); + return false; + } + + @Override + public void endVisit(MySqlAlterTableOption x) { + + } + + @Override + public void endVisit(MySqlCreateTableStatement x) { + + } + + @Override + public boolean visit(MySqlHelpStatement x) { + print("HELP "); + x.getContent().accept(this); + return false; + } + + @Override + public void endVisit(MySqlHelpStatement x) { + + } + + @Override + public boolean visit(MySqlCharExpr x) { + print(x.toString()); + return false; + } + + @Override + public void endVisit(MySqlCharExpr x) { + + } + + @Override + public boolean visit(MySqlUnique x) { + if (x.getName() != null) { + print("CONSTRAINT "); + x.getName().accept(this); + print(' '); + } + + print("UNIQUE"); + + if (x.getIndexType() != null) { + print(" USING "); + print(x.getIndexType()); + } + + print(" ("); + printAndAccept(x.getColumns(), ", "); + print(")"); + + return false; + } + + @Override + public void endVisit(MySqlUnique x) { + + } + + @Override + public boolean visit(MySqlForeignKey x) { + if (x.getName() != null) { + print("CONSTRAINT "); + x.getName().accept(this); + print(' '); + } + + print("FOREIGN KEY ("); + printAndAccept(x.getReferencedColumns(), ", "); + print(")"); + + print(" REFERENCES "); + x.getReferencedTableName().accept(this); + + print(" ("); + printAndAccept(x.getReferencedColumns(), ", "); + print(")"); + return false; + } + + @Override + public void endVisit(MySqlForeignKey x) { + + } + + @Override + public boolean visit(MySqlAlterTableDiscardTablespace x) { + print("DISCARD TABLESPACE"); + return false; + } + + @Override + public void endVisit(MySqlAlterTableDiscardTablespace x) { + + } + + @Override + public boolean visit(MySqlAlterTableImportTablespace x) { + print("IMPORT TABLESPACE"); + return false; + } + + @Override + public void endVisit(MySqlAlterTableImportTablespace x) { + + } + + @Override + public boolean visit(SQLAssignItem x) { + x.getTarget().accept(this); + if (!"NAMES".equalsIgnoreCase(x.getTarget().toString())) { + print(" = "); + } + x.getValue().accept(this); + return false; + } + + @Override + public boolean visit(TableSpaceOption x) { + x.getName().accept(this); + + if (x.getStorage() != null) { + print(' '); + x.getStorage().accept(this); + } + return false; + } + + @Override + public void endVisit(TableSpaceOption x) { + + } + + @Override + public boolean visit(MySqlPartitioningDef x) { + print("PARTITION "); + x.getName().accept(this); + if (x.getValues() != null) { + print(' '); + x.getValues().accept(this); + } + + if (x.getDataDirectory() != null) { + incrementIndent(); + println(); + print("DATA DIRECTORY "); + x.getDataDirectory().accept(this); + decrementIndent(); + } + if (x.getIndexDirectory() != null) { + incrementIndent(); + println(); + print("INDEX DIRECTORY "); + x.getIndexDirectory().accept(this); + decrementIndent(); + } + + return false; + } + + @Override + public void endVisit(MySqlPartitioningDef x) { + + } + + @Override + public boolean visit(LessThanValues x) { + print("VALUES LESS THAN ("); + printAndAccept(x.getItems(), ", "); + print(')'); + return false; + } + + @Override + public void endVisit(LessThanValues x) { + + } + + @Override + public boolean visit(InValues x) { + print("VALUES IN ("); + printAndAccept(x.getItems(), ", "); + print(')'); + return false; + } + + @Override + public void endVisit(InValues x) { + + } + +} // diff --git a/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java b/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java index 3d344a6b5c..36ea07a12e 100644 --- a/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java @@ -1,1482 +1,1490 @@ -/* - * 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.visitor; - -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; -import java.sql.Blob; -import java.sql.Clob; -import java.sql.NClob; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -import com.alibaba.druid.sql.ast.SQLCommentHint; -import com.alibaba.druid.sql.ast.SQLDataType; -import com.alibaba.druid.sql.ast.SQLExpr; -import com.alibaba.druid.sql.ast.SQLObject; -import com.alibaba.druid.sql.ast.SQLOrderBy; -import com.alibaba.druid.sql.ast.SQLOver; -import com.alibaba.druid.sql.ast.SQLSetQuantifier; -import com.alibaba.druid.sql.ast.SQLStatement; -import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr; -import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr; -import com.alibaba.druid.sql.ast.expr.SQLAllExpr; -import com.alibaba.druid.sql.ast.expr.SQLAnyExpr; -import com.alibaba.druid.sql.ast.expr.SQLBetweenExpr; -import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; -import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator; -import com.alibaba.druid.sql.ast.expr.SQLCaseExpr; -import com.alibaba.druid.sql.ast.expr.SQLCastExpr; -import com.alibaba.druid.sql.ast.expr.SQLCharExpr; -import com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr; -import com.alibaba.druid.sql.ast.expr.SQLDefaultExpr; -import com.alibaba.druid.sql.ast.expr.SQLExistsExpr; -import com.alibaba.druid.sql.ast.expr.SQLHexExpr; -import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; -import com.alibaba.druid.sql.ast.expr.SQLInListExpr; -import com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr; -import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr; -import com.alibaba.druid.sql.ast.expr.SQLListExpr; -import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr; -import com.alibaba.druid.sql.ast.expr.SQLNCharExpr; -import com.alibaba.druid.sql.ast.expr.SQLNotExpr; -import com.alibaba.druid.sql.ast.expr.SQLNullExpr; -import com.alibaba.druid.sql.ast.expr.SQLNumberExpr; -import com.alibaba.druid.sql.ast.expr.SQLObjectCreateExpr; -import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; -import com.alibaba.druid.sql.ast.expr.SQLQueryExpr; -import com.alibaba.druid.sql.ast.expr.SQLSomeExpr; -import com.alibaba.druid.sql.ast.expr.SQLUnaryExpr; -import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr; -import com.alibaba.druid.sql.ast.statement.NotNullConstraint; -import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddColumn; -import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddForeignKey; -import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddPrimaryKey; -import com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn; -import com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableConstraint; -import com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableKeys; -import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropColumnItem; -import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint; -import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropForeinKey; -import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex; -import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropPrimaryKey; -import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint; -import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableKeys; -import com.alibaba.druid.sql.ast.statement.SQLAlterTableItem; -import com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement; -import com.alibaba.druid.sql.ast.statement.SQLAssignItem; -import com.alibaba.druid.sql.ast.statement.SQLCallStatement; -import com.alibaba.druid.sql.ast.statement.SQLCharactorDataType; -import com.alibaba.druid.sql.ast.statement.SQLCheck; -import com.alibaba.druid.sql.ast.statement.SQLColumnCheck; -import com.alibaba.druid.sql.ast.statement.SQLColumnConstraint; -import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition; -import com.alibaba.druid.sql.ast.statement.SQLColumnPrimaryKey; -import com.alibaba.druid.sql.ast.statement.SQLColumnUniqueIndex; -import com.alibaba.druid.sql.ast.statement.SQLCommentStatement; -import com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement; -import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement; -import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement; -import com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement; -import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement; -import com.alibaba.druid.sql.ast.statement.SQLDropViewStatement; -import com.alibaba.druid.sql.ast.statement.SQLExprHint; -import com.alibaba.druid.sql.ast.statement.SQLExprTableSource; -import com.alibaba.druid.sql.ast.statement.SQLInsertStatement; -import com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause; -import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; -import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource.JoinType; -import com.alibaba.druid.sql.ast.statement.SQLReleaseSavePointStatement; -import com.alibaba.druid.sql.ast.statement.SQLRollbackStatement; -import com.alibaba.druid.sql.ast.statement.SQLSavePointStatement; -import com.alibaba.druid.sql.ast.statement.SQLSelect; -import com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause; -import com.alibaba.druid.sql.ast.statement.SQLSelectItem; -import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem; -import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; -import com.alibaba.druid.sql.ast.statement.SQLSelectStatement; -import com.alibaba.druid.sql.ast.statement.SQLSetStatement; -import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource; -import com.alibaba.druid.sql.ast.statement.SQLTableElement; -import com.alibaba.druid.sql.ast.statement.SQLTruncateStatement; -import com.alibaba.druid.sql.ast.statement.SQLUnionQuery; -import com.alibaba.druid.sql.ast.statement.SQLUniqueConstraint; -import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem; -import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement; -import com.alibaba.druid.sql.ast.statement.SQLUseStatement; -import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause; - -public class SQLASTOutputVisitor extends SQLASTVisitorAdapter { - - protected final Appendable appender; - private String indent = "\t"; - private int indentCount = 0; - private boolean prettyFormat = true; - - private List parameters; - - public SQLASTOutputVisitor(Appendable appender){ - this.appender = appender; - } - - public List getParameters() { - if (parameters == null) { - parameters = new ArrayList(); - } - - return parameters; - } - - public void setParameters(List parameters) { - this.parameters = parameters; - } - - public int getIndentCount() { - return indentCount; - } - - public Appendable getAppender() { - return appender; - } - - public boolean isPrettyFormat() { - return prettyFormat; - } - - public void setPrettyFormat(boolean prettyFormat) { - this.prettyFormat = prettyFormat; - } - - public void decrementIndent() { - this.indentCount -= 1; - } - - public void incrementIndent() { - this.indentCount += 1; - } - - public void print(char value) { - try { - this.appender.append(value); - } catch (IOException e) { - throw new RuntimeException("println error", e); - } - } - - public void print(int value) { - print(Integer.toString(value)); - } - - public void print(Date date) { - SimpleDateFormat dateFormat; - if (date instanceof java.sql.Timestamp) { - dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); - } else { - dateFormat = new SimpleDateFormat("yyyy-MM-dd"); - } - print("'" + dateFormat.format(date) + "'"); - } - - public void print(long value) { - print(Long.toString(value)); - } - - public void print(String text) { - try { - this.appender.append(text); - } catch (IOException e) { - throw new RuntimeException("println error", e); - } - } - - protected void printAlias(String alias) { - if ((alias != null) && (alias.length() > 0)) { - print(" "); - print(alias); - } - } - - protected void printAndAccept(List nodes, String seperator) { - for (int i = 0, size = nodes.size(); i < size; ++i) { - if (i != 0) { - print(seperator); - } - nodes.get(i).accept(this); - } - } - - protected void printSelectList(List selectList) { - incrementIndent(); - for (int i = 0, size = selectList.size(); i < size; ++i) { - if (i != 0) { - if (i % 5 == 0) { - println(); - } - - print(", "); - } - - selectList.get(i).accept(this); - } - decrementIndent(); - } - - protected void printlnAndAccept(List nodes, String seperator) { - for (int i = 0, size = nodes.size(); i < size; ++i) { - if (i != 0) { - println(seperator); - } - - ((SQLObject) nodes.get(i)).accept(this); - } - } - - public void printIndent() { - for (int i = 0; i < this.indentCount; ++i) { - print(this.indent); - } - } - - public void println() { - if (!isPrettyFormat()) { - print(' '); - return; - } - - print("\n"); - printIndent(); - } - - public void println(String text) { - print(text); - println(); - } - - // //////////////////// - - public boolean visit(SQLBetweenExpr x) { - x.getTestExpr().accept(this); - - if (x.isNot()) { - print(" NOT BETWEEN "); - } else { - print(" BETWEEN "); - } - - x.getBeginExpr().accept(this); - print(" AND "); - x.getEndExpr().accept(this); - - return false; - } - - public boolean visit(SQLBinaryOpExpr x) { - SQLObject parent = x.getParent(); - boolean isRoot = parent instanceof SQLSelectQueryBlock; - boolean relational = x.getOperator() == SQLBinaryOperator.BooleanAnd - || x.getOperator() == SQLBinaryOperator.BooleanOr; - - if (isRoot && relational) { - incrementIndent(); - } - - List groupList = new ArrayList(); - SQLExpr left = x.getLeft(); - for (;;) { - if (left instanceof SQLBinaryOpExpr && ((SQLBinaryOpExpr) left).getOperator() == x.getOperator()) { - SQLBinaryOpExpr binaryLeft = (SQLBinaryOpExpr) left; - groupList.add(binaryLeft.getRight()); - left = binaryLeft.getLeft(); - } else { - groupList.add(left); - break; - } - } - - for (int i = groupList.size() - 1; i >= 0; --i) { - SQLExpr item = groupList.get(i); - visitBinaryLeft(item, x.getOperator()); - - if (relational) { - println(); - } else { - print(" "); - } - print(x.getOperator().name); - print(" "); - } - - visitorBinaryRight(x); - - if (isRoot && relational) { - decrementIndent(); - } - - return false; - } - - private void visitorBinaryRight(SQLBinaryOpExpr x) { - if (x.getRight() instanceof SQLBinaryOpExpr) { - SQLBinaryOpExpr right = (SQLBinaryOpExpr) x.getRight(); - boolean rightRational = right.getOperator() == SQLBinaryOperator.BooleanAnd - || right.getOperator() == SQLBinaryOperator.BooleanOr; - - if (right.getOperator().priority >= x.getOperator().priority) { - if (rightRational) { - incrementIndent(); - } - - print('('); - right.accept(this); - print(')'); - - if (rightRational) { - decrementIndent(); - } - } else { - right.accept(this); - } - } else { - x.getRight().accept(this); - } - } - - private void visitBinaryLeft(SQLExpr left, SQLBinaryOperator op) { - if (left instanceof SQLBinaryOpExpr) { - SQLBinaryOpExpr binaryLeft = (SQLBinaryOpExpr) left; - boolean leftRational = binaryLeft.getOperator() == SQLBinaryOperator.BooleanAnd - || binaryLeft.getOperator() == SQLBinaryOperator.BooleanOr; - - if (binaryLeft.getOperator().priority > op.priority) { - if (leftRational) { - incrementIndent(); - } - print('('); - left.accept(this); - print(')'); - - if (leftRational) { - decrementIndent(); - } - } else { - left.accept(this); - } - } else { - left.accept(this); - } - } - - public boolean visit(SQLCaseExpr x) { - print("CASE "); - if (x.getValueExpr() != null) { - x.getValueExpr().accept(this); - print(" "); - } - - printAndAccept(x.getItems(), " "); - - if (x.getElseExpr() != null) { - print(" ELSE "); - x.getElseExpr().accept(this); - } - - print(" END"); - return false; - } - - public boolean visit(SQLCaseExpr.Item x) { - print("WHEN "); - x.getConditionExpr().accept(this); - print(" THEN "); - x.getValueExpr().accept(this); - return false; - } - - public boolean visit(SQLCastExpr x) { - print("CAST("); - x.getExpr().accept(this); - print(" AS "); - x.getDataType().accept(this); - print(")"); - - return false; - } - - public boolean visit(SQLCharExpr x) { - if ((x.getText() == null) || (x.getText().length() == 0)) { - print("NULL"); - } else { - print("'"); - print(x.getText().replaceAll("'", "''")); - print("'"); - } - - return false; - } - - public boolean visit(SQLDataType x) { - print(x.getName()); - if (x.getArguments().size() > 0) { - print("("); - printAndAccept(x.getArguments(), ", "); - print(")"); - } - - return false; - } - - public boolean visit(SQLCharactorDataType x) { - visit((SQLDataType) x); - return false; - } - - public boolean visit(SQLExistsExpr x) { - if (x.isNot()) { - print("NOT EXISTS ("); - } else { - print("EXISTS ("); - } - incrementIndent(); - x.getSubQuery().accept(this); - decrementIndent(); - print(")"); - return false; - } - - public boolean visit(SQLIdentifierExpr x) { - print(x.getName()); - return false; - } - - public boolean visit(SQLInListExpr x) { - x.getExpr().accept(this); - - if (x.isNot()) { - print(" NOT IN ("); - } else { - print(" IN ("); - } - - printAndAccept(x.getTargetList(), ", "); - print(')'); - return false; - } - - public boolean visit(SQLIntegerExpr x) { - print(x.getNumber().toString()); - return false; - } - - public boolean visit(SQLMethodInvokeExpr x) { - if (x.getOwner() != null) { - x.getOwner().accept(this); - print("."); - } - print(x.getMethodName()); - print("("); - printAndAccept(x.getParameters(), ", "); - print(")"); - return false; - } - - public boolean visit(SQLAggregateExpr x) { - print(x.getMethodName()); - print("("); - - if (x.getOption() != null) { - print(x.getOption().toString()); - print(' '); - } - - printAndAccept(x.getArguments(), ", "); - print(")"); - - if (x.getOver() != null) { - print(" "); - x.getOver().accept(this); - } - return false; - } - - public boolean visit(SQLAllColumnExpr x) { - print("*"); - return true; - } - - public boolean visit(SQLNCharExpr x) { - if ((x.getText() == null) || (x.getText().length() == 0)) { - print("NULL"); - } else { - print("N'"); - print(x.getText().replace("'", "''")); - print("'"); - } - return false; - } - - public boolean visit(SQLNotExpr x) { - print("NOT "); - x.getExpr().accept(this); - return false; - } - - public boolean visit(SQLNullExpr x) { - print("NULL"); - return false; - } - - public boolean visit(SQLNumberExpr x) { - print(x.getNumber().toString()); - return false; - } - - public boolean visit(SQLObjectCreateExpr x) { - throw new UnsupportedOperationException(); - } - - public boolean visit(SQLPropertyExpr x) { - x.getOwner().accept(this); - print("."); - print(x.getName()); - return false; - } - - public boolean visit(SQLQueryExpr x) { - SQLObject parent = x.getParent(); - if (parent instanceof SQLSelect) { - parent = parent.getParent(); - } - - if (parent instanceof SQLStatement) { - incrementIndent(); - - println(); - x.getSubQuery().accept(this); - - decrementIndent(); - } else if (parent instanceof ValuesClause) { - println(); - x.getSubQuery().accept(this); - println(); - } else { - print("("); - incrementIndent(); - println(); - x.getSubQuery().accept(this); - println(); - decrementIndent(); - print(")"); - } - return false; - } - - public boolean visit(SQLSelectGroupByClause x) { - if (x.getItems().size() > 0) { - print("GROUP BY "); - printAndAccept(x.getItems(), ", "); - } - - if (x.getHaving() != null) { - println(); - print("HAVING "); - x.getHaving().accept(this); - } - return false; - } - - public boolean visit(SQLSelect x) { - x.getQuery().setParent(x); - - if (x.getWithSubQuery() != null) { - x.getWithSubQuery().accept(this); - println(); - } - - x.getQuery().accept(this); - - if (x.getOrderBy() != null) { - println(); - x.getOrderBy().accept(this); - } - - return false; - } - - public boolean visit(SQLSelectQueryBlock x) { - print("SELECT "); - - if (SQLSetQuantifier.ALL == x.getDistionOption()) { - print("ALL "); - } else if (SQLSetQuantifier.DISTINCT == x.getDistionOption()) { - print("DISTINCT "); - } else if (SQLSetQuantifier.UNIQUE == x.getDistionOption()) { - print("UNIQUE "); - } - - printSelectList(x.getSelectList()); - - if (x.getFrom() != null) { - println(); - print("FROM "); - x.getFrom().accept(this); - } - - if (x.getWhere() != null) { - println(); - print("WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - if (x.getGroupBy() != null) { - println(); - x.getGroupBy().accept(this); - } - - return false; - } - - public boolean visit(SQLSelectItem x) { - x.getExpr().accept(this); - - if ((x.getAlias() != null) && (x.getAlias().length() > 0)) { - print(" AS "); - print(x.getAlias()); - } - return false; - } - - public boolean visit(SQLOrderBy x) { - if (x.getItems().size() > 0) { - print("ORDER BY "); - - printAndAccept(x.getItems(), ", "); - } - return false; - } - - public boolean visit(SQLSelectOrderByItem x) { - x.getExpr().accept(this); - if (x.getType() != null) { - print(" "); - print(x.getType().name().toUpperCase()); - } - - if (x.getCollate() != null) { - print(" COLLATE "); - print(x.getCollate()); - } - - return false; - } - - public boolean visit(SQLExprTableSource x) { - x.getExpr().accept(this); - - if (x.getAlias() != null) { - print(' '); - print(x.getAlias()); - } - - return false; - } - - public boolean visit(SQLSelectStatement stmt) { - SQLSelect select = stmt.getSelect(); - - select.accept(this); - - return false; - } - - public boolean visit(SQLVariantRefExpr x) { - int index = x.getIndex(); - - if (parameters == null || index >= parameters.size()) { - print(x.getName()); - return false; - } - - Object param = parameters.get(index); - printParameter(param); - return false; - } - - public void printParameter(Object param) { - if (param == null) { - print("NULL"); - return; - } - - if (param instanceof Number // - || param instanceof Boolean) { - print(param.toString()); - return; - } - - if (param instanceof String) { - SQLCharExpr charExpr = new SQLCharExpr((String) param); - visit(charExpr); - return; - } - - if (param instanceof Date) { - print((Date) param); - return; - } - - if (param instanceof InputStream) { - print("'"); - return; - } - - if (param instanceof Reader) { - print("'"); - return; - } - - if (param instanceof Blob) { - print("'"); - return; - } - - if (param instanceof NClob) { - print("'"); - return; - } - - if (param instanceof Clob) { - print("'"); - return; - } - - print("'" + param.getClass().getName() + "'"); - } - - public boolean visit(SQLDropTableStatement x) { - print("DROP TABLE "); - printAndAccept(x.getTableSources(), ", "); - return false; - } - - public boolean visit(SQLDropViewStatement x) { - print("DROP VIEW "); - printAndAccept(x.getTableSources(), ", "); - return false; - } - - public boolean visit(SQLTableElement x) { - if (x instanceof SQLColumnDefinition) { - return visit((SQLColumnDefinition) x); - } - - throw new RuntimeException("TODO"); - } - - public boolean visit(SQLColumnDefinition x) { - x.getName().accept(this); - - if (x.getDataType() != null) { - print(' '); - x.getDataType().accept(this); - } - - if (x.getDefaultExpr() != null) { - visitColumnDefault(x); - } - - for (SQLColumnConstraint item : x.getConstaints()) { - print(' '); - item.accept(this); - } - - if (x.getEnable() != null) { - if (x.getEnable().booleanValue()) { - print(" ENABLE"); - } - } - - return false; - } - - protected void visitColumnDefault(SQLColumnDefinition x) { - print(" DEFAULT "); - x.getDefaultExpr().accept(this); - } - - public boolean visit(SQLDeleteStatement x) { - print("DELETE FROM "); - - x.getTableName().accept(this); - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - public boolean visit(SQLCurrentOfCursorExpr x) { - print("CURRENT OF "); - x.getCursorName().accept(this); - return false; - } - - public boolean visit(SQLInsertStatement x) { - print("INSERT INTO "); - - x.getTableSource().accept(this); - - if (x.getColumns().size() > 0) { - incrementIndent(); - println(); - print("("); - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - if (i % 5 == 0) { - println(); - } - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - decrementIndent(); - } - - if (x.getValues() != null) { - println(); - print("VALUES"); - println(); - x.getValues().accept(this); - } else { - if (x.getQuery() != null) { - println(); - x.getQuery().setParent(x); - x.getQuery().accept(this); - } - } - - return false; - } - - public boolean visit(SQLUpdateSetItem x) { - x.getColumn().accept(this); - print(" = "); - x.getValue().accept(this); - return false; - } - - public boolean visit(SQLUpdateStatement x) { - print("UPDATE "); - - x.getTableSource().accept(this); - - println(); - print("SET "); - for (int i = 0, size = x.getItems().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getItems().get(i).accept(this); - } - - if (x.getWhere() != null) { - println(); - print("WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - public boolean visit(SQLCreateTableStatement x) { - print("CREATE TABLE "); - if (SQLCreateTableStatement.Type.GLOBAL_TEMPORARY.equals(x.getType())) { - print("GLOBAL TEMPORARY "); - } else if (SQLCreateTableStatement.Type.LOCAL_TEMPORARY.equals(x.getType())) { - print("LOCAL TEMPORARY "); - } - - x.getName().accept(this); - - int size = x.getTableElementList().size(); - - if (size > 0) { - print(" ("); - incrementIndent(); - println(); - for (int i = 0; i < size; ++i) { - if (i != 0) { - print(","); - println(); - } - x.getTableElementList().get(i).accept(this); - } - decrementIndent(); - println(); - print(")"); - } - - return false; - } - - public boolean visit(SQLUniqueConstraint x) { - if (x.getName() != null) { - print("CONSTRAINT "); - x.getName().accept(this); - print(' '); - } - - print("UNIQUE ("); - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - return false; - } - - public boolean visit(NotNullConstraint x) { - print("NOT NULL"); - return false; - } - - @Override - public boolean visit(SQLUnionQuery x) { - x.getLeft().accept(this); - println(); - print(x.getOperator().name); - println(); - - boolean needParen = false; - - if (x.getOrderBy() != null) { - needParen = true; - } - - if (needParen) { - print('('); - x.getRight().accept(this); - print(')'); - } else { - x.getRight().accept(this); - } - - if (x.getOrderBy() != null) { - println(); - x.getOrderBy().accept(this); - } - - return false; - } - - @Override - public boolean visit(SQLUnaryExpr x) { - print(x.getOperator().name); - - SQLExpr expr = x.getExpr(); - - switch (x.getOperator()) { - case BINARY: - case Prior: - case ConnectByRoot: - print(' '); - expr.accept(this); - return false; - default: - break; - } - - if (expr instanceof SQLBinaryOpExpr) { - print('('); - expr.accept(this); - print(')'); - } else if (expr instanceof SQLUnaryExpr) { - print('('); - expr.accept(this); - print(')'); - } else { - expr.accept(this); - } - return false; - } - - @Override - public boolean visit(SQLHexExpr x) { - print("0x"); - print(x.getHex()); - - String charset = (String) x.getAttribute("USING"); - if (charset != null) { - print(" USING "); - print(charset); - } - - return false; - } - - @Override - public boolean visit(SQLSetStatement x) { - print("SET "); - printAndAccept(x.getItems(), ", "); - return false; - } - - @Override - public boolean visit(SQLAssignItem x) { - x.getTarget().accept(this); - print(" = "); - x.getValue().accept(this); - return false; - } - - @Override - public boolean visit(SQLCallStatement x) { - print("CALL "); - x.getProcedureName().accept(this); - print('('); - printAndAccept(x.getParameters(), ", "); - print(')'); - return false; - } - - @Override - public boolean visit(SQLJoinTableSource x) { - x.getLeft().accept(this); - incrementIndent(); - - if (x.getJoinType() == JoinType.COMMA) { - print(","); - } else { - println(); - print(JoinType.toString(x.getJoinType())); - } - print(" "); - x.getRight().accept(this); - - if (x.getCondition() != null) { - incrementIndent(); - print(" ON "); - x.getCondition().accept(this); - decrementIndent(); - } - - if (x.getUsing().size() > 0) { - print(" USING ("); - printAndAccept(x.getUsing(), ", "); - print(")"); - } - - if (x.getAlias() != null) { - print(" AS "); - print(x.getAlias()); - } - - decrementIndent(); - - return false; - } - - @Override - public boolean visit(ValuesClause x) { - print("("); - incrementIndent(); - for (int i = 0, size = x.getValues().size(); i < size; ++i) { - if (i != 0) { - if (i % 5 == 0) { - println(); - } - print(", "); - } - - SQLExpr expr = x.getValues().get(i); - expr.setParent(x); - expr.accept(this); - } - decrementIndent(); - print(")"); - return false; - } - - @Override - public boolean visit(SQLSomeExpr x) { - print("SOME ("); - - incrementIndent(); - x.getSubQuery().accept(this); - decrementIndent(); - print(")"); - return false; - } - - @Override - public boolean visit(SQLAnyExpr x) { - print("ANY ("); - - incrementIndent(); - x.getSubQuery().accept(this); - decrementIndent(); - print(")"); - return false; - } - - @Override - public boolean visit(SQLAllExpr x) { - print("ALL ("); - - incrementIndent(); - x.getSubQuery().accept(this); - decrementIndent(); - print(")"); - return false; - } - - @Override - public boolean visit(SQLInSubQueryExpr x) { - x.getExpr().accept(this); - if (x.isNot()) { - print(" NOT IN ("); - } else { - print(" IN ("); - } - - incrementIndent(); - x.getSubQuery().accept(this); - decrementIndent(); - print(")"); - - return false; - } - - @Override - public boolean visit(SQLListExpr x) { - print("("); - printAndAccept(x.getItems(), ", "); - print(")"); - - return false; - } - - @Override - public boolean visit(SQLSubqueryTableSource x) { - print("("); - incrementIndent(); - x.getSelect().accept(this); - println(); - decrementIndent(); - print(")"); - - if (x.getAlias() != null) { - print(' '); - print(x.getAlias()); - } - - return false; - } - - @Override - public boolean visit(SQLTruncateStatement x) { - print("TRUNCATE TABLE "); - printAndAccept(x.getTableSources(), ", "); - return false; - } - - @Override - public boolean visit(SQLDefaultExpr x) { - print("DEFAULT"); - return false; - } - - @Override - public void endVisit(SQLCommentStatement x) { - - } - - @Override - public boolean visit(SQLCommentStatement x) { - print("COMMENT ON "); - if (x.getType() != null) { - print(x.getType().name()); - print(" "); - } - x.getOn().accept(this); - - print(" IS "); - x.getComment().accept(this); - - return false; - } - - @Override - public boolean visit(SQLUseStatement x) { - print("USE "); - x.getDatabase().accept(this); - return false; - } - - @Override - public boolean visit(SQLAlterTableAddColumn x) { - print("ADD ("); - printAndAccept(x.getColumns(), ", "); - print(")"); - return false; - } - - @Override - public boolean visit(SQLAlterTableDropColumnItem x) { - print("DROP COLUMN "); - x.getColumnName().accept(this); - return false; - } - - @Override - public void endVisit(SQLAlterTableAddColumn x) { - - } - - @Override - public boolean visit(SQLDropIndexStatement x) { - print("DROP INDEX "); - x.getIndexName().accept(this); - print(" ON "); - x.getTableName().accept(this); - return false; - } - - @Override - public boolean visit(SQLSavePointStatement x) { - print("SAVEPOINT "); - x.getName().accept(this); - return false; - } - - @Override - public boolean visit(SQLReleaseSavePointStatement x) { - print("RELEASE SAVEPOINT "); - x.getName().accept(this); - return false; - } - - @Override - public boolean visit(SQLRollbackStatement x) { - print("ROLLBACK"); - if (x.getTo() != null) { - print(" TO "); - x.getTo().accept(this); - } - return false; - } - - public boolean visit(SQLCommentHint x) { - print("/*"); - print(x.getText()); - print("*/"); - return false; - } - - @Override - public boolean visit(SQLCreateDatabaseStatement x) { - print("CREATE DATABASE "); - x.getName().accept(this); - return false; - } - - @Override - public boolean visit(SQLAlterTableDropIndex x) { - print("DROP INDEX "); - x.getIndexName().accept(this); - return false; - } - - @Override - public boolean visit(SQLAlterTableAddPrimaryKey x) { - print("ADD "); - x.getPrimaryKey().accept(this); - return false; - } - - @Override - public boolean visit(SQLOver x) { - print("OVER ("); - if (x.getPartitionBy().size() > 0) { - printAndAccept(x.getPartitionBy(), ", "); - print(' '); - } - if (x.getOrderBy() != null) { - x.getOrderBy().accept(this); - } - print(")"); - return false; - } - - @Override - public boolean visit(SQLColumnPrimaryKey x) { - print(" PRIMARY KEY"); - return false; - } - - @Override - public boolean visit(SQLColumnCheck x) { - print("CHECK "); - x.getExpr().accept(this); - return false; - } - - @Override - public boolean visit(SQLColumnUniqueIndex x) { - print(" UNIQUE INDEX"); - return false; - } - - @Override - public boolean visit(SQLWithSubqueryClause x) { - print("WITH"); - if (x.getRecursive() == Boolean.TRUE) { - print(" RECURSIVE"); - } - incrementIndent(); - println(); - printlnAndAccept(x.getEntries(), ", "); - decrementIndent(); - return false; - } - - @Override - public boolean visit(SQLWithSubqueryClause.Entry x) { - x.getName().accept(this); - - if (x.getColumns().size() > 0) { - print(" ("); - printAndAccept(x.getColumns(), ", "); - print(")"); - } - println(); - print("AS"); - println(); - print("("); - incrementIndent(); - println(); - x.getSubQuery().accept(this); - decrementIndent(); - println(); - print(")"); - - return false; - } - - @Override - public boolean visit(SQLAlterTableAlterColumn x) { - print("ALTER COLUMN "); - x.getColumn().accept(this); - return false; - } - - @Override - public boolean visit(SQLCheck x) { - if (x.getName() != null) { - print("CONSTRAINT "); - x.getName().accept(this); - print(' '); - } - print("CHECK ("); - x.getExpr().accept(this); - print(')'); - return false; - } - - @Override - public boolean visit(SQLAlterTableDropForeinKey x) { - print("DROP FOREIGN KEY "); - x.getIndexName().accept(this); - return false; - } - - @Override - public boolean visit(SQLAlterTableDropPrimaryKey x) { - print("DROP PRIMARY KEY"); - return false; - } - - @Override - public boolean visit(SQLAlterTableEnableKeys x) { - print("ENABLE KEYS"); - return false; - } - - @Override - public boolean visit(SQLAlterTableDisableKeys x) { - print("DISABLE KEYS"); - return false; - } - - @Override - public boolean visit(SQLAlterTableAddForeignKey x) { - print("ADD "); - x.getForeignKey().accept(this); - return false; - } - - public boolean visit(SQLAlterTableDisableConstraint x) { - print("DISABLE CONSTRAINT "); - x.getConstraintName().accept(this); - return false; - } - - public boolean visit(SQLAlterTableEnableConstraint x) { - print("ENABLE CONSTRAINT "); - x.getConstraintName().accept(this); - return false; - } - - @Override - public boolean visit(SQLAlterTableDropConstraint x) { - print("DROP CONSTRAINT "); - x.getConstraintName().accept(this); - return false; - } - - @Override - public boolean visit(SQLAlterTableStatement x) { - print("ALTER TABLE "); - x.getName().accept(this); - incrementIndent(); - for (int i = 0; i < x.getItems().size(); ++i) { - SQLAlterTableItem item = x.getItems().get(i); - if (i != 0) { - print(','); - } - println(); - item.accept(this); - } - decrementIndent(); - return false; - } - - @Override - public boolean visit(SQLExprHint x) { - x.getExpr().accept(this); - return false; - } -} +/* + * 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.visitor; + +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.NClob; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import com.alibaba.druid.sql.ast.SQLCommentHint; +import com.alibaba.druid.sql.ast.SQLDataType; +import com.alibaba.druid.sql.ast.SQLExpr; +import com.alibaba.druid.sql.ast.SQLObject; +import com.alibaba.druid.sql.ast.SQLOrderBy; +import com.alibaba.druid.sql.ast.SQLOver; +import com.alibaba.druid.sql.ast.SQLSetQuantifier; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr; +import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr; +import com.alibaba.druid.sql.ast.expr.SQLAllExpr; +import com.alibaba.druid.sql.ast.expr.SQLAnyExpr; +import com.alibaba.druid.sql.ast.expr.SQLBetweenExpr; +import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; +import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator; +import com.alibaba.druid.sql.ast.expr.SQLCaseExpr; +import com.alibaba.druid.sql.ast.expr.SQLCastExpr; +import com.alibaba.druid.sql.ast.expr.SQLCharExpr; +import com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr; +import com.alibaba.druid.sql.ast.expr.SQLDefaultExpr; +import com.alibaba.druid.sql.ast.expr.SQLExistsExpr; +import com.alibaba.druid.sql.ast.expr.SQLHexExpr; +import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; +import com.alibaba.druid.sql.ast.expr.SQLInListExpr; +import com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr; +import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr; +import com.alibaba.druid.sql.ast.expr.SQLListExpr; +import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr; +import com.alibaba.druid.sql.ast.expr.SQLNCharExpr; +import com.alibaba.druid.sql.ast.expr.SQLNotExpr; +import com.alibaba.druid.sql.ast.expr.SQLNullExpr; +import com.alibaba.druid.sql.ast.expr.SQLNumberExpr; +import com.alibaba.druid.sql.ast.expr.SQLObjectCreateExpr; +import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; +import com.alibaba.druid.sql.ast.expr.SQLQueryExpr; +import com.alibaba.druid.sql.ast.expr.SQLSomeExpr; +import com.alibaba.druid.sql.ast.expr.SQLUnaryExpr; +import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr; +import com.alibaba.druid.sql.ast.statement.NotNullConstraint; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddColumn; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddForeignKey; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddPrimaryKey; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableConstraint; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableKeys; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropColumnItem; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropForeinKey; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropPrimaryKey; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableKeys; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableItem; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement; +import com.alibaba.druid.sql.ast.statement.SQLAssignItem; +import com.alibaba.druid.sql.ast.statement.SQLCallStatement; +import com.alibaba.druid.sql.ast.statement.SQLCharactorDataType; +import com.alibaba.druid.sql.ast.statement.SQLCheck; +import com.alibaba.druid.sql.ast.statement.SQLColumnCheck; +import com.alibaba.druid.sql.ast.statement.SQLColumnConstraint; +import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition; +import com.alibaba.druid.sql.ast.statement.SQLColumnPrimaryKey; +import com.alibaba.druid.sql.ast.statement.SQLColumnUniqueIndex; +import com.alibaba.druid.sql.ast.statement.SQLCommentStatement; +import com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement; +import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement; +import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement; +import com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement; +import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement; +import com.alibaba.druid.sql.ast.statement.SQLDropViewStatement; +import com.alibaba.druid.sql.ast.statement.SQLExprHint; +import com.alibaba.druid.sql.ast.statement.SQLExprTableSource; +import com.alibaba.druid.sql.ast.statement.SQLInsertStatement; +import com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause; +import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; +import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource.JoinType; +import com.alibaba.druid.sql.ast.statement.SQLReleaseSavePointStatement; +import com.alibaba.druid.sql.ast.statement.SQLRollbackStatement; +import com.alibaba.druid.sql.ast.statement.SQLSavePointStatement; +import com.alibaba.druid.sql.ast.statement.SQLSelect; +import com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause; +import com.alibaba.druid.sql.ast.statement.SQLSelectItem; +import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem; +import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; +import com.alibaba.druid.sql.ast.statement.SQLSelectStatement; +import com.alibaba.druid.sql.ast.statement.SQLSetStatement; +import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource; +import com.alibaba.druid.sql.ast.statement.SQLTableElement; +import com.alibaba.druid.sql.ast.statement.SQLTruncateStatement; +import com.alibaba.druid.sql.ast.statement.SQLUnionQuery; +import com.alibaba.druid.sql.ast.statement.SQLUniqueConstraint; +import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem; +import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement; +import com.alibaba.druid.sql.ast.statement.SQLUseStatement; +import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause; + +public class SQLASTOutputVisitor extends SQLASTVisitorAdapter { + + protected final Appendable appender; + private String indent = "\t"; + private int indentCount = 0; + private boolean prettyFormat = true; + + private List parameters; + + public SQLASTOutputVisitor(Appendable appender){ + this.appender = appender; + } + + public int getParametersSize() { + if (parameters == null) { + return 0; + } + + return this.parameters.size(); + } + + public List getParameters() { + if (parameters == null) { + parameters = new ArrayList(); + } + + return parameters; + } + + public void setParameters(List parameters) { + this.parameters = parameters; + } + + public int getIndentCount() { + return indentCount; + } + + public Appendable getAppender() { + return appender; + } + + public boolean isPrettyFormat() { + return prettyFormat; + } + + public void setPrettyFormat(boolean prettyFormat) { + this.prettyFormat = prettyFormat; + } + + public void decrementIndent() { + this.indentCount -= 1; + } + + public void incrementIndent() { + this.indentCount += 1; + } + + public void print(char value) { + try { + this.appender.append(value); + } catch (IOException e) { + throw new RuntimeException("println error", e); + } + } + + public void print(int value) { + print(Integer.toString(value)); + } + + public void print(Date date) { + SimpleDateFormat dateFormat; + if (date instanceof java.sql.Timestamp) { + dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + } else { + dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + } + print("'" + dateFormat.format(date) + "'"); + } + + public void print(long value) { + print(Long.toString(value)); + } + + public void print(String text) { + try { + this.appender.append(text); + } catch (IOException e) { + throw new RuntimeException("println error", e); + } + } + + protected void printAlias(String alias) { + if ((alias != null) && (alias.length() > 0)) { + print(" "); + print(alias); + } + } + + protected void printAndAccept(List nodes, String seperator) { + for (int i = 0, size = nodes.size(); i < size; ++i) { + if (i != 0) { + print(seperator); + } + nodes.get(i).accept(this); + } + } + + protected void printSelectList(List selectList) { + incrementIndent(); + for (int i = 0, size = selectList.size(); i < size; ++i) { + if (i != 0) { + if (i % 5 == 0) { + println(); + } + + print(", "); + } + + selectList.get(i).accept(this); + } + decrementIndent(); + } + + protected void printlnAndAccept(List nodes, String seperator) { + for (int i = 0, size = nodes.size(); i < size; ++i) { + if (i != 0) { + println(seperator); + } + + ((SQLObject) nodes.get(i)).accept(this); + } + } + + public void printIndent() { + for (int i = 0; i < this.indentCount; ++i) { + print(this.indent); + } + } + + public void println() { + if (!isPrettyFormat()) { + print(' '); + return; + } + + print("\n"); + printIndent(); + } + + public void println(String text) { + print(text); + println(); + } + + // //////////////////// + + public boolean visit(SQLBetweenExpr x) { + x.getTestExpr().accept(this); + + if (x.isNot()) { + print(" NOT BETWEEN "); + } else { + print(" BETWEEN "); + } + + x.getBeginExpr().accept(this); + print(" AND "); + x.getEndExpr().accept(this); + + return false; + } + + public boolean visit(SQLBinaryOpExpr x) { + SQLObject parent = x.getParent(); + boolean isRoot = parent instanceof SQLSelectQueryBlock; + boolean relational = x.getOperator() == SQLBinaryOperator.BooleanAnd + || x.getOperator() == SQLBinaryOperator.BooleanOr; + + if (isRoot && relational) { + incrementIndent(); + } + + List groupList = new ArrayList(); + SQLExpr left = x.getLeft(); + for (;;) { + if (left instanceof SQLBinaryOpExpr && ((SQLBinaryOpExpr) left).getOperator() == x.getOperator()) { + SQLBinaryOpExpr binaryLeft = (SQLBinaryOpExpr) left; + groupList.add(binaryLeft.getRight()); + left = binaryLeft.getLeft(); + } else { + groupList.add(left); + break; + } + } + + for (int i = groupList.size() - 1; i >= 0; --i) { + SQLExpr item = groupList.get(i); + visitBinaryLeft(item, x.getOperator()); + + if (relational) { + println(); + } else { + print(" "); + } + print(x.getOperator().name); + print(" "); + } + + visitorBinaryRight(x); + + if (isRoot && relational) { + decrementIndent(); + } + + return false; + } + + private void visitorBinaryRight(SQLBinaryOpExpr x) { + if (x.getRight() instanceof SQLBinaryOpExpr) { + SQLBinaryOpExpr right = (SQLBinaryOpExpr) x.getRight(); + boolean rightRational = right.getOperator() == SQLBinaryOperator.BooleanAnd + || right.getOperator() == SQLBinaryOperator.BooleanOr; + + if (right.getOperator().priority >= x.getOperator().priority) { + if (rightRational) { + incrementIndent(); + } + + print('('); + right.accept(this); + print(')'); + + if (rightRational) { + decrementIndent(); + } + } else { + right.accept(this); + } + } else { + x.getRight().accept(this); + } + } + + private void visitBinaryLeft(SQLExpr left, SQLBinaryOperator op) { + if (left instanceof SQLBinaryOpExpr) { + SQLBinaryOpExpr binaryLeft = (SQLBinaryOpExpr) left; + boolean leftRational = binaryLeft.getOperator() == SQLBinaryOperator.BooleanAnd + || binaryLeft.getOperator() == SQLBinaryOperator.BooleanOr; + + if (binaryLeft.getOperator().priority > op.priority) { + if (leftRational) { + incrementIndent(); + } + print('('); + left.accept(this); + print(')'); + + if (leftRational) { + decrementIndent(); + } + } else { + left.accept(this); + } + } else { + left.accept(this); + } + } + + public boolean visit(SQLCaseExpr x) { + print("CASE "); + if (x.getValueExpr() != null) { + x.getValueExpr().accept(this); + print(" "); + } + + printAndAccept(x.getItems(), " "); + + if (x.getElseExpr() != null) { + print(" ELSE "); + x.getElseExpr().accept(this); + } + + print(" END"); + return false; + } + + public boolean visit(SQLCaseExpr.Item x) { + print("WHEN "); + x.getConditionExpr().accept(this); + print(" THEN "); + x.getValueExpr().accept(this); + return false; + } + + public boolean visit(SQLCastExpr x) { + print("CAST("); + x.getExpr().accept(this); + print(" AS "); + x.getDataType().accept(this); + print(")"); + + return false; + } + + public boolean visit(SQLCharExpr x) { + if ((x.getText() == null) || (x.getText().length() == 0)) { + print("NULL"); + } else { + print("'"); + print(x.getText().replaceAll("'", "''")); + print("'"); + } + + return false; + } + + public boolean visit(SQLDataType x) { + print(x.getName()); + if (x.getArguments().size() > 0) { + print("("); + printAndAccept(x.getArguments(), ", "); + print(")"); + } + + return false; + } + + public boolean visit(SQLCharactorDataType x) { + visit((SQLDataType) x); + return false; + } + + public boolean visit(SQLExistsExpr x) { + if (x.isNot()) { + print("NOT EXISTS ("); + } else { + print("EXISTS ("); + } + incrementIndent(); + x.getSubQuery().accept(this); + decrementIndent(); + print(")"); + return false; + } + + public boolean visit(SQLIdentifierExpr x) { + print(x.getName()); + return false; + } + + public boolean visit(SQLInListExpr x) { + x.getExpr().accept(this); + + if (x.isNot()) { + print(" NOT IN ("); + } else { + print(" IN ("); + } + + printAndAccept(x.getTargetList(), ", "); + print(')'); + return false; + } + + public boolean visit(SQLIntegerExpr x) { + print(x.getNumber().toString()); + return false; + } + + public boolean visit(SQLMethodInvokeExpr x) { + if (x.getOwner() != null) { + x.getOwner().accept(this); + print("."); + } + print(x.getMethodName()); + print("("); + printAndAccept(x.getParameters(), ", "); + print(")"); + return false; + } + + public boolean visit(SQLAggregateExpr x) { + print(x.getMethodName()); + print("("); + + if (x.getOption() != null) { + print(x.getOption().toString()); + print(' '); + } + + printAndAccept(x.getArguments(), ", "); + print(")"); + + if (x.getOver() != null) { + print(" "); + x.getOver().accept(this); + } + return false; + } + + public boolean visit(SQLAllColumnExpr x) { + print("*"); + return true; + } + + public boolean visit(SQLNCharExpr x) { + if ((x.getText() == null) || (x.getText().length() == 0)) { + print("NULL"); + } else { + print("N'"); + print(x.getText().replace("'", "''")); + print("'"); + } + return false; + } + + public boolean visit(SQLNotExpr x) { + print("NOT "); + x.getExpr().accept(this); + return false; + } + + public boolean visit(SQLNullExpr x) { + print("NULL"); + return false; + } + + public boolean visit(SQLNumberExpr x) { + print(x.getNumber().toString()); + return false; + } + + public boolean visit(SQLObjectCreateExpr x) { + throw new UnsupportedOperationException(); + } + + public boolean visit(SQLPropertyExpr x) { + x.getOwner().accept(this); + print("."); + print(x.getName()); + return false; + } + + public boolean visit(SQLQueryExpr x) { + SQLObject parent = x.getParent(); + if (parent instanceof SQLSelect) { + parent = parent.getParent(); + } + + if (parent instanceof SQLStatement) { + incrementIndent(); + + println(); + x.getSubQuery().accept(this); + + decrementIndent(); + } else if (parent instanceof ValuesClause) { + println(); + x.getSubQuery().accept(this); + println(); + } else { + print("("); + incrementIndent(); + println(); + x.getSubQuery().accept(this); + println(); + decrementIndent(); + print(")"); + } + return false; + } + + public boolean visit(SQLSelectGroupByClause x) { + if (x.getItems().size() > 0) { + print("GROUP BY "); + printAndAccept(x.getItems(), ", "); + } + + if (x.getHaving() != null) { + println(); + print("HAVING "); + x.getHaving().accept(this); + } + return false; + } + + public boolean visit(SQLSelect x) { + x.getQuery().setParent(x); + + if (x.getWithSubQuery() != null) { + x.getWithSubQuery().accept(this); + println(); + } + + x.getQuery().accept(this); + + if (x.getOrderBy() != null) { + println(); + x.getOrderBy().accept(this); + } + + return false; + } + + public boolean visit(SQLSelectQueryBlock x) { + print("SELECT "); + + if (SQLSetQuantifier.ALL == x.getDistionOption()) { + print("ALL "); + } else if (SQLSetQuantifier.DISTINCT == x.getDistionOption()) { + print("DISTINCT "); + } else if (SQLSetQuantifier.UNIQUE == x.getDistionOption()) { + print("UNIQUE "); + } + + printSelectList(x.getSelectList()); + + if (x.getFrom() != null) { + println(); + print("FROM "); + x.getFrom().accept(this); + } + + if (x.getWhere() != null) { + println(); + print("WHERE "); + x.getWhere().setParent(x); + x.getWhere().accept(this); + } + + if (x.getGroupBy() != null) { + println(); + x.getGroupBy().accept(this); + } + + return false; + } + + public boolean visit(SQLSelectItem x) { + x.getExpr().accept(this); + + if ((x.getAlias() != null) && (x.getAlias().length() > 0)) { + print(" AS "); + print(x.getAlias()); + } + return false; + } + + public boolean visit(SQLOrderBy x) { + if (x.getItems().size() > 0) { + print("ORDER BY "); + + printAndAccept(x.getItems(), ", "); + } + return false; + } + + public boolean visit(SQLSelectOrderByItem x) { + x.getExpr().accept(this); + if (x.getType() != null) { + print(" "); + print(x.getType().name().toUpperCase()); + } + + if (x.getCollate() != null) { + print(" COLLATE "); + print(x.getCollate()); + } + + return false; + } + + public boolean visit(SQLExprTableSource x) { + x.getExpr().accept(this); + + if (x.getAlias() != null) { + print(' '); + print(x.getAlias()); + } + + return false; + } + + public boolean visit(SQLSelectStatement stmt) { + SQLSelect select = stmt.getSelect(); + + select.accept(this); + + return false; + } + + public boolean visit(SQLVariantRefExpr x) { + int index = x.getIndex(); + + if (parameters == null || index >= parameters.size()) { + print(x.getName()); + return false; + } + + Object param = parameters.get(index); + printParameter(param); + return false; + } + + public void printParameter(Object param) { + if (param == null) { + print("NULL"); + return; + } + + if (param instanceof Number // + || param instanceof Boolean) { + print(param.toString()); + return; + } + + if (param instanceof String) { + SQLCharExpr charExpr = new SQLCharExpr((String) param); + visit(charExpr); + return; + } + + if (param instanceof Date) { + print((Date) param); + return; + } + + if (param instanceof InputStream) { + print("'"); + return; + } + + if (param instanceof Reader) { + print("'"); + return; + } + + if (param instanceof Blob) { + print("'"); + return; + } + + if (param instanceof NClob) { + print("'"); + return; + } + + if (param instanceof Clob) { + print("'"); + return; + } + + print("'" + param.getClass().getName() + "'"); + } + + public boolean visit(SQLDropTableStatement x) { + print("DROP TABLE "); + printAndAccept(x.getTableSources(), ", "); + return false; + } + + public boolean visit(SQLDropViewStatement x) { + print("DROP VIEW "); + printAndAccept(x.getTableSources(), ", "); + return false; + } + + public boolean visit(SQLTableElement x) { + if (x instanceof SQLColumnDefinition) { + return visit((SQLColumnDefinition) x); + } + + throw new RuntimeException("TODO"); + } + + public boolean visit(SQLColumnDefinition x) { + x.getName().accept(this); + + if (x.getDataType() != null) { + print(' '); + x.getDataType().accept(this); + } + + if (x.getDefaultExpr() != null) { + visitColumnDefault(x); + } + + for (SQLColumnConstraint item : x.getConstaints()) { + print(' '); + item.accept(this); + } + + if (x.getEnable() != null) { + if (x.getEnable().booleanValue()) { + print(" ENABLE"); + } + } + + return false; + } + + protected void visitColumnDefault(SQLColumnDefinition x) { + print(" DEFAULT "); + x.getDefaultExpr().accept(this); + } + + public boolean visit(SQLDeleteStatement x) { + print("DELETE FROM "); + + x.getTableName().accept(this); + + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().setParent(x); + x.getWhere().accept(this); + } + + return false; + } + + public boolean visit(SQLCurrentOfCursorExpr x) { + print("CURRENT OF "); + x.getCursorName().accept(this); + return false; + } + + public boolean visit(SQLInsertStatement x) { + print("INSERT INTO "); + + x.getTableSource().accept(this); + + if (x.getColumns().size() > 0) { + incrementIndent(); + println(); + print("("); + for (int i = 0, size = x.getColumns().size(); i < size; ++i) { + if (i != 0) { + if (i % 5 == 0) { + println(); + } + print(", "); + } + x.getColumns().get(i).accept(this); + } + print(")"); + decrementIndent(); + } + + if (x.getValues() != null) { + println(); + print("VALUES"); + println(); + x.getValues().accept(this); + } else { + if (x.getQuery() != null) { + println(); + x.getQuery().setParent(x); + x.getQuery().accept(this); + } + } + + return false; + } + + public boolean visit(SQLUpdateSetItem x) { + x.getColumn().accept(this); + print(" = "); + x.getValue().accept(this); + return false; + } + + public boolean visit(SQLUpdateStatement x) { + print("UPDATE "); + + x.getTableSource().accept(this); + + println(); + print("SET "); + for (int i = 0, size = x.getItems().size(); i < size; ++i) { + if (i != 0) { + print(", "); + } + x.getItems().get(i).accept(this); + } + + if (x.getWhere() != null) { + println(); + print("WHERE "); + x.getWhere().setParent(x); + x.getWhere().accept(this); + } + + return false; + } + + public boolean visit(SQLCreateTableStatement x) { + print("CREATE TABLE "); + if (SQLCreateTableStatement.Type.GLOBAL_TEMPORARY.equals(x.getType())) { + print("GLOBAL TEMPORARY "); + } else if (SQLCreateTableStatement.Type.LOCAL_TEMPORARY.equals(x.getType())) { + print("LOCAL TEMPORARY "); + } + + x.getName().accept(this); + + int size = x.getTableElementList().size(); + + if (size > 0) { + print(" ("); + incrementIndent(); + println(); + for (int i = 0; i < size; ++i) { + if (i != 0) { + print(","); + println(); + } + x.getTableElementList().get(i).accept(this); + } + decrementIndent(); + println(); + print(")"); + } + + return false; + } + + public boolean visit(SQLUniqueConstraint x) { + if (x.getName() != null) { + print("CONSTRAINT "); + x.getName().accept(this); + print(' '); + } + + print("UNIQUE ("); + for (int i = 0, size = x.getColumns().size(); i < size; ++i) { + if (i != 0) { + print(", "); + } + x.getColumns().get(i).accept(this); + } + print(")"); + return false; + } + + public boolean visit(NotNullConstraint x) { + print("NOT NULL"); + return false; + } + + @Override + public boolean visit(SQLUnionQuery x) { + x.getLeft().accept(this); + println(); + print(x.getOperator().name); + println(); + + boolean needParen = false; + + if (x.getOrderBy() != null) { + needParen = true; + } + + if (needParen) { + print('('); + x.getRight().accept(this); + print(')'); + } else { + x.getRight().accept(this); + } + + if (x.getOrderBy() != null) { + println(); + x.getOrderBy().accept(this); + } + + return false; + } + + @Override + public boolean visit(SQLUnaryExpr x) { + print(x.getOperator().name); + + SQLExpr expr = x.getExpr(); + + switch (x.getOperator()) { + case BINARY: + case Prior: + case ConnectByRoot: + print(' '); + expr.accept(this); + return false; + default: + break; + } + + if (expr instanceof SQLBinaryOpExpr) { + print('('); + expr.accept(this); + print(')'); + } else if (expr instanceof SQLUnaryExpr) { + print('('); + expr.accept(this); + print(')'); + } else { + expr.accept(this); + } + return false; + } + + @Override + public boolean visit(SQLHexExpr x) { + print("0x"); + print(x.getHex()); + + String charset = (String) x.getAttribute("USING"); + if (charset != null) { + print(" USING "); + print(charset); + } + + return false; + } + + @Override + public boolean visit(SQLSetStatement x) { + print("SET "); + printAndAccept(x.getItems(), ", "); + return false; + } + + @Override + public boolean visit(SQLAssignItem x) { + x.getTarget().accept(this); + print(" = "); + x.getValue().accept(this); + return false; + } + + @Override + public boolean visit(SQLCallStatement x) { + print("CALL "); + x.getProcedureName().accept(this); + print('('); + printAndAccept(x.getParameters(), ", "); + print(')'); + return false; + } + + @Override + public boolean visit(SQLJoinTableSource x) { + x.getLeft().accept(this); + incrementIndent(); + + if (x.getJoinType() == JoinType.COMMA) { + print(","); + } else { + println(); + print(JoinType.toString(x.getJoinType())); + } + print(" "); + x.getRight().accept(this); + + if (x.getCondition() != null) { + incrementIndent(); + print(" ON "); + x.getCondition().accept(this); + decrementIndent(); + } + + if (x.getUsing().size() > 0) { + print(" USING ("); + printAndAccept(x.getUsing(), ", "); + print(")"); + } + + if (x.getAlias() != null) { + print(" AS "); + print(x.getAlias()); + } + + decrementIndent(); + + return false; + } + + @Override + public boolean visit(ValuesClause x) { + print("("); + incrementIndent(); + for (int i = 0, size = x.getValues().size(); i < size; ++i) { + if (i != 0) { + if (i % 5 == 0) { + println(); + } + print(", "); + } + + SQLExpr expr = x.getValues().get(i); + expr.setParent(x); + expr.accept(this); + } + decrementIndent(); + print(")"); + return false; + } + + @Override + public boolean visit(SQLSomeExpr x) { + print("SOME ("); + + incrementIndent(); + x.getSubQuery().accept(this); + decrementIndent(); + print(")"); + return false; + } + + @Override + public boolean visit(SQLAnyExpr x) { + print("ANY ("); + + incrementIndent(); + x.getSubQuery().accept(this); + decrementIndent(); + print(")"); + return false; + } + + @Override + public boolean visit(SQLAllExpr x) { + print("ALL ("); + + incrementIndent(); + x.getSubQuery().accept(this); + decrementIndent(); + print(")"); + return false; + } + + @Override + public boolean visit(SQLInSubQueryExpr x) { + x.getExpr().accept(this); + if (x.isNot()) { + print(" NOT IN ("); + } else { + print(" IN ("); + } + + incrementIndent(); + x.getSubQuery().accept(this); + decrementIndent(); + print(")"); + + return false; + } + + @Override + public boolean visit(SQLListExpr x) { + print("("); + printAndAccept(x.getItems(), ", "); + print(")"); + + return false; + } + + @Override + public boolean visit(SQLSubqueryTableSource x) { + print("("); + incrementIndent(); + x.getSelect().accept(this); + println(); + decrementIndent(); + print(")"); + + if (x.getAlias() != null) { + print(' '); + print(x.getAlias()); + } + + return false; + } + + @Override + public boolean visit(SQLTruncateStatement x) { + print("TRUNCATE TABLE "); + printAndAccept(x.getTableSources(), ", "); + return false; + } + + @Override + public boolean visit(SQLDefaultExpr x) { + print("DEFAULT"); + return false; + } + + @Override + public void endVisit(SQLCommentStatement x) { + + } + + @Override + public boolean visit(SQLCommentStatement x) { + print("COMMENT ON "); + if (x.getType() != null) { + print(x.getType().name()); + print(" "); + } + x.getOn().accept(this); + + print(" IS "); + x.getComment().accept(this); + + return false; + } + + @Override + public boolean visit(SQLUseStatement x) { + print("USE "); + x.getDatabase().accept(this); + return false; + } + + @Override + public boolean visit(SQLAlterTableAddColumn x) { + print("ADD ("); + printAndAccept(x.getColumns(), ", "); + print(")"); + return false; + } + + @Override + public boolean visit(SQLAlterTableDropColumnItem x) { + print("DROP COLUMN "); + x.getColumnName().accept(this); + return false; + } + + @Override + public void endVisit(SQLAlterTableAddColumn x) { + + } + + @Override + public boolean visit(SQLDropIndexStatement x) { + print("DROP INDEX "); + x.getIndexName().accept(this); + print(" ON "); + x.getTableName().accept(this); + return false; + } + + @Override + public boolean visit(SQLSavePointStatement x) { + print("SAVEPOINT "); + x.getName().accept(this); + return false; + } + + @Override + public boolean visit(SQLReleaseSavePointStatement x) { + print("RELEASE SAVEPOINT "); + x.getName().accept(this); + return false; + } + + @Override + public boolean visit(SQLRollbackStatement x) { + print("ROLLBACK"); + if (x.getTo() != null) { + print(" TO "); + x.getTo().accept(this); + } + return false; + } + + public boolean visit(SQLCommentHint x) { + print("/*"); + print(x.getText()); + print("*/"); + return false; + } + + @Override + public boolean visit(SQLCreateDatabaseStatement x) { + print("CREATE DATABASE "); + x.getName().accept(this); + return false; + } + + @Override + public boolean visit(SQLAlterTableDropIndex x) { + print("DROP INDEX "); + x.getIndexName().accept(this); + return false; + } + + @Override + public boolean visit(SQLAlterTableAddPrimaryKey x) { + print("ADD "); + x.getPrimaryKey().accept(this); + return false; + } + + @Override + public boolean visit(SQLOver x) { + print("OVER ("); + if (x.getPartitionBy().size() > 0) { + printAndAccept(x.getPartitionBy(), ", "); + print(' '); + } + if (x.getOrderBy() != null) { + x.getOrderBy().accept(this); + } + print(")"); + return false; + } + + @Override + public boolean visit(SQLColumnPrimaryKey x) { + print(" PRIMARY KEY"); + return false; + } + + @Override + public boolean visit(SQLColumnCheck x) { + print("CHECK "); + x.getExpr().accept(this); + return false; + } + + @Override + public boolean visit(SQLColumnUniqueIndex x) { + print(" UNIQUE INDEX"); + return false; + } + + @Override + public boolean visit(SQLWithSubqueryClause x) { + print("WITH"); + if (x.getRecursive() == Boolean.TRUE) { + print(" RECURSIVE"); + } + incrementIndent(); + println(); + printlnAndAccept(x.getEntries(), ", "); + decrementIndent(); + return false; + } + + @Override + public boolean visit(SQLWithSubqueryClause.Entry x) { + x.getName().accept(this); + + if (x.getColumns().size() > 0) { + print(" ("); + printAndAccept(x.getColumns(), ", "); + print(")"); + } + println(); + print("AS"); + println(); + print("("); + incrementIndent(); + println(); + x.getSubQuery().accept(this); + decrementIndent(); + println(); + print(")"); + + return false; + } + + @Override + public boolean visit(SQLAlterTableAlterColumn x) { + print("ALTER COLUMN "); + x.getColumn().accept(this); + return false; + } + + @Override + public boolean visit(SQLCheck x) { + if (x.getName() != null) { + print("CONSTRAINT "); + x.getName().accept(this); + print(' '); + } + print("CHECK ("); + x.getExpr().accept(this); + print(')'); + return false; + } + + @Override + public boolean visit(SQLAlterTableDropForeinKey x) { + print("DROP FOREIGN KEY "); + x.getIndexName().accept(this); + return false; + } + + @Override + public boolean visit(SQLAlterTableDropPrimaryKey x) { + print("DROP PRIMARY KEY"); + return false; + } + + @Override + public boolean visit(SQLAlterTableEnableKeys x) { + print("ENABLE KEYS"); + return false; + } + + @Override + public boolean visit(SQLAlterTableDisableKeys x) { + print("DISABLE KEYS"); + return false; + } + + @Override + public boolean visit(SQLAlterTableAddForeignKey x) { + print("ADD "); + x.getForeignKey().accept(this); + return false; + } + + public boolean visit(SQLAlterTableDisableConstraint x) { + print("DISABLE CONSTRAINT "); + x.getConstraintName().accept(this); + return false; + } + + public boolean visit(SQLAlterTableEnableConstraint x) { + print("ENABLE CONSTRAINT "); + x.getConstraintName().accept(this); + return false; + } + + @Override + public boolean visit(SQLAlterTableDropConstraint x) { + print("DROP CONSTRAINT "); + x.getConstraintName().accept(this); + return false; + } + + @Override + public boolean visit(SQLAlterTableStatement x) { + print("ALTER TABLE "); + x.getName().accept(this); + incrementIndent(); + for (int i = 0; i < x.getItems().size(); ++i) { + SQLAlterTableItem item = x.getItems().get(i); + if (i != 0) { + print(','); + } + println(); + item.accept(this); + } + decrementIndent(); + return false; + } + + @Override + public boolean visit(SQLExprHint x) { + x.getExpr().accept(this); + return false; + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest3.java b/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest3.java index cd23cccb21..91f0a36075 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest3.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest3.java @@ -1,45 +1,45 @@ -/* - * 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.pool.basic; - -import java.sql.SQLException; - -import junit.framework.TestCase; - -import com.alibaba.druid.mock.MockResultSet; -import com.alibaba.druid.pool.DruidPooledStatement; - -public class PoolableStatementTest3 extends TestCase { - - public void test_clearResultSetError() throws Exception { - final MockResultSet rs = new MockResultSet(null) { - - public void close() throws SQLException { - throw new SQLException(); - } - }; - - DruidPooledStatement stmt = new DruidPooledStatement(null, null) { - - public void close() throws SQLException { - resultSetTrace.add(rs); - clearResultSet(); - } - }; - stmt.close(); - - } -} +/* + * 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.pool.basic; + +import java.sql.SQLException; + +import junit.framework.TestCase; + +import com.alibaba.druid.mock.MockResultSet; +import com.alibaba.druid.pool.DruidPooledStatement; + +public class PoolableStatementTest3 extends TestCase { + + public void test_clearResultSetError() throws Exception { + final MockResultSet rs = new MockResultSet(null) { + + public void close() throws SQLException { + throw new SQLException(); + } + }; + + DruidPooledStatement stmt = new DruidPooledStatement(null, null) { + + public void close() throws SQLException { + addResultSetTrace(rs); + clearResultSet(); + } + }; + stmt.close(); + + } +}