Class: Tina4::Drivers::MssqlDriver

Inherits:
Object
  • Object
show all
Includes:
SchemaSplit
Defined in:
lib/tina4/drivers/mssql_driver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SchemaSplit

#split_schema

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



9
10
11
# File 'lib/tina4/drivers/mssql_driver.rb', line 9

def connection
  @connection
end

Instance Method Details

#apply_limit(sql, limit, offset = 0) ⇒ Object



63
64
65
# File 'lib/tina4/drivers/mssql_driver.rb', line 63

def apply_limit(sql, limit, offset = 0)
  "#{sql} OFFSET #{offset} ROWS FETCH NEXT #{limit} ROWS ONLY"
end

#begin_transactionObject



67
68
69
# File 'lib/tina4/drivers/mssql_driver.rb', line 67

def begin_transaction
  @connection.execute("BEGIN TRANSACTION").do
end

#closeObject



30
31
32
# File 'lib/tina4/drivers/mssql_driver.rb', line 30

def close
  @connection&.close
end

#columns(table_name) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/tina4/drivers/mssql_driver.rb', line 95

def columns(table_name)
  # v3.13.14 (#48): honour a schema-qualified name; bare names match any schema.
  schema, tbl = split_schema(table_name)
  sql = "SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS " \
        "WHERE TABLE_NAME = ? AND (? IS NULL OR TABLE_SCHEMA = ?)"
  rows = execute_query(sql, [tbl, schema, schema])
  rows.map do |r|
    {
      name: r[:COLUMN_NAME] || r[:column_name],
      type: r[:DATA_TYPE] || r[:data_type],
      nullable: (r[:IS_NULLABLE] || r[:is_nullable]) == "YES",
      default: r[:COLUMN_DEFAULT] || r[:column_default],
      primary_key: false
    }
  end
end

#commitObject



71
72
73
# File 'lib/tina4/drivers/mssql_driver.rb', line 71

def commit
  @connection.execute("COMMIT").do
end

#connect(connection_string, username: nil, password: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tina4/drivers/mssql_driver.rb', line 11

def connect(connection_string, username: nil, password: nil)
  begin
    require "tiny_tds"
  rescue LoadError
    raise LoadError,
          "The 'tiny_tds' gem is required for MSSQL connections. Install one of:\n" \
          "    bundle add tiny_tds     # if your project uses Bundler\n" \
          "    gem install tiny_tds    # bare driver"
  end
  uri = parse_connection(connection_string)
  @connection = TinyTds::Client.new(
    host: uri[:host],
    port: uri[:port] || 1433,
    username: username || uri[:username],
    password: password || uri[:password],
    database: uri[:database]
  )
end

#execute(sql, params = []) ⇒ Object



42
43
44
45
46
# File 'lib/tina4/drivers/mssql_driver.rb', line 42

def execute(sql, params = [])
  effective_sql = interpolate_params(sql, params)
  result = @connection.execute(effective_sql)
  result.do
end

#execute_query(sql, params = []) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/tina4/drivers/mssql_driver.rb', line 34

def execute_query(sql, params = [])
  effective_sql = interpolate_params(sql, params)
  result = @connection.execute(effective_sql)
  rows = result.each(symbolize_keys: true).to_a
  result.cancel if result.respond_to?(:cancel)
  rows
end

#last_insert_idObject



48
49
50
51
52
53
# File 'lib/tina4/drivers/mssql_driver.rb', line 48

def last_insert_id
  result = @connection.execute("SELECT SCOPE_IDENTITY() AS id")
  row = result.first
  result.cancel if result.respond_to?(:cancel)
  row[:id]&.to_i
end

#placeholderObject



55
56
57
# File 'lib/tina4/drivers/mssql_driver.rb', line 55

def placeholder
  "?"
end

#placeholders(count) ⇒ Object



59
60
61
# File 'lib/tina4/drivers/mssql_driver.rb', line 59

def placeholders(count)
  (["?"] * count).join(", ")
end

#rollbackObject



75
76
77
# File 'lib/tina4/drivers/mssql_driver.rb', line 75

def rollback
  @connection.execute("ROLLBACK").do
end

#table_exists?(name) ⇒ Boolean

v3.13.14 (#48): honour a schema-qualified name (“dbo.widget”); a bare name matches in any schema (NULL guard skips the schema filter).

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
# File 'lib/tina4/drivers/mssql_driver.rb', line 81

def table_exists?(name)
  schema, tbl = split_schema(name)
  sql = "SELECT 1 FROM INFORMATION_SCHEMA.TABLES " \
        "WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = ? " \
        "AND (? IS NULL OR TABLE_SCHEMA = ?)"
  rows = execute_query(sql, [tbl, schema, schema])
  !rows.empty?
end

#tablesObject



90
91
92
93
# File 'lib/tina4/drivers/mssql_driver.rb', line 90

def tables
  rows = execute_query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'")
  rows.map { |r| r[:TABLE_NAME] || r[:table_name] }
end