Unverified 提交 19f5f2de authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1276 from katzyn/misc

Improve support of ARRAY and SQLXML in JDBC layer
...@@ -872,21 +872,30 @@ public class JdbcCallableStatement extends JdbcPreparedStatement implements ...@@ -872,21 +872,30 @@ public class JdbcCallableStatement extends JdbcPreparedStatement implements
} }
/** /**
* [Not supported] Returns the value of the specified column as a SQLXML * Returns the value of the specified column as a SQLXML object.
* object. *
* @param parameterIndex the parameter index (1, 2, ...)
* @return the value
* @throws SQLException if the column is not found or if this object is
* closed
*/ */
@Override @Override
public SQLXML getSQLXML(int parameterIndex) throws SQLException { public SQLXML getSQLXML(int parameterIndex) throws SQLException {
throw unsupported("SQLXML"); checkRegistered(parameterIndex);
return getOpenResultSet().getSQLXML(parameterIndex);
} }
/** /**
* [Not supported] Returns the value of the specified column as a SQLXML * Returns the value of the specified column as a SQLXML object.
* object. *
* @param parameterName the parameter name
* @return the value
* @throws SQLException if the column is not found or if this object is
* closed
*/ */
@Override @Override
public SQLXML getSQLXML(String parameterName) throws SQLException { public SQLXML getSQLXML(String parameterName) throws SQLException {
throw unsupported("SQLXML"); return getSQLXML(getIndexForName(parameterName));
} }
/** /**
...@@ -1584,12 +1593,16 @@ public class JdbcCallableStatement extends JdbcPreparedStatement implements ...@@ -1584,12 +1593,16 @@ public class JdbcCallableStatement extends JdbcPreparedStatement implements
} }
/** /**
* [Not supported] Sets the value of a parameter as a SQLXML object. * Sets the value of a parameter as a SQLXML object.
*
* @param parameterName the parameter name
* @param x the value
* @throws SQLException if this object is closed
*/ */
@Override @Override
public void setSQLXML(String parameterName, SQLXML x) public void setSQLXML(String parameterName, SQLXML x)
throws SQLException { throws SQLException {
throw unsupported("SQLXML"); setSQLXML(getIndexForName(parameterName), x);
} }
/** /**
......
...@@ -2489,19 +2489,57 @@ public class JdbcResultSet extends TraceObject implements ResultSet, JdbcResultS ...@@ -2489,19 +2489,57 @@ public class JdbcResultSet extends TraceObject implements ResultSet, JdbcResultS
} }
/** /**
* [Not supported] * Updates a column in the current or insert row.
*
* @param columnIndex (1,2,...)
* @param x the value
* @param length the length
* @throws SQLException if the result set is closed or not updatable
*/ */
@Override @Override
public void updateArray(int columnIndex, Array x) throws SQLException { public void updateArray(int columnIndex, Array x) throws SQLException {
throw unsupported("setArray"); try {
if (isDebugEnabled()) {
debugCode("updateArray(" + columnIndex + ", x);");
}
checkClosed();
Value v;
if (x == null) {
v = ValueNull.INSTANCE;
} else {
v = DataType.convertToValue(stat.session, x.getArray(), Value.ARRAY);
}
update(columnIndex, v);
} catch (Exception e) {
throw logAndConvert(e);
}
} }
/** /**
* [Not supported] * Updates a column in the current or insert row.
*
* @param columnLabel the column label
* @param x the value
* @param length the length
* @throws SQLException if the result set is closed or not updatable
*/ */
@Override @Override
public void updateArray(String columnLabel, Array x) throws SQLException { public void updateArray(String columnLabel, Array x) throws SQLException {
throw unsupported("setArray"); try {
if (isDebugEnabled()) {
debugCode("updateArray(" + quote(columnLabel) + ", x);");
}
checkClosed();
Value v;
if (x == null) {
v = ValueNull.INSTANCE;
} else {
v = DataType.convertToValue(stat.session, x.getArray(), Value.ARRAY);
}
update(columnLabel, v);
} catch (Exception e) {
throw logAndConvert(e);
}
} }
/** /**
......
...@@ -7,6 +7,7 @@ package org.h2.value; ...@@ -7,6 +7,7 @@ package org.h2.value;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays; import java.util.Arrays;
import org.h2.engine.Constants; import org.h2.engine.Constants;
...@@ -135,8 +136,8 @@ public class ValueArray extends Value { ...@@ -135,8 +136,8 @@ public class ValueArray extends Value {
} }
@Override @Override
public void set(PreparedStatement prep, int parameterIndex) { public void set(PreparedStatement prep, int parameterIndex) throws SQLException {
throw throwUnsupportedExceptionForType("PreparedStatement.set"); prep.setArray(parameterIndex, prep.getConnection().createArrayOf("NULL", (Object[]) getObject()));
} }
@Override @Override
......
...@@ -91,8 +91,6 @@ public class TestCallableStatement extends TestDb { ...@@ -91,8 +91,6 @@ public class TestCallableStatement extends TestDb {
getRef(1); getRef(1);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, call). assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, call).
getRowId(1); getRowId(1);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, call).
getSQLXML(1);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, call). assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, call).
getURL("a"); getURL("a");
...@@ -102,8 +100,6 @@ public class TestCallableStatement extends TestDb { ...@@ -102,8 +100,6 @@ public class TestCallableStatement extends TestDb {
getRef("a"); getRef("a");
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, call). assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, call).
getRowId("a"); getRowId("a");
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, call).
getSQLXML("a");
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, call). assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, call).
setURL(1, (URL) null); setURL(1, (URL) null);
...@@ -116,9 +112,6 @@ public class TestCallableStatement extends TestDb { ...@@ -116,9 +112,6 @@ public class TestCallableStatement extends TestDb {
setURL("a", (URL) null); setURL("a", (URL) null);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, call). assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, call).
setRowId("a", (RowId) null); setRowId("a", (RowId) null);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, call).
setSQLXML("a", (SQLXML) null);
} }
private void testCallWithResultSet(Connection conn) throws SQLException { private void testCallWithResultSet(Connection conn) throws SQLException {
...@@ -332,6 +325,8 @@ public class TestCallableStatement extends TestDb { ...@@ -332,6 +325,8 @@ public class TestCallableStatement extends TestDb {
assertEquals("ABC", call.getClob("B").getSubString(1, 3)); assertEquals("ABC", call.getClob("B").getSubString(1, 3));
assertEquals("ABC", call.getNClob(2).getSubString(1, 3)); assertEquals("ABC", call.getNClob(2).getSubString(1, 3));
assertEquals("ABC", call.getNClob("B").getSubString(1, 3)); assertEquals("ABC", call.getNClob("B").getSubString(1, 3));
assertEquals("ABC", call.getSQLXML(2).getString());
assertEquals("ABC", call.getSQLXML("B").getString());
try { try {
call.getString(100); call.getString(100);
...@@ -397,6 +392,11 @@ public class TestCallableStatement extends TestDb { ...@@ -397,6 +392,11 @@ public class TestCallableStatement extends TestDb {
call.setNString("B", "xyz"); call.setNString("B", "xyz");
call.executeUpdate(); call.executeUpdate();
assertEquals("XYZ", call.getString("B")); assertEquals("XYZ", call.getString("B"));
SQLXML xml = conn.createSQLXML();
xml.setString("<x>xyz</x>");
call.setSQLXML("B", xml);
call.executeUpdate();
assertEquals("<X>XYZ</X>", call.getString("B"));
// test for exceptions after closing // test for exceptions after closing
call.close(); call.close();
......
...@@ -165,10 +165,6 @@ public class TestResultSet extends TestDb { ...@@ -165,10 +165,6 @@ public class TestResultSet extends TestDb {
updateRef(1, (Ref) null); updateRef(1, (Ref) null);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, rs). assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, rs).
updateRef("x", (Ref) null); updateRef("x", (Ref) null);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, rs).
updateArray(1, (Array) null);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, rs).
updateArray("x", (Array) null);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, rs). assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, rs).
updateRowId(1, (RowId) null); updateRowId(1, (RowId) null);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, rs). assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, rs).
...@@ -1807,6 +1803,35 @@ public class TestResultSet extends TestDb { ...@@ -1807,6 +1803,35 @@ public class TestResultSet extends TestDb {
assertThrows(ErrorCode.OBJECT_CLOSED, array).getResultSet(); assertThrows(ErrorCode.OBJECT_CLOSED, array).getResultSet();
assertFalse(rs.next()); assertFalse(rs.next());
try (Statement s = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)) {
rs = s.executeQuery("SELECT * FROM TEST ORDER BY ID");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
rs.updateArray(2, conn.createArrayOf("INT", new Object[] {10, 20}));
rs.updateRow();
assertTrue(rs.next());
rs.updateArray("VALUE", conn.createArrayOf("INT", new Object[] {11, 22}));
rs.updateRow();
assertFalse(rs.next());
rs.moveToInsertRow();
rs.updateInt(1, 3);
rs.updateArray(2, null);
rs.insertRow();
}
rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertEquals(new Object[] {10, 20}, (Object[]) rs.getObject(2));
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertEquals(new Object[] {11, 22}, (Object[]) rs.getObject(2));
assertTrue(rs.next());
assertEquals(3, rs.getInt(1));
assertNull(rs.getObject(2));
assertFalse(rs.next());
stat.execute("DROP TABLE TEST"); stat.execute("DROP TABLE TEST");
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论