Class: SolidObjects::DatabaseAdapter
- Inherits:
-
Object
- Object
- SolidObjects::DatabaseAdapter
show all
- Defined in:
- lib/solid_objects/database_adapter.rb,
sig/generated/lib/solid_objects/database_adapter.rbs
Constant Summary
collapse
- TRANSACTION_CLOCK =
:solid_objects_transaction_clock
- TRANSACTION_CLOCK_SCOPE =
:solid_objects_transaction_clock_scope
- FAMILIES =
An adapter name is a client name, not a protocol name. Trilogy reports
"Trilogy" while speaking MySQL, so a pattern that only knows the mysql2
gem rejects a database Solid Objects fully supports. Every decision that
depends on the database goes through this one table, so a client cannot
be accepted in one place and rejected in another.
{
postgresql: /postgres/i,
mysql: /mysql|trilogy/i,
sqlite: /sqlite/i
}.freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of DatabaseAdapter.
50
51
52
53
|
# File 'lib/solid_objects/database_adapter.rb', line 50
def initialize(connection)
@connection_pool = connection.respond_to?(:pool) ? connection.pool : nil
@fixed_connection = connection_pool ? nil : connection
end
|
Instance Attribute Details
#connection_pool ⇒ Object
Returns the value of attribute connection_pool.
151
152
153
|
# File 'lib/solid_objects/database_adapter.rb', line 151
def connection_pool
@connection_pool
end
|
#fixed_connection ⇒ Object
Returns the value of attribute fixed_connection.
151
152
153
|
# File 'lib/solid_objects/database_adapter.rb', line 151
def fixed_connection
@fixed_connection
end
|
Class Method Details
.family(connection) ⇒ Symbol?
23
24
25
26
27
28
29
|
# File 'lib/solid_objects/database_adapter.rb', line 23
def family(connection)
adapter_name = connection.adapter_name
FAMILIES.each do |family, pattern|
return family if adapter_name.match?(pattern)
end
nil
end
|
Instance Method Details
#additional_server_reasons ⇒ Array[String]
88
89
90
|
# File 'lib/solid_objects/database_adapter.rb', line 88
def additional_server_reasons
[]
end
|
#claim_lock ⇒ String?
98
99
100
|
# File 'lib/solid_objects/database_adapter.rb', line 98
def claim_lock
nil
end
|
This method returns an undefined value.
180
181
|
# File 'lib/solid_objects/database_adapter.rb', line 180
def configure_transaction_deadline(_connection)
end
|
#current_time_expression ⇒ String
103
104
105
|
# File 'lib/solid_objects/database_adapter.rb', line 103
def current_time_expression
"CURRENT_TIMESTAMP"
end
|
#deadline_error?(_error) ⇒ Boolean
184
185
186
|
# File 'lib/solid_objects/database_adapter.rb', line 184
def deadline_error?(_error)
false
end
|
145
146
147
|
# File 'lib/solid_objects/database_adapter.rb', line 145
def lock_candidates(relation)
claim_lock ? relation.lock(claim_lock) : relation
end
|
#minimum_server_version ⇒ Gem::Version?
The oldest server the adapter has been exercised against. Reported rather
than enforced: refusing to boot on an untested server would be a worse
failure than running on one.
59
60
61
|
# File 'lib/solid_objects/database_adapter.rb', line 59
def minimum_server_version
nil
end
|
#read_database_now ⇒ Time
167
168
169
170
171
172
|
# File 'lib/solid_objects/database_adapter.rb', line 167
def read_database_now
value = with_connection do |connection|
connection.select_value("SELECT #{current_time_expression}")
end
value.is_a?(Time) ? value.utc : Time.parse("#{value} UTC").utc
end
|
#server_version ⇒ Gem::Version
64
65
66
67
68
|
# File 'lib/solid_objects/database_adapter.rb', line 64
def server_version
with_connection do |connection|
Gem::Version.new(connection.database_version.to_s)
end
end
|
#supports_skip_locked? ⇒ Boolean
93
94
95
|
# File 'lib/solid_objects/database_adapter.rb', line 93
def supports_skip_locked?
false
end
|
#transaction { ... } ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/solid_objects/database_adapter.rb', line 125
def transaction(&block)
raise DatabaseDeadlineExceeded, "synchronous invocation deadline expired" if SyncDeadline.expired?
with_connection do |connection|
with_transaction_deadline(connection) do
connection.transaction(requires_new: true) do
configure_transaction_deadline(connection)
with_transaction_clock { block.call }
end
end
end
rescue => error
raise unless deadline_error?(error)
raise DatabaseDeadlineExceeded,
"database lock wait exceeded the synchronous invocation deadline",
cause: error
end
|
#unsupported_server_reasons(observed = nil) ⇒ Array[String]
One observed version decides both the status and the message. Reading it
again could let a transient failure replace an already determined result.
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/solid_objects/database_adapter.rb', line 73
def unsupported_server_reasons(observed = nil)
observed ||= server_version
reasons = []
minimum = minimum_server_version
if minimum && observed < minimum
reasons << "#{self.class.name.demodulize} #{observed} is older than " \
"Solid Objects requires, which is #{minimum}"
end
reasons.concat(additional_server_reasons)
reasons
rescue => error
[ "the database server could not be verified: #{error.class}: #{error.message}" ]
end
|
#with_connection {|arg0| ... } ⇒ Object
189
190
191
192
193
|
# File 'lib/solid_objects/database_adapter.rb', line 189
def with_connection
return yield fixed_connection unless connection_pool
connection_pool.with_connection { |connection| yield connection }
end
|
#with_lock_probe { ... } ⇒ Object
120
121
122
|
# File 'lib/solid_objects/database_adapter.rb', line 120
def with_lock_probe
yield
end
|
#with_lock_retry { ... } ⇒ Object
115
116
117
|
# File 'lib/solid_objects/database_adapter.rb', line 115
def with_lock_retry
yield
end
|
#with_transaction_clock { ... } ⇒ Object
#with_transaction_deadline(_connection) { ... } ⇒ Object
175
176
177
|
# File 'lib/solid_objects/database_adapter.rb', line 175
def with_transaction_deadline(_connection)
yield
end
|