Class: Tidewave::Tools::ExecuteSqlQuery

Inherits:
Tidewave::Tool show all
Defined in:
lib/tidewave/tools/execute_sql_query.rb

Constant Summary collapse

DESCRIPTION =
<<~DESCRIPTION.freeze
  Executes the given SQL query against the database connection.
  Returns the result as a Ruby data structure.

  Note that the output is limited to 50 rows at a time. If you need to see more, perform additional calls
  using LIMIT and OFFSET in the query. If you know that only specific columns are relevant,
  only include those in the SELECT clause.

  You can use this tool to select user data, manipulate entries, and introspect the application data domain.
  Always ensure to use the correct SQL commands for the database you are using.

  For PostgreSQL, use $1, $2, etc. for parameter placeholders.
  For MySQL, use ? for parameter placeholders.
DESCRIPTION

Instance Method Summary collapse

Methods inherited from Tidewave::Tool

descendants, inherited, #validate_and_call

Constructor Details

#initialize(options = {}) ⇒ ExecuteSqlQuery

Returns a new instance of ExecuteSqlQuery.



19
20
21
# File 'lib/tidewave/tools/execute_sql_query.rb', line 19

def initialize(options = {})
  @database_adapter = Tidewave::DatabaseAdapter.for(options[:orm_adapter]) if options[:orm_adapter]
end

Instance Method Details

#call(arguments_hash) ⇒ Object



48
49
50
51
52
# File 'lib/tidewave/tools/execute_sql_query.rb', line 48

def call(arguments_hash)
  query = arguments_hash.fetch("query")
  arguments = arguments_hash.fetch("arguments", [])
  @database_adapter.execute_query(query, arguments)
end

#definitionObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tidewave/tools/execute_sql_query.rb', line 23

def definition
  return nil unless @database_adapter

  {
    "name" => "execute_sql_query",
    "description" => DESCRIPTION,
    "inputSchema" => {
      "type" => "object",
      "properties" => {
        "query" => {
          "type" => "string",
          "minLength" => 1,
          "description" => "The SQL query to execute. For PostgreSQL, use $1, $2 placeholders. For MySQL, use ? placeholders."
        },
        "arguments" => {
          "type" => "array",
          "items" => {},
          "description" => "The arguments to pass to the query. The query must contain corresponding parameter placeholders."
        }
      },
      "required" => [ "query" ]
    }
  }
end