Permalink
Browse files

Handle IDENTITY_INSERT.

  • Loading branch information...
1 parent b05ab5a commit 7864d47ffe80610c71bceb9827489a12f45d41a3 @rvernagus rvernagus committed Jun 29, 2009
Showing with 34 additions and 0 deletions.
  1. +34 −0 mssql_adapter.rb
View
34 mssql_adapter.rb
@@ -186,9 +186,14 @@ def execute(sql, name = nil)
#log(sql, name) do
# TODO: @async
begin
+ table_name = get_table_name_from_sql(sql)
+ set_identity_insert_on(table_name) if (is_insert_sql(sql) and includes_id_field(sql))
+
command = System::Data::SqlClient::SqlCommand.new sql, @connection
command.transaction = @transaction
command.execute_non_query
+
+ set_identity_insert_off(table_name) if (is_insert_sql(sql) and includes_id_field(sql))
rescue System::Data::SqlClient::SqlException
raise ActiveRecord::StatementInvalid, "#{$!}"
end
@@ -371,6 +376,31 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil)
end
end
+ def has_identity_column(table_name)
+ result = select "SELECT OBJECTPROPERTY(OBJECT_ID('#{table_name}'), 'TableHasIdentity') AS has_identity"
+ result.first["has_identity"] == 1
+ end
+
+ def is_insert_sql(sql)
+ sql =~ /^insert/i
+ end
+
+ def includes_id_field(sql)
+ sql =~ /\[id\]|\[\w*_id\]/i
+ end
+
+ def set_identity_insert_on(table_name)
+ set_identity_insert table_name, "ON" if has_identity_column table_name
+ end
+
+ def set_identity_insert_off(table_name)
+ set_identity_insert table_name, "OFF" if has_identity_column table_name
+ end
+
+ def set_identity_insert(table_name, setting)
+ execute "SET IDENTITY_INSERT #{table_name} #{setting}"
+ end
+
protected
# Returns the version of the connected SQL Server.
def mssql_version
@@ -484,6 +514,10 @@ def column_definitions(table_name) #:nodoc:
c.colid
end_sql
end
+
+ def get_table_name_from_sql(sql)
+ sql.split(" ", 4)[2]
+ end
end
end
end

0 comments on commit 7864d47

Please sign in to comment.