Class: RDBr::Access::ReadOnly
- Inherits:
-
Object
- Object
- RDBr::Access::ReadOnly
- Defined in:
- lib/rdbr/access/read_only.rb
Constant Summary collapse
- MODE =
'read-only'.freeze
- READ_STATEMENT =
/\A\s*SELECT\b/i
Instance Attribute Summary collapse
-
#statement_timeout ⇒ Object
readonly
Returns the value of attribute statement_timeout.
Instance Method Summary collapse
- #allow_http_method?(method) ⇒ Boolean
- #configure(connection, adapter_name:, mariadb: false) ⇒ Object
-
#initialize(statement_timeout: 5000) ⇒ ReadOnly
constructor
A new instance of ReadOnly.
- #mode ⇒ Object
- #validate_statement!(sql) ⇒ Object
Constructor Details
#initialize(statement_timeout: 5000) ⇒ ReadOnly
Returns a new instance of ReadOnly.
9 10 11 |
# File 'lib/rdbr/access/read_only.rb', line 9 def initialize(statement_timeout: 5000) @statement_timeout = positive_integer(statement_timeout, 'statement timeout') end |
Instance Attribute Details
#statement_timeout ⇒ Object (readonly)
Returns the value of attribute statement_timeout.
7 8 9 |
# File 'lib/rdbr/access/read_only.rb', line 7 def statement_timeout @statement_timeout end |
Instance Method Details
#allow_http_method?(method) ⇒ Boolean
17 18 19 |
# File 'lib/rdbr/access/read_only.rb', line 17 def allow_http_method?(method) [ 'GET', 'HEAD' ].include?(method) end |
#configure(connection, adapter_name:, mariadb: false) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rdbr/access/read_only.rb', line 27 def configure(connection, adapter_name:, mariadb: false) case adapter_name when 'postgresql', 'postgis' connection.execute('SET default_transaction_read_only = on') connection.execute("SET statement_timeout = #{statement_timeout}") when 'sqlite', 'sqlite3' connection.execute('PRAGMA query_only = ON') connection.execute("PRAGMA busy_timeout = #{statement_timeout}") when 'mysql', 'mysql2', 'trilogy' connection.execute('SET SESSION TRANSACTION READ ONLY') configure_mysql_timeout(connection, mariadb) else raise ArgumentError, "unsupported query adapter: #{adapter_name}" end end |
#mode ⇒ Object
13 14 15 |
# File 'lib/rdbr/access/read_only.rb', line 13 def mode MODE end |
#validate_statement!(sql) ⇒ Object
21 22 23 24 25 |
# File 'lib/rdbr/access/read_only.rb', line 21 def validate_statement!(sql) return if READ_STATEMENT.match?(sql.to_s) raise Query::AccessViolationError, 'Only read-only SELECT statements are permitted' end |