Permalink
Browse files

add :mssql_unicode_strings opt to mssql db's to enable/disable unicod…

…e prefix
  • Loading branch information...
semmons99 committed Mar 30, 2011
1 parent d1ea265 commit b24da42622c84da1f84808a97013ca7328091aa0
View
@@ -6,6 +6,11 @@ module ADO
class Database < Sequel::Database
set_adapter_scheme :ado
+ # Whether all strings should be prefixed with "N" to imply treat as
+ # unicode strings. True by default, can be set to false to greatly
+ # increase performance depending on your database configuration.
+ attr_accessor :mssql_unicode_strings
+
def initialize(opts)
super
case @opts[:conn_string]
@@ -20,6 +25,7 @@ def initialize(opts)
extend Sequel::ADO::MSSQL::DatabaseMethods
end
end
+ @mssql_unicode_strings = typecast_value_boolean(@opts.fetch(:mssql_unicode_strings, true))
end
# In addition to the usual database options,
@@ -92,6 +98,17 @@ def disconnect_connection(conn)
end
class Dataset < Sequel::Dataset
+ # Whether all strings should be prefixed with "N" to imply treat as
+ # unicode strings. True by default, can be set to false to greatly
+ # increase performance depending on your database configuration.
+ attr_accessor :mssql_unicode_strings
+
+ # Use the mssql_unicode_strings default setting from the database
+ def initialize(db, opts={})
+ @mssql_unicode_strings = db.mssql_unicode_strings
+ super
+ end
+
def fetch_rows(sql)
execute(sql) do |s|
@columns = cols = s.Fields.extend(Enumerable).map{|column| output_identifier(column.Name)}
@@ -101,6 +101,11 @@ class Database < Sequel::Database
# True by default, can be set to false to roughly double performance when
# fetching rows.
attr_accessor :convert_types
+
+ # Whether all strings should be prefixed with "N" to imply treat as
+ # unicode strings. True by default, can be set to false to greatly
+ # increase performance depending on your database configuration.
+ attr_accessor :mssql_unicode_strings
# Call the DATABASE_SETUP proc directly after initialization,
# so the object always uses sub adapter specific code. Also,
@@ -109,6 +114,7 @@ class Database < Sequel::Database
def initialize(opts)
super
@convert_types = typecast_value_boolean(@opts.fetch(:convert_types, true))
+ @mssql_unicode_strings = typecast_value_boolean(@opts.fetch(:mssql_unicode_strings, true))
raise(Error, "No connection string specified") unless uri
resolved_uri = jndi? ? get_uri_from_jndi : uri
@@ -530,10 +536,17 @@ def execute_insert(sql, opts={}, &block)
# Uses the database's setting by default, can be set to false to roughly
# double performance when fetching rows.
attr_accessor :convert_types
+
+ # Whether all strings should be prefixed with "N" to imply treat as
+ # unicode strings. True by default, can be set to false to greatly
+ # increase performance depending on your database configuration.
+ attr_accessor :mssql_unicode_strings
# Use the convert_types default setting from the database
+ # Use the mssql_unicode_strings default setting from the database
def initialize(db, opts={})
@convert_types = db.convert_types
+ @mssql_unicode_strings = db.mssql_unicode_strings
super
end
@@ -9,6 +9,11 @@ class Database < Sequel::Database
DRV_NAME_GUARDS = '{%s}'.freeze
DISCONNECT_ERRORS = /\A08S01/.freeze
+ # Whether all strings should be prefixed with "N" to imply treat as
+ # unicode strings. True by default, can be set to false to greatly
+ # increase performance depending on your database configuration.
+ attr_accessor :mssql_unicode_strings
+
def initialize(opts)
super
case @opts[:db_type]
@@ -19,6 +24,7 @@ def initialize(opts)
Sequel.ts_require 'adapters/shared/progress'
extend Sequel::Progress::DatabaseMethods
end
+ @mssql_unicode_strings = typecast_value_boolean(@opts.fetch(:mssql_unicode_strings, true))
end
def connect(server)
@@ -87,6 +93,17 @@ class Dataset < Sequel::Dataset
ODBC_DATE_FORMAT = "{d '%Y-%m-%d'}".freeze
TIMESTAMP_FORMAT="{ts '%Y-%m-%d %H:%M:%S'}".freeze
+ # Whether all strings should be prefixed with "N" to imply treat as
+ # unicode strings. True by default, can be set to false to greatly
+ # increase performance depending on your database configuration.
+ attr_accessor :mssql_unicode_strings
+
+ # Use the mssql_unicode_strings default setting from the database
+ def initialize(db, opts={})
+ @mssql_unicode_strings = db.mssql_unicode_strings
+ super
+ end
+
def fetch_rows(sql)
execute(sql) do |s|
i = -1
@@ -14,7 +14,7 @@ module DatabaseMethods
# The types to check for 0 scale to transform :decimal types
# to :integer.
DECIMAL_TYPE_RE = /number|numeric|decimal/io
-
+
# Microsoft SQL Server uses the :mssql type.
def database_type
:mssql
@@ -441,9 +441,12 @@ def literal_blob(v)
blob
end
- # Use unicode string syntax for all strings. Don't double backslashes.
+ # Optionally use unicode string syntax for all strings. Don't double
+ # backslashes.
def literal_string(v)
- "N'#{v.gsub(/'/, "''")}'"
+ str = "'#{v.gsub(/'/, "''")}'"
+ str = "N#{str}" unless @mssql_unicode_strings == false
+ str
end
# Use 0 for false on MSSQL
@@ -0,0 +1,54 @@
+require File.join(File.dirname(File.expand_path(__FILE__)), 'spec_helper.rb')
+
+if RUBY_PLATFORM =~ /java/
+ describe "JDBC" do
+ context "mssql_unicode_strings is default/true" do
+ let(:db) { Sequel.jdbc("jdbc:sqlserver://localhost") }
+ subject { db[:tb1].filter(:col1 => "test").sql }
+
+ it { should == "SELECT * FROM TB1 WHERE (COL1 = N'test')" }
+ end
+
+ context "mssql_unicode_strings is false" do
+ let(:db) { Sequel.jdbc("jdbc:sqlserver://localhost",
+ :mssql_unicode_strings => false) }
+ subject { db[:tb1].filter(:col1 => "test").sql }
+
+ it { should == "SELECT * FROM TB1 WHERE (COL1 = 'test')" }
+ end
+ end
+else
+ describe "ADO" do
+ context "mssql_unicode_strings is default/true" do
+ let(:db) { Sequel.ado(:conn_string => "dummy connection string") }
+ subject { db[:tb1].filter(:col1 => "test").sql }
+
+ it { should == "SELECT * FROM TB1 WHERE (COL1 = N'test')" }
+ end
+
+ context "mssql_unicode_strings is false" do
+ let(:db) { Sequel.ado(:conn_string => "dummy connection string",
+ :mssql_unicode_strings => false) }
+ subject { db[:tb1].filter(:col1 => "test").sql }
+
+ it { should == "SELECT * FROM TB1 WHERE (COL1 = 'test')" }
+ end
+ end
+
+ describe "ODBC" do
+ context "mssql_unicode_strings is default/true" do
+ let(:db) { Sequel.odbc("dummy", :db_type => "mssql") }
+ subject { db[:tb1].filter(:col1 => "test").sql }
+
+ it { should == "SELECT * FROM TB1 WHERE (COL1 = N'test')" }
+ end
+
+ context "mssql_unicode_strings is false" do
+ let(:db) { Sequel.odbc("dummy", :db_type => "mssql",
+ :mssql_unicode_strings => false) }
+ subject { db[:tb1].filter(:col1 => "test").sql }
+
+ it { should == "SELECT * FROM TB1 WHERE (COL1 = 'test')" }
+ end
+ end
+end

0 comments on commit b24da42

Please sign in to comment.