Skip to content

Commit

Permalink
build_query is the same as execute and select for middleware authors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Rothstein committed Feb 16, 2010
1 parent 91e8b74 commit 75034c2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
8 changes: 6 additions & 2 deletions lib/queryer.rb
Expand Up @@ -6,12 +6,16 @@

class Queryer
class QueryMaker
def build_query(env)
env["nk.query_class"].new(env["nk.connection"], env["nk.query_string"])
end

def select(env)
env["nk.query"].new(env["nk.connection"], env["nk.query_string"]).select
build_query(env).select
end

def execute(env)
env["nk.query"].new(env["nk.connection"], env["nk.query_string"]).execute
build_query(env).execute
end
end
end
16 changes: 11 additions & 5 deletions lib/queryer/client.rb
Expand Up @@ -4,16 +4,22 @@
class Queryer
class Client
def initialize(pool, queryer)
@pool = pool
@pool = pool
@queryer = queryer
end

def select(query_string)
with_connection(query_string) { |env| @queryer.select(env) }
with_connection(query_string) do |env|
@queryer.build_query(env)
@queryer.select(env)
end
end

def execute(query_string)
with_connection(query_string) { |env| @queryer.execute(env) }
with_connection(query_string) do |env|
@queryer.build_query(env)
@queryer.execute(env)
end
end

def transaction
Expand All @@ -28,7 +34,7 @@ def env(connection, query_string)
{
"nk.connection" => connection,
"nk.query_string" => query_string,
"nk.query" => Query
"nk.query_class" => Query
}
end

Expand Down
4 changes: 4 additions & 0 deletions lib/queryer/middleware/core.rb
Expand Up @@ -13,6 +13,10 @@ def select(env)
def execute(env)
delegate(:execute, env)
end

def build_query(env)
delegate(:build_query, env)
end

def delegate(method, env)
@queryer.send(method, env)
Expand Down
45 changes: 18 additions & 27 deletions lib/queryer/middleware/middleware.rb
Expand Up @@ -6,7 +6,11 @@ def initialize(queryer, stats)
end

def delegate(method, env)
@stats.measure(method) { super }
case method
when :execute, :select
@stats.measure(method) { super }
else super
end
end
end

Expand All @@ -17,40 +21,27 @@ def initialize(queryer, timeout)
end

def delegate(method, env)
result = ::Timeout.timeout(@timeout) { super }
puts "Did not timeout! Yay fast database!"
result
case method
when :execute, :select
result = ::Timeout.timeout(@timeout) { super }
puts "Did not timeout! Yay fast database!"
result
else super
end
end
end

class Memoizing < Middleware
class QueryFactory
@memo = {}
def self.for(query)
@memo[query] ||= new(query)
end

def initialize(query)
@query = query
@memo = {}
end

def new(conn, query_string)
@memo[[conn, query_string]] ||= begin
puts "Instantiating Query Object"
@query.new(conn, query_string)
end
def build_query(env)
@memo[[env['nk.connection'], env['nk.query_string']]] ||= begin
puts "Instantiating Query Object"
super
end
end

def initialize(queryer)
super(queryer)
@wrap_memo = {}
end

def delegate(method, env)
env["nk.query"] = Memoizing::QueryFactory.for(env["nk.query"])
super
@memo = {}
end
end
end

0 comments on commit 75034c2

Please sign in to comment.