Class: RailsDb::SqlQuery
- Inherits:
-
Object
- Object
- RailsDb::SqlQuery
- Includes:
- Connection
- Defined in:
- lib/rails_db/sql_query.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#explain ⇒ Object
readonly
Returns the value of attribute explain.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#sql_explain ⇒ Object
readonly
Returns the value of attribute sql_explain.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(query, sql_explain = true) ⇒ SqlQuery
constructor
A new instance of SqlQuery.
- #load_data ⇒ Object
- #load_explain ⇒ Object
- #to_csv ⇒ Object
- #valid? ⇒ Boolean
Methods included from Connection
#column_names, #column_properties, #columns, #connection, #to_param
Constructor Details
#initialize(query, sql_explain = true) ⇒ SqlQuery
Returns a new instance of SqlQuery.
7 8 9 10 |
# File 'lib/rails_db/sql_query.rb', line 7 def initialize(query, sql_explain = true) @query = query @sql_explain = sql_explain end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/rails_db/sql_query.rb', line 5 def data @data end |
#explain ⇒ Object (readonly)
Returns the value of attribute explain.
5 6 7 |
# File 'lib/rails_db/sql_query.rb', line 5 def explain @explain end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
5 6 7 |
# File 'lib/rails_db/sql_query.rb', line 5 def query @query end |
#sql_explain ⇒ Object (readonly)
Returns the value of attribute sql_explain.
5 6 7 |
# File 'lib/rails_db/sql_query.rb', line 5 def sql_explain @sql_explain end |
Instance Method Details
#execute ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/rails_db/sql_query.rb', line 24 def execute if valid? load_data load_explain if sql_explain History.add(query) end self end |
#load_data ⇒ Object
20 21 22 |
# File 'lib/rails_db/sql_query.rb', line 20 def load_data @data ||= SqlQueryData.new(self).load_data end |
#load_explain ⇒ Object
16 17 18 |
# File 'lib/rails_db/sql_query.rb', line 16 def load_explain @explain ||= SqlExplain.new(self).load_data end |
#to_csv ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/rails_db/sql_query.rb', line 33 def to_csv CSV.generate do |csv| csv << data.columns data.rows.each do |row| csv << row end end end |
#valid? ⇒ Boolean
12 13 14 |
# File 'lib/rails_db/sql_query.rb', line 12 def valid? query.present? end |