Class: LabileRecord::Query
- Inherits:
-
Array
- Object
- Array
- LabileRecord::Query
- Defined in:
- lib/labilerecord.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Instance Method Summary collapse
- #connection ⇒ Object
- #exec! ⇒ Object
-
#initialize(query_string) ⇒ Query
constructor
A new instance of Query.
- #parse_fields ⇒ Object
- #parse_result_data ⇒ Object
- #to_insert_sql(table_name = nil) ⇒ Object
Constructor Details
#initialize(query_string) ⇒ Query
Returns a new instance of Query.
35 36 37 |
# File 'lib/labilerecord.rb', line 35 def initialize(query_string) @string = query_string end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
32 33 34 |
# File 'lib/labilerecord.rb', line 32 def fields @fields end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
31 32 33 |
# File 'lib/labilerecord.rb', line 31 def result @result end |
#string ⇒ Object (readonly)
Returns the value of attribute string.
33 34 35 |
# File 'lib/labilerecord.rb', line 33 def string @string end |
Instance Method Details
#connection ⇒ Object
71 72 73 |
# File 'lib/labilerecord.rb', line 71 def connection LabileRecord::Base.connection end |
#exec! ⇒ Object
39 40 41 42 43 44 |
# File 'lib/labilerecord.rb', line 39 def exec! @result = connection.exec(@string) parse_fields parse_result_data self end |
#parse_fields ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/labilerecord.rb', line 60 def parse_fields @fields = @field_names = [] @result.fields.each_with_index do |field_name, i| pg_field_type_id = @result.ftype(i) type = connection.exec("SELECT typname FROM pg_type WHERE oid = #{pg_field_type_id}") field_type_name = type[0][type.fields[0]].to_s @fields << Field.new( field_name, field_type_name, pg_field_type_id) end end |
#parse_result_data ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/labilerecord.rb', line 46 def parse_result_data columns = @result.fields row_count = @result.num_tuples field_names = @fields.map {|field| field.name} # iterate rows (0..(row_count-1)).each do |row_index| row = Row.new(field_names) columns.each do |column_name| row << @result[row_index][column_name] end send "<<", row end end |
#to_insert_sql(table_name = nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/labilerecord.rb', line 75 def to_insert_sql(table_name=nil) # return: [INSERT INTO table_name] (column_list) VALUES(value_list); sql = "" each do |row| non_nil_column_names = [] non_nil_values = [] row.each_with_index do |column, i| non_nil_column_names << fields[i].name if !column.nil? non_nil_values << column if !column.nil? end sql += %Q[ #{"INSERT INTO " + table_name.to_s if table_name} (#{ non_nil_column_names.map {|c| '"' + c + '"'} * "," }) VALUES (#{ non_nil_values.map {|c| "'" + c + "'"} * "," }); ].strip + "\n" end sql end |