Class: SqlQuery
- Inherits:
-
Object
- Object
- SqlQuery
- Defined in:
- lib/sql_query.rb
Defined Under Namespace
Classes: Config
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Class Method Summary collapse
Instance Method Summary collapse
- #execute ⇒ Object
- #explain ⇒ Object
-
#initialize(file_name, options = {}) ⇒ SqlQuery
constructor
A new instance of SqlQuery.
- #prepared_for_logs ⇒ Object
- #pretty_sql ⇒ Object
- #quote(value) ⇒ Object
- #sql ⇒ Object
Constructor Details
#initialize(file_name, options = {}) ⇒ SqlQuery
Returns a new instance of SqlQuery.
6 7 8 9 10 11 12 13 |
# File 'lib/sql_query.rb', line 6 def initialize(file_name, = {}) unless file_name.is_a?(String) || file_name.is_a?(Symbol) fail ArgumentError, 'SQL file name should be String or Symbol' end @sql_filename = file_name prepare_variables() @connection = ActiveRecord::Base.connection end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
4 5 6 |
# File 'lib/sql_query.rb', line 4 def connection @connection end |
Class Method Details
.config ⇒ Object
45 46 47 |
# File 'lib/sql_query.rb', line 45 def self.config @config ||= Config.new end |
.config=(value) ⇒ Object
41 42 43 |
# File 'lib/sql_query.rb', line 41 def self.config=(value) @config = value end |
.configure {|config| ... } ⇒ Object
49 50 51 |
# File 'lib/sql_query.rb', line 49 def self.configure yield(config) end |
Instance Method Details
#execute ⇒ Object
21 22 23 |
# File 'lib/sql_query.rb', line 21 def execute connection.execute(prepared_for_logs).entries end |
#explain ⇒ Object
15 16 17 18 19 |
# File 'lib/sql_query.rb', line 15 def explain msg = "EXPLAIN for: \n#{ sql }\n" msg += connection.explain(sql) pretty(msg) end |
#prepared_for_logs ⇒ Object
37 38 39 |
# File 'lib/sql_query.rb', line 37 def prepared_for_logs sql.gsub(/(\n|\s)+/,' ') end |
#pretty_sql ⇒ Object
29 30 31 |
# File 'lib/sql_query.rb', line 29 def pretty_sql pretty(sql.dup) end |
#quote(value) ⇒ Object
33 34 35 |
# File 'lib/sql_query.rb', line 33 def quote(value) connection.quote(value) end |
#sql ⇒ Object
25 26 27 |
# File 'lib/sql_query.rb', line 25 def sql @sql ||= ERB.new(File.read(file_path)).result(binding) end |