Class: Qreport::Connection::Query
- Inherits:
-
Object
- Object
- Qreport::Connection::Query
- Defined in:
- lib/qreport/connection/query.rb
Instance Attribute Summary collapse
-
#cmd_status ⇒ Object
Returns the value of attribute cmd_status.
-
#cmd_status_raw ⇒ Object
Returns the value of attribute cmd_status_raw.
-
#cmd_tuples ⇒ Object
Returns the value of attribute cmd_tuples.
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#conn ⇒ Object
Returns the value of attribute conn.
-
#error ⇒ Object
Returns the value of attribute error.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#fmods ⇒ Object
Returns the value of attribute fmods.
-
#ftypes ⇒ Object
Returns the value of attribute ftypes.
-
#nfields ⇒ Object
Returns the value of attribute nfields.
-
#options ⇒ Object
Returns the value of attribute options.
-
#rows ⇒ Object
Returns the value of attribute rows.
-
#sql ⇒ Object
Returns the value of attribute sql.
-
#sql_prepared ⇒ Object
Returns the value of attribute sql_prepared.
-
#type_names ⇒ Object
Returns the value of attribute type_names.
Instance Method Summary collapse
- #extract_results!(result) ⇒ Object
- #prepare_sql(sql) ⇒ Object
- #run! ⇒ Object
- #sql_replace_arguments(sql, arguments) ⇒ Object
- #sql_replace_match(sql) ⇒ Object
Instance Attribute Details
#cmd_status ⇒ Object
Returns the value of attribute cmd_status.
8 9 10 |
# File 'lib/qreport/connection/query.rb', line 8 def cmd_status @cmd_status end |
#cmd_status_raw ⇒ Object
Returns the value of attribute cmd_status_raw.
8 9 10 |
# File 'lib/qreport/connection/query.rb', line 8 def cmd_status_raw @cmd_status_raw end |
#cmd_tuples ⇒ Object
Returns the value of attribute cmd_tuples.
8 9 10 |
# File 'lib/qreport/connection/query.rb', line 8 def cmd_tuples @cmd_tuples end |
#columns ⇒ Object
Returns the value of attribute columns.
11 12 13 |
# File 'lib/qreport/connection/query.rb', line 11 def columns @columns end |
#conn ⇒ Object
Returns the value of attribute conn.
6 7 8 |
# File 'lib/qreport/connection/query.rb', line 6 def conn @conn end |
#error ⇒ Object
Returns the value of attribute error.
8 9 10 |
# File 'lib/qreport/connection/query.rb', line 8 def error @error end |
#fields ⇒ Object
Returns the value of attribute fields.
9 10 11 |
# File 'lib/qreport/connection/query.rb', line 9 def fields @fields end |
#fmods ⇒ Object
Returns the value of attribute fmods.
9 10 11 |
# File 'lib/qreport/connection/query.rb', line 9 def fmods @fmods end |
#ftypes ⇒ Object
Returns the value of attribute ftypes.
9 10 11 |
# File 'lib/qreport/connection/query.rb', line 9 def ftypes @ftypes end |
#nfields ⇒ Object
Returns the value of attribute nfields.
9 10 11 |
# File 'lib/qreport/connection/query.rb', line 9 def nfields @nfields end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/qreport/connection/query.rb', line 6 def end |
#rows ⇒ Object
Returns the value of attribute rows.
11 12 13 |
# File 'lib/qreport/connection/query.rb', line 11 def rows @rows end |
#sql ⇒ Object
Returns the value of attribute sql.
6 7 8 |
# File 'lib/qreport/connection/query.rb', line 6 def sql @sql end |
#sql_prepared ⇒ Object
Returns the value of attribute sql_prepared.
7 8 9 |
# File 'lib/qreport/connection/query.rb', line 7 def sql_prepared @sql_prepared end |
#type_names ⇒ Object
Returns the value of attribute type_names.
10 11 12 |
# File 'lib/qreport/connection/query.rb', line 10 def type_names @type_names end |
Instance Method Details
#extract_results!(result) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/qreport/connection/query.rb', line 88 def extract_results! result error = result. error = nil if error.empty? @error = error @cmd_status_raw = result.cmd_status @cmd_tuples = result.cmd_tuples @nfields = result.nfields @ntuples = result.ntuples @fields = result.fields @ftypes = (0 ... nfields).map{|i| result.ftype(i) } @fmods = (0 ... nfields).map{|i| result.fmod(i) } @rows = result.to_a type_names rows self ensure result.clear @conn = nil end |
#prepare_sql(sql) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/qreport/connection/query.rb', line 33 def prepare_sql sql sql = sql.sub(/[\s\n]*;[\s\n]*\Z/, '') if .key?(:limit) sql = conn.with_limit sql, [:limit] end if arguments = [:arguments] if values = arguments[:names_and_values] n = conn.safe_sql(values.keys * ', ') v = conn.safe_sql(values.keys.map{|k| ":#{k}"} * ', ') sql = sql_replace_arguments(sql, :NAMES => n, :VALUES => v, :NAMES_AND_VALUES => conn.safe_sql("( #{n} ) VALUES ( #{v} )"), :SET_VALUES => conn.safe_sql(values.keys.map{|k| "#{conn.escape_identifier(k)} = :#{k}"} * ', ')) arguments = arguments.merge(values) end sql = sql_replace_arguments(sql, arguments) end sql end |
#run! ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/qreport/connection/query.rb', line 13 def run! @error = nil @fields = @ftypes = @mods = EMPTY_Array @nfields = 0 sql = @sql_prepared = prepare_sql self.sql if conn.verbose || [:verbose] out = conn.verbose_stream out.puts "\n-- =================================================================== --" out.puts sql out.puts "-- ==== --" end return self if [:dry_run] if result = conn.run_query!(sql, self, ) extract_results! result end self ensure @conn = nil end |
#sql_replace_arguments(sql, arguments) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/qreport/connection/query.rb', line 54 def sql_replace_arguments sql, arguments sql = sql.gsub(/(:(\w+)\b([?]?))/) do | m | name = $2.to_sym optional = ! $3.empty? if arguments.key?(name) || optional val = arguments[name] unless optional && val.nil? val = conn.escape_value(val) end conn.verbose_stream.puts " #{name} => #{val}" if [:verbose_arguments] val else $1 end end sql = sql_replace_match sql end |
#sql_replace_match(sql) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/qreport/connection/query.rb', line 72 def sql_replace_match sql sql = sql.gsub(/:~\s*\{\{([^\}]+?)\}\}\s*\{\{([^\}]+?)\}\}/) do | m | expr = $1 val = $2 case expr when /\A\s*BETWEEN\b/ "(#{val} #{expr})" when "NULL" "(#{val} IS NULL)" else "(#{val} = #{expr})" end end sql end |