Skip to content

Commit 5d644bb

Browse files
committed
Modernize some code
1 parent 9d8906a commit 5d644bb

File tree

1 file changed

+36
-44
lines changed

1 file changed

+36
-44
lines changed

src/java/arjdbc/jdbc/RubyJdbcConnection.java

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -610,11 +610,7 @@ public final IRubyObject connection(final ThreadContext context, final IRubyObje
610610
return convertJavaToRuby( connection.unwrap(Connection.class) );
611611
}
612612
}
613-
catch (AbstractMethodError e) {
614-
debugStackTrace(context, e);
615-
warn(context, "driver/pool connection does not support unwrapping: " + e);
616-
}
617-
catch (SQLException e) {
613+
catch (AbstractMethodError | SQLException e) {
618614
debugStackTrace(context, e);
619615
warn(context, "driver/pool connection does not support unwrapping: " + e);
620616
}
@@ -860,27 +856,25 @@ private static String[] createStatementPk(IRubyObject pk) {
860856
*/
861857
@JRubyMethod(name = "execute_insert_pk", required = 2)
862858
public IRubyObject execute_insert_pk(final ThreadContext context, final IRubyObject sql, final IRubyObject pk) {
863-
return withConnection(context, new Callable<IRubyObject>() {
864-
public IRubyObject call(final Connection connection) throws SQLException {
865-
Statement statement = null;
866-
final String query = sqlString(sql);
867-
try {
868-
869-
statement = createStatement(context, connection);
859+
return withConnection(context, connection -> {
860+
Statement statement = null;
861+
final String query = sqlString(sql);
862+
try {
870863

871-
if (pk == context.nil || pk == context.fals || !supportsGeneratedKeys(connection)) {
872-
statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
873-
} else {
874-
statement.executeUpdate(query, createStatementPk(pk));
875-
}
864+
statement = createStatement(context, connection);
876865

877-
return mapGeneratedKeys(context, connection, statement);
878-
} catch (final SQLException e) {
879-
debugErrorSQL(context, query);
880-
throw e;
881-
} finally {
882-
close(statement);
866+
if (pk == context.nil || pk == context.fals || !supportsGeneratedKeys(connection)) {
867+
statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
868+
} else {
869+
statement.executeUpdate(query, createStatementPk(pk));
883870
}
871+
872+
return mapGeneratedKeys(context, connection, statement);
873+
} catch (final SQLException e) {
874+
debugErrorSQL(context, query);
875+
throw e;
876+
} finally {
877+
close(statement);
884878
}
885879
});
886880
}
@@ -903,26 +897,24 @@ public IRubyObject execute_insert(final ThreadContext context, final IRubyObject
903897
@JRubyMethod(name = "execute_insert_pk", required = 3)
904898
public IRubyObject execute_insert_pk(final ThreadContext context, final IRubyObject sql, final IRubyObject binds,
905899
final IRubyObject pk) {
906-
return withConnection(context, new Callable<IRubyObject>() {
907-
public IRubyObject call(final Connection connection) throws SQLException {
908-
PreparedStatement statement = null;
909-
final String query = sqlString(sql);
910-
try {
911-
if (pk == context.nil || pk == context.fals || !supportsGeneratedKeys(connection)) {
912-
statement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
913-
} else {
914-
statement = connection.prepareStatement(query, createStatementPk(pk));
915-
}
916-
917-
setStatementParameters(context, connection, statement, (RubyArray) binds);
918-
statement.executeUpdate();
919-
return mapGeneratedKeys(context, connection, statement);
920-
} catch (final SQLException e) {
921-
debugErrorSQL(context, query);
922-
throw e;
923-
} finally {
924-
close(statement);
900+
return withConnection(context, connection -> {
901+
PreparedStatement statement = null;
902+
final String query = sqlString(sql);
903+
try {
904+
if (pk == context.nil || pk == context.fals || !supportsGeneratedKeys(connection)) {
905+
statement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
906+
} else {
907+
statement = connection.prepareStatement(query, createStatementPk(pk));
925908
}
909+
910+
setStatementParameters(context, connection, statement, (RubyArray) binds);
911+
statement.executeUpdate();
912+
return mapGeneratedKeys(context, connection, statement);
913+
} catch (final SQLException e) {
914+
debugErrorSQL(context, query);
915+
throw e;
916+
} finally {
917+
close(statement);
926918
}
927919
});
928920
}
@@ -1012,12 +1004,12 @@ public IRubyObject execute_query_raw(final ThreadContext context, final IRubyObj
10121004
binds = null;
10131005
} else { // (sql, binds)
10141006
maxRows = 0;
1015-
binds = (RubyArray) TypeConverter.checkArrayType(args[1]);
1007+
binds = (RubyArray) TypeConverter.checkArrayType(context, args[1]);
10161008
}
10171009
break;
10181010
case 3: // (sql, max_rows, binds)
10191011
maxRows = RubyNumeric.fix2int(args[1]);
1020-
binds = (RubyArray) TypeConverter.checkArrayType(args[2]);
1012+
binds = (RubyArray) TypeConverter.checkArrayType(context, args[2]);
10211013
break;
10221014
default: // (sql) 1-arg
10231015
maxRows = 0;

0 commit comments

Comments
 (0)