Class: JDBCHelpers::QueryResultsToHashOfArrays
- Defined in:
- lib/jdbc_helpers.rb
Overview
Creates a hash of arrays of hashes from a SQL query
Instance Attribute Summary collapse
-
#results ⇒ Object
Returns the value of attribute results.
Instance Method Summary collapse
-
#initialize(db_connect, statement, key_field, logger = nil) ⇒ QueryResultsToHashOfArrays
constructor
A new instance of QueryResultsToHashOfArrays.
-
#rs_to_hash(rs, index_key_field, multi_val) ⇒ Hash
converts a JDBC recordset to an array of hashes, with one hash per record creates a hash from a jdbc record set index_key_field is the field you want to use as the top level hash key…
Methods inherited from Base
#cleanse_statement, #convert_to_string_classes
Constructor Details
#initialize(db_connect, statement, key_field, logger = nil) ⇒ QueryResultsToHashOfArrays
Returns a new instance of QueryResultsToHashOfArrays.
158 159 160 161 162 163 164 165 166 |
# File 'lib/jdbc_helpers.rb', line 158 def initialize(db_connect, statement, key_field, logger = nil) @logger = logger ? logger : Logger.new(STDOUT) stmt = db_connect.create_statement @logger.info "executing query in thread #{Thread.current.object_id}:\n#{cleanse_statement(statement)}" start = Time.new.utc rs = stmt.execute_query(statement) @logger.info "query executed #{Time.new.utc - start} seconds" @results=rs_to_hash(rs, key_field, true) end |
Instance Attribute Details
#results ⇒ Object
Returns the value of attribute results.
152 153 154 |
# File 'lib/jdbc_helpers.rb', line 152 def results @results end |
Instance Method Details
#rs_to_hash(rs, index_key_field, multi_val) ⇒ Hash
converts a JDBC recordset to an array of hashes, with one hash per record creates a hash from a jdbc record set index_key_field is the field you want to use as the top level hash key… and should exist in the record set multi_val=true will create an array below each index_key_filed, false will create a hash as the child
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/jdbc_helpers.rb', line 177 def rs_to_hash(rs, index_key_field, multi_val) # setting default hash value is necessary for appending to arrays hash=Hash.new{ |h, k| h[k] = [] } # get basic metadata for the recordset = rs.getMetaData cols = .getColumnCount.to_i # loop through the records to add them into hash while rs.next do # if multi_val is not true... create new hash value as an empty hash if it doesn't already exist hash[rs.getString(index_key_field)]={} if (!hash[rs.getString(index_key_field)] and !multi_val) # if multi_val is true... create new hash value as an empty array if it doesn't already exist hash[rs.getString(index_key_field)]=[] if (!hash[rs.getString(index_key_field)] and multi_val) # r is a temporary hash for the row being processed r=Hash.new # add each row to r (1..cols).each do |c| r[.get_column_name(c)] = rs.getObject(c) if convert_to_string_classes.include?(r[.get_column_name(c)].class) r[.get_column_name(c)] = r[.get_column_name(c)].to_s end end # each cols # set hash value to r if not multi_val hash[rs.getString(index_key_field)] = r if !multi_val # append hash to r if multi_val hash[rs.getString(index_key_field)] << r if multi_val end # while # completed hash is returned return hash end |