@@ -727,7 +727,10 @@ public synchronized IRubyObject reconnect(final ThreadContext context) {
727
727
728
728
private void connectImpl (final boolean forceConnection ) throws SQLException {
729
729
setConnection ( forceConnection ? newConnection () : null );
730
- if ( forceConnection ) configureConnection ();
730
+ if (forceConnection ) {
731
+ if (getConnectionImpl () == null ) throw new SQLException ("Didn't get a connection. Wrong URL?" );
732
+ configureConnection ();
733
+ }
731
734
}
732
735
733
736
@ JRubyMethod (name = "read_only?" )
@@ -885,23 +888,45 @@ protected IRubyObject mapExecuteResult(final ThreadContext context,
885
888
return mapQueryResult (context , connection , resultSet );
886
889
}
887
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
+
888
906
/**
889
907
* Executes an INSERT SQL statement
890
908
* @param context
891
909
* @param sql
910
+ * @param pk Rails PK
892
911
* @return ActiveRecord::Result
893
912
* @throws SQLException
894
913
*/
895
- @ JRubyMethod (name = "execute_insert " , required = 1 )
896
- public IRubyObject execute_insert (final ThreadContext context , final IRubyObject sql ) {
914
+ @ JRubyMethod (name = "execute_insert_pk " , required = 2 )
915
+ public IRubyObject execute_insert_pk (final ThreadContext context , final IRubyObject sql , final IRubyObject pk ) {
897
916
return withConnection (context , new Callable <IRubyObject >() {
898
917
public IRubyObject call (final Connection connection ) throws SQLException {
899
918
Statement statement = null ;
900
919
final String query = sqlString (sql );
901
920
try {
902
921
903
922
statement = createStatement (context , connection );
904
- 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
+
905
930
return mapGeneratedKeys (context , connection , statement );
906
931
907
932
} catch (final SQLException e ) {
@@ -914,23 +939,35 @@ public IRubyObject call(final Connection connection) throws SQLException {
914
939
});
915
940
}
916
941
942
+ @ Deprecated
943
+ @ JRubyMethod (name = "execute_insert" , required = 1 )
944
+ public IRubyObject execute_insert (final ThreadContext context , final IRubyObject sql ) {
945
+ return execute_insert_pk (context , sql , context .nil );
946
+ }
947
+
917
948
/**
918
949
* Executes an INSERT SQL statement using a prepared statement
919
950
* @param context
920
951
* @param sql
921
952
* @param binds RubyArray of values to be bound to the query
953
+ * @param pk Rails PK
922
954
* @return ActiveRecord::Result
923
955
* @throws SQLException
924
956
*/
925
- @ JRubyMethod (name = "execute_insert" , required = 2 )
926
- public IRubyObject execute_insert (final ThreadContext context , final IRubyObject sql , final IRubyObject binds ) {
957
+ @ JRubyMethod (name = "execute_insert_pk" , required = 3 )
958
+ public IRubyObject execute_insert_pk (final ThreadContext context , final IRubyObject sql , final IRubyObject binds ,
959
+ final IRubyObject pk ) {
927
960
return withConnection (context , new Callable <IRubyObject >() {
928
961
public IRubyObject call (final Connection connection ) throws SQLException {
929
962
PreparedStatement statement = null ;
930
963
final String query = sqlString (sql );
931
964
try {
965
+ if (pk == context .nil || pk == context .fals || !supportsGeneratedKeys (connection )) {
966
+ statement = connection .prepareStatement (query , Statement .RETURN_GENERATED_KEYS );
967
+ } else {
968
+ statement = connection .prepareStatement (query , createStatementPk (pk ));
969
+ }
932
970
933
- statement = connection .prepareStatement (query , Statement .RETURN_GENERATED_KEYS );
934
971
setStatementParameters (context , connection , statement , (RubyArray ) binds );
935
972
statement .executeUpdate ();
936
973
return mapGeneratedKeys (context , connection , statement );
@@ -945,6 +982,12 @@ public IRubyObject call(final Connection connection) throws SQLException {
945
982
});
946
983
}
947
984
985
+ @ Deprecated
986
+ @ JRubyMethod (name = "execute_insert" , required = 2 )
987
+ public IRubyObject execute_insert (final ThreadContext context , final IRubyObject binds , final IRubyObject sql ) {
988
+ return execute_insert_pk (context , sql , binds , context .nil );
989
+ }
990
+
948
991
/**
949
992
* Executes an UPDATE (DELETE) SQL statement
950
993
* @param context
0 commit comments