Class: JDBCHelpers::QueryResultsToJSONFile
- Defined in:
- lib/jdbc_helpers.rb
Overview
class
Instance Method Summary collapse
-
#initialize(db_connect, statement, file_object, formatter = nil, logger = nil) ⇒ QueryResultsToJSONFile
constructor
runs a SQL query, then writes the results as JSON objects to a provided file object.
-
#json_formatter ⇒ proc
proc must handle two inputs, |file_object, record hash|.
-
#rs_to_json_file(rs, file_object, formatter) ⇒ Object
outputs a JDBC result set to a formatted file formatter defaults to JSON output unless you provide your own proc.
Methods inherited from Base
#cleanse_statement, #convert_to_string_classes
Constructor Details
#initialize(db_connect, statement, file_object, formatter = nil, logger = nil) ⇒ QueryResultsToJSONFile
runs a SQL query, then writes the results as JSON objects to a provided file object. by use of the formatter parameter, you can change the output to any format you desire (CSV, XML, etc) see json_formatter method for an example of a proc to perform formatting
225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/jdbc_helpers.rb', line 225 def initialize(db_connect, statement, file_object, formatter = nil, logger = nil) @logger = logger ? logger : Logger.new(STDOUT) stmt = db_connect.create_statement @logger.info "executing query: #{cleanse_statement(statement)}" start = Time.new.utc rs = stmt.execute_query(statement) @logger.info "query executed initial results #{Time.new.utc - start} seconds" record_count = rs_to_json_file(rs, file_object, formatter) @logger.info "query export time #{Time.new.utc - start} seconds" @logger.info "#{record_count} rows exported" end |
Instance Method Details
#json_formatter ⇒ proc
proc must handle two inputs, |file_object, record hash|
274 275 276 |
# File 'lib/jdbc_helpers.rb', line 274 def json_formatter Proc.new { |f,h| f.puts h.to_json } end |
#rs_to_json_file(rs, file_object, formatter) ⇒ Object
outputs a JDBC result set to a formatted file formatter defaults to JSON output unless you provide your own proc
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/jdbc_helpers.rb', line 242 def rs_to_json_file(rs, file_object, formatter) # default formatter outputs json objects for each row formatter = json_formatter unless formatter # get basic metadata for the recordset = rs.getMetaData cols = .getColumnCount.to_i record_count = 0 # loop through the records to add them into hash while rs.next do # 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 # formatter handles output of r to file formatter.call(file_object, r) record_count += 1 end # while return record_count end |