-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
connection.cr
121 lines (102 loc) · 3.23 KB
/
connection.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
module DB
# Database driver implementors must subclass `Connection`.
#
# Represents one active connection to a database.
#
# Users should never instantiate a `Connection` manually. Use `DB#open` or `Database#connection`.
#
# Refer to `QueryMethods` for documentation about querying the database through this connection.
#
# ### Note to implementors
#
# The connection must be initialized in `#initialize` and closed in `#do_close`.
#
# Override `#build_prepared_statement` method in order to return a prepared `Statement` to allow querying.
# Override `#build_unprepared_statement` method in order to return a unprepared `Statement` to allow querying.
# See also `Statement` to define how the statements are executed.
#
# If at any give moment the connection is lost a DB::ConnectionLost should be raised. This will
# allow the connection pool to try to reconnect or use another connection if available.
#
abstract class Connection
include Disposable
include SessionMethods(Connection, Statement)
include BeginTransaction
# :nodoc:
getter context
@statements_cache = StringKeyCache(Statement).new
@transaction = false
getter? prepared_statements : Bool
# :nodoc:
property auto_release : Bool = true
def initialize(@context : ConnectionContext)
@prepared_statements = @context.prepared_statements?
end
# :nodoc:
def fetch_or_build_prepared_statement(query) : Statement
@statements_cache.fetch(query) { build_prepared_statement(query) }
end
# :nodoc:
abstract def build_prepared_statement(query) : Statement
# :nodoc:
abstract def build_unprepared_statement(query) : Statement
def begin_transaction : Transaction
raise DB::Error.new("There is an existing transaction in this connection") if @transaction
@transaction = true
create_transaction
end
protected def create_transaction : Transaction
TopLevelTransaction.new(self)
end
protected def do_close
@statements_cache.each_value &.close
@statements_cache.clear
@context.discard self
end
# :nodoc:
protected def before_checkout
@auto_release = true
end
# :nodoc:
protected def after_release
end
# return this connection to the pool
# managed by the database. Should be used
# only if the connection was obtained by `Database#checkout`.
def release
@context.release(self)
end
# :nodoc:
def release_from_statement
self.release if @auto_release && !@transaction
end
# :nodoc:
def release_from_transaction
@transaction = false
end
# :nodoc:
def perform_begin_transaction
self.unprepared.exec "BEGIN"
end
# :nodoc:
def perform_commit_transaction
self.unprepared.exec "COMMIT"
end
# :nodoc:
def perform_rollback_transaction
self.unprepared.exec "ROLLBACK"
end
# :nodoc:
def perform_create_savepoint(name)
self.unprepared.exec "SAVEPOINT #{name}"
end
# :nodoc:
def perform_release_savepoint(name)
self.unprepared.exec "RELEASE SAVEPOINT #{name}"
end
# :nodoc:
def perform_rollback_savepoint(name)
self.unprepared.exec "ROLLBACK TO #{name}"
end
end
end