Skip to content

Commit

Permalink
wrap logging around the actual query call itself.
Browse files Browse the repository at this point in the history
This is to be consistent with the way the mysql2 adapter times queries
  • Loading branch information
tenderlove committed Oct 4, 2013
1 parent 37cd223 commit ffbefc7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 50 deletions.
Expand Up @@ -134,35 +134,31 @@ def substitute_at(column, index)
end

def exec_query(sql, name = 'SQL', binds = [])
log(sql, name, binds) do
result = without_prepared_statement?(binds) ? exec_no_cache(sql, binds) :
exec_cache(sql, binds)

types = {}
fields = result.fields
fields.each_with_index do |fname, i|
ftype = result.ftype i
fmod = result.fmod i
types[fname] = OID::TYPE_MAP.fetch(ftype, fmod) { |oid, mod|
warn "unknown OID: #{fname}(#{oid}) (#{sql})"
OID::Identity.new
}
end

ret = ActiveRecord::Result.new(fields, result.values, types)
result.clear
return ret
result = without_prepared_statement?(binds) ? exec_no_cache(sql, name, binds) :
exec_cache(sql, name, binds)

types = {}
fields = result.fields
fields.each_with_index do |fname, i|
ftype = result.ftype i
fmod = result.fmod i
types[fname] = OID::TYPE_MAP.fetch(ftype, fmod) { |oid, mod|
warn "unknown OID: #{fname}(#{oid}) (#{sql})"
OID::Identity.new
}
end

ret = ActiveRecord::Result.new(fields, result.values, types)
result.clear
return ret
end

def exec_delete(sql, name = 'SQL', binds = [])
log(sql, name, binds) do
result = without_prepared_statement?(binds) ? exec_no_cache(sql, binds) :
exec_cache(sql, binds)
affected = result.cmd_tuples
result.clear
affected
end
result = without_prepared_statement?(binds) ? exec_no_cache(sql, name, binds) :
exec_cache(sql, name, binds)
affected = result.cmd_tuples
result.clear
affected
end
alias :exec_update :exec_delete

Expand Down
Expand Up @@ -767,35 +767,39 @@ def initialize_type_map

FEATURE_NOT_SUPPORTED = "0A000" # :nodoc:

def exec_no_cache(sql, binds)
@connection.async_exec(sql)
def exec_no_cache(sql, name, binds)
log(sql, name, binds) { @connection.async_exec(sql) }
end

def exec_cache(sql, binds)
def exec_cache(sql, name, binds)
stmt_key = prepare_statement(sql)

# Clear the queue
@connection.get_last_result
@connection.send_query_prepared(stmt_key, binds.map { |col, val|
type_cast(val, col)
})
@connection.block
@connection.get_last_result
rescue PGError => e
# Get the PG code for the failure. Annoyingly, the code for
# prepared statements whose return value may have changed is
# FEATURE_NOT_SUPPORTED. Check here for more details:
# http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/cache/plancache.c#l573
begin
code = e.result.result_error_field(PGresult::PG_DIAG_SQLSTATE)
rescue
raise e
end
if FEATURE_NOT_SUPPORTED == code
@statements.delete sql_key(sql)
retry
else
raise e
log(sql, name, binds) do
begin
# Clear the queue
@connection.get_last_result
@connection.send_query_prepared(stmt_key, binds.map { |col, val|
type_cast(val, col)
})
@connection.block
@connection.get_last_result
rescue PGError => e
# Get the PG code for the failure. Annoyingly, the code for
# prepared statements whose return value may have changed is
# FEATURE_NOT_SUPPORTED. Check here for more details:
# http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/cache/plancache.c#l573
begin
code = e.result.result_error_field(PGresult::PG_DIAG_SQLSTATE)
rescue
raise e
end
if FEATURE_NOT_SUPPORTED == code
@statements.delete sql_key(sql)
retry
else
raise e
end
end
end
end

Expand Down

0 comments on commit ffbefc7

Please sign in to comment.