mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Add logging for executing queries (#134)
* Add logging for executing queries Arguments are translated to Log::Metadata::Value via DB::Statement#arg_to_log method. DB::Statement#before_query_or_exec & after_query_or_exec protected methods can be used to hook and run around the statement execution * Move the metadata converter to a module * Replace before/after with def_around_query_or_exec macro * Update src/db/enumerable_concat.cr Co-authored-by: Ary Borenszweig <asterite@gmail.com> Co-authored-by: Ary Borenszweig <asterite@gmail.com>
This commit is contained in:
parent
fad9e70353
commit
7253551849
6 changed files with 147 additions and 16 deletions
|
@ -48,11 +48,11 @@ class FooDriver < DB::Driver
|
|||
end
|
||||
|
||||
class FooConnection < DB::Connection
|
||||
def build_prepared_statement(query) : DB::Statement
|
||||
FooStatement.new(self)
|
||||
def build_prepared_statement(command) : DB::Statement
|
||||
FooStatement.new(self, command)
|
||||
end
|
||||
|
||||
def build_unprepared_statement(query) : DB::Statement
|
||||
def build_unprepared_statement(command) : DB::Statement
|
||||
raise "not implemented"
|
||||
end
|
||||
end
|
||||
|
@ -112,7 +112,7 @@ class BarDriver < DB::Driver
|
|||
|
||||
class BarConnection < DB::Connection
|
||||
def build_prepared_statement(query) : DB::Statement
|
||||
BarStatement.new(self)
|
||||
BarStatement.new(self, query)
|
||||
end
|
||||
|
||||
def build_unprepared_statement(query) : DB::Statement
|
||||
|
|
|
@ -96,22 +96,22 @@ class DummyDriver < DB::Driver
|
|||
class DummyStatement < DB::Statement
|
||||
property params
|
||||
|
||||
def initialize(connection, @query : String, @prepared : Bool)
|
||||
def initialize(connection, command : String, @prepared : Bool)
|
||||
@params = Hash(Int32 | String, DB::Any | Array(DB::Any)).new
|
||||
super(connection)
|
||||
raise DB::Error.new(query) if query == "syntax error"
|
||||
super(connection, command)
|
||||
raise DB::Error.new(command) if command == "syntax error"
|
||||
end
|
||||
|
||||
protected def perform_query(args : Enumerable) : DB::ResultSet
|
||||
@connection.as(DummyConnection).check
|
||||
set_params args
|
||||
DummyResultSet.new self, @query
|
||||
DummyResultSet.new self, command
|
||||
end
|
||||
|
||||
protected def perform_exec(args : Enumerable) : DB::ExecResult
|
||||
@connection.as(DummyConnection).check
|
||||
set_params args
|
||||
raise DB::Error.new("forced exception due to query") if @query == "raise"
|
||||
raise DB::Error.new("forced exception due to query") if command == "raise"
|
||||
DB::ExecResult.new 0i64, 0_i64
|
||||
end
|
||||
|
||||
|
@ -149,9 +149,9 @@ class DummyDriver < DB::Driver
|
|||
|
||||
@@last_result_set : self?
|
||||
|
||||
def initialize(statement, query)
|
||||
def initialize(statement, command)
|
||||
super(statement)
|
||||
@top_values = query.split.map { |r| r.split(',') }.to_a
|
||||
@top_values = command.split.map { |r| r.split(',') }.to_a
|
||||
@column_count = @top_values.size > 0 ? @top_values[0].size : 2
|
||||
|
||||
@@last_result_set = self
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require "./spec_helper"
|
||||
require "log/spec"
|
||||
|
||||
describe DB::Statement do
|
||||
it "should build prepared statements" do
|
||||
|
@ -217,4 +218,40 @@ describe DB::Statement do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "logging" do
|
||||
it "exec with no arguments" do
|
||||
Log.capture(DB::Log.source) do |logs|
|
||||
with_dummy do |db|
|
||||
db.exec "42"
|
||||
end
|
||||
|
||||
entry = logs.check(:debug, /Executing query/i).entry
|
||||
entry.data[:query].should eq("42")
|
||||
entry.data[:args].as_a.should be_empty
|
||||
end
|
||||
end
|
||||
|
||||
it "query with arguments" do
|
||||
Log.capture(DB::Log.source) do |logs|
|
||||
with_dummy do |db|
|
||||
db.exec "1, ?", args: ["a"]
|
||||
db.exec "2, ?", "a"
|
||||
db.exec "3, ?", ["a"]
|
||||
end
|
||||
|
||||
entry = logs.check(:debug, /Executing query/i).entry
|
||||
entry.data[:query].should eq("1, ?")
|
||||
entry.data[:args][0].as_s.should eq("a")
|
||||
|
||||
entry = logs.check(:debug, /Executing query/i).entry
|
||||
entry.data[:query].should eq("2, ?")
|
||||
entry.data[:args][0].as_s.should eq("a")
|
||||
|
||||
entry = logs.check(:debug, /Executing query/i).entry
|
||||
entry.data[:query].should eq("3, ?")
|
||||
entry.data[:args][0][0].as_s.should eq("a")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue