Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mssql failure when inserting into tables with composite primary keys #1079

Merged
merged 1 commit into from Dec 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/java/arjdbc/mssql/MSSQLRubyJdbcConnection.java
Expand Up @@ -200,6 +200,57 @@ public IRubyObject call(final Connection connection) throws SQLException {
});
}

/**
* Executes an INSERT SQL statement
* @param context
* @param sql
* @param pk Rails PK
* @return ActiveRecord::Result
* @throws SQLException
*/
@Override
@JRubyMethod(name = "execute_insert_pk", required = 2)
public IRubyObject execute_insert_pk(final ThreadContext context, final IRubyObject sql, final IRubyObject pk) {

// MSSQL does not like composite primary keys here so chop it if there is more than one column
IRubyObject modifiedPk = pk;

if (pk instanceof RubyArray) {
RubyArray ary = (RubyArray) pk;
if (ary.size() > 0) {
modifiedPk = ary.eltInternal(0);
}
}

return super.execute_insert_pk(context, sql, modifiedPk);
}

/**
* Executes an INSERT SQL statement using a prepared statement
* @param context
* @param sql
* @param binds RubyArray of values to be bound to the query
* @param pk Rails PK
* @return ActiveRecord::Result
* @throws SQLException
*/
@Override
@JRubyMethod(name = "execute_insert_pk", required = 3)
public IRubyObject execute_insert_pk(final ThreadContext context, final IRubyObject sql, final IRubyObject binds,
final IRubyObject pk) {
// MSSQL does not like composite primary keys here so chop it if there is more than one column
IRubyObject modifiedPk = pk;

if (pk instanceof RubyArray) {
RubyArray ary = (RubyArray) pk;
if (ary.size() > 0) {
modifiedPk = ary.eltInternal(0);
}
}

return super.execute_insert_pk(context, sql, binds, modifiedPk);
}

@Override
protected Integer jdbcTypeFor(final String type) {

Expand Down