Class: Ruby::Text2sql::SQLExecutor
- Inherits:
-
Object
- Object
- Ruby::Text2sql::SQLExecutor
- Defined in:
- lib/ruby/text2sql/sql_executor.rb
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(allowed_actions: [:select], sql_query: nil) ⇒ SQLExecutor
constructor
A new instance of SQLExecutor.
Constructor Details
#initialize(allowed_actions: [:select], sql_query: nil) ⇒ SQLExecutor
Returns a new instance of SQLExecutor.
6 7 8 9 |
# File 'lib/ruby/text2sql/sql_executor.rb', line 6 def initialize(allowed_actions: [:select], sql_query: nil) @allowed_actions = allowed_actions.map(&:to_sym) @sql_query = sql_query end |
Instance Method Details
#execute ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ruby/text2sql/sql_executor.rb', line 11 def execute return { status: :failed, error: "Action ':#{query_type}' is not allowed." } unless query_allowed?(query_type) if query_type == :select result = ActiveRecord::Base.connection.execute(@sql_query) result.to_a else begin ActiveRecord::Base.transaction do ActiveRecord::Base.connection.execute(@sql_query) end { status: :success } rescue StandardError => e { status: :failed, error: e. } end end end |