Class: Tidewave::Tools::ExecuteSqlQuery

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

Constant Summary collapse

DESCRIPTION =
"Executes the given SQL query against the database connection.\nReturns the result as a Ruby data structure.\n\nNote that the output is limited to 50 rows at a time. If you need to see more, perform additional calls\nusing LIMIT and OFFSET in the query. If you know that only specific columns are relevant,\nonly include those in the SELECT clause.\n\nYou can use this tool to select user data, manipulate entries, and introspect the application data domain.\nAlways ensure to use the correct SQL commands for the database you are using.\n\nFor PostgreSQL, use $1, $2, etc. for parameter placeholders.\nFor MySQL, use ? for parameter placeholders.\n".freeze

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
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tidewave/tools/execute_sql_query.rb', line 48

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

  preamble = if result[:row_count] > result[:rows].length
    "      Query returned \#{result[:row_count]} rows. Only the first \#{result[:rows].length} rows are included in the result. Use your database's pagination syntax, such as LIMIT + OFFSET, to show more rows if applicable.\n\n    TEXT\n  else\n    \"\"\n  end\n\n  preamble + result.inspect\nend\n"

#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