Class: LLMDB::Adapters::Base
- Inherits:
-
Object
- Object
- LLMDB::Adapters::Base
- Defined in:
- lib/llmdb/adapters/base.rb
Direct Known Subclasses
Constant Summary collapse
- SAFE_PREFIXES =
%w[SELECT WITH EXPLAIN].freeze
Instance Method Summary collapse
- #execute(sql, permission_mode: :read_only, max_rows: 500, confirm: nil, classifier: nil) ⇒ Object
-
#initialize(config) ⇒ Base
constructor
A new instance of Base.
- #table_schema(table_name) ⇒ Object
- #tables ⇒ Object
Constructor Details
#initialize(config) ⇒ Base
Returns a new instance of Base.
8 9 10 11 12 13 |
# File 'lib/llmdb/adapters/base.rb', line 8 def initialize(config) @config = config @db = Sequel.connect(connection_params) rescue Sequel::DatabaseConnectionError => e raise ConnectionError, "Could not connect to #{@config.adapter} database: #{e.}" end |
Instance Method Details
#execute(sql, permission_mode: :read_only, max_rows: 500, confirm: nil, classifier: nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/llmdb/adapters/base.rb', line 25 def execute(sql, permission_mode: :read_only, max_rows: 500, confirm: nil, classifier: nil) case when :read_only execute_read_only(sql, max_rows) when :ask execute_with_confirmation(sql, max_rows, confirm, classifier) when :full fetch_rows(sql, max_rows) else raise ConfigurationError, "Unknown permission_mode: #{.inspect}" end rescue Sequel::DatabaseError => e raise QueryError, "Query failed: #{e.}" rescue Sequel::Error => e raise QueryError, "Unexpected error: #{e.}" end |
#table_schema(table_name) ⇒ Object
21 22 23 |
# File 'lib/llmdb/adapters/base.rb', line 21 def table_schema(table_name) { columns: columns_for(table_name), foreign_keys: foreign_keys_for(table_name) } end |
#tables ⇒ Object
15 16 17 18 19 |
# File 'lib/llmdb/adapters/base.rb', line 15 def tables @db.tables.map(&:to_s).sort rescue Sequel::Error => e raise QueryError, "Failed to list tables: #{e.}" end |