Class: RDBr::Query::Connection
- Inherits:
-
Object
- Object
- RDBr::Query::Connection
show all
- Defined in:
- lib/rdbr/query/connection.rb
Defined Under Namespace
Modules: Records
Classes: Record
Constant Summary
collapse
- DEFAULT_OPTIONS =
{
statement_timeout: 5000, pool_size: 5, checkout_timeout: 5000, max_value_bytes: 1_048_576
}.freeze
Instance Method Summary
collapse
Constructor Details
#initialize(database_url, options: {}, access_policy: nil) ⇒ Connection
Returns a new instance of Connection.
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/rdbr/query/connection.rb', line 38
def initialize(database_url, options: {}, access_policy: nil)
options = DEFAULT_OPTIONS.merge(options)
@record_class, @record_class_name = Records.build(Record)
@access_policy = access_policy || Access::ReadOnly.new(statement_timeout: options.fetch(:statement_timeout))
@value_limiter = ValueLimiter.new(max_bytes: options.fetch(:max_value_bytes))
@record_class.establish_connection(
url: database_url,
pool: positive_integer(options.fetch(:pool_size), 'pool size'),
checkout_timeout: positive_integer(options.fetch(:checkout_timeout), 'checkout timeout').fdiv(1000)
)
with_connection do |database_connection|
@adapter_name = database_connection.adapter_name.downcase
@mariadb = database_connection.select_value('SELECT VERSION()').to_s.include?('MariaDB') if mysql?
@access_policy.configure(database_connection, adapter_name: @adapter_name, mariadb: @mariadb)
end
rescue ActiveRecord::ActiveRecordError, ArgumentError => e
raise ExecutionError, "Unable to configure read-only database connection (#{e.class})"
end
|
Instance Method Details
#disconnect ⇒ Object
85
86
87
88
|
# File 'lib/rdbr/query/connection.rb', line 85
def disconnect
@record_class.connection_pool.disconnect!
Records.remove(@record_class_name)
end
|
#quote(value) ⇒ Object
61
62
63
|
# File 'lib/rdbr/query/connection.rb', line 61
def quote(value)
with_connection{|database_connection| database_connection.quote(value) }
end
|
#quote_identifier(identifier) ⇒ Object
57
58
59
|
# File 'lib/rdbr/query/connection.rb', line 57
def quote_identifier(identifier)
with_connection{|database_connection| database_connection.quote_column_name(identifier.to_s) }
end
|
#select_all(sql) ⇒ Object
65
66
67
68
69
70
71
72
73
|
# File 'lib/rdbr/query/connection.rb', line 65
def select_all(sql)
@access_policy.validate_statement!(sql)
with_connection do |database_connection|
configure_session(database_connection)
@value_limiter.call(database_connection.select_all(sql).to_a)
end
rescue ActiveRecord::ActiveRecordError => e
raise classified_error(e), classified_message(e)
end
|
#select_value(sql) ⇒ Object
75
76
77
78
79
80
81
82
83
|
# File 'lib/rdbr/query/connection.rb', line 75
def select_value(sql)
@access_policy.validate_statement!(sql)
with_connection do |database_connection|
configure_session(database_connection)
@value_limiter.call(database_connection.select_value(sql))
end
rescue ActiveRecord::ActiveRecordError => e
raise classified_error(e), classified_message(e)
end
|