Class: RailsConsolePro::QueryBuilderResult
- Inherits:
-
Object
- Object
- RailsConsolePro::QueryBuilderResult
- Defined in:
- lib/rails_console_pro/query_builder_result.rb
Overview
Value object for query builder results
Instance Attribute Summary collapse
-
#explain_result ⇒ Object
readonly
Returns the value of attribute explain_result.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
-
#sql ⇒ Object
readonly
Returns the value of attribute sql.
-
#statistics ⇒ Object
readonly
Returns the value of attribute statistics.
Instance Method Summary collapse
- #analyze ⇒ Object
- #count ⇒ Object
- #execute ⇒ Object
- #exists? ⇒ Boolean
-
#export_to_file(file_path, format: nil) ⇒ Object
Export to file.
-
#initialize(relation:, sql:, explain_result: nil, statistics: {}, model_class: nil) ⇒ QueryBuilderResult
constructor
A new instance of QueryBuilderResult.
- #to_a ⇒ Object
-
#to_html(style: :default) ⇒ Object
Export to HTML.
-
#to_json(pretty: true) ⇒ Object
Export to JSON.
-
#to_yaml ⇒ Object
Export to YAML.
Constructor Details
#initialize(relation:, sql:, explain_result: nil, statistics: {}, model_class: nil) ⇒ QueryBuilderResult
8 9 10 11 12 13 14 |
# File 'lib/rails_console_pro/query_builder_result.rb', line 8 def initialize(relation:, sql:, explain_result: nil, statistics: {}, model_class: nil) @relation = relation @sql = sql @explain_result = explain_result @statistics = statistics @model_class = model_class || (relation.respond_to?(:klass) ? relation.klass : nil) end |
Instance Attribute Details
#explain_result ⇒ Object (readonly)
Returns the value of attribute explain_result.
6 7 8 |
# File 'lib/rails_console_pro/query_builder_result.rb', line 6 def explain_result @explain_result end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
6 7 8 |
# File 'lib/rails_console_pro/query_builder_result.rb', line 6 def model_class @model_class end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
6 7 8 |
# File 'lib/rails_console_pro/query_builder_result.rb', line 6 def relation @relation end |
#sql ⇒ Object (readonly)
Returns the value of attribute sql.
6 7 8 |
# File 'lib/rails_console_pro/query_builder_result.rb', line 6 def sql @sql end |
#statistics ⇒ Object (readonly)
Returns the value of attribute statistics.
6 7 8 |
# File 'lib/rails_console_pro/query_builder_result.rb', line 6 def statistics @statistics end |
Instance Method Details
#analyze ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/rails_console_pro/query_builder_result.rb', line 16 def analyze return self if explain_result return self if sql.nil? # Can't analyze if SQL generation failed explain_cmd = Commands::ExplainCommand.new @explain_result = explain_cmd.execute(relation) self end |
#count ⇒ Object
35 36 37 38 |
# File 'lib/rails_console_pro/query_builder_result.rb', line 35 def count return 0 if sql.nil? relation.count end |
#execute ⇒ Object
25 26 27 28 |
# File 'lib/rails_console_pro/query_builder_result.rb', line 25 def execute return nil if sql.nil? relation.load end |
#exists? ⇒ Boolean
40 41 42 43 |
# File 'lib/rails_console_pro/query_builder_result.rb', line 40 def exists? return false if sql.nil? relation.exists? end |
#export_to_file(file_path, format: nil) ⇒ Object
Export to file
61 62 63 |
# File 'lib/rails_console_pro/query_builder_result.rb', line 61 def export_to_file(file_path, format: nil) FormatExporter.export_to_file(self, file_path, format: format) end |
#to_a ⇒ Object
30 31 32 33 |
# File 'lib/rails_console_pro/query_builder_result.rb', line 30 def to_a return [] if sql.nil? relation.to_a end |
#to_html(style: :default) ⇒ Object
Export to HTML
56 57 58 |
# File 'lib/rails_console_pro/query_builder_result.rb', line 56 def to_html(style: :default) FormatExporter.to_html(self, title: "Query Builder: #{model_class.name}", style: style) end |
#to_json(pretty: true) ⇒ Object
Export to JSON
46 47 48 |
# File 'lib/rails_console_pro/query_builder_result.rb', line 46 def to_json(pretty: true) FormatExporter.to_json(self, pretty: pretty) end |
#to_yaml ⇒ Object
Export to YAML
51 52 53 |
# File 'lib/rails_console_pro/query_builder_result.rb', line 51 def to_yaml FormatExporter.to_yaml(self) end |