Class: DohDb::Handle
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
-
#initialize(mysqlh, row_builder = nil) ⇒ Handle
constructor
A new instance of Handle.
- #insert(statement) ⇒ Object
- #insert_hash(hash, table, ignore = nil) ⇒ Object
- #multi_select(statement_infos, dflt_row_builder = nil) ⇒ Object
- #query(statement) ⇒ Object
- #replace_hash(hash, table) ⇒ 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.
- #update(statement) ⇒ Object
- #update_hash(hash, table, primary_key_value, primary_key_name) ⇒ Object
- #update_row(statement) ⇒ Object
Constructor Details
#initialize(mysqlh, row_builder = nil) ⇒ Handle
Returns a new instance of Handle.
12 13 14 15 |
# File 'lib/doh/mysql/handle.rb', line 12 def initialize(mysqlh, row_builder = nil) @mysqlh = mysqlh @row_builder = row_builder || TypedRowBuilder.new end |
Instance Method Details
#close ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/doh/mysql/handle.rb', line 17 def close unless closed? dohlog.info("closing raw mysql handle: #@mysqlh") @mysqlh.close @mysqlh = nil end end |
#closed? ⇒ Boolean
25 26 27 |
# File 'lib/doh/mysql/handle.rb', line 25 def closed? @mysqlh.nil? end |
#insert(statement) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/doh/mysql/handle.rb', line 54 def insert(statement) generic_query(statement) retval = @mysqlh.insert_id dohlog.info("insert_id was #{retval}") retval end |
#insert_hash(hash, table, ignore = nil) ⇒ Object
61 62 63 64 |
# File 'lib/doh/mysql/handle.rb', line 61 def insert_hash(hash, table, ignore = nil) if ignore then keyword = 'INSERT IGNORE' else keyword = 'INSERT' end insert_hash_helper(hash, table, keyword) end |
#multi_select(statement_infos, dflt_row_builder = nil) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/doh/mysql/handle.rb', line 143 def multi_select(statement_infos, dflt_row_builder = nil) statements = [] row_builders = [] statement_infos.each do |info| if info.is_a?(Array) statements.push(info.first) row_builders.push(info.last) else statements.push(info) row_builders.push(nil) end end sqlstr = statements.join(';') dohlog.info(sqlstr) @mysqlh.query(sqlstr) retval = [] while true custom_builder = row_builders.shift if @mysqlh.field_count > 0 result_set = @mysqlh.store_result dohlog.info("selected #{result_set.num_rows} rows") rows = get_row_builder(custom_builder || dflt_row_builder).build_rows(result_set) result_set.free retval.push(rows) end break unless @mysqlh.next_result end retval rescue Exception => excpt dohlog.error("caught exception during query: #{sqlstr}", excpt) raise end |
#query(statement) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/doh/mysql/handle.rb', line 29 def query(statement) generic_query(statement) retval = @mysqlh.affected_rows dohlog.info("affected #{retval} rows") retval end |
#replace_hash(hash, table) ⇒ Object
66 67 68 |
# File 'lib/doh/mysql/handle.rb', line 66 def replace_hash(hash, table) insert_hash_helper(hash, table, 'REPLACE') 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.
72 73 74 75 76 77 78 |
# File 'lib/doh/mysql/handle.rb', line 72 def select(statement, row_builder = nil) result_set = generic_query(statement) dohlog.info("selected #{result_set.num_rows} rows") rows = get_row_builder(row_builder).build_rows(result_set) result_set.free 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.
100 101 102 |
# File 'lib/doh/mysql/handle.rb', line 100 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.
139 140 141 |
# File 'lib/doh/mysql/handle.rb', line 139 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.
106 107 108 109 |
# File 'lib/doh/mysql/handle.rb', line 106 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.
92 93 94 95 96 |
# File 'lib/doh/mysql/handle.rb', line 92 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.
83 84 85 86 87 |
# File 'lib/doh/mysql/handle.rb', line 83 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.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/doh/mysql/handle.rb', line 115 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 rows.build_hash { |row| [row.at(0), row.at(1)] } else key_field = rows.first.keys.first rows.build_hash 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.
134 135 136 |
# File 'lib/doh/mysql/handle.rb', line 134 def select_values(statement, row_builder = nil) select(statement, row_builder).collect { |row| row.values } end |
#update(statement) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/doh/mysql/handle.rb', line 36 def update(statement) generic_query(statement) retval = @mysqlh.affected_rows dohlog.info("updated #{retval} rows") retval end |
#update_hash(hash, table, primary_key_value, primary_key_name) ⇒ Object
49 50 51 52 |
# File 'lib/doh/mysql/handle.rb', line 49 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
43 44 45 46 47 |
# File 'lib/doh/mysql/handle.rb', line 43 def update_row(statement) retval = update(statement) raise UnexpectedQueryResult, "updated #{retval} rows; expected 1" unless retval == 1 retval end |