Skip to content

Commit 25578da

Browse files
committed
Simple sqlserver specific test for #quote_string. Move all idenity insert methods around for org.
1 parent 6068805 commit 25578da

File tree

3 files changed

+42
-38
lines changed

3 files changed

+42
-38
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -840,8 +840,41 @@ def pk_and_sequence_for(table_name)
840840
@connection["AutoCommit"] = true
841841
end
842842

843+
843844
private
844845

846+
# IDENTITY INSERTS =========================================#
847+
848+
def with_identity_insert_enabled(table_name, &block)
849+
set_identity_insert(table_name, true)
850+
yield
851+
ensure
852+
set_identity_insert(table_name, false)
853+
end
854+
855+
def set_identity_insert(table_name, enable = true)
856+
execute "SET IDENTITY_INSERT #{table_name} #{enable ? 'ON' : 'OFF'}"
857+
rescue Exception => e
858+
raise ActiveRecordError, "IDENTITY_INSERT could not be turned #{enable ? 'ON' : 'OFF'} for table #{table_name}"
859+
end
860+
861+
def query_requires_identity_insert?(sql)
862+
table_name = get_table_name(sql)
863+
id_column = identity_column(table_name)
864+
sql =~ /INSERT[^(]+\([^)]*\[#{id_column}\][^)]*\)/ ? table_name : nil
865+
end
866+
867+
def identity_column(table_name)
868+
@table_columns ||= {}
869+
@table_columns[table_name] = columns(table_name) if @table_columns[table_name] == nil
870+
@table_columns[table_name].each do |col|
871+
return col.name if col.identity
872+
end
873+
return nil
874+
end
875+
876+
877+
845878
def __indexes(table_name, name = nil)
846879
indexes = []
847880
execute("EXEC sp_helpindex '#{table_name}'", name) do |handle|
@@ -875,23 +908,7 @@ def select(sql, name = nil, ignore_special_columns = false)
875908
end
876909
result
877910
end
878-
879-
# Turns IDENTITY_INSERT ON for table during execution of the block
880-
# N.B. This sets the state of IDENTITY_INSERT to OFF after the
881-
# block has been executed without regard to its previous state
882-
def with_identity_insert_enabled(table_name, &block)
883-
set_identity_insert(table_name, true)
884-
yield
885-
ensure
886-
set_identity_insert(table_name, false)
887-
end
888-
889-
def set_identity_insert(table_name, enable = true)
890-
execute "SET IDENTITY_INSERT #{table_name} #{enable ? 'ON' : 'OFF'}"
891-
rescue Exception => e
892-
raise ActiveRecordError, "IDENTITY_INSERT could not be turned #{enable ? 'ON' : 'OFF'} for table #{table_name}"
893-
end
894-
911+
895912
def get_table_name(sql)
896913
if sql =~ /^\s*insert\s+into\s+([^\(\s]+)\s*|^\s*update\s+([^\(\s]+)\s*/i
897914
$1 || $2
@@ -901,23 +918,7 @@ def get_table_name(sql)
901918
nil
902919
end
903920
end
904-
905-
def identity_column(table_name)
906-
@table_columns ||= {}
907-
@table_columns[table_name] = columns(table_name) if @table_columns[table_name] == nil
908-
@table_columns[table_name].each do |col|
909-
return col.name if col.identity
910-
end
911-
912-
return nil
913-
end
914-
915-
def query_requires_identity_insert?(sql)
916-
table_name = get_table_name(sql)
917-
id_column = identity_column(table_name)
918-
sql =~ /INSERT[^(]+\([^)]*\[#{id_column}\][^)]*\)/ ? table_name : nil
919-
end
920-
921+
921922
def change_order_direction(order)
922923
order.split(",").collect {|fragment|
923924
case fragment

test/cases/adapter_test_sqlserver.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
require 'models/topic'
44

55
class AdapterTestSqlserver < ActiveRecord::TestCase
6-
7-
fixtures :binaries
8-
6+
97
def setup
108
@connection = ActiveRecord::Base.connection
119
end
@@ -66,6 +64,11 @@ def setup
6664
assert_equal '0', @connection.quoted_false
6765
end
6866

67+
should 'not escape backslash characters like abstract adapter' do
68+
string_with_backslashs = "\\n"
69+
assert_equal string_with_backslashs, @connection.quote_string(string_with_backslashs)
70+
end
71+
6972
end
7073

7174
context 'For DatabaseStatements' do

test/cases/connection_test_sqlserver.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def setup
1313
end
1414

1515

16-
should 'affecte rows' do
16+
should 'affect rows' do
1717
assert Topic.connection.instance_variable_get("@connection")["AutoCommit"]
1818
topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }
1919
updated = Topic.update(topic_data.keys, topic_data.values)

0 commit comments

Comments
 (0)