Class: Sqlreport::Result
- Inherits:
-
Object
- Object
- Sqlreport::Result
- Defined in:
- lib/sqlreport/result.rb
Overview
Result
Instance Method Summary collapse
- #columns ⇒ Object
- #connection ⇒ Object
-
#initialize(query, db_config: false) ⇒ Result
constructor
A new instance of Result.
- #result(validate: true) ⇒ Object
- #rows ⇒ Object
- #to_csv(include_headers: true, separator: ",", quote_char: '"') ⇒ Object
- #write_csv(path, include_headers: true, separator: ",", quote_char: '"') ⇒ Object
Constructor Details
#initialize(query, db_config: false) ⇒ Result
Returns a new instance of Result.
9 10 11 12 13 |
# File 'lib/sqlreport/result.rb', line 9 def initialize(query, db_config: false) @query = query @db_config = db_config @response = nil end |
Instance Method Details
#columns ⇒ Object
32 33 34 |
# File 'lib/sqlreport/result.rb', line 32 def columns @response.columns end |
#connection ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/sqlreport/result.rb', line 15 def connection @connection ||= if @db_config ActiveRecord::Base.establish_connection(@db_config) else ActiveRecord::Base.connection end end |
#result(validate: true) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/sqlreport/result.rb', line 23 def result(validate: true) connection validations = validate_input if validate return validations if validations @response = @connection.exec_query(@query) self end |
#rows ⇒ Object
36 37 38 |
# File 'lib/sqlreport/result.rb', line 36 def rows @response.rows end |
#to_csv(include_headers: true, separator: ",", quote_char: '"') ⇒ Object
40 41 42 43 44 45 |
# File 'lib/sqlreport/result.rb', line 40 def to_csv(include_headers: true, separator: ",", quote_char: '"') CSV.generate(col_sep: separator, quote_char: quote_char) do |csv| csv << @response.columns if include_headers @response.rows.each { |row| csv << row } end end |
#write_csv(path, include_headers: true, separator: ",", quote_char: '"') ⇒ Object
47 48 49 50 51 |
# File 'lib/sqlreport/result.rb', line 47 def write_csv(path, include_headers: true, separator: ",", quote_char: '"') data = to_csv(include_headers: include_headers, separator: separator, quote_char: quote_char) File.write(path, data) true end |