Skip to content

Commit 566bde1

Browse files
committed
Support Statement.NO_GENERATED_KEYS option
Add a proper behaviour when Connection.prepareStatement, Statement.execute, and Statement.executeUpdate are called with the Statement.NO_GENERATED_KEYS flag. Return an empty generated keys when the Statement.NO_GENERATED_KEYS is set as a special case of a generated keys absence. Fixes: #78
1 parent aaab7cd commit 566bde1

File tree

7 files changed

+108
-8
lines changed

7 files changed

+108
-8
lines changed

Diff for: src/main/java/org/tarantool/jdbc/SQLConnection.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.tarantool.CommunicationException;
44
import org.tarantool.JDBCBridge;
55
import org.tarantool.TarantoolConnection;
6+
import org.tarantool.util.JdbcConstants;
67
import org.tarantool.util.SQLStates;
78

89
import java.io.IOException;
@@ -40,7 +41,6 @@
4041
import static org.tarantool.jdbc.SQLDriver.PROP_SOCKET_TIMEOUT;
4142
import static org.tarantool.jdbc.SQLDriver.PROP_USER;
4243

43-
@SuppressWarnings("Since15")
4444
public class SQLConnection implements Connection {
4545

4646
private static final int UNSET_HOLDABILITY = 0;
@@ -341,7 +341,12 @@ public CallableStatement prepareCall(String sql, int resultSetType, int resultSe
341341

342342
@Override
343343
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
344-
throw new SQLFeatureNotSupportedException();
344+
checkNotClosed();
345+
JdbcConstants.checkGeneratedKeysConstant(autoGeneratedKeys);
346+
if (autoGeneratedKeys != Statement.NO_GENERATED_KEYS) {
347+
throw new SQLFeatureNotSupportedException();
348+
}
349+
return prepareStatement(sql);
345350
}
346351

347352
@Override

Diff for: src/main/java/org/tarantool/jdbc/SQLStatement.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.tarantool.jdbc;
22

33
import org.tarantool.JDBCBridge;
4+
import org.tarantool.util.JdbcConstants;
45

56
import java.sql.Connection;
67
import java.sql.ResultSet;
@@ -220,12 +221,18 @@ public boolean getMoreResults(int current) throws SQLException {
220221

221222
@Override
222223
public ResultSet getGeneratedKeys() throws SQLException {
223-
throw new SQLFeatureNotSupportedException();
224+
checkNotClosed();
225+
return new SQLResultSet(JDBCBridge.EMPTY, this);
224226
}
225227

226228
@Override
227229
public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
228-
throw new SQLFeatureNotSupportedException();
230+
checkNotClosed();
231+
JdbcConstants.checkGeneratedKeysConstant(autoGeneratedKeys);
232+
if (autoGeneratedKeys != Statement.NO_GENERATED_KEYS) {
233+
throw new SQLFeatureNotSupportedException();
234+
}
235+
return executeUpdate(sql);
229236
}
230237

231238
@Override
@@ -240,7 +247,12 @@ public int executeUpdate(String sql, String[] columnNames) throws SQLException {
240247

241248
@Override
242249
public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
243-
throw new SQLFeatureNotSupportedException();
250+
checkNotClosed();
251+
JdbcConstants.checkGeneratedKeysConstant(autoGeneratedKeys);
252+
if (autoGeneratedKeys != Statement.NO_GENERATED_KEYS) {
253+
throw new SQLFeatureNotSupportedException();
254+
}
255+
return execute(sql);
244256
}
245257

246258
@Override

Diff for: src/main/java/org/tarantool/util/JdbcConstants.java

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77

88
public class JdbcConstants {
99

10+
public static void checkGeneratedKeysConstant(int autoGeneratedKeys) throws SQLException {
11+
if (autoGeneratedKeys != Statement.NO_GENERATED_KEYS
12+
&& autoGeneratedKeys != Statement.RETURN_GENERATED_KEYS) {
13+
throw new SQLNonTransientException("", SQLStates.INVALID_PARAMETER_VALUE.getSqlState());
14+
}
15+
}
16+
1017
public static void checkHoldabilityConstant(int holdability) throws SQLException {
1118
if (holdability != ResultSet.CLOSE_CURSORS_AT_COMMIT
1219
&& holdability != ResultSet.HOLD_CURSORS_OVER_COMMIT) {

Diff for: src/test/java/org/tarantool/jdbc/JdbcConnectionIT.java

+19
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,23 @@ public void testPrepareHoldableStatement() throws SQLException {
192192
});
193193
}
194194

195+
@Test
196+
public void testGeneratedKeys() throws SQLException {
197+
String sql = "SELECT * FROM test";
198+
PreparedStatement preparedStatement = conn.prepareStatement(sql, Statement.NO_GENERATED_KEYS);
199+
assertNotNull(preparedStatement);
200+
preparedStatement.close();
201+
202+
assertThrows(SQLFeatureNotSupportedException.class, () -> conn.prepareStatement(sql, new int[] { 1 }));
203+
assertThrows(SQLFeatureNotSupportedException.class, () -> conn.prepareStatement(sql, new String[] { "id" }));
204+
205+
assertThrows(SQLException.class, () -> conn.prepareStatement(sql, Integer.MAX_VALUE));
206+
assertThrows(SQLException.class, () -> conn.prepareStatement(sql, Integer.MIN_VALUE));
207+
assertThrows(SQLException.class, () -> conn.prepareStatement(sql, -76));
208+
assertThrows(
209+
SQLFeatureNotSupportedException.class,
210+
() -> conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
211+
);
212+
}
195213
}
214+

Diff for: src/test/java/org/tarantool/jdbc/JdbcDatabaseMetaDataIT.java

+5
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,9 @@ public void testSupportsResultSetHoldability() throws SQLException {
254254
assertFalse(meta.supportsResultSetHoldability(42));
255255
}
256256

257+
@Test
258+
public void testSupportGeneratedKeys() throws SQLException {
259+
assertFalse(meta.supportsGetGeneratedKeys());
260+
}
261+
257262
}

