mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Add type argument to QueryMethods module (#108)
This commit is contained in:
parent
0100b47754
commit
adc755e5e4
10 changed files with 30 additions and 30 deletions
|
@ -3,7 +3,7 @@ require "./spec_helper"
|
||||||
module GenericResultSet
|
module GenericResultSet
|
||||||
@index = 0
|
@index = 0
|
||||||
|
|
||||||
def move_next
|
def move_next : Bool
|
||||||
@index = 0
|
@index = 0
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
@ -48,11 +48,11 @@ class FooDriver < DB::Driver
|
||||||
end
|
end
|
||||||
|
|
||||||
class FooConnection < DB::Connection
|
class FooConnection < DB::Connection
|
||||||
def build_prepared_statement(query)
|
def build_prepared_statement(query) : DB::Statement
|
||||||
FooStatement.new(self)
|
FooStatement.new(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_unprepared_statement(query)
|
def build_unprepared_statement(query) : DB::Statement
|
||||||
raise "not implemented"
|
raise "not implemented"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -111,11 +111,11 @@ class BarDriver < DB::Driver
|
||||||
end
|
end
|
||||||
|
|
||||||
class BarConnection < DB::Connection
|
class BarConnection < DB::Connection
|
||||||
def build_prepared_statement(query)
|
def build_prepared_statement(query) : DB::Statement
|
||||||
BarStatement.new(self)
|
BarStatement.new(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_unprepared_statement(query)
|
def build_unprepared_statement(query) : DB::Statement
|
||||||
raise "not implemented"
|
raise "not implemented"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,11 +22,11 @@ class DummyDriver < DB::Driver
|
||||||
@@connections.try &.clear
|
@@connections.try &.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_prepared_statement(query)
|
def build_prepared_statement(query) : DB::Statement
|
||||||
DummyStatement.new(self, query, true)
|
DummyStatement.new(self, query, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_unprepared_statement(query)
|
def build_unprepared_statement(query) : DB::Statement
|
||||||
DummyStatement.new(self, query, false)
|
DummyStatement.new(self, query, false)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -102,13 +102,13 @@ class DummyDriver < DB::Driver
|
||||||
raise DB::Error.new(query) if query == "syntax error"
|
raise DB::Error.new(query) if query == "syntax error"
|
||||||
end
|
end
|
||||||
|
|
||||||
protected def perform_query(args : Enumerable)
|
protected def perform_query(args : Enumerable) : DB::ResultSet
|
||||||
@connection.as(DummyConnection).check
|
@connection.as(DummyConnection).check
|
||||||
set_params args
|
set_params args
|
||||||
DummyResultSet.new self, @query
|
DummyResultSet.new self, @query
|
||||||
end
|
end
|
||||||
|
|
||||||
protected def perform_exec(args : Enumerable)
|
protected def perform_exec(args : Enumerable) : DB::ExecResult
|
||||||
@connection.as(DummyConnection).check
|
@connection.as(DummyConnection).check
|
||||||
set_params args
|
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 @query == "raise"
|
||||||
|
@ -161,16 +161,16 @@ class DummyDriver < DB::Driver
|
||||||
@@last_result_set.not_nil!
|
@@last_result_set.not_nil!
|
||||||
end
|
end
|
||||||
|
|
||||||
def move_next
|
def move_next : Bool
|
||||||
@values = @top_values.shift?
|
@values = @top_values.shift?
|
||||||
!!@values
|
!!@values
|
||||||
end
|
end
|
||||||
|
|
||||||
def column_count
|
def column_count : Int32
|
||||||
@column_count
|
@column_count
|
||||||
end
|
end
|
||||||
|
|
||||||
def column_name(index)
|
def column_name(index) : String
|
||||||
"c#{index}"
|
"c#{index}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
def fetch_or_build_prepared_statement(query)
|
def fetch_or_build_prepared_statement(query) : Statement
|
||||||
@statements_cache.fetch(query) { build_prepared_statement(query) }
|
@statements_cache.fetch(query) { build_prepared_statement(query) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ module DB
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
abstract def build_unprepared_statement(query) : Statement
|
abstract def build_unprepared_statement(query) : Statement
|
||||||
|
|
||||||
def begin_transaction
|
def begin_transaction : Transaction
|
||||||
raise DB::Error.new("There is an existing transaction in this connection") if @transaction
|
raise DB::Error.new("There is an existing transaction in this connection") if @transaction
|
||||||
@transaction = true
|
@transaction = true
|
||||||
create_transaction
|
create_transaction
|
||||||
|
|
|
@ -37,7 +37,7 @@ module DB
|
||||||
getter pool
|
getter pool
|
||||||
|
|
||||||
# Returns the uri with the connection settings to the database
|
# Returns the uri with the connection settings to the database
|
||||||
getter uri
|
getter uri : URI
|
||||||
|
|
||||||
getter? prepared_statements : Bool
|
getter? prepared_statements : Bool
|
||||||
|
|
||||||
|
@ -86,17 +86,17 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
def fetch_or_build_prepared_statement(query)
|
def fetch_or_build_prepared_statement(query) : PoolStatement
|
||||||
@statements_cache.fetch(query) { build_prepared_statement(query) }
|
@statements_cache.fetch(query) { build_prepared_statement(query) }
|
||||||
end
|
end
|
||||||
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
def build_prepared_statement(query)
|
def build_prepared_statement(query) : PoolStatement
|
||||||
PoolPreparedStatement.new(self, query)
|
PoolPreparedStatement.new(self, query)
|
||||||
end
|
end
|
||||||
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
def build_unprepared_statement(query)
|
def build_unprepared_statement(query) : PoolStatement
|
||||||
PoolUnpreparedStatement.new(self, query)
|
PoolUnpreparedStatement.new(self, query)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ module DB
|
||||||
|
|
||||||
# builds a statement over a real connection
|
# builds a statement over a real connection
|
||||||
# the connection is registered in `@connections`
|
# the connection is registered in `@connections`
|
||||||
private def build_statement
|
private def build_statement : Statement
|
||||||
clean_connections
|
clean_connections
|
||||||
conn, existing = @db.checkout_some(@connections)
|
conn, existing = @db.checkout_some(@connections)
|
||||||
begin
|
begin
|
||||||
|
|
|
@ -14,7 +14,7 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
# builds a statement over a real connection
|
# builds a statement over a real connection
|
||||||
private def build_statement
|
private def build_statement : Statement
|
||||||
conn = @db.pool.checkout
|
conn = @db.pool.checkout
|
||||||
begin
|
begin
|
||||||
conn.unprepared.build(@query)
|
conn.unprepared.build(@query)
|
||||||
|
|
|
@ -17,9 +17,9 @@ module DB
|
||||||
#
|
#
|
||||||
# Including `QueryMethods` requires a `build(query) : Statement` method that is not expected
|
# Including `QueryMethods` requires a `build(query) : Statement` method that is not expected
|
||||||
# to be called directly.
|
# to be called directly.
|
||||||
module QueryMethods
|
module QueryMethods(Stmt)
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
abstract def build(query) : Statement
|
abstract def build(query) : Stmt
|
||||||
|
|
||||||
# Executes a *query* and returns a `ResultSet` with the results.
|
# Executes a *query* and returns a `ResultSet` with the results.
|
||||||
# The `ResultSet` must be closed manually.
|
# The `ResultSet` must be closed manually.
|
||||||
|
|
|
@ -8,7 +8,7 @@ module DB
|
||||||
#
|
#
|
||||||
# This module serves for dsl reuse over session like objects.
|
# This module serves for dsl reuse over session like objects.
|
||||||
module SessionMethods(Session, Stmt)
|
module SessionMethods(Session, Stmt)
|
||||||
include QueryMethods
|
include QueryMethods(Stmt)
|
||||||
|
|
||||||
# Returns whether by default the statements should
|
# Returns whether by default the statements should
|
||||||
# be prepared or not.
|
# be prepared or not.
|
||||||
|
@ -49,7 +49,7 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
struct PreparedQuery(Session, Stmt)
|
struct PreparedQuery(Session, Stmt)
|
||||||
include QueryMethods
|
include QueryMethods(Stmt)
|
||||||
|
|
||||||
def initialize(@session : Session)
|
def initialize(@session : Session)
|
||||||
end
|
end
|
||||||
|
@ -60,7 +60,7 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
struct UnpreparedQuery(Session, Stmt)
|
struct UnpreparedQuery(Session, Stmt)
|
||||||
include QueryMethods
|
include QueryMethods(Stmt)
|
||||||
|
|
||||||
def initialize(@session : Session)
|
def initialize(@session : Session)
|
||||||
end
|
end
|
||||||
|
|
|
@ -63,12 +63,12 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
# See `QueryMethods#exec`
|
# See `QueryMethods#exec`
|
||||||
def exec
|
def exec : DB::ExecResult
|
||||||
perform_exec_and_release(Slice(Any).empty)
|
perform_exec_and_release(Slice(Any).empty)
|
||||||
end
|
end
|
||||||
|
|
||||||
# See `QueryMethods#exec`
|
# See `QueryMethods#exec`
|
||||||
def exec(args : Array)
|
def exec(args : Array) : DB::ExecResult
|
||||||
perform_exec_and_release(args)
|
perform_exec_and_release(args)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -79,12 +79,12 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
# See `QueryMethods#query`
|
# See `QueryMethods#query`
|
||||||
def query
|
def query : DB::ResultSet
|
||||||
perform_query_with_rescue Tuple.new
|
perform_query_with_rescue Tuple.new
|
||||||
end
|
end
|
||||||
|
|
||||||
# See `QueryMethods#query`
|
# See `QueryMethods#query`
|
||||||
def query(args : Array)
|
def query(args : Array) : DB::ResultSet
|
||||||
perform_query_with_rescue args
|
perform_query_with_rescue args
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
class TopLevelTransaction < Transaction
|
class TopLevelTransaction < Transaction
|
||||||
getter connection
|
getter connection : Connection
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
property savepoint_name : String? = nil
|
property savepoint_name : String? = nil
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue