11package org .tarantool .jdbc ;
22
3+ import org .tarantool .SqlProtoUtils ;
4+ import org .tarantool .jdbc .type .TarantoolSqlType ;
35import org .tarantool .util .JdbcConstants ;
46import org .tarantool .util .SQLStates ;
57
1315import java .sql .SQLWarning ;
1416import java .sql .Statement ;
1517import java .util .ArrayList ;
18+ import java .util .Collections ;
1619import java .util .List ;
1720import java .util .concurrent .TimeUnit ;
1821import java .util .concurrent .atomic .AtomicBoolean ;
2730 */
2831public class SQLStatement implements TarantoolStatement {
2932
30- protected final SQLConnection connection ;
33+ private static final String GENERATED_KEY_COLUMN_NAME = "GENERATED_KEY" ;
34+
35+ protected final TarantoolConnection connection ;
36+ private final SQLResultSet emptyGeneratedKeys ;
3137
3238 /**
3339 * Current result set / update count associated to this statement.
3440 */
3541 protected SQLResultSet resultSet ;
3642 protected int updateCount ;
43+ protected SQLResultSet generatedKeys ;
3744
3845 private List <String > batchQueries = new ArrayList <>();
3946
@@ -61,10 +68,12 @@ public class SQLStatement implements TarantoolStatement {
6168 private final AtomicBoolean isClosed = new AtomicBoolean (false );
6269
6370 protected SQLStatement (SQLConnection sqlConnection ) throws SQLException {
64- this .connection = sqlConnection ;
65- this .resultSetType = ResultSet .TYPE_FORWARD_ONLY ;
66- this .resultSetConcurrency = ResultSet .CONCUR_READ_ONLY ;
67- this .resultSetHoldability = sqlConnection .getHoldability ();
71+ this (
72+ sqlConnection ,
73+ ResultSet .TYPE_FORWARD_ONLY ,
74+ ResultSet .CONCUR_READ_ONLY ,
75+ sqlConnection .getHoldability ()
76+ );
6877 }
6978
7079 protected SQLStatement (SQLConnection sqlConnection ,
@@ -75,37 +84,34 @@ protected SQLStatement(SQLConnection sqlConnection,
7584 this .resultSetType = resultSetType ;
7685 this .resultSetConcurrency = resultSetConcurrency ;
7786 this .resultSetHoldability = resultSetHoldability ;
87+ this .emptyGeneratedKeys = this .generatedKeys = executeGeneratedKeys (Collections .emptyList ());
7888 }
7989
8090 @ Override
8191 public ResultSet executeQuery (String sql ) throws SQLException {
8292 checkNotClosed ();
83- if (!executeInternal (sql )) {
93+ if (!executeInternal (NO_GENERATED_KEYS , sql )) {
8494 throw new SQLException ("No results were returned" , SQLStates .NO_DATA .getSqlState ());
8595 }
8696 return resultSet ;
8797 }
8898
8999 @ Override
90100 public int executeUpdate (String sql ) throws SQLException {
91- checkNotClosed ();
92- if (executeInternal (sql )) {
93- throw new SQLException (
94- "Result was returned but nothing was expected" ,
95- SQLStates .TOO_MANY_RESULTS .getSqlState ()
96- );
97- }
98- return updateCount ;
101+ return executeUpdate (sql , NO_GENERATED_KEYS );
99102 }
100103
101104 @ Override
102105 public int executeUpdate (String sql , int autoGeneratedKeys ) throws SQLException {
103106 checkNotClosed ();
104107 JdbcConstants .checkGeneratedKeysConstant (autoGeneratedKeys );
105- if (autoGeneratedKeys != Statement .NO_GENERATED_KEYS ) {
106- throw new SQLFeatureNotSupportedException ();
108+ if (executeInternal (autoGeneratedKeys , sql )) {
109+ throw new SQLException (
110+ "Result was returned but nothing was expected" ,
111+ SQLStates .TOO_MANY_RESULTS .getSqlState ()
112+ );
107113 }
108- return executeUpdate ( sql ) ;
114+ return updateCount ;
109115 }
110116
111117 @ Override
@@ -195,17 +201,14 @@ public void setCursorName(String name) throws SQLException {
195201 @ Override
196202 public boolean execute (String sql ) throws SQLException {
197203 checkNotClosed ();
198- return executeInternal (sql );
204+ return executeInternal (NO_GENERATED_KEYS , sql );
199205 }
200206
201207 @ Override
202208 public boolean execute (String sql , int autoGeneratedKeys ) throws SQLException {
203209 checkNotClosed ();
204210 JdbcConstants .checkGeneratedKeysConstant (autoGeneratedKeys );
205- if (autoGeneratedKeys != Statement .NO_GENERATED_KEYS ) {
206- throw new SQLFeatureNotSupportedException ();
207- }
208- return execute (sql );
211+ return executeInternal (autoGeneratedKeys , sql );
209212 }
210213
211214 @ Override
@@ -321,7 +324,7 @@ public Connection getConnection() throws SQLException {
321324 @ Override
322325 public ResultSet getGeneratedKeys () throws SQLException {
323326 checkNotClosed ();
324- return new SQLResultSet ( SQLResultHolder . ofEmptyQuery (), this ) ;
327+ return generatedKeys ;
325328 }
326329
327330 @ Override
@@ -401,6 +404,7 @@ protected void discardLastResults() throws SQLException {
401404 clearWarnings ();
402405 updateCount = -1 ;
403406 resultSet = null ;
407+ generatedKeys = emptyGeneratedKeys ;
404408
405409 if (lastResultSet != null ) {
406410 try {
@@ -419,7 +423,7 @@ protected void discardLastResults() throws SQLException {
419423 *
420424 * @return {@code true}, if the result is a ResultSet object;
421425 */
422- protected boolean executeInternal (String sql , Object ... params ) throws SQLException {
426+ protected boolean executeInternal (int autoGeneratedKeys , String sql , Object ... params ) throws SQLException {
423427 discardLastResults ();
424428 SQLResultHolder holder ;
425429 try {
@@ -433,6 +437,9 @@ protected boolean executeInternal(String sql, Object... params) throws SQLExcept
433437 resultSet = new SQLResultSet (holder , this );
434438 }
435439 updateCount = holder .getUpdateCount ();
440+ if (autoGeneratedKeys == Statement .RETURN_GENERATED_KEYS ) {
441+ generatedKeys = executeGeneratedKeys (holder .getGeneratedIds ());
442+ }
436443 return holder .isQueryResult ();
437444 }
438445
@@ -474,4 +481,13 @@ protected void checkNotClosed() throws SQLException {
474481 }
475482 }
476483
484+ protected SQLResultSet executeGeneratedKeys (List <Integer > generatedKeys ) throws SQLException {
485+ SqlProtoUtils .SQLMetaData sqlMetaData =
486+ new SqlProtoUtils .SQLMetaData (GENERATED_KEY_COLUMN_NAME , TarantoolSqlType .INTEGER );
487+ List <List <Object >> rows = generatedKeys .stream ()
488+ .map (Collections ::<Object >singletonList )
489+ .collect (Collectors .toList ());
490+ return createResultSet (SQLResultHolder .ofQuery (Collections .singletonList (sqlMetaData ), rows ));
491+ }
492+
477493}
0 commit comments