Class: DohDb::Handle

Inherits:
Object
  • Object
show all
Defined in:
lib/dohmysql/handle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Handle

Returns a new instance of Handle.



19
20
21
22
23
24
25
# File 'lib/dohmysql/handle.rb', line 19

def initialize(config)
  @config = config.dup
  @testing_rollback = false
  @config[:reconnect] = true if !@config.keys.include?(:reconnect)
  @mysqlh = nil
  reopen
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



17
18
19
# File 'lib/dohmysql/handle.rb', line 17

def config
  @config
end

#mysqlhObject (readonly)

Returns the value of attribute mysqlh.



17
18
19
# File 'lib/dohmysql/handle.rb', line 17

def mysqlh
  @mysqlh
end

Instance Method Details

#closeObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/dohmysql/handle.rb', line 27

def close
  unless closed?
    begin
      DohDb.logger.call('connection', "closing connection: id #{@mysqlh.thread_id}")
      @mysqlh.close
    ensure
      @mysqlh = nil
    end
  end
end

#closed?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/dohmysql/handle.rb', line 38

def closed?
  @mysqlh.nil?
end

#finish_selectObject



205
206
207
208
209
210
211
212
# File 'lib/dohmysql/handle.rb', line 205

def finish_select
  result_set = @mysqlh.async_result
  DohDb.logger.call('result', "async selected #{result_set.size} rows")
  rows = get_row_builder.build_rows(result_set)
  rows.each do |dbrow|
    @async_block.call(dbrow)
  end
end

#insert(statement) ⇒ Object



67
68
69
70
71
72
# File 'lib/dohmysql/handle.rb', line 67

def insert(statement)
  generic_query(statement)
  retval = @mysqlh.last_id
  DohDb.logger.call('result', "insert_id was #{retval}")
  retval
end

#insert_hash(hash, table, quote_strings = true) ⇒ Object



74
75
76
# File 'lib/dohmysql/handle.rb', line 74

def insert_hash(hash, table, quote_strings = true)
  insert_hash_helper(hash, table, 'INSERT', quote_strings)
end

#insert_hashes(hashes, table, quote_strings = true) ⇒ Object



78
79
80
# File 'lib/dohmysql/handle.rb', line 78

def insert_hashes(hashes, table, quote_strings = true)
  insert_hashes_helper(hashes, table, 'INSERT', quote_strings)
end

#insert_ignore_hash(hash, table, quote_strings = true) ⇒ Object



82
83
84
# File 'lib/dohmysql/handle.rb', line 82

def insert_ignore_hash(hash, table, quote_strings = true)
  insert_hash_helper(hash, table, 'INSERT IGNORE', quote_strings)
end

#insert_ignore_hashes(hash, table, quote_strings = true) ⇒ Object



86
87
88
# File 'lib/dohmysql/handle.rb', line 86

def insert_ignore_hashes(hash, table, quote_strings = true)
  insert_hashes_helper(hash, table, 'INSERT IGNORE', quote_strings)
end

#query(statement) ⇒ Object



42
43
44
45
46
47
# File 'lib/dohmysql/handle.rb', line 42

def query(statement)
  generic_query(statement)
  retval = @mysqlh.affected_rows
  DohDb.logger.call('result', "affected #{retval} rows")
  retval
end

#replace_hash(hash, table, quote_strings = true) ⇒ Object



90
91
92
# File 'lib/dohmysql/handle.rb', line 90

def replace_hash(hash, table, quote_strings = true)
  insert_hash_helper(hash, table, 'REPLACE', quote_strings)
end

#replace_hashes(hash, table, quote_strings = true) ⇒ Object



94
95
96
# File 'lib/dohmysql/handle.rb', line 94

def replace_hashes(hash, table, quote_strings = true)
  insert_hashes_helper(hash, table, 'REPLACE', quote_strings)
end

#select(statement, build_arg = nil) ⇒ Object

The most generic form of select. It calls to_s on the statement object to facilitate the use of sql builder objects.



100
101
102
103
104
105
# File 'lib/dohmysql/handle.rb', line 100

def select(statement, build_arg = nil)
  result_set = generic_query(statement)
  DohDb.logger.call('result', "selected #{result_set.size} rows")
  rows = get_row_builder(build_arg).build_rows(result_set)
  rows
end

#select_field(statement, build_arg = nil) ⇒ Object

Simple convenience wrapper around select_row. Returns the first (and typically, the only) field from the selected row.



127
128
129
# File 'lib/dohmysql/handle.rb', line 127

def select_field(statement, build_arg = nil)
  select_row(statement, build_arg).at(0)
end

#select_list(statement, build_arg = nil) ⇒ Object

Returns an array of the first (and typically, the only) field of every row in the result set.



166
167
168
# File 'lib/dohmysql/handle.rb', line 166

