class Sequel::ADO::Database
Constants
- CommandTimeout
- Provider
Attributes
Public Instance Methods
In addition to the usual database options, the following options have an effect:
- :command_timeout
-
Sets the time in seconds to wait while attempting to execute a command before cancelling the attempt and generating an error. Specifically, it sets the ADO CommandTimeout property.
- :driver
-
The driver to use in the ADO connection string. If not provided, a default of “SQL Server” is used.
- :conn_string
-
The full ADO connection string. If this is provided, the usual options are ignored.
- :provider
-
Sets the Provider of this ADO connection (for example, “SQLOLEDB”). If you don't specify a provider, the default one used by WIN32OLE has major problems, such as creating a new native database connection for every query, which breaks things such as temporary tables.
Pay special attention to the :provider option, as without specifying a provider, many things will be broken. The SQLNCLI10 provider appears to work well if you are connecting to Microsoft SQL Server, but it is not the default as that is not always available and would break backwards compatability.
# File lib/sequel/adapters/ado.rb, line 111 def connect(server) opts = server_opts(server) s = opts[:conn_string] || "driver=#{opts[:driver]};server=#{opts[:host]};database=#{opts[:database]}#{";uid=#{opts[:user]};pwd=#{opts[:password]}" if opts[:user]}" handle = WIN32OLE.new('ADODB.Connection') handle.CommandTimeout = opts[:command_timeout] if opts[:command_timeout] handle.Provider = opts[:provider] if opts[:provider] handle.Open(s) handle end
# File lib/sequel/adapters/ado.rb, line 121 def disconnect_connection(conn) conn.Close rescue WIN32OLERuntimeError nil end
# File lib/sequel/adapters/ado.rb, line 158 def execute(sql, opts=OPTS) synchronize(opts[:server]) do |conn| begin r = log_connection_yield(sql, conn){conn.Execute(sql)} begin yield r if defined?(yield) ensure begin r.close rescue ::WIN32OLERuntimeError end end rescue ::WIN32OLERuntimeError => e raise_error(e) end end nil end
Just execute so it doesn't attempt to return the number of rows modified.
# File lib/sequel/adapters/ado.rb, line 133 def execute_ddl(sql, opts=OPTS) execute(sql, opts) end
Use pass by reference in WIN32OLE to get the number of affected rows, unless is a provider is in use (since some providers don't seem to return the number of affected rows, but the default provider appears to).
# File lib/sequel/adapters/ado.rb, line 146 def execute_dui(sql, opts=OPTS) return super if opts[:provider] synchronize(opts[:server]) do |conn| begin log_connection_yield(sql, conn){conn.Execute(sql, 1)} WIN32OLE::ARGV[1] rescue ::WIN32OLERuntimeError => e raise_error(e) end end end
Just execute so it doesn't attempt to return the number of rows modified.
# File lib/sequel/adapters/ado.rb, line 138 def execute_insert(sql, opts=OPTS) execute(sql, opts) end
# File lib/sequel/adapters/ado.rb, line 127 def freeze @conversion_procs.freeze super end
Private Instance Methods
# File lib/sequel/adapters/ado.rb, line 179 def adapter_initialize case @opts[:conn_string] when /Microsoft\.(Jet|ACE)\.OLEDB/io require_relative 'ado/access' extend Sequel::ADO::Access::DatabaseMethods self.dataset_class = ADO::Access::Dataset else @opts[:driver] ||= 'SQL Server' case @opts[:driver] when 'SQL Server' require_relative 'ado/mssql' extend Sequel::ADO::MSSQL::DatabaseMethods self.dataset_class = ADO::MSSQL::Dataset set_mssql_unicode_strings end end @conversion_procs = CONVERSION_PROCS.dup @conversion_procs[AdDBTimeStamp] = method(:adb_timestamp_to_application_timestamp) super end
# File lib/sequel/adapters/ado.rb, line 202 def adb_timestamp_to_application_timestamp(v) # This hard codes a timestamp_precision of 6 when converting. # That is the default timestamp_precision, but the ado/mssql adapter uses a timestamp_precision # of 3. However, timestamps returned by ado/mssql have nsec values that end up rounding to a # the same value as if a timestamp_precision of 3 was hard coded (either xxx999yzz, where y is # 5-9 or xxx000yzz where y is 0-4). # # ADO subadapters should override this they would like a different timestamp precision and the # this code does not work for them (for example, if they provide full nsec precision). # # Note that fractional second handling for WIN32OLE objects is not correct on ruby <2.2 to_application_timestamp([v.year, v.month, v.day, v.hour, v.min, v.sec, (v.nsec/1000.0).round * 1000]) end
The ADO adapter's default provider doesn't support transactions, since it creates a new native connection for each query. So Sequel only attempts to use transactions if an explicit :provider is given.
# File lib/sequel/adapters/ado.rb, line 223 def begin_transaction(conn, opts=OPTS) super if @opts[:provider] end
# File lib/sequel/adapters/ado.rb, line 227 def commit_transaction(conn, opts=OPTS) super if @opts[:provider] end
# File lib/sequel/adapters/ado.rb, line 231 def database_error_classes [::WIN32OLERuntimeError] end
# File lib/sequel/adapters/ado.rb, line 216 def dataset_class_default Dataset end
# File lib/sequel/adapters/ado.rb, line 235 def disconnect_error?(e, opts) super || (e.is_a?(::WIN32OLERuntimeError) && e.message =~ /Communication link failure/) end
# File lib/sequel/adapters/ado.rb, line 239 def rollback_transaction(conn, opts=OPTS) super if @opts[:provider] end