Class: DohDb::Handle
- Inherits:
-
Object
- Object
- DohDb::Handle
- Defined in:
- lib/doh/mysql/handle.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#mysqlh ⇒ Object
readonly
Returns the value of attribute mysqlh.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
-
#initialize(config) ⇒ Handle
constructor
A new instance of Handle.
- #insert(statement) ⇒ Object
- #insert_hash(hash, table, quote_strings = true) ⇒ Object
- #insert_hashes(hashes, table, quote_strings = true) ⇒ Object
- #insert_ignore_hash(hash, table, quote_strings = true) ⇒ Object
- #insert_ignore_hashes(hash, table, quote_strings = true) ⇒ Object
- #query(statement) ⇒ Object
- #replace_hash(hash, table, quote_strings = true) ⇒ Object
-
#select(statement, row_builder = nil) ⇒ Object
The most generic form of select.
-
#select_field(statement, row_builder = nil) ⇒ Object
Simple convenience wrapper around select_row.
-
#select_list(statement, row_builder = nil) ⇒ Object
Returns an array of the first (and typically, the only) field of every row in the result set.
-
#select_optional_field(statement, row_builder = nil) ⇒ Object
Simple convenience wrapper around select_optional_row.
-
#select_optional_row(statement, row_builder = nil) ⇒ Object
Simple convenience wrapper around the generic select call.
-
#select_row(statement, row_builder = nil) ⇒ Object
Simple convenience wrapper around the generic select call.
-
#select_transpose(statement, row_builder = nil) ⇒ Object
Rows in the result set must have 2 or more fields.
-
#select_values(statement, row_builder = nil) ⇒ Object
Returns an array of arrays, where the individual arrays contain just the values from each database row – they lack field names.
- #test_transaction_rollback ⇒ Object
- #transaction ⇒ Object
- #update(statement) ⇒ Object
- #update_hash(hash, table, primary_key_value, primary_key_name) ⇒ Object
- #update_row(statement) ⇒ Object
Constructor Details
#initialize(config) ⇒ Handle
Returns a new instance of Handle.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/doh/mysql/handle.rb', line 17 def initialize(config) @config = config @testing_rollback = false log_config = @config.dup @config[:reconnect] = true if !@config.keys.include?(:reconnect) log_config.delete(:password) DohDb.logger.call('connection', "creating connection with config: #{log_config}") @mysqlh = Mysql2::Client.new(@config) DohDb.logger.call('connection', "new connection created: id #{@mysqlh.thread_id}") end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
15 16 17 |
# File 'lib/doh/mysql/handle.rb', line 15 def config @config end |
#mysqlh ⇒ Object (readonly)
Returns the value of attribute mysqlh.
15 16 17 |
# File 'lib/doh/mysql/handle.rb', line 15 def mysqlh @mysqlh end |
Instance Method Details
#close ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/doh/mysql/handle.rb', line 28 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
39 40 41 |
# File 'lib/doh/mysql/handle.rb', line 39 def closed? @mysqlh.nil? end |
#insert(statement) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/doh/mysql/handle.rb', line 68 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
75 76 77 |
# File 'lib/doh/mysql/handle.rb', line 75 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
79 80 81 |
# File 'lib/doh/mysql/handle.rb', line 79 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
83 84 85 |
# File 'lib/doh/mysql/handle.rb', line 83 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
87 88 89 |
# File 'lib/doh/mysql/handle.rb', line 87 def insert_ignore_hashes(hash, table, quote_strings = true) insert_hashes_helper(hash, table, 'INSERT IGNORE', quote_strings) end |
#query(statement) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/doh/mysql/handle.rb', line 43 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
91 92 93 |
# File 'lib/doh/mysql/handle.rb', line 91 def replace_hash(hash, table, quote_strings = true) insert_hash_helper(hash, table, 'REPLACE', quote_strings) end |
#select(statement, row_builder = nil) ⇒ Object
The most generic form of select. It calls to_s on the statement object to facilitate the use of sql builder objects.
97 98 99 100 101 102 |
# File 'lib/doh/mysql/handle.rb', line 97 def select(statement, row_builder = nil) result_set = generic_query(statement) DohDb.logger.call('result', "selected #{result_set.size} rows") rows = get_row_builder(row_builder).build_rows(result_set) rows end |
#select_field(statement, row_builder = nil) ⇒ Object
Simple convenience wrapper around select_row. Returns the first (and typically, the only) field from the selected row.
124 125 126 |
# File 'lib/doh/mysql/handle.rb', line 124 def select_field(statement, row_builder = nil) select_row(statement, row_builder).at(0) end |
#select_list(statement, row_builder = nil) ⇒ Object
Returns an array of the first (and typically, the only) field of every row in the result set.
163 164 165 |
# File 'lib/doh/mysql/handle.rb', line 163 def select_list(statement, row_builder = nil) select(statement, row_builder).collect { |row| row.at(0) } end |
#select_optional_field(statement, row_builder = 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.
130 131 132 133 |
# File 'lib/doh/mysql/handle.rb', line 130 def select_optional_field(statement, row_builder = nil) row = select_optional_row(statement, row_builder) row && row.at(0) end |
#select_optional_row(statement, row_builder = 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.
116 117 118 119 120 |
# File 'lib/doh/mysql/handle.rb', line 116 def select_optional_row(statement, row_builder = nil) rows = select(statement, row_builder) 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, row_builder = 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.
107 108 109 110 111 |
# File 'lib/doh/mysql/handle.rb', line 107 def select_row(statement, row_builder = nil) rows = select(statement, row_builder) raise UnexpectedQueryResult, "selected #{rows.size} rows; expected 1" unless rows.size == 1 rows[0] end |
#select_transpose(statement, row_builder = 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.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/doh/mysql/handle.rb', line 139 def select_transpose(statement, row_builder = nil) rows = select(statement, row_builder) 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, row_builder = nil) ⇒ Object
Returns an array of arrays, where the individual arrays contain just the values from each database row – they lack field names.
158 159 160 |
# File 'lib/doh/mysql/handle.rb', line 158 def select_values(statement, row_builder = nil) select(statement, row_builder).collect { |row| row.values } end |
#test_transaction_rollback ⇒ Object
182 183 184 185 186 187 188 189 |
# File 'lib/doh/mysql/handle.rb', line 182 def test_transaction_rollback begin @testing_rollback = true yield(self) ensure @testing_rollback = false end end |
#transaction ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/doh/mysql/handle.rb', line 167 def transaction query("START TRANSACTION") need_rollback = true begin retval = yield(self) if !@testing_rollback query("COMMIT") need_rollback = false end ensure query("ROLLBACK") if need_rollback end retval end |
#update(statement) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/doh/mysql/handle.rb', line 50 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
63 64 65 66 |
# File 'lib/doh/mysql/handle.rb', line 63 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
57 58 59 60 61 |
# File 'lib/doh/mysql/handle.rb', line 57 def update_row(statement) retval = update(statement) raise UnexpectedQueryResult, "updated #{retval} rows; expected 1" unless retval == 1 retval end |