Diff for: src/test/java/org/tarantool/jdbc/JdbcPreparedStatementIT.java

+15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.sql.PreparedStatement;
1010
import java.sql.ResultSet;
1111
import java.sql.SQLException;
12+
import java.sql.Statement;
1213

1314
import static org.junit.jupiter.api.Assertions.assertEquals;
1415
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -104,6 +105,8 @@ public void testExecuteReturnsUpdateCount() throws Exception {
104105

105106
assertEquals("ten", getRow("test", 10).get(1));
106107
assertEquals("twenty", getRow("test", 20).get(1));
108+
109+
conn.createStatement().execute("DELETE FROM test WHERE id IN (10, 20)");
107110
}
108111

109112
@Test void testForbiddenMethods() throws SQLException {
@@ -161,6 +164,18 @@ public void execute() throws Throwable {
161164
assertEquals(3, i);
162165
}
163166

167+
@Test
168+
public void testSupportGeneratedKeys() throws SQLException {
169+
prep = conn.prepareStatement("INSERT INTO test values (50, 'fifty')", Statement.NO_GENERATED_KEYS);
170+
assertFalse(prep.execute());
171+
assertEquals(1, prep.getUpdateCount());
172+
173+
ResultSet generatedKeys = prep.getGeneratedKeys();
174+
assertNotNull(generatedKeys);
175+
assertEquals(ResultSet.TYPE_FORWARD_ONLY, generatedKeys.getType());
176+
assertEquals(ResultSet.CONCUR_READ_ONLY, generatedKeys.getConcurrency());
177+
}
178+
164179
@Test
165180
public void testSetByte() throws SQLException {
166181
makeHelper(Byte.class)

Diff for: src/test/java/org/tarantool/jdbc/JdbcStatementIT.java

+40-3
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,14 @@ public void testClosedConnection() throws Exception {
7878
@Override
7979
public void execute() throws Throwable {
8080
switch (step) {
81-
case 0: stmt.executeQuery("TEST");
81+
case 0:
82+
stmt.executeQuery("TEST");
8283
break;
83-
case 1: stmt.executeUpdate("TEST");
84+
case 1:
85+
stmt.executeUpdate("TEST");
8486
break;
85-
case 2: stmt.execute("TEST");
87+
case 2:
88+
stmt.execute("TEST");
8689
break;
8790
default:
8891
fail();
@@ -93,4 +96,38 @@ public void execute() throws Throwable {
9396
}
9497
assertEquals(3, i);
9598
}
99+
100+
@Test
101+
public void testSupportedGeneratedKeys() throws SQLException {
102+
int affectedRows = stmt.executeUpdate(
103+
"INSERT INTO test(id, val) VALUES (50, 'fifty')",
104+
Statement.NO_GENERATED_KEYS
105+
);
106+
assertEquals(1, affectedRows);
107+
ResultSet generatedKeys = stmt.getGeneratedKeys();
108+
assertNotNull(generatedKeys);
109+
assertEquals(ResultSet.TYPE_FORWARD_ONLY, generatedKeys.getType());
110+
assertEquals(ResultSet.CONCUR_READ_ONLY, generatedKeys.getConcurrency());
111+
}
112+
113+
@Test
114+
void testUnsupportedGeneratedKeys() {
115+
assertThrows(
116+
SQLException.class,
117+
() -> stmt.executeUpdate(
118+
"INSERT INTO test(id, val) VALUES (100, 'hundred'), (1000, 'thousand')",
119+
Statement.RETURN_GENERATED_KEYS
120+
)
121+
);
122+
123+
int[] wrongConstants = {Integer.MAX_VALUE, Integer.MIN_VALUE, -31, 344};
124+
for (int wrongConstant : wrongConstants) {
125+
assertThrows(SQLException.class,
126+
() -> stmt.executeUpdate(
127+
"INSERT INTO test(id, val) VALUES (100, 'hundred'), (1000, 'thousand')",
128+
wrongConstant
129+
)
130+
);
131+
}
132+
}
96133
}

0 commit comments

Comments
 (0)