@@ -888,23 +888,45 @@ protected IRubyObject mapExecuteResult(final ThreadContext context,
888
888
return mapQueryResult (context , connection , resultSet );
889
889
}
890
890
891
+ private static String [] createStatementPk (IRubyObject pk ) {
892
+ String [] statementPk ;
893
+ if (pk instanceof RubyArray ) {
894
+ RubyArray ary = (RubyArray ) pk ;
895
+ int size = ary .size ();
896
+ statementPk = new String [size ];
897
+ for (int i = 0 ; i < size ; i ++) {
898
+ statementPk [i ] = sqlString (ary .eltInternal (i ));
899
+ }
900
+ } else {
901
+ statementPk = new String [] { sqlString (pk ) };
902
+ }
903
+ return statementPk ;
904
+ }
905
+
891
906
/**
892
907
* Executes an INSERT SQL statement
893
908
* @param context
894
909
* @param sql
910
+ * @param pk Rails PK
895
911
* @return ActiveRecord::Result
896
912
* @throws SQLException
897
913
*/
898
- @ JRubyMethod (name = "execute_insert" , required = 1 )
899
- public IRubyObject execute_insert (final ThreadContext context , final IRubyObject sql ) {
914
+ @ JRubyMethod (name = "execute_insert" , required = 2 )
915
+ public IRubyObject execute_insert (final ThreadContext context , final IRubyObject sql , final IRubyObject pk ) {
900
916
return withConnection (context , new Callable <IRubyObject >() {
901
917
public IRubyObject call (final Connection connection ) throws SQLException {
902
918
Statement statement = null ;
903
919
final String query = sqlString (sql );
904
920
try {
905
921
906
922
statement = createStatement (context , connection );
907
- statement .executeUpdate (query , Statement .RETURN_GENERATED_KEYS );
923
+
924
+ if (pk == context .nil || pk == context .fals || !supportsGeneratedKeys (connection )) {
925
+ statement .executeUpdate (query , Statement .RETURN_GENERATED_KEYS );
926
+ } else {
927
+ statement .executeUpdate (query , createStatementPk (pk ));
928
+ }
929
+
908
930
return mapGeneratedKeys (context , connection , statement );
909
931
910
932
} catch (final SQLException e ) {
@@ -922,18 +944,24 @@ public IRubyObject call(final Connection connection) throws SQLException {
922
944
* @param context
923
945
* @param sql
924
946
* @param binds RubyArray of values to be bound to the query
947
+ * @param pk Rails PK
925
948
* @return ActiveRecord::Result
926
949
* @throws SQLException
927
950
*/
928
- @ JRubyMethod (name = "execute_insert" , required = 2 )
929
- public IRubyObject execute_insert (final ThreadContext context , final IRubyObject sql , final IRubyObject binds ) {
951
+ @ JRubyMethod (name = "execute_insert" , required = 3 )
952
+ public IRubyObject execute_insert (final ThreadContext context , final IRubyObject sql , final IRubyObject binds ,
953
+ final IRubyObject pk ) {
930
954
return withConnection (context , new Callable <IRubyObject >() {
931
955
public IRubyObject call (final Connection connection ) throws SQLException {
932
956
PreparedStatement statement = null ;
933
957
final String query = sqlString (sql );
934
958
try {
959
+ if (pk == context .nil || pk == context .fals || !supportsGeneratedKeys (connection )) {
960
+ statement = connection .prepareStatement (query , Statement .RETURN_GENERATED_KEYS );
961
+ } else {
962
+ statement = connection .prepareStatement (query , createStatementPk (pk ));
963
+ }
935
964
936
- statement = connection .prepareStatement (query , Statement .RETURN_GENERATED_KEYS );
937
965
setStatementParameters (context , connection , statement , (RubyArray ) binds );
938
966
statement .executeUpdate ();
939
967
return mapGeneratedKeys (context , connection , statement );
0 commit comments