diff --git a/agent/src/main/java/com/fanruan/agent/jdbc/AgentArray.java b/agent/src/main/java/com/fanruan/agent/jdbc/AgentArray.java index c87a17c..bd226ae 100644 --- a/agent/src/main/java/com/fanruan/agent/jdbc/AgentArray.java +++ b/agent/src/main/java/com/fanruan/agent/jdbc/AgentArray.java @@ -1,5 +1,6 @@ package com.fanruan.agent.jdbc; +import com.fanruan.agent.jdbc.resultset.AgentResultSet; import com.fanruan.annotation.BindClass; import java.sql.Array; @@ -51,22 +52,22 @@ public class AgentArray implements Array { @Override public ResultSet getResultSet() throws SQLException { - return array.getResultSet(); + return new AgentResultSet(array.getResultSet()); } @Override public ResultSet getResultSet(Map> map) throws SQLException { - return array.getResultSet(map); + return new AgentResultSet(array.getResultSet(map)); } @Override public ResultSet getResultSet(long index, int count) throws SQLException { - return array.getResultSet(index, count); + return new AgentResultSet(array.getResultSet(index, count)); } @Override public ResultSet getResultSet(long index, int count, Map> map) throws SQLException { - return array.getResultSet(index, count, map); + return new AgentResultSet(array.getResultSet(index, count, map)); } @Override diff --git a/agent/src/main/java/com/fanruan/agent/jdbc/AgentBlob.java b/agent/src/main/java/com/fanruan/agent/jdbc/AgentBlob.java index 26d0874..cc00aa8 100644 --- a/agent/src/main/java/com/fanruan/agent/jdbc/AgentBlob.java +++ b/agent/src/main/java/com/fanruan/agent/jdbc/AgentBlob.java @@ -56,7 +56,7 @@ public class AgentBlob implements Blob { @Override public OutputStream setBinaryStream(long pos) throws SQLException { - return blob.setBinaryStream(pos); + return new AgentOutputStream(blob.setBinaryStream(pos)); } @Override diff --git a/agent/src/main/java/com/fanruan/agent/jdbc/AgentClob.java b/agent/src/main/java/com/fanruan/agent/jdbc/AgentClob.java index 12022c7..e0ef474 100644 --- a/agent/src/main/java/com/fanruan/agent/jdbc/AgentClob.java +++ b/agent/src/main/java/com/fanruan/agent/jdbc/AgentClob.java @@ -33,12 +33,12 @@ public class AgentClob implements Clob { @Override public Reader getCharacterStream() throws SQLException { - return clob.getCharacterStream(); + return new AgentReader(clob.getCharacterStream()); } @Override public InputStream getAsciiStream() throws SQLException { - return clob.getAsciiStream(); + return new AgentInputStream(clob.getAsciiStream()); } @Override @@ -63,12 +63,12 @@ public class AgentClob implements Clob { @Override public OutputStream setAsciiStream(long pos) throws SQLException { - return clob.setAsciiStream(pos); + return new AgentOutputStream(clob.setAsciiStream(pos)); } @Override public Writer setCharacterStream(long pos) throws SQLException { - return clob.setCharacterStream(pos); + return new AgentWriter(clob.setCharacterStream(pos)); } @Override @@ -83,6 +83,6 @@ public class AgentClob implements Clob { @Override public Reader getCharacterStream(long pos, long length) throws SQLException { - return clob.getCharacterStream(pos, length); + return new AgentReader(clob.getCharacterStream(pos, length)); } } diff --git a/agent/src/main/java/com/fanruan/agent/jdbc/AgentNClob.java b/agent/src/main/java/com/fanruan/agent/jdbc/AgentNClob.java new file mode 100644 index 0000000..bd9923a --- /dev/null +++ b/agent/src/main/java/com/fanruan/agent/jdbc/AgentNClob.java @@ -0,0 +1,89 @@ +package com.fanruan.agent.jdbc; + +import com.fanruan.annotation.BindClass; + +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; +import java.sql.Clob; +import java.sql.NClob; +import java.sql.SQLException; + +/** + * @author Yichen Dai + * @date 2022/9/9 10:49 + */ +@BindClass +public class AgentNClob implements NClob { + private NClob nClob; + + public AgentNClob(NClob nClob){ + this.nClob = nClob; + } + + @Override + public long length() throws SQLException { + return nClob.length(); + } + + @Override + public String getSubString(long pos, int length) throws SQLException { + return nClob.getSubString(pos, length); + } + + @Override + public Reader getCharacterStream() throws SQLException { + return new AgentReader(nClob.getCharacterStream()); + } + + @Override + public InputStream getAsciiStream() throws SQLException { + return new AgentInputStream(nClob.getAsciiStream()); + } + + @Override + public long position(String searchstr, long start) throws SQLException { + return nClob.position(searchstr, start); + } + + @Override + public long position(Clob searchstr, long start) throws SQLException { + return nClob.position(searchstr, start); + } + + @Override + public int setString(long pos, String str) throws SQLException { + return nClob.setString(pos, str); + } + + @Override + public int setString(long pos, String str, int offset, int len) throws SQLException { + return nClob.setString(pos, str, offset, len); + } + + @Override + public OutputStream setAsciiStream(long pos) throws SQLException { + return new AgentOutputStream(nClob.setAsciiStream(pos)); + } + + @Override + public Writer setCharacterStream(long pos) throws SQLException { + return new AgentWriter(nClob.setCharacterStream(pos)); + } + + @Override + public void truncate(long len) throws SQLException { + nClob.truncate(len); + } + + @Override + public void free() throws SQLException { + nClob.free(); + } + + @Override + public Reader getCharacterStream(long pos, long length) throws SQLException { + return new AgentReader(nClob.getCharacterStream(pos, length)); + } +} diff --git a/agent/src/main/java/com/fanruan/agent/jdbc/AgentOutputStream.java b/agent/src/main/java/com/fanruan/agent/jdbc/AgentOutputStream.java new file mode 100644 index 0000000..d9f1865 --- /dev/null +++ b/agent/src/main/java/com/fanruan/agent/jdbc/AgentOutputStream.java @@ -0,0 +1,44 @@ +package com.fanruan.agent.jdbc; + +import com.fanruan.annotation.BindClass; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * @author Yichen Dai + * @date 2022/9/9 10:49 + */ +@BindClass +public class AgentOutputStream extends OutputStream { + private OutputStream outputStream; + + public AgentOutputStream(OutputStream outputStream){ + this.outputStream = outputStream; + } + + @Override + public void write(int b) throws IOException { + this.outputStream.write(b); + } + + @Override + public void write(byte b[]) throws IOException { + outputStream.write(b); + } + + @Override + public void write(byte b[], int off, int len) throws IOException { + outputStream.write(b, off, len); + } + + @Override + public void flush() throws IOException { + outputStream.flush(); + } + + @Override + public void close() throws IOException { + outputStream.close(); + } +} diff --git a/agent/src/main/java/com/fanruan/agent/jdbc/AgentRowId.java b/agent/src/main/java/com/fanruan/agent/jdbc/AgentRowId.java new file mode 100644 index 0000000..9eb0b89 --- /dev/null +++ b/agent/src/main/java/com/fanruan/agent/jdbc/AgentRowId.java @@ -0,0 +1,23 @@ +package com.fanruan.agent.jdbc; + +import com.fanruan.annotation.BindClass; + +import java.sql.RowId; + +/** + * @author Yichen Dai + * @date 2022/9/9 17:31 + */ +@BindClass +public class AgentRowId implements RowId { + RowId rowId; + + public AgentRowId(RowId rowId){ + this.rowId = rowId; + } + + @Override + public byte[] getBytes() { + return rowId.getBytes(); + } +} diff --git a/agent/src/main/java/com/fanruan/agent/jdbc/AgentSavepoint.java b/agent/src/main/java/com/fanruan/agent/jdbc/AgentSavepoint.java new file mode 100644 index 0000000..2228e8a --- /dev/null +++ b/agent/src/main/java/com/fanruan/agent/jdbc/AgentSavepoint.java @@ -0,0 +1,29 @@ +package com.fanruan.agent.jdbc; + +import com.fanruan.annotation.BindClass; + +import java.sql.SQLException; +import java.sql.Savepoint; + +/** + * @author Yichen Dai + * @date 2022/9/9 16:39 + */ +@BindClass +public class AgentSavepoint implements Savepoint { + Savepoint savepoint; + + public AgentSavepoint(Savepoint savepoint){ + this.savepoint = savepoint; + } + + @Override + public int getSavepointId() throws SQLException { + return savepoint.getSavepointId(); + } + + @Override + public String getSavepointName() throws SQLException { + return savepoint.getSavepointName(); + } +} diff --git a/agent/src/main/java/com/fanruan/agent/jdbc/AgentStruct.java b/agent/src/main/java/com/fanruan/agent/jdbc/AgentStruct.java index 61797d3..ef057b9 100644 --- a/agent/src/main/java/com/fanruan/agent/jdbc/AgentStruct.java +++ b/agent/src/main/java/com/fanruan/agent/jdbc/AgentStruct.java @@ -14,22 +14,22 @@ import java.util.Map; public class AgentStruct implements Struct { Struct struct; - AgentStruct(Struct struct){ + public AgentStruct(Struct struct){ this.struct = struct; } @Override public String getSQLTypeName() throws SQLException { - return null; + return struct.getSQLTypeName(); } @Override public Object[] getAttributes() throws SQLException { - return new Object[0]; + return struct.getAttributes(); } @Override public Object[] getAttributes(Map> map) throws SQLException { - return new Object[0]; + return struct.getAttributes(map); } } diff --git a/agent/src/main/java/com/fanruan/agent/jdbc/AgentWriter.java b/agent/src/main/java/com/fanruan/agent/jdbc/AgentWriter.java new file mode 100644 index 0000000..066f321 --- /dev/null +++ b/agent/src/main/java/com/fanruan/agent/jdbc/AgentWriter.java @@ -0,0 +1,76 @@ +package com.fanruan.agent.jdbc; + +import com.fanruan.annotation.BindClass; + +import java.io.IOException; +import java.io.Writer; + +/** + * @author Yichen Dai + * @date 2022/9/9 10:48 + */ +@BindClass +public class AgentWriter extends Writer { + private Writer writer; + + public AgentWriter(Writer writer){ + this.writer = writer; + } + + @Override + public void write(int c) throws IOException { + writer.write(c); + } + + @Override + public void write(char cbuf[]) throws IOException { + writer.write(cbuf); + } + + @Override + public void write(char[] cbuf, int off, int len) throws IOException { + writer.write(cbuf, off, len); + } + + @Override + public void write(String str) throws IOException { + writer.write(str); + } + + @Override + public void write(String str, int off, int len) throws IOException { + writer.write(str, off, len); + } + + /** + * chain calling return this instead of new Agent Class + * @throws IOException + */ + @Override + public Writer append(CharSequence csq) throws IOException { + writer.append(csq); + return this; + } + + @Override + public Writer append(CharSequence csq, int start, int end) throws IOException { + writer.append(csq, start, end); + return this; + } + + @Override + public Writer append(char c) throws IOException { + writer.append(c); + return this; + } + + @Override + public void flush() throws IOException { + writer.flush(); + } + + @Override + public void close() throws IOException { + writer.close(); + } +} diff --git a/agent/src/main/java/com/fanruan/agent/jdbc/connection/AgentConnection.java b/agent/src/main/java/com/fanruan/agent/jdbc/connection/AgentConnection.java index 5b724be..9abb131 100644 --- a/agent/src/main/java/com/fanruan/agent/jdbc/connection/AgentConnection.java +++ b/agent/src/main/java/com/fanruan/agent/jdbc/connection/AgentConnection.java @@ -1,6 +1,6 @@ package com.fanruan.agent.jdbc.connection; -import com.fanruan.agent.jdbc.AgentDataBaseMetaData; +import com.fanruan.agent.jdbc.*; import com.fanruan.agent.jdbc.statement.AgentCallableStatement; import com.fanruan.agent.jdbc.statement.AgentPreparedStatement; import com.fanruan.agent.jdbc.statement.AgentStatement; @@ -157,12 +157,12 @@ public class AgentConnection implements Connection { @Override public Savepoint setSavepoint() throws SQLException { - return conn.setSavepoint(); + return new AgentSavepoint(conn.setSavepoint()); } @Override public Savepoint setSavepoint(String name) throws SQLException { - return conn.setSavepoint(name); + return new AgentSavepoint(conn.setSavepoint(name)); } @Override @@ -207,12 +207,12 @@ public class AgentConnection implements Connection { @Override public Clob createClob() throws SQLException { - return conn.createClob(); + return new AgentClob(conn.createClob()); } @Override public Blob createBlob() throws SQLException { - return conn.createBlob(); + return new AgentBlob(conn.createBlob()); } @Override @@ -252,12 +252,12 @@ public class AgentConnection implements Connection { @Override public Array createArrayOf(String typeName, Object[] elements) throws SQLException { - return conn.createArrayOf(typeName, elements); + return new AgentArray(conn.createArrayOf(typeName, elements)); } @Override public Struct createStruct(String typeName, Object[] attributes) throws SQLException { - return conn.createStruct(typeName, attributes); + return new AgentStruct(conn.createStruct(typeName, attributes)); } @Override diff --git a/agent/src/main/java/com/fanruan/agent/jdbc/driver/AgentDriver.java b/agent/src/main/java/com/fanruan/agent/jdbc/driver/AgentDriver.java index 66d4970..80738f1 100644 --- a/agent/src/main/java/com/fanruan/agent/jdbc/driver/AgentDriver.java +++ b/agent/src/main/java/com/fanruan/agent/jdbc/driver/AgentDriver.java @@ -14,11 +14,6 @@ import java.util.logging.Logger; @BindClass public class AgentDriver implements Driver { - - static public final int DRIVER_VERSION_MAJOR = 1; - static public final int DRIVER_VERSION_MINOR = 1; - - //依靠静态函数块注册驱动 static{ try { DriverManager.registerDriver(new AgentDriver()); @@ -34,17 +29,7 @@ public class AgentDriver implements Driver { @Override public boolean acceptsURL(String url) throws SQLException { - Enumeration registeredDrivers = DriverManager.getDrivers(); - while (registeredDrivers.hasMoreElements()) { - Driver driver = registeredDrivers.nextElement(); - if(driver instanceof AgentDriver){ - continue; - } - if(driver.acceptsURL(url)){ - return true; - } - } - return false; + return true; } @Override @@ -54,12 +39,12 @@ public class AgentDriver implements Driver { @Override public int getMajorVersion() { - return DRIVER_VERSION_MAJOR; + return 0; } @Override public int getMinorVersion() { - return DRIVER_VERSION_MINOR; + return 0; } @Override diff --git a/agent/src/main/java/com/fanruan/agent/jdbc/resultset/AgentResultSet.java b/agent/src/main/java/com/fanruan/agent/jdbc/resultset/AgentResultSet.java index f687ee3..1d67f55 100644 --- a/agent/src/main/java/com/fanruan/agent/jdbc/resultset/AgentResultSet.java +++ b/agent/src/main/java/com/fanruan/agent/jdbc/resultset/AgentResultSet.java @@ -212,7 +212,7 @@ public class AgentResultSet implements ResultSet { @Override public ResultSetMetaData getMetaData() throws SQLException { - return resultSet.getMetaData(); + return new AgentResultSetMetaData(resultSet.getMetaData()); } @Override @@ -717,12 +717,12 @@ public class AgentResultSet implements ResultSet { @Override public RowId getRowId(int columnIndex) throws SQLException { - return resultSet.getRowId(columnIndex); + return new AgentRowId(resultSet.getRowId(columnIndex)); } @Override public RowId getRowId(String columnLabel) throws SQLException { - return resultSet.getRowId(columnLabel); + return new AgentRowId(resultSet.getRowId(columnLabel)); } @Override @@ -767,12 +767,12 @@ public class AgentResultSet implements ResultSet { @Override public NClob getNClob(int columnIndex) throws SQLException { - return resultSet.getNClob(columnIndex); + return new AgentNClob(resultSet.getNClob(columnIndex)); } @Override public NClob getNClob(String columnLabel) throws SQLException { - return resultSet.getNClob(columnLabel); + return new AgentNClob(resultSet.getNClob(columnLabel)); } @Override diff --git a/agent/src/main/java/com/fanruan/agent/jdbc/statement/AgentCallableStatement.java b/agent/src/main/java/com/fanruan/agent/jdbc/statement/AgentCallableStatement.java index 0ee3c20..c11c600 100644 --- a/agent/src/main/java/com/fanruan/agent/jdbc/statement/AgentCallableStatement.java +++ b/agent/src/main/java/com/fanruan/agent/jdbc/statement/AgentCallableStatement.java @@ -1,9 +1,7 @@ package com.fanruan.agent.jdbc.statement; -import com.fanruan.agent.jdbc.AgentArray; -import com.fanruan.agent.jdbc.AgentBlob; -import com.fanruan.agent.jdbc.AgentClob; -import com.fanruan.agent.jdbc.AgentParameterMetaData; +import com.fanruan.agent.jdbc.*; +import com.fanruan.agent.jdbc.resultset.AgentResultSet; import com.fanruan.annotation.BindClass; import java.io.InputStream; @@ -394,12 +392,12 @@ public class AgentCallableStatement implements java.sql.CallableStatement { @Override public Clob getClob(String parameterName) throws SQLException { - return cst.getClob(parameterName); + return new AgentClob(cst.getClob(parameterName)); } @Override public Array getArray(String parameterName) throws SQLException { - return cst.getArray(parameterName); + return new AgentArray(cst.getArray(parameterName)); } @Override @@ -424,12 +422,12 @@ public class AgentCallableStatement implements java.sql.CallableStatement { @Override public RowId getRowId(int parameterIndex) throws SQLException { - return cst.getRowId(parameterIndex); + return new AgentRowId(cst.getRowId(parameterIndex)); } @Override public RowId getRowId(String parameterName) throws SQLException { - return cst.getRowId(parameterName); + return new AgentRowId(cst.getRowId(parameterName)); } @Override @@ -469,12 +467,12 @@ public class AgentCallableStatement implements java.sql.CallableStatement { @Override public NClob getNClob(int parameterIndex) throws SQLException { - return cst.getNClob(parameterIndex); + return new AgentNClob(cst.getNClob(parameterIndex)); } @Override public NClob getNClob(String parameterName) throws SQLException { - return cst.getNClob(parameterName); + return new AgentNClob(cst.getNClob(parameterName)); } @Override @@ -504,22 +502,22 @@ public class AgentCallableStatement implements java.sql.CallableStatement { @Override public Reader getNCharacterStream(int parameterIndex) throws SQLException { - return cst.getNCharacterStream(parameterIndex); + return new AgentReader(cst.getNCharacterStream(parameterIndex)); } @Override public Reader getNCharacterStream(String parameterName) throws SQLException { - return cst.getNCharacterStream(parameterName); + return new AgentReader(cst.getNCharacterStream(parameterName)); } @Override public Reader getCharacterStream(int parameterIndex) throws SQLException { - return cst.getCharacterStream(parameterIndex); + return new AgentReader(cst.getCharacterStream(parameterIndex)); } @Override public Reader getCharacterStream(String parameterName) throws SQLException { - return cst.getCharacterStream(parameterName); + return new AgentReader(cst.getCharacterStream(parameterName)); } @Override @@ -594,7 +592,7 @@ public class AgentCallableStatement implements java.sql.CallableStatement { @Override public ResultSet executeQuery() throws SQLException { - return cst.executeQuery(); + return new AgentResultSet(cst.executeQuery()); } @Override @@ -739,7 +737,7 @@ public class AgentCallableStatement implements java.sql.CallableStatement { @Override public ResultSetMetaData getMetaData() throws SQLException { - return cst.getMetaData(); + return new AgentResultSetMetaData(cst.getMetaData()); } @Override @@ -869,7 +867,7 @@ public class AgentCallableStatement implements java.sql.CallableStatement { @Override public ResultSet executeQuery(String sql) throws SQLException { - return cst.executeQuery(sql); + return new AgentResultSet(cst.executeQuery(sql)); } @Override @@ -944,7 +942,7 @@ public class AgentCallableStatement implements java.sql.CallableStatement { @Override public ResultSet getResultSet() throws SQLException { - return cst.getResultSet(); + return new AgentResultSet(cst.getResultSet()); } @Override @@ -1014,7 +1012,7 @@ public class AgentCallableStatement implements java.sql.CallableStatement { @Override public ResultSet getGeneratedKeys() throws SQLException { - return cst.getGeneratedKeys(); + return new AgentResultSet(cst.getGeneratedKeys()); } @Override diff --git a/service/src/main/java/com/fanruan/proxy/interceptor/Interceptor.java b/service/src/main/java/com/fanruan/proxy/interceptor/Interceptor.java index 108575d..5ea9485 100644 --- a/service/src/main/java/com/fanruan/proxy/interceptor/Interceptor.java +++ b/service/src/main/java/com/fanruan/proxy/interceptor/Interceptor.java @@ -37,6 +37,8 @@ public class Interceptor implements MethodInterceptor { private SocketIOClient client; private Properties info; + public Interceptor(){} + public Interceptor(Class clazz, Properties info){ this.clazz = clazz; this.info = info; diff --git a/service/src/main/java/com/fanruan/service/jdbc/ServiceArray.java b/service/src/main/java/com/fanruan/service/jdbc/ServiceArray.java index b23ae26..5e24cf9 100644 --- a/service/src/main/java/com/fanruan/service/jdbc/ServiceArray.java +++ b/service/src/main/java/com/fanruan/service/jdbc/ServiceArray.java @@ -1,6 +1,8 @@ package com.fanruan.service.jdbc; import com.fanruan.annotation.RemoteClass; +import com.fanruan.proxy.ProxyFactory; +import com.fanruan.service.jdbc.resultset.ServiceResultSet; import java.sql.Array; import java.sql.ResultSet; @@ -46,22 +48,30 @@ public class ServiceArray extends BasedBind implements Array { @Override public ResultSet getResultSet() throws SQLException { - return null; + ServiceResultSet rs = (ServiceResultSet) ProxyFactory.getProxy(ServiceResultSet.class, info); + rs.setInfo(info); + return rs; } @Override public ResultSet getResultSet(Map> map) throws SQLException { - return null; + ServiceResultSet rs = (ServiceResultSet) ProxyFactory.getProxy(ServiceResultSet.class, info); + rs.setInfo(info); + return rs; } @Override public ResultSet getResultSet(long index, int count) throws SQLException { - return null; + ServiceResultSet rs = (ServiceResultSet) ProxyFactory.getProxy(ServiceResultSet.class, info); + rs.setInfo(info); + return rs; } @Override public ResultSet getResultSet(long index, int count, Map> map) throws SQLException { - return null; + ServiceResultSet rs = (ServiceResultSet) ProxyFactory.getProxy(ServiceResultSet.class, info); + rs.setInfo(info); + return rs; } @Override diff --git a/service/src/main/java/com/fanruan/service/jdbc/ServiceBlob.java b/service/src/main/java/com/fanruan/service/jdbc/ServiceBlob.java index 65c3fe6..f0a9be0 100644 --- a/service/src/main/java/com/fanruan/service/jdbc/ServiceBlob.java +++ b/service/src/main/java/com/fanruan/service/jdbc/ServiceBlob.java @@ -54,7 +54,7 @@ public class ServiceBlob extends BasedBind implements Blob { @Override public OutputStream setBinaryStream(long pos) throws SQLException { - return null; + return (OutputStream) ProxyFactory.getProxy(OutputStream.class, info); } @Override diff --git a/service/src/main/java/com/fanruan/service/jdbc/ServiceClob.java b/service/src/main/java/com/fanruan/service/jdbc/ServiceClob.java index 0423d51..40a28f8 100644 --- a/service/src/main/java/com/fanruan/service/jdbc/ServiceClob.java +++ b/service/src/main/java/com/fanruan/service/jdbc/ServiceClob.java @@ -1,6 +1,7 @@ package com.fanruan.service.jdbc; import com.fanruan.annotation.RemoteClass; +import com.fanruan.proxy.ProxyFactory; import java.io.InputStream; import java.io.OutputStream; @@ -28,12 +29,12 @@ public class ServiceClob extends BasedBind implements Clob { @Override public Reader getCharacterStream() throws SQLException { - return null; + return (Reader) ProxyFactory.getProxy(ServiceReader.class, info); } @Override public InputStream getAsciiStream() throws SQLException { - return null; + return (InputStream) ProxyFactory.getProxy(InputStream.class, info); } @Override @@ -58,12 +59,12 @@ public class ServiceClob extends BasedBind implements Clob { @Override public OutputStream setAsciiStream(long pos) throws SQLException { - return null; + return (OutputStream) ProxyFactory.getProxy(ServiceOutputStream.class, info); } @Override public Writer setCharacterStream(long pos) throws SQLException { - return null; + return (Writer) ProxyFactory.getProxy(ServiceWriter.class, info); } @Override @@ -78,6 +79,6 @@ public class ServiceClob extends BasedBind implements Clob { @Override public Reader getCharacterStream(long pos, long length) throws SQLException { - return null; + return (Reader) ProxyFactory.getProxy(ServiceReader.class, info); } } diff --git a/service/src/main/java/com/fanruan/service/jdbc/ServiceNClob.java b/service/src/main/java/com/fanruan/service/jdbc/ServiceNClob.java new file mode 100644 index 0000000..0aad8d7 --- /dev/null +++ b/service/src/main/java/com/fanruan/service/jdbc/ServiceNClob.java @@ -0,0 +1,85 @@ +package com.fanruan.service.jdbc; + +import com.fanruan.annotation.RemoteClass; +import com.fanruan.proxy.ProxyFactory; + +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; +import java.sql.Clob; +import java.sql.NClob; +import java.sql.SQLException; + +/** + * @author Yichen Dai + * @date 2022/9/9 10:45 + */ +@RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.AgentNClob") +public class ServiceNClob extends BasedBind implements NClob { + + @Override + public long length() throws SQLException { + return 0; + } + + @Override + public String getSubString(long pos, int length) throws SQLException { + return null; + } + + @Override + public Reader getCharacterStream() throws SQLException { + return (Reader) ProxyFactory.getProxy(ServiceReader.class, info); + } + + @Override + public InputStream getAsciiStream() throws SQLException { + return (InputStream) ProxyFactory.getProxy(ServiceInputStream.class, info); + } + + @Override + public long position(String searchstr, long start) throws SQLException { + return 0; + } + + @Override + public long position(Clob searchstr, long start) throws SQLException { + return 0; + } + + @Override + public int setString(long pos, String str) throws SQLException { + return 0; + } + + @Override + public int setString(long pos, String str, int offset, int len) throws SQLException { + return 0; + } + + @Override + public OutputStream setAsciiStream(long pos) throws SQLException { + return (OutputStream) ProxyFactory.getProxy(ServiceOutputStream.class, info); + } + + @Override + public Writer setCharacterStream(long pos) throws SQLException { + return (Writer) ProxyFactory.getProxy(ServiceWriter.class, info); + } + + @Override + public void truncate(long len) throws SQLException { + + } + + @Override + public void free() throws SQLException { + + } + + @Override + public Reader getCharacterStream(long pos, long length) throws SQLException { + return (Reader) ProxyFactory.getProxy(ServiceReader.class, info); + } +} diff --git a/service/src/main/java/com/fanruan/service/jdbc/ServiceOutputStream.java b/service/src/main/java/com/fanruan/service/jdbc/ServiceOutputStream.java new file mode 100644 index 0000000..abf6250 --- /dev/null +++ b/service/src/main/java/com/fanruan/service/jdbc/ServiceOutputStream.java @@ -0,0 +1,60 @@ +package com.fanruan.service.jdbc; + +import com.fanruan.annotation.LocalMethod; +import com.fanruan.annotation.RemoteClass; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * @author Yichen Dai + * @date 2022/9/9 10:47 + */ +@RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.AgentOutputStream") +public class ServiceOutputStream extends OutputStream { + private String ID; + + @LocalMethod + public String getID(){ + return this.ID; + } + + @LocalMethod + public void setID(String ID){ + this.ID = ID; + } + + @Override + public void write(int b) throws IOException { + } + + @LocalMethod + @Override + public int hashCode(){ + return super.hashCode(); + } + + @LocalMethod + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } + + @LocalMethod + @Override + protected Object clone() throws CloneNotSupportedException { + return super.clone(); + } + + @LocalMethod + @Override + public String toString() { + return super.toString(); + } + + @LocalMethod + @Override + protected void finalize() throws Throwable { + super.finalize(); + } +} diff --git a/service/src/main/java/com/fanruan/service/jdbc/ServiceRowId.java b/service/src/main/java/com/fanruan/service/jdbc/ServiceRowId.java new file mode 100644 index 0000000..6438580 --- /dev/null +++ b/service/src/main/java/com/fanruan/service/jdbc/ServiceRowId.java @@ -0,0 +1,18 @@ +package com.fanruan.service.jdbc; + +import com.fanruan.annotation.RemoteClass; + +import java.sql.RowId; + +/** + * @author Yichen Dai + * @date 2022/9/9 17:30 + */ +@RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.AgentRowId") +public class ServiceRowId extends BasedBind implements RowId { + + @Override + public byte[] getBytes() { + return new byte[0]; + } +} diff --git a/service/src/main/java/com/fanruan/service/jdbc/ServiceSavepoint.java b/service/src/main/java/com/fanruan/service/jdbc/ServiceSavepoint.java new file mode 100644 index 0000000..d9d89c2 --- /dev/null +++ b/service/src/main/java/com/fanruan/service/jdbc/ServiceSavepoint.java @@ -0,0 +1,24 @@ +package com.fanruan.service.jdbc; + +import com.fanruan.annotation.RemoteClass; + +import java.sql.SQLException; +import java.sql.Savepoint; + +/** + * @author Yichen Dai + * @date 2022/9/9 16:38 + */ +@RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.AgentSavepoint") +public class ServiceSavepoint extends BasedBind implements Savepoint { + + @Override + public int getSavepointId() throws SQLException { + return 0; + } + + @Override + public String getSavepointName() throws SQLException { + return null; + } +} diff --git a/service/src/main/java/com/fanruan/service/jdbc/ServiceWriter.java b/service/src/main/java/com/fanruan/service/jdbc/ServiceWriter.java new file mode 100644 index 0000000..fd2faef --- /dev/null +++ b/service/src/main/java/com/fanruan/service/jdbc/ServiceWriter.java @@ -0,0 +1,88 @@ +package com.fanruan.service.jdbc; + +import com.fanruan.annotation.LocalMethod; +import com.fanruan.annotation.RemoteClass; + +import java.io.IOException; +import java.io.Writer; + +/** + * @author Yichen Dai + * @date 2022/9/9 10:47 + */ +@RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.AgentWrite") +public class ServiceWriter extends Writer { + private String ID; + + @LocalMethod + public String getID(){ + return this.ID; + } + + @LocalMethod + public void setID(String ID){ + this.ID = ID; + } + + @Override + public void write(char[] cbuf, int off, int len) throws IOException { + } + + @Override + public void flush() throws IOException { + } + + @Override + public void close() throws IOException { + + } + + @LocalMethod + @Override + public int hashCode(){ + return super.hashCode(); + } + + @LocalMethod + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } + + @LocalMethod + @Override + protected Object clone() throws CloneNotSupportedException { + return super.clone(); + } + + @LocalMethod + @Override + public String toString() { + return super.toString(); + } + + @LocalMethod + @Override + protected void finalize() throws Throwable { + super.finalize(); + } + + /** + * chain calling return this instead of new Agent Class + * @throws IOException + */ + @Override + public Writer append(CharSequence csq) throws IOException { + return this; + } + + @Override + public Writer append(CharSequence csq, int start, int end) throws IOException { + return this; + } + + @Override + public Writer append(char c) throws IOException { + return this; + } +} diff --git a/service/src/main/java/com/fanruan/service/jdbc/connection/ServiceConnection.java b/service/src/main/java/com/fanruan/service/jdbc/connection/ServiceConnection.java index 3a6fe08..3791f62 100644 --- a/service/src/main/java/com/fanruan/service/jdbc/connection/ServiceConnection.java +++ b/service/src/main/java/com/fanruan/service/jdbc/connection/ServiceConnection.java @@ -1,6 +1,7 @@ package com.fanruan.service.jdbc.connection; +import com.fanruan.annotation.LocalMethod; import com.fanruan.annotation.NotImplemented; import com.fanruan.annotation.RemoteClass; import com.fanruan.service.jdbc.*; @@ -43,6 +44,7 @@ public class ServiceConnection extends BasedBind implements Connection { public CallableStatement prepareCall(String sql) throws SQLException { ServiceCallableStatement cst = (ServiceCallableStatement) ProxyFactory.getProxy(ServiceCallableStatement.class, info); cst.setInfo(info); + cst.setConnection(this); return cst; } @@ -64,10 +66,11 @@ public class ServiceConnection extends BasedBind implements Connection { public void commit() throws SQLException { } - @Override + @LocalMethod + @NotImplemented public void rollback() throws SQLException { - + throw new SQLException("Not Implemented!"); } @Override @@ -139,6 +142,7 @@ public class ServiceConnection extends BasedBind implements Connection { public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { ServicePreparedStatement pst = (ServicePreparedStatement) ProxyFactory.getProxy(ServicePreparedStatement.class, info); pst.setInfo(info); + pst.setConnection(this); return pst; } @@ -146,6 +150,7 @@ public class ServiceConnection extends BasedBind implements Connection { public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { ServiceCallableStatement cst = (ServiceCallableStatement) ProxyFactory.getProxy(ServiceCallableStatement.class, info); cst.setInfo(info); + cst.setConnection(this); return cst; } @@ -170,23 +175,31 @@ public class ServiceConnection extends BasedBind implements Connection { } @Override + @LocalMethod + @NotImplemented public Savepoint setSavepoint() throws SQLException { - return null; + throw new SQLException("Not Implemented!"); } @Override + @LocalMethod + @NotImplemented public Savepoint setSavepoint(String name) throws SQLException { - return null; + throw new SQLException("Not Implemented!"); } @Override + @LocalMethod + @NotImplemented public void rollback(Savepoint savepoint) throws SQLException { - + throw new SQLException("Not Implemented!"); } @Override + @LocalMethod + @NotImplemented public void releaseSavepoint(Savepoint savepoint) throws SQLException { - + throw new SQLException("Not Implemented!"); } @Override @@ -201,6 +214,7 @@ public class ServiceConnection extends BasedBind implements Connection { public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { ServicePreparedStatement pst = (ServicePreparedStatement) ProxyFactory.getProxy(ServicePreparedStatement.class, info); pst.setInfo(info); + pst.setConnection(this); return pst; } @@ -208,6 +222,7 @@ public class ServiceConnection extends BasedBind implements Connection { public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { ServiceCallableStatement cst = (ServiceCallableStatement) ProxyFactory.getProxy(ServiceCallableStatement.class, info); cst.setInfo(info); + cst.setConnection(this); return cst; } @@ -215,6 +230,7 @@ public class ServiceConnection extends BasedBind implements Connection { public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { ServicePreparedStatement pst = (ServicePreparedStatement) ProxyFactory.getProxy(ServicePreparedStatement.class, info); pst.setInfo(info); + pst.setConnection(this); return pst; } @@ -222,6 +238,7 @@ public class ServiceConnection extends BasedBind implements Connection { public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { ServicePreparedStatement pst = (ServicePreparedStatement) ProxyFactory.getProxy(ServicePreparedStatement.class, info); pst.setInfo(info); + pst.setConnection(this); return pst; } @@ -229,6 +246,7 @@ public class ServiceConnection extends BasedBind implements Connection { public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { ServicePreparedStatement pst = (ServicePreparedStatement) ProxyFactory.getProxy(ServicePreparedStatement.class, info); pst.setInfo(info); + pst.setConnection(this); return pst; } @@ -248,12 +266,14 @@ public class ServiceConnection extends BasedBind implements Connection { @Override public NClob createNClob() throws SQLException { - return null; + return (NClob) ProxyFactory.getProxy(ServiceNClob.class, info); } @Override + @NotImplemented + @LocalMethod public SQLXML createSQLXML() throws SQLException { - return null; + throw new SQLException("Not Implemented"); } @Override @@ -322,12 +342,12 @@ public class ServiceConnection extends BasedBind implements Connection { @Override @NotImplemented + @LocalMethod public T unwrap(Class iface) throws SQLException { - throw new NotImplementedException(); + throw new SQLException("Not Implemented"); } @Override - public boolean isWrapperFor(Class iface) throws NotImplementedException { return true; } diff --git a/service/src/main/java/com/fanruan/service/jdbc/driver/ServiceDriver.java b/service/src/main/java/com/fanruan/service/jdbc/driver/ServiceDriver.java index 3db83a7..df3932a 100644 --- a/service/src/main/java/com/fanruan/service/jdbc/driver/ServiceDriver.java +++ b/service/src/main/java/com/fanruan/service/jdbc/driver/ServiceDriver.java @@ -1,5 +1,7 @@ package com.fanruan.service.jdbc.driver; +import com.fanruan.annotation.LocalMethod; +import com.fanruan.annotation.NotImplemented; import com.fanruan.annotation.RemoteClass; import com.fanruan.service.jdbc.AbstractBind; import com.fanruan.service.jdbc.BasedBind; @@ -20,7 +22,6 @@ public class ServiceDriver extends BasedBind implements Driver { static public final int DRIVER_VERSION_MAJOR = 1; static public final int DRIVER_VERSION_MINOR = 1; - //依靠静态函数块注册驱动 static{ try { DriverManager.registerDriver((ServiceDriver) ProxyFactory.getProxy(ServiceDriver.class, null)); @@ -43,32 +44,39 @@ public class ServiceDriver extends BasedBind implements Driver { } @Override + @LocalMethod public boolean acceptsURL(String url){ return true; } @Override + @LocalMethod public DriverPropertyInfo[] getPropertyInfo(String url, Properties info){ return new DriverPropertyInfo[0]; } @Override + @LocalMethod public int getMajorVersion() { return DRIVER_VERSION_MAJOR; } @Override + @LocalMethod public int getMinorVersion() { return DRIVER_VERSION_MINOR; } @Override + @LocalMethod public boolean jdbcCompliant() { return false; } @Override + @LocalMethod + @NotImplemented public Logger getParentLogger() throws SQLFeatureNotSupportedException { - return null; + throw new SQLFeatureNotSupportedException("Not Implemented"); } } diff --git a/service/src/main/java/com/fanruan/service/jdbc/resultset/ServiceResultSet.java b/service/src/main/java/com/fanruan/service/jdbc/resultset/ServiceResultSet.java index 31a057f..2043258 100644 --- a/service/src/main/java/com/fanruan/service/jdbc/resultset/ServiceResultSet.java +++ b/service/src/main/java/com/fanruan/service/jdbc/resultset/ServiceResultSet.java @@ -731,12 +731,12 @@ public class ServiceResultSet extends BasedBind implements ResultSet { @Override public RowId getRowId(int columnIndex) throws SQLException { - return null; + return (RowId) ProxyFactory.getProxy(RowId.class, info); } @Override public RowId getRowId(String columnLabel) throws SQLException { - return null; + return (RowId) ProxyFactory.getProxy(RowId.class, info); } @Override @@ -781,32 +781,44 @@ public class ServiceResultSet extends BasedBind implements ResultSet { @Override public NClob getNClob(int columnIndex) throws SQLException { - return null; + ServiceNClob nclob = (ServiceNClob) ProxyFactory.getProxy(ServiceNClob.class, info); + nclob.setInfo(info); + return nclob; } @Override public NClob getNClob(String columnLabel) throws SQLException { - return null; + ServiceNClob nclob = (ServiceNClob) ProxyFactory.getProxy(ServiceNClob.class, info); + nclob.setInfo(info); + return nclob; } @Override + @LocalMethod + @NotImplemented public SQLXML getSQLXML(int columnIndex) throws SQLException { - return null; + throw new SQLException("Not Implemented"); } @Override + @LocalMethod + @NotImplemented public SQLXML getSQLXML(String columnLabel) throws SQLException { - return null; + throw new SQLException("Not Implemented"); } @Override + @LocalMethod + @NotImplemented public void updateSQLXML(int columnIndex, SQLXML xmlObject) throws SQLException { - + throw new SQLException("Not Implemented"); } @Override + @LocalMethod + @NotImplemented public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLException { - + throw new SQLException("Not Implemented"); } @Override diff --git a/service/src/main/java/com/fanruan/service/jdbc/statement/ServiceCallableStatement.java b/service/src/main/java/com/fanruan/service/jdbc/statement/ServiceCallableStatement.java index 41f01df..38cc0b8 100644 --- a/service/src/main/java/com/fanruan/service/jdbc/statement/ServiceCallableStatement.java +++ b/service/src/main/java/com/fanruan/service/jdbc/statement/ServiceCallableStatement.java @@ -1,12 +1,12 @@ package com.fanruan.service.jdbc.statement; +import com.fanruan.annotation.LocalMethod; +import com.fanruan.annotation.NotImplemented; import com.fanruan.annotation.RemoteClass; import com.fanruan.proxy.ProxyFactory; -import com.fanruan.service.jdbc.AbstractBind; -import com.fanruan.service.jdbc.BasedBind; -import com.fanruan.service.jdbc.ServiceParameterMetaData; -import com.fanruan.service.jdbc.ServiceResultSetMetaData; +import com.fanruan.service.jdbc.*; import com.fanruan.service.jdbc.resultset.ServiceResultSet; +import org.checkerframework.checker.units.qual.A; import java.io.InputStream; import java.io.Reader; @@ -25,6 +25,13 @@ import java.util.Properties; @RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.statement.AgentCallableStatement") public class ServiceCallableStatement extends BasedBind implements CallableStatement { + private Connection connection; + + @LocalMethod + public void setConnection(Connection connection){ + this.connection = connection; + } + public ServiceCallableStatement() {} @Override @@ -129,17 +136,17 @@ public class ServiceCallableStatement extends BasedBind implements CallableState @Override public Blob getBlob(int parameterIndex) throws SQLException { - return null; + return (Blob) ProxyFactory.getProxy(ServiceBlob.class, info); } @Override public Clob getClob(int parameterIndex) throws SQLException { - return null; + return (Clob) ProxyFactory.getProxy(ServiceClob.class, info); } @Override public Array getArray(int parameterIndex) throws SQLException { - return null; + return (Array) ProxyFactory.getProxy(ServiceArray.class, info); } @Override @@ -389,17 +396,17 @@ public class ServiceCallableStatement extends BasedBind implements CallableState @Override public Blob getBlob(String parameterName) throws SQLException { - return null; + return (Blob) ProxyFactory.getProxy(ServiceBlob.class, info); } @Override public Clob getClob(String parameterName) throws SQLException { - return null; + return (Clob) ProxyFactory.getProxy(ServiceClob.class, info); } @Override public Array getArray(String parameterName) throws SQLException { - return null; + return (Array) ProxyFactory.getProxy(ServiceArray.class, info); } @Override @@ -424,12 +431,12 @@ public class ServiceCallableStatement extends BasedBind implements CallableState @Override public RowId getRowId(int parameterIndex) throws SQLException { - return null; + return (RowId) ProxyFactory.getProxy(ServiceRowId.class, info); } @Override public RowId getRowId(String parameterName) throws SQLException { - return null; + return (RowId) ProxyFactory.getProxy(ServiceRowId.class, info); } @Override @@ -469,12 +476,12 @@ public class ServiceCallableStatement extends BasedBind implements CallableState @Override public NClob getNClob(int parameterIndex) throws SQLException { - return null; + return (NClob) ProxyFactory.getProxy(ServiceNClob.class, info); } @Override public NClob getNClob(String parameterName) throws SQLException { - return null; + return (NClob) ProxyFactory.getProxy(ServiceNClob.class, info); } @Override @@ -483,11 +490,15 @@ public class ServiceCallableStatement extends BasedBind implements CallableState } @Override + @LocalMethod + @NotImplemented public SQLXML getSQLXML(int parameterIndex) throws SQLException { return null; } @Override + @LocalMethod + @NotImplemented public SQLXML getSQLXML(String parameterName) throws SQLException { return null; } @@ -504,22 +515,22 @@ public class ServiceCallableStatement extends BasedBind implements CallableState @Override public Reader getNCharacterStream(int parameterIndex) throws SQLException { - return null; + return (Reader) ProxyFactory.getProxy(ServiceReader.class, info); } @Override public Reader getNCharacterStream(String parameterName) throws SQLException { - return null; + return (Reader) ProxyFactory.getProxy(ServiceReader.class, info); } @Override public Reader getCharacterStream(int parameterIndex) throws SQLException { - return null; + return (Reader) ProxyFactory.getProxy(ServiceReader.class, info); } @Override public Reader getCharacterStream(String parameterName) throws SQLException { - return null; + return (Reader) ProxyFactory.getProxy(ServiceReader.class, info); } @Override @@ -735,8 +746,10 @@ public class ServiceCallableStatement extends BasedBind implements CallableState } @Override + @LocalMethod + @NotImplemented public void setArray(int parameterIndex, Array x) throws SQLException { - + throw new SQLException("Not Implemented!"); } @Override @@ -948,7 +961,9 @@ public class ServiceCallableStatement extends BasedBind implements CallableState @Override public ResultSet getResultSet() throws SQLException { - return null; + ServiceResultSet rs = (ServiceResultSet) ProxyFactory.getProxy(ServiceResultSet.class, info); + rs.setInfo(info); + return rs; } @Override @@ -1007,8 +1022,9 @@ public class ServiceCallableStatement extends BasedBind implements CallableState } @Override + @LocalMethod public Connection getConnection() throws SQLException { - return null; + return this.connection; } @Override @@ -1018,7 +1034,9 @@ public class ServiceCallableStatement extends BasedBind implements CallableState @Override public ResultSet getGeneratedKeys() throws SQLException { - return null; + ServiceResultSet rs = (ServiceResultSet) ProxyFactory.getProxy(ServiceResultSet.class, info); + rs.setInfo(info); + return rs; } @Override diff --git a/service/src/main/java/com/fanruan/service/jdbc/statement/ServicePreparedStatement.java b/service/src/main/java/com/fanruan/service/jdbc/statement/ServicePreparedStatement.java index 25f97fb..a9f4aa5 100644 --- a/service/src/main/java/com/fanruan/service/jdbc/statement/ServicePreparedStatement.java +++ b/service/src/main/java/com/fanruan/service/jdbc/statement/ServicePreparedStatement.java @@ -22,6 +22,12 @@ import java.util.Properties; */ @RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.statement.AgentPreparedStatement") public class ServicePreparedStatement extends BasedBind implements PreparedStatement { + private Connection connection; + + @LocalMethod + public void setConnection(Connection connection){ + this.connection = connection; + } @Override public ResultSet executeQuery() throws SQLException { @@ -166,8 +172,10 @@ public class ServicePreparedStatement extends BasedBind implements PreparedState } @Override + @LocalMethod + @NotImplemented public void setArray(int parameterIndex, Array x) throws SQLException { - + throw new SQLException("Not Implemented!"); } @Override @@ -440,8 +448,9 @@ public class ServicePreparedStatement extends BasedBind implements PreparedState } @Override + @LocalMethod public Connection getConnection() throws SQLException { - return null; + return this.connection; } @Override diff --git a/test/src/test/java/com/fanruan/AbstractDriverTest.java b/test/src/test/java/com/fanruan/AbstractDriverTest.java index 0fc7150..80fab3a 100644 --- a/test/src/test/java/com/fanruan/AbstractDriverTest.java +++ b/test/src/test/java/com/fanruan/AbstractDriverTest.java @@ -52,7 +52,7 @@ public class AbstractDriverTest { */ public static Connection getConnection() throws SQLException { Driver driver = (ServiceDriver) ProxyFactory.getProxy(ServiceDriver.class, null); - Connection conn = driver.connect("jdbc:hsqldb:mem:test;sql.syntax_mys=true", info); + Connection conn = driver.connect("jdbc:hsqldb:mem:test;sql.syntax_mys=true", info); return conn; } } diff --git a/test/src/test/java/com/fanruan/ConnectionTest.java b/test/src/test/java/com/fanruan/ConnectionTest.java index 00b9216..e60b22f 100644 --- a/test/src/test/java/com/fanruan/ConnectionTest.java +++ b/test/src/test/java/com/fanruan/ConnectionTest.java @@ -129,7 +129,7 @@ public class ConnectionTest extends BaseJDBCTest{ public void testUnwrapper() throws SQLException { boolean canUnwrap = connection.isWrapperFor(Connection.class); Assertions.assertTrue(canUnwrap); - Assertions.assertThrows(NotImplementedException.class, + Assertions.assertThrows(SQLException.class, () -> connection.unwrap(null)); } @@ -315,6 +315,8 @@ public class ConnectionTest extends BaseJDBCTest{ @Test void testCreate() throws SQLException{ Assertions.assertNotNull(connection.createClob()); - + Assertions.assertNotNull(connection.createBlob()); + Assertions.assertNotNull(connection.createArrayOf("INTEGER", new Object[]{1})); + Assertions.assertNotNull(connection.createStruct("INTEGER", new Object[]{1})); } } diff --git a/test/src/test/java/com/fanruan/DriverTest.java b/test/src/test/java/com/fanruan/DriverTest.java new file mode 100644 index 0000000..3a7ce99 --- /dev/null +++ b/test/src/test/java/com/fanruan/DriverTest.java @@ -0,0 +1,39 @@ +package com.fanruan; + +import com.fanruan.agent.jdbc.driver.AgentDriver; +import com.fanruan.service.jdbc.driver.ServiceDriver; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; + +import java.sql.Driver; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.util.Enumeration; + +/** + * @author Yichen Dai + * @date 2022/9/9 16:44 + */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class DriverTest{ + + @Test + public void test() throws SQLException, ClassNotFoundException { + Class.forName("com.fanruan.service.jdbc.driver.ServiceDriver"); + Enumeration registeredDrivers = DriverManager.getDrivers(); + while (registeredDrivers.hasMoreElements()) { + Driver driver = registeredDrivers.nextElement(); + if(driver instanceof ServiceDriver){ + Assertions.assertEquals(0, driver.getPropertyInfo("jdbc:hsqldb:mem:test;sql.syntax_mys=true", null).length); + Assertions.assertEquals(ServiceDriver.DRIVER_VERSION_MAJOR, driver.getMajorVersion()); + Assertions.assertEquals(ServiceDriver.DRIVER_VERSION_MINOR, driver.getMinorVersion()); + Assertions.assertFalse(driver.jdbcCompliant()); + Assertions.assertThrows(SQLFeatureNotSupportedException.class, () -> driver.getParentLogger()); + } + } + + } +} diff --git a/test/src/test/java/com/fanruan/ResultSetTest.java b/test/src/test/java/com/fanruan/ResultSetTest.java index 378c8c4..182620a 100644 --- a/test/src/test/java/com/fanruan/ResultSetTest.java +++ b/test/src/test/java/com/fanruan/ResultSetTest.java @@ -3,24 +3,21 @@ package com.fanruan; import com.fanruan.cache.BeanCacheImpl; import com.fanruan.handler.DispatcherImpl; import com.fanruan.pojo.message.RpcRequest; -import com.fanruan.service.jdbc.AbstractBind; -import com.fanruan.service.jdbc.ServiceBlob; -import com.fanruan.service.jdbc.ServiceReader; +import com.fanruan.service.jdbc.*; import com.fanruan.service.jdbc.resultset.ServiceResultSet; +import com.fanruan.service.jdbc.statement.ServiceCallableStatement; +import com.fanruan.service.jdbc.statement.ServicePreparedStatement; import com.fanruan.service.jdbc.statement.ServiceStatement; -import org.hsqldb.jdbc.JDBCBlob; -import org.hsqldb.jdbc.JDBCClob; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; - -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; +import org.hsqldb.jdbc.*; +import org.hsqldb.types.Type; +import org.junit.jupiter.api.*; + +import java.io.*; import java.math.BigDecimal; import java.net.URL; import java.sql.*; +import java.util.HashMap; +import java.util.Map; /** * @author Yichen Dai @@ -153,9 +150,10 @@ public class ResultSetTest extends BaseJDBCTest{ Assertions.assertEquals('a', resultSet.getCharacterStream(11).read()); Assertions.assertEquals('a', resultSet.getCharacterStream("testClob").read()); - Assertions.assertNull(resultSet.getCursorName()); Assertions.assertNotNull(resultSet.getMetaData()); + + closeSQLObjects(statement, prepareStatement); } @Test @@ -172,4 +170,295 @@ public class ResultSetTest extends BaseJDBCTest{ Assertions.assertThrows(SQLException.class, () -> resultSet.unwrap(null)); Assertions.assertFalse(resultSet.isWrapperFor(null)); } + + @Test + public void testBlob() throws SQLException, IOException { + Statement statement = connection.createStatement(); + + statement.execute("create table TestBlob" + + " (testBlob BLOB(1024));" + ); + + PreparedStatement prepareStatement = connection.prepareStatement("INSERT INTO TestBlob VALUES(?);"); + byte[] chuck = { + (byte)0x0f, (byte)0x00, (byte)0x00, + (byte)0x00, (byte)0x00, (byte)0x00 + }; + + Blob blob1 = new JDBCBlob(chuck); + prepareStatement.setBlob(1, blob1); + prepareStatement.executeUpdate(); + + ResultSet resultSet = statement.executeQuery("SELECT * FROM TestBlob"); + resultSet.next(); + Blob blob2 = resultSet.getBlob(1); + + Assertions.assertEquals(blob1.length(), blob2.length()); + Assertions.assertEquals(blob1.getBytes(1,1)[0], blob2.getBytes(1,1)[0]); + Assertions.assertEquals(blob1.getBinaryStream().read(), blob2.getBinaryStream().read()); + Assertions.assertEquals(blob1.getBinaryStream(1, 6).read(), blob2.getBinaryStream(1, 6).read()); + + ServiceBlob serviceBlob = (ServiceBlob) blob2; + String IDtoInvoke = serviceBlob.getID(); + DispatcherImpl dispatcher = agent.dispatcherImpl; + BeanCacheImpl beanCache = dispatcher.CACHE.getBeanCache(dbName); + ServiceBlob blob3 = (ServiceBlob) ProxyFactoryIT.getProxy(ServiceBlob.class, map); + + Assertions.assertThrows(SQLFeatureNotSupportedException.class, () -> { + blob3.position(chuck, 1); + RpcRequest request = map.get(null); + request.setIDToInvoke(IDtoInvoke); + dispatcher.invokeAsRequest( + request, + beanCache); + } + ); + + Assertions.assertThrows(SQLFeatureNotSupportedException.class, () -> { + blob3.position(blob1, 1); + RpcRequest request = map.get(null); + request.setIDToInvoke(IDtoInvoke); + dispatcher.invokeAsRequest( + request, + beanCache); + } + ); + + Assertions.assertThrows(SQLException.class, () -> { + blob3.setBytes(1, chuck); + RpcRequest request = map.get(null); + request.setIDToInvoke(IDtoInvoke); + dispatcher.invokeAsRequest( + request, + beanCache); + } + ); + + Assertions.assertThrows(SQLException.class, () -> { + blob3.setBytes(1, chuck, 1, 6); + RpcRequest request = map.get(null); + request.setIDToInvoke(IDtoInvoke); + dispatcher.invokeAsRequest( + request, + beanCache); + } + ); + + blob2.truncate(6); + blob2.free(); + + closeSQLObjects(statement, prepareStatement); + } + + @Test + public void testClob() throws SQLException, IOException { + Statement statement = connection.createStatement(); + + statement.execute("create table TestClob" + + " (testClob varchar(20));" + ); + + PreparedStatement prepareStatement = connection.prepareStatement("INSERT INTO TestClob VALUES(?);"); + + Clob clob1 = new JDBCClob("abcd"); + + prepareStatement.setClob(1, clob1); + prepareStatement.executeUpdate(); + + ResultSet resultSet = statement.executeQuery("SELECT * FROM TestClob"); + resultSet.next(); + Clob clob2 = resultSet.getClob(1); + + Assertions.assertEquals(clob1.length(), clob2.length()); + Assertions.assertEquals(clob1.getSubString(1,2), clob1.getSubString(1,2)); + Assertions.assertEquals(clob1.getCharacterStream().read(), clob2.getCharacterStream().read()); + Assertions.assertEquals(clob1.getCharacterStream(1, 2).read(), clob2.getCharacterStream(1, 2).read()); + Assertions.assertEquals(clob1.getCharacterStream().read(), clob2.getCharacterStream().read()); + Assertions.assertEquals(clob1.getCharacterStream(1, 2).read(), clob2.getCharacterStream(1, 2).read()); + Assertions.assertEquals(clob1.position("abcd", 1), clob2.position("abcd", 1)); + Assertions.assertEquals(clob1.position(clob1, 1), clob2.position(clob1, 1)); + + clob2.getCharacterStream().close(); + clob2.getAsciiStream().close(); + + ServiceClob serviceClob = (ServiceClob) clob2; + String IDtoInvoke = serviceClob.getID(); + DispatcherImpl dispatcher = agent.dispatcherImpl; + BeanCacheImpl beanCache = dispatcher.CACHE.getBeanCache(dbName); + ServiceClob clob3 = (ServiceClob) ProxyFactoryIT.getProxy(ServiceClob.class, map); + + Assertions.assertThrows(SQLException.class, () -> { + clob3.setString(1, ""); + RpcRequest request = map.get(null); + request.setIDToInvoke(IDtoInvoke); + dispatcher.invokeAsRequest( + request, + beanCache); + } + ); + + Assertions.assertThrows(SQLException.class, () -> { + clob3.setString(1, "chuck", 1, 6); + RpcRequest request = map.get(null); + request.setIDToInvoke(IDtoInvoke); + dispatcher.invokeAsRequest( + request, + beanCache); + } + ); + + Assertions.assertThrows(SQLException.class, () -> { + clob3.setAsciiStream(1); + RpcRequest request = map.get(null); + request.setIDToInvoke(IDtoInvoke); + dispatcher.invokeAsRequest( + request, + beanCache); + } + ); + + Assertions.assertThrows(SQLException.class, () -> { + clob3.setCharacterStream(1); + RpcRequest request = map.get(null); + request.setIDToInvoke(IDtoInvoke); + dispatcher.invokeAsRequest( + request, + beanCache); + } + ); + closeSQLObjects(statement, prepareStatement); + } + + @Test + public void testArray() throws SQLException { + Connection connection = getConnection(); + Statement statement = connection.createStatement(); + + statement.execute("create table TestArray" + + " (testArray DOUBLE ARRAY);" + ); + + statement.executeUpdate("INSERT INTO TestArray VALUES ARRAY[3.45, 23.64, 14.01]"); + + ResultSet resultSet = statement.executeQuery("SELECT * FROM TestArray"); + resultSet.next(); + Array array = resultSet.getArray(1); + + Assertions.assertEquals("DOUBLE", array.getBaseTypeName()); + Assertions.assertEquals(Types.DOUBLE, array.getBaseType()); + Map> map = new HashMap<>(); + map.put("DOUBLE", Double.class); + Object o1 = array.getArray(); + Object[] arr1 = (Object[]) o1; + Assertions.assertEquals(3.45, arr1[0]); + Object o2 = array.getArray(map); + Object[] arr2 = (Object[]) o2; + Assertions.assertEquals(3.45, arr2[0]); + Object o3 = array.getArray(1, 1); + Object[] arr3 = (Object[]) o3; + Assertions.assertEquals(3.45, arr3[0]); + Object o4 = array.getArray(1, 1, map); + Object[] arr4 = (Object[]) o4; + Assertions.assertEquals(3.45, arr4[0]); + + ResultSet resultSet1 = array.getResultSet(); + Assertions.assertEquals(3, getSizeOfResultSet(resultSet1)); + + ResultSet resultSet2 = array.getResultSet(map); + Assertions.assertEquals(3, getSizeOfResultSet(resultSet2)); + + ResultSet resultSet3 = array.getResultSet(1, 1); + Assertions.assertEquals(1, getSizeOfResultSet(resultSet3)); + + ResultSet resultSet4 = array.getResultSet(1, 1, map); + Assertions.assertEquals(1, getSizeOfResultSet(resultSet4)); + array.free(); + } + + @Test + @Disabled("hsql not support") + public void testStruct() throws SQLException { + } + + @Test + public void testNClob() throws SQLException, IOException { + Statement statement = connection.createStatement(); + + statement.execute("create table TestNClob" + + " (testNClob varchar(20));" + ); + + PreparedStatement prepareStatement = connection.prepareStatement("INSERT INTO TestNClob VALUES(?);"); + + NClob nClob1 = new JDBCNClob("abcd"); + + prepareStatement.setNClob(1, nClob1); + prepareStatement.executeUpdate(); + + ResultSet resultSet = statement.executeQuery("SELECT * FROM TestNClob"); + resultSet.next(); + NClob nClob2 = resultSet.getNClob(1); + + Assertions.assertEquals(nClob1.length(), nClob2.length()); + Assertions.assertEquals(nClob1.getSubString(1,2), nClob2.getSubString(1,2)); + nClob1.getCharacterStream().read(); + Reader reader = nClob2.getCharacterStream(); + reader.read(); +// Assertions.assertEquals(nClob1.getCharacterStream().read(), nClob2.getCharacterStream().read()); + Assertions.assertEquals(nClob1.getCharacterStream(1, 2).read(), nClob2.getCharacterStream(1, 2).read()); + Assertions.assertEquals(nClob1.getCharacterStream().read(), nClob2.getCharacterStream().read()); + Assertions.assertEquals(nClob1.getCharacterStream(1, 2).read(), nClob2.getCharacterStream(1, 2).read()); + Assertions.assertEquals(nClob1.position("abcd", 1), nClob2.position("abcd", 1)); + Assertions.assertEquals(nClob1.position(nClob1, 1), nClob2.position(nClob1, 1)); + + nClob2.getCharacterStream().close(); + nClob2.getAsciiStream().close(); + + ServiceNClob serviceNClob = (ServiceNClob) nClob2; + String IDtoInvoke = serviceNClob.getID(); + DispatcherImpl dispatcher = agent.dispatcherImpl; + BeanCacheImpl beanCache = dispatcher.CACHE.getBeanCache(dbName); + ServiceNClob nClob3 = (ServiceNClob) ProxyFactoryIT.getProxy(ServiceNClob.class, map); + + Assertions.assertThrows(SQLException.class, () -> { + nClob3.setString(1, ""); + RpcRequest request = map.get(null); + request.setIDToInvoke(IDtoInvoke); + dispatcher.invokeAsRequest( + request, + beanCache); + } + ); + + Assertions.assertThrows(SQLException.class, () -> { + nClob3.setString(1, "chuck", 1, 6); + RpcRequest request = map.get(null); + request.setIDToInvoke(IDtoInvoke); + dispatcher.invokeAsRequest( + request, + beanCache); + } + ); + + Assertions.assertThrows(SQLException.class, () -> { + nClob3.setAsciiStream(1); + RpcRequest request = map.get(null); + request.setIDToInvoke(IDtoInvoke); + dispatcher.invokeAsRequest( + request, + beanCache); + } + ); + + Assertions.assertThrows(SQLException.class, () -> { + nClob3.setCharacterStream(1); + RpcRequest request = map.get(null); + request.setIDToInvoke(IDtoInvoke); + dispatcher.invokeAsRequest( + request, + beanCache); + } + ); + closeSQLObjects(statement, prepareStatement); + } } diff --git a/test/src/test/java/com/fanruan/SavePointTest.java b/test/src/test/java/com/fanruan/SavePointTest.java new file mode 100644 index 0000000..5759494 --- /dev/null +++ b/test/src/test/java/com/fanruan/SavePointTest.java @@ -0,0 +1,34 @@ +package com.fanruan; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; + +import java.sql.*; + +/** + * @author Yichen Dai + * @date 2022/9/13 16:31 + */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class SavePointTest extends BaseJDBCTest{ + Connection connection = null; + + @BeforeAll + public void setUp() throws SQLException { + openSocket(); + this.connection = getConnection(); + } + + @Test + void testSavePoint() throws SQLException{ + Statement statement = connection.createStatement(); + connection.setAutoCommit(false); + Assertions.assertThrows(SQLException.class, () -> connection.setSavepoint()); + Assertions.assertThrows(SQLException.class, () -> connection.setSavepoint("")); + Assertions.assertThrows(SQLException.class, () -> connection.rollback()); + Assertions.assertThrows(SQLException.class, () -> connection.rollback(new FakeSavepoint())); + Assertions.assertThrows(SQLException.class, () -> connection.releaseSavepoint(new FakeSavepoint())); + } +} diff --git a/test/src/test/java/com/fanruan/StatementTest.java b/test/src/test/java/com/fanruan/StatementTest.java new file mode 100644 index 0000000..4d5cbcd --- /dev/null +++ b/test/src/test/java/com/fanruan/StatementTest.java @@ -0,0 +1,171 @@ +package com.fanruan; + +import org.junit.jupiter.api.*; + +import java.sql.*; + +/** + * @author Yichen Dai + * @date 2022/9/6 9:17 + */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class StatementTest extends BaseJDBCTest{ + + Connection connection = null; + Statement statement = null; + + @BeforeAll + public void setUp() throws SQLException { + openSocket(); + connection = getConnection(); + this.statement = connection.createStatement(); + } + + @Test + public void testGetConnection() throws SQLException{ + Assertions.assertEquals(this.connection, statement.getConnection()); + } + + @Test + public void testFieldSize() throws SQLException{ + Statement statement = connection.createStatement(); + statement.setMaxFieldSize(1000); + // HSQLDB always returns zero, meaning there is no limit. + Assertions.assertEquals(0, statement.getMaxFieldSize()); + closeSQLObjects(statement); + } + + @Test + public void testMaxRows() throws SQLException{ + Statement statement = connection.createStatement(); + statement.setMaxRows(1000); + Assertions.assertEquals(1000, statement.getMaxRows()); + closeSQLObjects(statement); + } + + @Test + public void testSet() throws SQLException{ + Statement statement = connection.createStatement(); + statement.setEscapeProcessing(false); + + statement.setCursorName("name"); + + statement.setPoolable(false); + + closeSQLObjects(statement); + } + + @Test + public void testQueryTimeout() throws SQLException{ + Statement statement = connection.createStatement(); + statement.setQueryTimeout(2000); + Assertions.assertEquals(2000, statement.getQueryTimeout()); + closeSQLObjects(statement); + } + + @Test + public void testWarnings() throws SQLException{ + Statement statement = connection.createStatement(); + statement.clearWarnings(); + Assertions.assertNull(statement.getWarnings()); + closeSQLObjects(statement); + } + + @Test + public void testGetResultSet() throws SQLException{ + Statement statement = connection.createStatement(); + + String SQL_DROP_IF_EXIST = "DROP TABLE IF EXISTS EMPLOYEE;"; + + String SQL_INSERT = "INSERT INTO EMPLOYEE VALUES 'Tim';"; + + String SQL_CREATE = "CREATE TABLE EMPLOYEE (NAME varchar(100));"; + + statement.execute(SQL_DROP_IF_EXIST); + statement.execute(SQL_CREATE); + statement.executeUpdate(SQL_INSERT); + + statement.execute("SELECT * FROM EMPLOYEE"); + + ResultSet resultSet = statement.getResultSet(); + Assertions.assertTrue(resultSet.next()); + closeSQLObjects(statement); + } + + @Test + public void testDirection() throws SQLException { + Statement statement = connection.createStatement(); + // Starting with 2.0, HSQLDB accepts any valid value. + statement.setFetchDirection(ResultSet.FETCH_FORWARD); + Assertions.assertEquals(ResultSet.FETCH_FORWARD, statement.getFetchDirection()); + closeSQLObjects(statement); + } + + @Test + public void testFetchSize() throws SQLException{ + Statement statement = connection.createStatement(); + statement.setFetchSize(0); + + Assertions.assertEquals(0, statement.getFetchSize()); + closeSQLObjects(statement); + } + + @Test + public void testWrap() throws SQLException { + Assertions.assertFalse(statement.isWrapperFor(null)); + Assertions.assertThrows(SQLException.class, () -> statement.unwrap(null)); + } + + @Test + public void testExecuteBatch() throws SQLException{ + String SQL_DROP_IF_EXIST = "DROP TABLE IF EXISTS EMPLOYEE;"; + + String SQL_INSERT = "INSERT INTO EMPLOYEE VALUES 'Tim';"; + + String SQL_CREATE = "CREATE TABLE EMPLOYEE (NAME varchar(100));"; + + Statement statement = connection.createStatement(); + statement.execute(SQL_DROP_IF_EXIST); + statement.execute(SQL_CREATE); + statement.clearBatch(); + + statement.addBatch(SQL_INSERT); + statement.addBatch(SQL_INSERT); + statement.addBatch(SQL_INSERT); + + int[] count = statement.executeBatch(); + + Assertions.assertEquals(3, count.length); + closeSQLObjects(statement); + } + + @Test + public void testGetResultSetInfo() throws SQLException { + Statement statement = connection.createStatement(); + + // HSQLDB supports CONCUR_READ_ONLY and CONCUR_UPDATABLE concurrency. + Assertions.assertEquals(ResultSet.CONCUR_READ_ONLY, statement.getResultSetConcurrency()); + + // HSQLDB 1.7.0 and later versions support TYPE_FORWARD_ONLY and TYPE_SCROLL_INSENSITIVE. + Assertions.assertEquals(ResultSet.TYPE_FORWARD_ONLY, statement.getResultSetType()); + + closeSQLObjects(statement); + } + + @Test + public void testGenerateKeys() throws SQLException{ + Statement statement = connection.createStatement(); + Assertions.assertEquals(0, getSizeOfResultSet(statement.getGeneratedKeys())); + closeSQLObjects(statement); + } + + @Test + void testHoldability() throws SQLException{ + Statement statement = connection.createStatement(); + + // HSQLDB returns true for both alternatives. + Assertions.assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, statement.getResultSetHoldability()); + + closeSQLObjects(statement); + } +}