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