Skip to content

Commit

Permalink
Escape ] characters in identifiers on Microsoft SQL Server
Browse files Browse the repository at this point in the history
This also adds a spec for identifier escaping.
  • Loading branch information
jeremyevans committed Mar 22, 2013
1 parent e7ddfc4 commit 725a241
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
=== HEAD

* Escape ] characters in identifiers on Microsoft SQL Server (jeremyevans)

* Add security guide (jeremyevans)

* Make validates_type handle false values correctly (jeremyevans) (#636)
Expand Down
6 changes: 3 additions & 3 deletions lib/sequel/adapters/shared/mssql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ module DatasetMethods
APOS_RE = Dataset::APOS_RE
DOUBLE_APOS = Dataset::DOUBLE_APOS
INTO = Dataset::INTO
DOUBLE_BRACKET_CLOSE = ']]'.freeze
DATEPART_SECOND_OPEN = "CAST((datepart(".freeze
DATEPART_SECOND_MIDDLE = ') + datepart(ns, '.freeze
DATEPART_SECOND_CLOSE = ")/1000000000.0) AS double precision)".freeze
Expand Down Expand Up @@ -570,10 +571,9 @@ def output(into, values)
clone({:output => output})
end

# MSSQL uses [] to quote identifiers. MSSQL does not support
# escaping of ], so you cannot use that character in an identifier.
# MSSQL uses [] to quote identifiers.
def quoted_identifier_append(sql, name)
sql << BRACKET_OPEN << name.to_s << BRACKET_CLOSE
sql << BRACKET_OPEN << name.to_s.gsub(/\]/, DOUBLE_BRACKET_CLOSE) << BRACKET_CLOSE
end

# The version of the database server.
Expand Down
5 changes: 5 additions & 0 deletions spec/integration/dataset_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@
INTEGRATION_DB.get(Sequel.cast(Sequel.blob("\1\2\3"), File).as(:a)).should == "\1\2\3"
end

cspecify "should properly escape identifiers", :db2 do
INTEGRATION_DB.create_table(:"\\'\"[]"){Integer :id}
INTEGRATION_DB.drop_table(:"\\'\"[]")
end

specify "should have a working table_exists?" do
t = :basdfdsafsaddsaf
INTEGRATION_DB.drop_table?(t)
Expand Down

0 comments on commit 725a241

Please sign in to comment.