2016-08-29 19:56:34 +00:00
|
|
|
module DB
|
|
|
|
# When a statement is to be executed in a DB that has a connection pool
|
|
|
|
# a statement from the DB needs to be able to represent a statement in any
|
|
|
|
# of the connections of the pool. Otherwise the user will need to deal with
|
|
|
|
# actual connections in some point.
|
2016-12-03 19:03:50 +00:00
|
|
|
abstract class PoolStatement
|
2016-08-29 19:56:34 +00:00
|
|
|
include StatementMethods
|
|
|
|
|
|
|
|
def initialize(@db : Database, @query : String)
|
|
|
|
end
|
|
|
|
|
|
|
|
# See `QueryMethods#exec`
|
|
|
|
def exec : ExecResult
|
2016-08-31 20:32:01 +00:00
|
|
|
statement_with_retry &.exec
|
2016-08-29 19:56:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# See `QueryMethods#exec`
|
2019-09-20 20:23:09 +00:00
|
|
|
def exec(*args_, args : Array? = nil) : ExecResult
|
|
|
|
statement_with_retry &.exec(*args_, args: args)
|
2016-08-29 19:56:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# See `QueryMethods#query`
|
|
|
|
def query : ResultSet
|
2016-08-31 20:32:01 +00:00
|
|
|
statement_with_retry &.query
|
2016-08-29 19:56:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# See `QueryMethods#query`
|
2019-09-20 20:23:09 +00:00
|
|
|
def query(*args_, args : Array? = nil) : ResultSet
|
|
|
|
statement_with_retry &.query(*args_, args: args)
|
2016-08-29 19:56:34 +00:00
|
|
|
end
|
|
|
|
|
2016-12-16 14:56:27 +00:00
|
|
|
# See `QueryMethods#scalar`
|
2019-09-20 20:23:09 +00:00
|
|
|
def scalar(*args_, args : Array? = nil)
|
|
|
|
statement_with_retry &.scalar(*args_, args: args)
|
2016-12-16 14:56:27 +00:00
|
|
|
end
|
|
|
|
|
2016-08-29 19:56:34 +00:00
|
|
|
# builds a statement over a real connection
|
2016-08-30 19:40:52 +00:00
|
|
|
# the conneciton is registered in `@connections`
|
2016-12-03 19:03:50 +00:00
|
|
|
private abstract def build_statement : Statement
|
2016-09-12 17:35:21 +00:00
|
|
|
|
2016-08-31 20:32:01 +00:00
|
|
|
private def statement_with_retry
|
|
|
|
@db.retry do
|
|
|
|
return yield build_statement
|
|
|
|
end
|
|
|
|
end
|
2016-08-29 19:56:34 +00:00
|
|
|
end
|
|
|
|
end
|