Class: Sequel::DB2::Dataset

Inherits:
Sequel::Dataset show all
Defined in:
lib/sequel_core/adapters/db2.rb

Constant Summary collapse

MAX_COL_SIZE =
256

Constants inherited from Sequel::Dataset

Sequel::Dataset::AND_SEPARATOR, Sequel::Dataset::BOOL_FALSE, Sequel::Dataset::BOOL_TRUE, Sequel::Dataset::COLUMN_CHANGE_OPTS, Sequel::Dataset::COLUMN_REF_RE1, Sequel::Dataset::COLUMN_REF_RE2, Sequel::Dataset::COLUMN_REF_RE3, Sequel::Dataset::COMMA_SEPARATOR, Sequel::Dataset::COUNT_FROM_SELF_OPTS, Sequel::Dataset::COUNT_OF_ALL_AS_COUNT, Sequel::Dataset::DATASET_CLASSES, Sequel::Dataset::DATE_FORMAT, Sequel::Dataset::MUTATION_METHODS, Sequel::Dataset::NOTIMPL_MSG, Sequel::Dataset::NULL, Sequel::Dataset::N_ARITY_OPERATORS, Sequel::Dataset::QUESTION_MARK, Sequel::Dataset::STOCK_COUNT_OPTS, Sequel::Dataset::STOCK_TRANSFORMS, Sequel::Dataset::TIMESTAMP_FORMAT, Sequel::Dataset::TWO_ARITY_OPERATORS, Sequel::Dataset::WILDCARD

Instance Attribute Summary

Attributes inherited from Sequel::Dataset

#db, #opts, #quote_identifiers, #row_proc

Instance Method Summary collapse

Methods inherited from Sequel::Dataset

#<<, #[], #[]=, #aliased_expression_sql, #all, #and, #as, #avg, #case_expression_sql, #clone, #column_all_sql, #columns, #columns!, #complex_expression_sql, #count, #create_or_replace_view, #create_view, dataset_classes, #def_mutation_method, def_mutation_method, #delete_sql, #each, #each_page, #empty?, #except, #exclude, #exists, #filter, #first, #first_source, #from, #from_self, #function_sql, #get, #graph, #grep, #group, #group_and_count, #having, inherited, #initialize, #insert_multiple, #insert_sql, #inspect, #intersect, #interval, #invert, #irregular_function_sql, #join_clause_sql, #join_on_clause_sql, #join_table, #join_using_clause_sql, #last, #limit, #map, #max, #min, #model_classes, #multi_insert, #multi_insert_sql, #naked, #or, #order, #order_more, #ordered_expression_sql, #paginate, #polymorphic_key, #print, #qualified_identifier_sql, #query, #quote_identifier, #quote_identifiers?, #quoted_identifier, #range, #reverse_order, #select, #select_all, #select_more, #select_sql, #set, #set_graph_aliases, #set_model, #single_record, #single_value, #subscript_sql, #sum, #symbol_to_column_ref, #table_exists?, #to_csv, #to_hash, #transform, #transform_load, #transform_save, #unfiltered, #union, #uniq, #unordered, #update_sql

Methods included from Enumerable

#send_each

Constructor Details

This class inherits a constructor from Sequel::Dataset

Instance Method Details

#convert_type(v) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/sequel_core/adapters/db2.rb', line 131

def convert_type(v)
  case v
  when DB2CLI::Date 
    DBI::Date.new(v.year, v.month, v.day)
  when DB2CLI::Time
    DBI::Time.new(v.hour, v.minute, v.second)
  when DB2CLI::Timestamp 
    DBI::Timestamp.new(v.year, v.month, v.day,
      v.hour, v.minute, v.second, v.fraction)
  when DB2CLI::Null
    nil
  else  
    v
  end
end

#delete(opts = nil) ⇒ Object



155
156
157
# File 'lib/sequel_core/adapters/db2.rb', line 155

def delete(opts = nil)
  @db.do delete_sql(opts)
end

#fetch_rows(sql, &block) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sequel_core/adapters/db2.rb', line 92

def fetch_rows(sql, &block)
  @db.synchronize do
    @db.execute(sql) do |sth|
      @column_info = get_column_info(sth)
      @columns = @column_info.map {|c| c[:name]}
      while (rc = SQLFetch(@handle)) != SQL_NO_DATA_FOUND
        @db.check_error(rc, "Could not fetch row")
        yield hash_row(sth)
      end
    end
  end
  self
end

#get_column_info(sth) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sequel_core/adapters/db2.rb', line 108

def get_column_info(sth)
  rc, column_count = SQLNumResultCols(sth)
  @db.check_error(rc, "Could not get number of result columns")

  (1..column_count).map do |i| 
    rc, name, buflen, datatype, size, digits, nullable = SQLDescribeCol(sth, i, MAX_COL_SIZE)
    @b.check_error(rc, "Could not describe column")
    
    {:name => name, :db2_type => datatype, :precision => size}
  end 
end

#hash_row(sth) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/sequel_core/adapters/db2.rb', line 120

def hash_row(sth)
  row = {}
  @column_info.each_with_index do |c, i|
    rc, v = SQLGetData(sth, i+1, c[:db2_type], c[:precision]) 
    @db.check_error(rc, "Could not get data")
    
    @row[c[:name]] = convert_type(v)
  end
  row
end

#insert(*values) ⇒ Object



147
148
149
# File 'lib/sequel_core/adapters/db2.rb', line 147

def insert(*values)
  @db.do insert_sql(*values)
end

#literal(v) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/sequel_core/adapters/db2.rb', line 81

def literal(v)
  case v
  when Time
    literal(v.iso8601)
  when Date, DateTime
    literal(v.to_s)
  else
    super
  end
end

#update(*args, &block) ⇒ Object



151
152
153
# File 'lib/sequel_core/adapters/db2.rb', line 151

def update(*args, &block)
  @db.do update_sql(*args, &block)
end