File: mssql.rb

package info (click to toggle)
ruby-sequel 5.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,408 kB
  • sloc: ruby: 113,747; makefile: 3
file content (62 lines) | stat: -rw-r--r-- 2,014 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen-string-literal: true

require_relative '../shared/mssql'

module Sequel
  module ADO
    module MSSQL
      module DatabaseMethods
        include Sequel::MSSQL::DatabaseMethods

        def execute_dui(sql, opts=OPTS)
          return super unless @opts[:provider]
          synchronize(opts[:server]) do |conn|
            begin
              sql = "SET NOCOUNT ON; #{sql}; SELECT @@ROWCOUNT"
              rst = log_connection_yield(sql, conn){conn.Execute(sql)}
              rst.GetRows[0][0]
            rescue ::WIN32OLERuntimeError => e
              raise_error(e)
            end
          end
        end

        private

        # 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.
        def begin_transaction(conn, opts=OPTS)
          super if @opts[:provider]
        end

        def commit_transaction(conn, opts=OPTS)
          super if @opts[:provider]
        end

        def rollback_transaction(conn, opts=OPTS)
          super if @opts[:provider]
        end
      end
      
      class Dataset < ADO::Dataset
        include Sequel::MSSQL::DatasetMethods

        # Use a nasty hack of multiple SQL statements in the same call and
        # having the last one return the most recently inserted id.  This
        # is necessary as ADO's default :provider uses a separate native
        # connection for each query.
        def insert(*values)
          return super if (@opts[:sql] && !@opts[:prepared_sql]) || @opts[:returning]
          with_sql("SET NOCOUNT ON; #{insert_sql(*values)}; SELECT CAST(SCOPE_IDENTITY() AS INTEGER)").single_value
        end
        
        # If you use a better :provider option for the database, you can get an
        # accurate number of rows matched.
        def provides_accurate_rows_matched?
          !!db.opts[:provider]
        end
      end
    end
  end
end