mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Merged branch master into feature/pool
This commit is contained in:
commit
3c91978d36
8 changed files with 39 additions and 15 deletions
|
@ -49,6 +49,7 @@ class DummyDriver < DB::Driver
|
||||||
|
|
||||||
protected def perform_exec(args : Enumerable)
|
protected def perform_exec(args : Enumerable)
|
||||||
set_params args
|
set_params args
|
||||||
|
raise "forced exception due to query" if @query == "raise"
|
||||||
DB::ExecResult.new 0i64, 0_i64
|
DB::ExecResult.new 0i64, 0_i64
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -237,6 +237,14 @@ describe DummyDriver do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should raise executing raise query" do
|
||||||
|
with_dummy do |db|
|
||||||
|
expect_raises do
|
||||||
|
db.exec "raise"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
{% for value in [1, 1_i64, "hello", 1.5, 1.5_f32] %}
|
{% for value in [1, 1_i64, "hello", 1.5, 1.5_f32] %}
|
||||||
it "should set positional arguments for {{value.id}}" do
|
it "should set positional arguments for {{value.id}}" do
|
||||||
with_dummy do |db|
|
with_dummy do |db|
|
||||||
|
|
|
@ -56,4 +56,12 @@ describe DB::ResultSet do
|
||||||
|
|
||||||
cols.should eq(["c0", "c1"])
|
cols.should eq(["c0", "c1"])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "gets all column names" do
|
||||||
|
with_dummy do |db|
|
||||||
|
db.query "1,2" do |rs|
|
||||||
|
rs.column_names.should eq(%w(c0 c1))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -118,4 +118,13 @@ describe DB::Statement do
|
||||||
rs.statement.should be(stmt)
|
rs.statement.should be(stmt)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "connection should be released if error occurs during exec" do
|
||||||
|
with_dummy do |db|
|
||||||
|
expect_raises do
|
||||||
|
db.exec "raise"
|
||||||
|
end
|
||||||
|
db.@in_pool.should be_true
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -138,4 +138,7 @@ module DB
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
macro mapping(**properties)
|
||||||
|
::DB.mapping({{properties}})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -35,7 +35,7 @@ module DB
|
||||||
# end
|
# end
|
||||||
# ```
|
# ```
|
||||||
def query(query, *args)
|
def query(query, *args)
|
||||||
prepare query, &.query(*args)
|
prepare(query).query(*args)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Executes a *query* and yields a `ResultSet` with the results.
|
# Executes a *query* and yields a `ResultSet` with the results.
|
||||||
|
@ -200,23 +200,13 @@ module DB
|
||||||
|
|
||||||
# Performs the `query` and returns an `ExecResult`
|
# Performs the `query` and returns an `ExecResult`
|
||||||
def exec(query, *args)
|
def exec(query, *args)
|
||||||
prepare query, &.exec(*args)
|
prepare(query).exec(*args)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Performs the `query` and returns a single scalar value
|
# Performs the `query` and returns a single scalar value
|
||||||
# puts db.scalar("SELECT MAX(name)").as(String) # => (a String)
|
# puts db.scalar("SELECT MAX(name)").as(String) # => (a String)
|
||||||
def scalar(query, *args)
|
def scalar(query, *args)
|
||||||
prepare query, &.scalar(*args)
|
prepare(query).scalar(*args)
|
||||||
end
|
|
||||||
|
|
||||||
private def prepare(query)
|
|
||||||
stm = prepare(query)
|
|
||||||
begin
|
|
||||||
yield stm
|
|
||||||
rescue ex
|
|
||||||
stm.release_connection
|
|
||||||
raise ex
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -61,6 +61,11 @@ module DB
|
||||||
# Returns the name of the column in `index` 0-based position.
|
# Returns the name of the column in `index` 0-based position.
|
||||||
abstract def column_name(index : Int32) : String
|
abstract def column_name(index : Int32) : String
|
||||||
|
|
||||||
|
# Returns the name of the columns.
|
||||||
|
def column_names
|
||||||
|
Array(String).new(column_count) { |i| column_name(i) }
|
||||||
|
end
|
||||||
|
|
||||||
# Reads the next column value
|
# Reads the next column value
|
||||||
abstract def read
|
abstract def read
|
||||||
|
|
||||||
|
|
|
@ -74,9 +74,9 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
private def perform_exec_and_release(args : Enumerable) : ExecResult
|
private def perform_exec_and_release(args : Enumerable) : ExecResult
|
||||||
res = perform_exec(args)
|
return perform_exec(args)
|
||||||
|
ensure
|
||||||
release_connection
|
release_connection
|
||||||
res
|
|
||||||
end
|
end
|
||||||
|
|
||||||
protected abstract def perform_query(args : Enumerable) : ResultSet
|
protected abstract def perform_query(args : Enumerable) : ResultSet
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue