Class: LabileRecord::Query

Inherits:
Array
  • Object
show all
Defined in:
lib/labilerecord.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_string) ⇒ Query

Returns a new instance of Query.



34
35
36
# File 'lib/labilerecord.rb', line 34

def initialize(query_string)
  @string = query_string
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



31
32
33
# File 'lib/labilerecord.rb', line 31

def fields
  @fields
end

#resultObject (readonly)

Returns the value of attribute result.



30
31
32
# File 'lib/labilerecord.rb', line 30

def result
  @result
end

#stringObject (readonly)

Returns the value of attribute string.



32
33
34
# File 'lib/labilerecord.rb', line 32

def string
  @string
end

Instance Method Details

#connectionObject



70
71
72
# File 'lib/labilerecord.rb', line 70

def connection
  LabileRecord::Base.connection
end

#exec!Object



38
39
40
41
42
43
# File 'lib/labilerecord.rb', line 38

def exec!
  @result = connection.exec(@string)
  parse_fields
  parse_result_data
  self
end

#field_by_name(name) ⇒ Object



112
113
114
115
116
# File 'lib/labilerecord.rb', line 112

def field_by_name(name)
  self.fields.each do |field|
    return field if field.name == name
  end
end

#parse_fieldsObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/labilerecord.rb', line 59

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_dataObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/labilerecord.rb', line 45

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, quote = "'") ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/labilerecord.rb', line 74

def to_insert_sql(table_name=nil, quote="'")
  # 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| quote_object(c) } * "," })\n]
    sql << "VALUES ("
    non_nil_values.each_with_index do |v, i|
      sql << quote_value(v, quote) + "::" + field_by_name(non_nil_column_names[i]).type
      sql << ',' if i < non_nil_values.length - 1
    end
    sql << ");\n"
  end
  sql
end

#to_static_set_sql(quote = "'") ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/labilerecord.rb', line 95

def to_static_set_sql(quote="'")
  rows_sql = ""
  self.each_with_index do |row, row_index|
    row_sql = "SELECT "
    self.fields.each_with_index do |field, field_index|
      row_field_value = row.send(field.name.to_sym)
      # builds: value + cast as column + [',' if not last row]
      row_sql << (row_field_value ? quote_value(row_field_value, quote) : "NULL")
      row_sql << "::" + field.type + %Q[ AS "#{field.name}"]
      row_sql << ", " if field_index < self.fields.length - 1
    end
    row_sql << " UNION\n" if row_index < self.length - 1
    rows_sql << row_sql
  end
  rows_sql
end