Class: RailsDb::SqlQuery

Inherits:
Object
  • Object
show all
Includes:
Connection
Defined in:
lib/rails_db/sql_query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/rails_db/sql_query.rb', line 5

def data
  @data
end

#explainObject (readonly)

Returns the value of attribute explain.



5
6
7
# File 'lib/rails_db/sql_query.rb', line 5

def explain
  @explain
end

#queryObject (readonly)

Returns the value of attribute query.



5
6
7
# File 'lib/rails_db/sql_query.rb', line 5

def query
  @query
end

#sql_explainObject (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

#executeObject



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_dataObject



20
21
22
# File 'lib/rails_db/sql_query.rb', line 20

def load_data
  @data    ||= SqlQueryData.new(self).load_data
end

#load_explainObject



16
17
18
# File 'lib/rails_db/sql_query.rb', line 16

def load_explain
  @explain ||= SqlExplain.new(self).load_data
end

#to_csvObject



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

Returns:

  • (Boolean)


12
13
14
# File 'lib/rails_db/sql_query.rb', line 12

def valid?
  query.present?
end