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`
|
|
|
|
def exec(*args) : ExecResult
|
2016-08-31 20:32:01 +00:00
|
|
|
statement_with_retry &.exec(*args)
|
2016-08-29 19:56:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# See `QueryMethods#exec`
|
|
|
|
def exec(args : Array) : ExecResult
|
2016-08-31 20:32:01 +00:00
|
|
|
statement_with_retry &.exec(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`
|
|
|
|
def query(*args) : ResultSet
|
2016-08-31 20:32:01 +00:00
|
|
|
statement_with_retry &.query(*args)
|
2016-08-29 19:56:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# See `QueryMethods#query`
|
|
|
|
def query(args : Array) : ResultSet
|
2016-08-31 20:32:01 +00:00
|
|
|
statement_with_retry &.query(args)
|
2016-08-29 19:56:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|