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 (57 lines) | stat: -rw-r--r-- 1,632 bytes parent folder | download | duplicates (4)
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
# frozen-string-literal: true

require_relative '../shared/mssql'

module Sequel
  module ODBC
    Sequel.synchronize do
      DATABASE_SETUP[:mssql] = proc do |db|
        db.extend Sequel::ODBC::MSSQL::DatabaseMethods
        db.dataset_class = Sequel::ODBC::MSSQL::Dataset
        db.send(:set_mssql_unicode_strings)
      end
    end

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

        def execute_insert(sql, opts=OPTS)
          synchronize(opts[:server]) do |conn|
            begin
              log_connection_yield(sql, conn){conn.do(sql)}
              begin
                last_insert_id_sql = 'SELECT SCOPE_IDENTITY()'
                s = log_connection_yield(last_insert_id_sql, conn){conn.run(last_insert_id_sql)}
                if (rows = s.fetch_all) and (row = rows.first) and (v = row.first)
                  Integer(v)
                end
              ensure
                s.drop if s
              end
            rescue ::ODBC::Error => e
              raise_error(e)
            end
          end
        end
      end
      class Dataset < ODBC::Dataset
        include Sequel::MSSQL::DatasetMethods

        private

        # Use ODBC format, not Microsoft format, as the ODBC layer does
        # some translation, but allow for millisecond precision.
        def default_timestamp_format
          "{ts '%Y-%m-%d %H:%M:%S%N'}"
        end

        # Use ODBC format, not Microsoft format, as the ODBC layer does
        # some translation.
        def literal_date(v)
          v.strftime("{d '%Y-%m-%d'}")
        end
      end
    end
  end
end