Module: Sequel::Pervasive::DatasetMethods

Defined in:
lib/sequel/pervasive_adapter.rb

Constant Summary collapse

SELECT_CLAUSE_METHODS =
Dataset.clause_methods(:select, %w'limit distinct columns from with join where group order having compounds')

Instance Method Summary collapse

Instance Method Details

#convert_odbc_value(v, column_type = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sequel/pervasive_adapter.rb', line 82

def convert_odbc_value(v, column_type=nil)
  case column_type
  when :date then
    Date.from_fos_days(v.to_i)
  when :time then
    Date.from_fos_days(0).to_time.utc + v.to_i.minutes
  else
    v = v.unpack('A*')[0] if v.is_a? String
    super(v)
  end
end

#fetch_rows(sql, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sequel/pervasive_adapter.rb', line 64

def fetch_rows(sql, &block)
  execute(sql) do |s|
    i = -1
    cols = s.columns(true).map{|c| [output_identifier(c.name), i+=1]}
    
    @columns = cols.map{|c| c.at(0)}

    if rows = s.fetch_all
      rows.each do |row|
        hash = {}
        cols.each{|n, i| hash[n] = convert_odbc_value(row[i], get_column_type(@columns[i]))}
        yield hash
      end
    end
  end
  self
end

#get_column_type(column_name) ⇒ Object



94
95
96
97
98
99
# File 'lib/sequel/pervasive_adapter.rb', line 94

def get_column_type(column_name)
  if model and model.respond_to?(:datatypes) and model.datatypes and model.datatypes[column_name]
    return model.datatypes[column_name][:type]
  end
  nil
end

#quoted_identifier(name, convert = true) ⇒ Object



103
104
105
# File 'lib/sequel/pervasive_adapter.rb', line 103

def quoted_identifier(name, convert=true)
  convert ? "\"#{convert_aliased_col_to_real_col(name)}\"" : "\"#{name}\""
end

#select_fields(hash) ⇒ Object

Take a table name and write out the field names for that table in this style: “‘DUDES`.`KID - DATE`”.lit where the table name is :dudes, and fields are [:kid_date]



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/sequel/pervasive_adapter.rb', line 110

def select_fields(hash)
  new_hash = Hash.new{|k, v| k[v]= {}}

  hash.each do |association, fields|
    new_hash[association][:fields] = fields

    if association == :self
      new_hash[association][:model] = self.model
      new_hash[association][:association_name] = self.model.table_name
    elsif association.to_s.match /(\w+)__(\w+)/  # crew_legs__position_code
      # not an assocation on the current model .. but another one
      new_hash[association][:association_name] = $2.to_s.upcase
      new_hash[association][:model] = model.association_reflection($1.to_sym)[:class].association_reflection($2.to_sym)[:class]
    else
      raise(Sequel::Error, "Invalid #{model} association: #{association}") unless model.association_reflection(association)
      new_hash[association][:association_name] = association.to_s.upcase
      new_hash[association][:model] = model.association_reflection(association)[:class]
    end
    fields = fields + new_hash[association][:model].primary_key unless fields[0] == :*
    new_hash[association][:fields] = fields
  end

  s = []
  new_hash.each do |association, hash|
    if hash[:fields].size == 1 and hash[:fields][0] == :*
      s << "#{quoted_identifier(hash[:association_name], false)}.*".lit
    else
      s << hash[:fields].collect do |field|
        raw_field_name = convert_aliased_col_to_real_col_with_model(field, hash[:model])
        as_alias = ''
        raw_field_name

        if association != :self and ( hash[:model].primary_key.include? field or field_duplicate(new_hash, association, field) )
          as_field_name = "#{hash[:association_name]}_#{raw_field_name}"
          as_alias = "AS #{quoted_identifier(as_field_name, false)}"
        end
        "#{quoted_identifier(hash[:association_name], false)}.#{quoted_identifier(raw_field_name, false)} #{as_alias}".lit
      end
    end
  end
  
  clone(:select => s.flatten)
end