Class: FluidDb2::TinyTds

Inherits:
Base
  • Object
show all
Defined in:
lib/fluiddb2/tiny_tds.rb

Instance Attribute Summary

Attributes inherited from Base

#connection, #verbose

Instance Method Summary collapse

Methods inherited from Base

#initialize, #reconnect, #verbose_log

Constructor Details

This class inherits a constructor from FluidDb2::Base

Instance Method Details

#beginObject

Transaction Semantics



140
141
142
# File 'lib/fluiddb2/tiny_tds.rb', line 140

def begin
  @connection.execute('BEGIN TRANSACTION')
end

#closeObject



70
71
72
# File 'lib/fluiddb2/tiny_tds.rb', line 70

def close
  @connection.close
end

#commitObject

Transaction Semantics



145
146
147
# File 'lib/fluiddb2/tiny_tds.rb', line 145

def commit
  @connection.execute('COMMIT')
end

#connectObject

Connect to Db.



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
63
64
65
66
67
68
# File 'lib/fluiddb2/tiny_tds.rb', line 12

def connect
  uri = @uri

  dataserver = uri.host
  database = uri.path.sub('/', '')
  username = URI.unescape(uri.user)
  password = uri.password

  if dataserver == '' || database == ''
    fail '*** You need to specify both a dataserver and a database for ' \
          'the tinytds driver. Expected format: ' \
          'tinytds://<user>:<pass>@<dataserver>/<database>\n' \
          '*** The specified dataserver should have an entry in ' \
          '/etc/freetds/freetds.conf'
  end

  if username == '' || password == ''
    puts '*** Warning - you will normally need to specify both a username ' \
         'and password for the tinytds driver to work correctly.'
  end

  hash = Hash[:username, username,
              :password, password,
              :database, database,
              :dataserver, dataserver]
  fail_over_data_server = nil
  unless uri.query.nil?
    cgi = CGI.parse(uri.query)
    hash[:timeout] = cgi['timeout'][0].to_i if cgi.key?('timeout')
    if cgi.key?('host')
      hash.delete(:dataserver)
      hash[:host] = dataserver
    end

    fail_over_data_server =
      cgi['failoverdataserver'][0] if cgi.key?('failoverdataserver')
  end

  begin
    @connection = ::TinyTds::Client.new(hash)
  rescue ::TinyTds::Error => e
    # Message for an incorrect password,
    # Login failed. The login is from an untrusted domain and cannot be used
    #  with Windows authentication.
    # Message for unavailable db
    # Cannot open user default database. Login failed.
    if e.message == 'Cannot open user default database. Login failed.' &&
       !fail_over_data_server.nil?
      hash[:dataserver] = fail_over_data_server
      @connection = ::TinyTds::Client.new(hash)
    else
      raise e
    end
  end

  fail 'Unable to connect to the database' unless @connection.active?
end

#escape_string(input) ⇒ Object



74
75
76
# File 'lib/fluiddb2/tiny_tds.rb', line 74

def escape_string(input)
  @connection.escape(input)
end

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



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/fluiddb2/tiny_tds.rb', line 122

def execute(sql, params = [], expected_affected_rows = nil)
  sql = FluidDb2.format_to_sql(sql, params)
  r = @connection.execute(sql)
  r.each

  if !expected_affected_rows.nil? &&
     r.affected_rows != expected_affected_rows
    msg = "Expected affected rows, #{expected_affected_rows}, " \
          "Actual affected rows, #{r.affected_rows}"
    fail ExpectedAffectedRowsError, msg
  end
end

#insert(_sql, _params) ⇒ Object



135
136
137
# File 'lib/fluiddb2/tiny_tds.rb', line 135

def insert(_sql, _params)
  fail 'Not implemented'
end

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fluiddb2/tiny_tds.rb', line 78

def query_for_array(sql, params = [])
  sql = FluidDb2.format_to_sql(sql, params)
  results = @connection.execute(sql)

  count = 0
  tuple = ''
  results.each do |row|
    count += 1
    fail FluidDb2::TooManyRowsError if count > 1
    tuple = row
  end
  fail FluidDb2::NoDataFoundError if count == 0
  tuple
end

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



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fluiddb2/tiny_tds.rb', line 110

def query_for_resultset(sql, params = [])
  sql = FluidDb2.format_to_sql(sql, params)
  results = @connection.execute(sql)

  list = []
  results.each do |row|
    list << row
  end

  list
end

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fluiddb2/tiny_tds.rb', line 93

def query_for_value(sql, params = [])
  sql = FluidDb2.format_to_sql(sql, params)
  results = @connection.execute(sql)

  count = 0
  value = ''
  results.each do |row|
    count += 1
    fail FluidDb2::TooManyRowsError if count > 1
    value = row[results.fields[0]]
  end

  fail FluidDb2::NoDataFoundError if count == 0

  value
end

#rollbackObject

Transaction Semantics



150
151
152
# File 'lib/fluiddb2/tiny_tds.rb', line 150

def rollback
  @connection.execute('ROLLBACK')
end