Class: Sequel::Postgres::Dataset

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel_pg/sequel_pg.rb,
lib/sequel_pg/model.rb

Overview

Add faster versions of Dataset#map, #as_hash, #to_hash_groups, #select_map, #select_order_map, and #select_hash

Instance Method Summary collapse

Instance Method Details

#all(&block) ⇒ Object

Delegate to with_sql_all using the default SQL



100
101
102
# File 'lib/sequel_pg/sequel_pg.rb', line 100

def all(&block)
  with_sql_all(sql, &block)
end

#as_hash(key_column, value_column = nil, opts = Sequel::OPTS) ⇒ Object Also known as: to_hash

In the case where both arguments given, use an optimized version.



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sequel_pg/sequel_pg.rb', line 66

def as_hash(key_column, value_column = nil, opts = Sequel::OPTS)
  if value_column && !opts[:hash]
    return super unless allow_sequel_pg_optimization?
    clone(:_sequel_pg_type=>:hash, :_sequel_pg_value=>[key_column, value_column]).fetch_rows(sql){|s| return s}
    {}
  elsif opts.empty?
    super(key_column, value_column)
  else
    super
  end
end

#as_set(column) ⇒ Object

simplecov:disable



35
36
37
38
39
# File 'lib/sequel_pg/sequel_pg.rb', line 35

def as_set(column)
  return super unless allow_sequel_pg_optimization?
  clone(:_sequel_pg_type=>:map_set, :_sequel_pg_value=>column).fetch_rows(sql){|s| return s}
  Set.new
end

#each(&block) ⇒ Object

If model loads are being optimized and this is a model load, use the optimized version.



4
5
6
7
8
# File 'lib/sequel_pg/model.rb', line 4

def each(&block)
  rp = row_proc
  return super unless allow_sequel_pg_optimization? && optimize_model_load?(rp)
  clone(:_sequel_pg_type=>:model, :_sequel_pg_value=>rp).fetch_rows(sql, &block)
end

#map(sym = nil) ⇒ Object

In the case where an argument is given, use an optimized version.



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

def map(sym=nil)
  if sym
    if block_given?
      super
    else
      return super unless allow_sequel_pg_optimization?
      clone(:_sequel_pg_type=>:map_array, :_sequel_pg_value=>sym).fetch_rows(sql){|a| return a}
      []
    end
  else
    super
  end
end

#optimize_model_loadObject



19
20
21
22
# File 'lib/sequel_pg/sequel_pg.rb', line 19

def optimize_model_load
  Sequel::Deprecation.deprecate("Dataset#optimize_model_load method is deprecated.  Optimized model loading is enabled by default.")
  opts.has_key?(:optimize_model_load) ?  opts[:optimize_model_load] : true
end

#optimize_model_load=(v) ⇒ Object



15
16
17
18
# File 'lib/sequel_pg/sequel_pg.rb', line 15

def optimize_model_load=(v)
  Sequel::Deprecation.deprecate("Dataset#optimize_model_load= mutation method is deprecated.  Switch to using Dataset#with_optimize_model_load, which returns a modified dataset")
  opts[:optimize_model_load] = v
end

#to_hash_groups(key_column, value_column = nil, opts = Sequel::OPTS) ⇒ Object

In the case where both arguments given, use an optimized version.



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sequel_pg/sequel_pg.rb', line 87

def to_hash_groups(key_column, value_column = nil, opts = Sequel::OPTS)
  if value_column && !opts[:hash]
    return super unless allow_sequel_pg_optimization?
    clone(:_sequel_pg_type=>:hash_groups, :_sequel_pg_value=>[key_column, value_column]).fetch_rows(sql){|s| return s}
    {}
  elsif opts.empty?
    super(key_column, value_column)
  else
    super
  end
end

#with_optimize_model_load(v) ⇒ Object

Return a modified copy with the optimize_model_load setting changed.



61
62
63
# File 'lib/sequel_pg/sequel_pg.rb', line 61

def with_optimize_model_load(v)
  clone(:optimize_model_load=>v)
end

#with_sql_all(sql, &block) ⇒ Object

Always use optimized version



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sequel_pg/sequel_pg.rb', line 107

def with_sql_all(sql, &block)
  return super unless allow_sequel_pg_optimization?

  clone(:_sequel_pg_type=>:all).fetch_rows(sql) do |array|
    if rp = row_proc
      array.map!{|h| rp.call(h)}
    end
    post_load(array)
    array.each(&block) if block
    return array
  end
  []
end