Skip to content

Commit 9783f81

Browse files
committed
Merge remote-tracking branch 'FairLoan/master'
* FairLoan/master: Adding tests for double quoted identity delimiter Renaming def to line up with SQL terminology. Adding recognition of double quote (") as a valid quote delimiter
2 parents 1b226af + 79880ab commit 9783f81

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/arjdbc/mssql/utils.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,40 @@ def get_table_name(sql, qualified = nil)
2323

2424
# protected
2525

26+
# See "Delimited Identifiers": http://msdn.microsoft.com/en-us/library/ms176027.aspx
27+
def remove_identifier_delimiters(keyword)
28+
if /\A(\[|")(.*)/m.match(keyword)
29+
delim, rest = $1, $2
30+
if delim == '[' && rest =~ /]\z/ || delim == '"' && rest =~ /"\z/
31+
return rest.chop
32+
end
33+
end
34+
keyword
35+
end
36+
2637
def unquote_table_name(table_name)
27-
unquote_column_name(table_name)
38+
remove_identifier_delimiters(table_name)
2839
end
2940

3041
def unquote_column_name(column_name)
31-
column_name.to_s.tr('[]', '')
42+
remove_identifier_delimiters(column_name)
3243
end
3344

3445
def unquote_string(string)
3546
string.to_s.gsub("''", "'")
3647
end
3748

3849
def unqualify_table_name(table_name)
39-
table_name.to_s.split('.').last.tr('[]', '')
50+
remove_identifier_delimiters(table_name.to_s.split('.').last)
4051
end
4152

4253
def unqualify_table_schema(table_name)
43-
table_name.to_s.split('.')[-2].gsub(/[\[\]]/, '') rescue nil
54+
remove_identifier_delimiters(table_name.to_s.split('.')[-2]) rescue nil
4455
end
4556

4657
def unqualify_db_name(table_name)
4758
table_names = table_name.to_s.split('.')
48-
table_names.length == 3 ? table_names.first.tr('[]', '') : nil
59+
table_names.length == 3 ? remove_identifier_delimiters(table_names.first) : nil
4960
end
5061

5162
end

test/db/mssql/identity_insert_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ class MSSQLIdentityInsertTest < Test::Unit::TestCase
77
def test_enable_identity_insert_when_necessary
88
Entry.connection.execute("INSERT INTO entries([id], [title]) VALUES (333, 'Title')")
99
Entry.connection.execute("INSERT INTO entries([title], [id]) VALUES ('Title', 344)")
10+
Entry.connection.execute(%Q(INSERT INTO entries("id", "title") VALUES (444, 'Title')))
11+
Entry.connection.execute(%Q(INSERT INTO entries("title", "id") VALUES ('Title', 455)))
1012
Entry.connection.execute("INSERT INTO entries(id, title) VALUES (666, 'Title')")
1113
Entry.connection.execute("INSERT INTO entries(id, title) (SELECT id+123, title FROM entries)")
1214
end
1315

1416
def test_dont_enable_identity_insert_when_unnecessary
1517
Entry.connection.execute("INSERT INTO entries([title]) VALUES ('[id]')")
18+
Entry.connection.execute(%Q(INSERT INTO entries("title") VALUES ('"a memorable quote"')))
1619
end
1720

1821
end

0 commit comments

Comments
 (0)