Skip to content

Commit 84388b6

Browse files
committed
Move Sequel::Model related optimization code to sequel_pg/model
This will allow it to be required separately to optimize model code if Sequel::Model was not already required when sequel_pg was loaded.
1 parent 2407329 commit 84388b6

File tree

3 files changed

+64
-28
lines changed

3 files changed

+64
-28
lines changed

lib/sequel_pg/model.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Sequel::Postgres::Dataset
2+
# If model loads are being optimized and this is a model load, use the optimized
3+
# version.
4+
def each(&block)
5+
rp = row_proc
6+
return super unless allow_sequel_pg_optimization? && optimize_model_load?(rp)
7+
clone(:_sequel_pg_type=>:model, :_sequel_pg_value=>rp).fetch_rows(sql, &block)
8+
end
9+
10+
# Avoid duplicate method warning
11+
alias with_sql_all with_sql_all
12+
13+
# Always use optimized version
14+
def with_sql_all(sql, &block)
15+
rp = row_proc
16+
return super unless allow_sequel_pg_optimization?
17+
18+
if optimize_model_load?(rp)
19+
clone(:_sequel_pg_type=>:all_model, :_sequel_pg_value=>row_proc).fetch_rows(sql) do |array|
20+
post_load(array)
21+
array.each(&block) if block
22+
return array
23+
end
24+
[]
25+
else
26+
clone(:_sequel_pg_type=>:all).fetch_rows(sql) do |array|
27+
if rp = row_proc
28+
array.map!{|h| rp.call(h)}
29+
end
30+
post_load(array)
31+
array.each(&block) if block
32+
return array
33+
end
34+
[]
35+
end
36+
end
37+
38+
private
39+
40+
# The model load can only be optimized if it's for a model and it's not a graphed dataset
41+
# or using a cursor.
42+
def optimize_model_load?(rp)
43+
rp.is_a?(Class) &&
44+
rp < Sequel::Model &&
45+
rp.method(:call).owner == Sequel::Model::ClassMethods &&
46+
opts[:optimize_model_load] != false
47+
end
48+
end

lib/sequel_pg/sequel_pg.rb

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,19 @@ def to_hash_groups(key_column, value_column = nil, opts = Sequel::OPTS)
9191
end
9292
end
9393

94-
# If model loads are being optimized and this is a model load, use the optimized
95-
# version.
96-
def each(&block)
97-
if optimize_model_load?
98-
clone(:_sequel_pg_type=>:model, :_sequel_pg_value=>row_proc).fetch_rows(sql, &block)
99-
else
100-
super
101-
end
102-
end
103-
10494
# Delegate to with_sql_all using the default SQL
10595
def all(&block)
10696
with_sql_all(sql, &block)
10797
end
10898

109-
# Always use optimized version
99+
# :nocov:
100+
# Generally overridden by the model support, only used if the model
101+
# support is not used.
110102
def with_sql_all(sql, &block)
111-
return super if opts[:graph] || opts[:cursor]
112-
type = optimize_model_load? ? :all_model : :all
113-
rp = row_proc
114-
clone(:_sequel_pg_type=>type, :_sequel_pg_value=>rp).fetch_rows(sql) do |array|
115-
if rp && type == :all
103+
return super unless allow_sequel_pg_optimization?
104+
105+
clone(:_sequel_pg_type=>:all).fetch_rows(sql) do |array|
106+
if rp = row_proc
116107
array.map!{|h| rp.call(h)}
117108
end
118109
post_load(array)
@@ -121,6 +112,7 @@ def with_sql_all(sql, &block)
121112
end
122113
[]
123114
end
115+
# :nocov:
124116

125117
protected
126118

@@ -170,19 +162,15 @@ def _select_set_single
170162
# :nocov:
171163
end
172164

165+
if defined?(Sequel::Model::ClassMethods)
166+
require_relative 'model'
167+
end
168+
173169
private
174170

175-
# The model load can only be optimized if it's for a model and it's not a graphed dataset
176-
# or using a cursor.
177-
def optimize_model_load?
178-
defined?(Sequel::Model::ClassMethods) &&
179-
(rp = row_proc) &&
180-
rp.is_a?(Class) &&
181-
rp < Sequel::Model &&
182-
rp.method(:call).owner == Sequel::Model::ClassMethods &&
183-
opts[:optimize_model_load] != false &&
184-
!opts[:use_cursor] &&
185-
!opts[:graph]
171+
# Whether to allow sequel_pg to optimize the each/all/with_sql_all call.
172+
def allow_sequel_pg_optimization?
173+
(!opts[:graph] || opts[:eager_graph]) && !opts[:cursor]
186174
end
187175
end
188176

sequel_pg.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ SEQUEL_PG_GEMSPEC = Gem::Specification.new do |s|
1212
s.email = "[email protected]"
1313
s.homepage = "http://github.com/jeremyevans/sequel_pg"
1414
s.required_ruby_version = ">= 1.9.3"
15-
s.files = %w(MIT-LICENSE CHANGELOG README.rdoc Rakefile ext/sequel_pg/extconf.rb ext/sequel_pg/sequel_pg.c lib/sequel_pg/sequel_pg.rb lib/sequel/extensions/pg_streaming.rb)
15+
s.files = %w(MIT-LICENSE CHANGELOG README.rdoc Rakefile ext/sequel_pg/extconf.rb ext/sequel_pg/sequel_pg.c lib/sequel_pg/sequel_pg.rb lib/sequel_pg/model.rb lib/sequel/extensions/pg_streaming.rb)
1616
s.license = 'MIT'
1717
s.extensions << 'ext/sequel_pg/extconf.rb'
1818
s.add_dependency("pg", [">= 0.18.0", "!= 1.2.0"])

0 commit comments

Comments
 (0)