def select_list(statement, build_arg = nil)
  select(statement, build_arg).collect { |row| row.at(0) }
end

#select_optional_field(statement, build_arg = nil) ⇒ Object

Simple convenience wrapper around select_optional_row. Returns the first (and typically, the only) field from the selected row, if any, or nil.



133
134
135
136
# File 'lib/dohmysql/handle.rb', line 133

def select_optional_field(statement, build_arg = nil)
  row = select_optional_row(statement, build_arg)
  row && row.at(0)
end

#select_optional_row(statement, build_arg = nil) ⇒ Object

Simple convenience wrapper around the generic select call. Throws an exception unless the result set is empty or a single row. Returns nil if the result set is empty, or the row selected.



119
120
121
122
123
# File 'lib/dohmysql/handle.rb', line 119

def select_optional_row(statement, build_arg = nil)
  rows = select(statement, build_arg)
  raise UnexpectedQueryResult, "selected #{rows.size} rows; expected 0 or 1" if rows.size > 1
  if rows.empty? then nil else rows[0] end
end

#select_row(statement, build_arg = nil) ⇒ Object

Simple convenience wrapper around the generic select call. Throws an exception unless the result set is a single row. Returns the row selected.



110
111
112
113
114
# File 'lib/dohmysql/handle.rb', line 110

def select_row(statement, build_arg = nil)
  rows = select(statement, build_arg)
  raise UnexpectedQueryResult, "selected #{rows.size} rows; expected 1" unless rows.size == 1
  rows[0]
end

#select_transpose(statement, build_arg = nil) ⇒ Object

Rows in the result set must have 2 or more fields. If there are 2 fields, returns a hash where each key is the first field in the result set, and the value is the second field. If there are more than 2 fields, returns a hash where each key is the first field in the result set, and the value is the row itself, as a Hash, and without the field used as a key.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/dohmysql/handle.rb', line 142

def select_transpose(statement, build_arg = nil)
  rows = select(statement, build_arg)
  return {} if rows.empty?
  field_count = rows.first.size
  if field_count < 2
    raise UnexpectedQueryResult, "must select at least 2 fields in order to transpose"
  elsif field_count == 2
    Doh.array_to_hash(rows) { |row| [row.at(0), row.at(1)] }
  else
    key_field = rows.first.keys.first
    Doh.array_to_hash(rows) do |row|
      value = row.to_h
      value.delete(key_field)
      [row.at(0), value]
    end
  end
end

#select_values(statement, build_arg = nil) ⇒ Object

Returns an array of arrays, where the individual arrays contain just the values from each database row – they lack field names.



161
162
163
# File 'lib/dohmysql/handle.rb', line 161

def select_values(statement, build_arg = nil)
  select(statement, build_arg).collect { |row| row.values }
end

#start_select(statement, &block) ⇒ Object



194
195
196
197
198
199
200
201
202
203
# File 'lib/dohmysql/handle.rb', line 194

def start_select(statement, &block)
  @async_block = block
  sqlstr = statement.to_s
  DohDb.logger.call('query', "starting async select: #{sqlstr}")
  @mysqlh.query(sqlstr, :async => true)
rescue Exception => excpt
  DohDb.logger.call('error', "caught exception #{excpt.message} starting aysnc query: #{sqlstr}", excpt)
  reopen
  raise
end

#test_transaction_rollbackObject



185
186
187
188
189
190
191
192
# File 'lib/dohmysql/handle.rb', line 185

def test_transaction_rollback
  begin
    @testing_rollback = true
    yield(self)
  ensure
    @testing_rollback = false
  end
end

#transactionObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/dohmysql/handle.rb', line 170

def transaction
  query("START TRANSACTION")
  need_rollback = true
  begin
    retval = yield(self)
    if !@testing_rollback
      query("COMMIT")
      need_rollback = false
    end
  ensure
    reopen if need_rollback
  end
  retval
end

#update(statement) ⇒ Object



49
50
51
52
53
54
# File 'lib/dohmysql/handle.rb', line 49

def update(statement)
  generic_query(statement)
  retval = @mysqlh.affected_rows
  DohDb.logger.call('result', "updated #{retval} rows")
  retval
end

#update_hash(hash, table, primary_key_value, primary_key_name) ⇒ Object



62
63
64
65
# File 'lib/dohmysql/handle.rb', line 62

def update_hash(hash, table, primary_key_value, primary_key_name)
  items = hash.keys.collect {|key| key + ' = ' + hash[key].to_sql}
  query("UPDATE #{table} SET #{items.join(', ')} WHERE #{primary_key_name} = #{primary_key_value.to_sql}")
end

#update_row(statement) ⇒ Object



56
57
58
59
60
# File 'lib/dohmysql/handle.rb', line 56

def update_row(statement)
  retval = update(statement)
  raise UnexpectedQueryResult, "updated #{retval} rows; expected 1" unless retval == 1
  retval
end