Class: SharedTools::Tools::DatabaseQueryTool
- Inherits:
-
RubyLLM::Tool
- Object
- RubyLLM::Tool
- SharedTools::Tools::DatabaseQueryTool
- Defined in:
- lib/shared_tools/tools/database_query_tool.rb
Class Method Summary collapse
Instance Method Summary collapse
- #available? ⇒ Boolean
-
#execute(query:, database: "default", limit: 100, timeout: 30, params: []) ⇒ Hash
Execute read-only database query.
-
#initialize(logger: nil) ⇒ DatabaseQueryTool
constructor
A new instance of DatabaseQueryTool.
Constructor Details
#initialize(logger: nil) ⇒ DatabaseQueryTool
Returns a new instance of DatabaseQueryTool.
91 92 93 94 |
# File 'lib/shared_tools/tools/database_query_tool.rb', line 91 def initialize(logger: nil) @logger = logger || RubyLLM.logger @connection_cache = {} end |
Class Method Details
.name ⇒ Object
14 |
# File 'lib/shared_tools/tools/database_query_tool.rb', line 14 def self.name = 'database_query' |
Instance Method Details
#available? ⇒ Boolean
16 17 18 |
# File 'lib/shared_tools/tools/database_query_tool.rb', line 16 def available? SEQUEL_AVAILABLE end |
#execute(query:, database: "default", limit: 100, timeout: 30, params: []) ⇒ Hash
Execute read-only database query
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/shared_tools/tools/database_query_tool.rb', line 105 def execute(query:, database: "default", limit: 100, timeout: 30, params: []) @logger.info("DatabaseQueryTool#execute database=#{database} limit=#{limit} timeout=#{timeout}") begin # Validate and sanitize inputs validate_query(query) limit = validate_limit(limit) timeout = validate_timeout(timeout) # Get or create database connection (cached for in-memory databases) db = get_connection(database, timeout) # Add LIMIT clause if not present limited_query = add_limit_to_query(query, limit) @logger.debug("Executing query: #{limited_query}") @logger.debug("With parameters: #{params.inspect}") if params && !params.empty? # Execute query with parameters start_time = Time.now results = if params && !params.empty? db[limited_query, *params].all else db[limited_query].all end execution_time = Time.now - start_time @logger.info("Query executed successfully: #{results.length} rows in #{execution_time.round(3)}s") { success: true, query: limited_query, row_count: results.length, data: results, database: database, execution_time: execution_time.round(3), executed_at: Time.now.iso8601 } rescue Sequel::DatabaseError => e @logger.error("Database error: #{e.}") { success: false, error: "Database error: #{e.}", error_type: "database_error", query: query, database: database } rescue => e @logger.error("Query execution failed: #{e.}") { success: false, error: e., error_type: e.class.name, query: query, database: database } end end |