|
| 1 | +<a id="camel.toolkits.sql_toolkit"></a> |
| 2 | + |
| 3 | +<a id="camel.toolkits.sql_toolkit.SQLToolkit"></a> |
| 4 | + |
| 5 | +## SQLToolkit |
| 6 | + |
| 7 | +```python |
| 8 | +class SQLToolkit(BaseToolkit): |
| 9 | +``` |
| 10 | + |
| 11 | +A toolkit for executing SQL queries against various SQL databases. |
| 12 | + |
| 13 | +This toolkit provides functionality to execute SQL queries with support |
| 14 | +for read-only and read-write modes. It currently supports DuckDB and |
| 15 | +SQLite, with extensibility for MySQL and other SQL databases. |
| 16 | + |
| 17 | +**Parameters:** |
| 18 | + |
| 19 | +- **database_path** (Optional[str]): Path to the database file. If None, uses an in-memory database. For DuckDB and SQLite, use ":memory:" for in-memory or a file path for persistent storage. (default: :obj:`None`) |
| 20 | +- **database_type** (`Literal["duckdb", "sqlite"]`): Type of database to use. Currently supports "duckdb" and "sqlite". (default: :obj:`"duckdb"`) |
| 21 | +- **read_only** (bool, optional): If True, only SELECT queries are allowed. Write operations (INSERT, UPDATE, DELETE, etc.) will be rejected. (default: :obj:`False`) |
| 22 | +- **timeout** (Optional[float], optional): The timeout for database operations in seconds. Defaults to 180 seconds if not specified. (default: :obj:`180.0`) |
| 23 | + |
| 24 | +<a id="camel.toolkits.sql_toolkit.SQLToolkit.__init__"></a> |
| 25 | + |
| 26 | +### __init__ |
| 27 | + |
| 28 | +```python |
| 29 | +def __init__( |
| 30 | + self, |
| 31 | + database_path: Optional[str] = None, |
| 32 | + database_type: Literal['duckdb', 'sqlite'] = 'duckdb', |
| 33 | + read_only: bool = False, |
| 34 | + timeout: Optional[float] = 180.0 |
| 35 | +): |
| 36 | +``` |
| 37 | + |
| 38 | +<a id="camel.toolkits.sql_toolkit.SQLToolkit._validate_database_type"></a> |
| 39 | + |
| 40 | +### _validate_database_type |
| 41 | + |
| 42 | +```python |
| 43 | +def _validate_database_type(self, database_type: str): |
| 44 | +``` |
| 45 | + |
| 46 | +Validate if the database type is supported. |
| 47 | + |
| 48 | +**Parameters:** |
| 49 | + |
| 50 | +- **database_type** (str): The database type to validate. |
| 51 | + |
| 52 | +<a id="camel.toolkits.sql_toolkit.SQLToolkit._create_connection"></a> |
| 53 | + |
| 54 | +### _create_connection |
| 55 | + |
| 56 | +```python |
| 57 | +def _create_connection(self): |
| 58 | +``` |
| 59 | + |
| 60 | +**Returns:** |
| 61 | + |
| 62 | + Union[duckdb.DuckDBPyConnection, sqlite3.Connection]: A database |
| 63 | +connection object. |
| 64 | + |
| 65 | +<a id="camel.toolkits.sql_toolkit.SQLToolkit._is_write_query"></a> |
| 66 | + |
| 67 | +### _is_write_query |
| 68 | + |
| 69 | +```python |
| 70 | +def _is_write_query(self, query: str): |
| 71 | +``` |
| 72 | + |
| 73 | +Check if a SQL query is a write operation. |
| 74 | + |
| 75 | +This method analyzes the query string to determine if it contains |
| 76 | +any write operations. It handles comments and case-insensitive |
| 77 | +matching. |
| 78 | + |
| 79 | +**Parameters:** |
| 80 | + |
| 81 | +- **query** (str): The SQL query to check. |
| 82 | + |
| 83 | +**Returns:** |
| 84 | + |
| 85 | + bool: True if the query is a write operation, False otherwise. |
| 86 | + |
| 87 | +<a id="camel.toolkits.sql_toolkit.SQLToolkit._quote_identifier"></a> |
| 88 | + |
| 89 | +### _quote_identifier |
| 90 | + |
| 91 | +```python |
| 92 | +def _quote_identifier(self, identifier: str): |
| 93 | +``` |
| 94 | + |
| 95 | +Safely quote a SQL identifier (table name, column name, etc.). |
| 96 | + |
| 97 | +This method validates and quotes SQL identifiers to prevent SQL |
| 98 | +injection. For DuckDB, identifiers are quoted with double quotes. Any |
| 99 | +double quotes within the identifier are escaped by doubling them. |
| 100 | + |
| 101 | +**Parameters:** |
| 102 | + |
| 103 | +- **identifier** (str): The identifier to quote (e.g., table name, column name). |
| 104 | + |
| 105 | +**Returns:** |
| 106 | + |
| 107 | + str: The safely quoted identifier. |
| 108 | + |
| 109 | +<a id="camel.toolkits.sql_toolkit.SQLToolkit.execute_query"></a> |
| 110 | + |
| 111 | +### execute_query |
| 112 | + |
| 113 | +```python |
| 114 | +def execute_query( |
| 115 | + self, |
| 116 | + query: str, |
| 117 | + params: Optional[Union[List[Union[str, int, float, bool, None]], Dict[str, Union[str, int, float, bool, None]]]] = None |
| 118 | +): |
| 119 | +``` |
| 120 | + |
| 121 | +Execute a SQL query and return results. |
| 122 | + |
| 123 | +This method executes a SQL query against the configured database and |
| 124 | +returns the results. For SELECT queries, returns a list of dictionaries |
| 125 | +where each dictionary represents a row. For write operations (INSERT, |
| 126 | +UPDATE, DELETE, etc.), returns a status dictionary with execution info. |
| 127 | + |
| 128 | +**Parameters:** |
| 129 | + |
| 130 | +- **query** (str): The SQL query to execute. params (Optional[Union[List[Union[str, int, float, bool, None]], Dict[str, Union[str, int, float, bool, None]]]], optional): Parameters for parameterized queries. Can be a list for positional parameters (with ? placeholders) or a dict for named parameters. Values can be strings, numbers, booleans, or None. Note: tuples are also accepted at runtime but should be passed as lists for type compatibility. (default: :obj:`None`) |
| 131 | + |
| 132 | +**Returns:** |
| 133 | + |
| 134 | + Union[List[Dict[str, Any]], Dict[str, Any], str]: |
| 135 | +- For SELECT queries: List of dictionaries with column names as |
| 136 | +keys and row values as values. |
| 137 | +- For write operations (INSERT, UPDATE, DELETE, CREATE, etc.): |
| 138 | +A dictionary with 'status', 'message', and optionally |
| 139 | +'rows_affected' keys. |
| 140 | +- For errors: An error message string starting with "Error:". |
| 141 | + |
| 142 | +<a id="camel.toolkits.sql_toolkit.SQLToolkit.list_tables"></a> |
| 143 | + |
| 144 | +### list_tables |
| 145 | + |
| 146 | +```python |
| 147 | +def list_tables(self): |
| 148 | +``` |
| 149 | + |
| 150 | +**Returns:** |
| 151 | + |
| 152 | + Union[List[str], str]: A list of table names in the database, |
| 153 | +or an error message string if the operation fails. |
| 154 | + |
| 155 | +<a id="camel.toolkits.sql_toolkit.SQLToolkit._get_table_schema"></a> |
| 156 | + |
| 157 | +### _get_table_schema |
| 158 | + |
| 159 | +```python |
| 160 | +def _get_table_schema(self, table_name: str): |
| 161 | +``` |
| 162 | + |
| 163 | +Internal helper method to get table schema information. |
| 164 | + |
| 165 | +**Parameters:** |
| 166 | + |
| 167 | +- **table_name** (str): The name of the table to describe. |
| 168 | + |
| 169 | +**Returns:** |
| 170 | + |
| 171 | + Union[Dict[str, Any], str]: A dictionary containing 'columns', |
| 172 | +'primary_keys', and 'foreign_keys', or an error message string |
| 173 | +if the operation fails. |
| 174 | + |
| 175 | +<a id="camel.toolkits.sql_toolkit.SQLToolkit.get_table_info"></a> |
| 176 | + |
| 177 | +### get_table_info |
| 178 | + |
| 179 | +```python |
| 180 | +def get_table_info(self, table_name: Optional[str] = None): |
| 181 | +``` |
| 182 | + |
| 183 | +Get comprehensive information about table(s) in the database. |
| 184 | + |
| 185 | +This method provides a summary of table information including schema, |
| 186 | +primary keys, foreign keys, and row counts. If table_name is provided, |
| 187 | +returns info for that specific table. Otherwise, returns info for all |
| 188 | +tables. |
| 189 | + |
| 190 | +**Parameters:** |
| 191 | + |
| 192 | +- **table_name** (Optional[str], optional): Name of a specific table to get info for. If None, returns info for all tables. (default: :obj:`None`) |
| 193 | + |
| 194 | +**Returns:** |
| 195 | + |
| 196 | + Union[Dict[str, Any], str]: A dictionary containing table |
| 197 | +information, or an error message string if the operation fails. |
| 198 | +If table_name is provided, returns info for that table with |
| 199 | +keys: 'table_name', 'columns', 'primary_keys', 'foreign_keys', |
| 200 | +'row_count'. Otherwise, returns a dictionary mapping table |
| 201 | +names to their info dictionaries. |
| 202 | + |
| 203 | +<a id="camel.toolkits.sql_toolkit.SQLToolkit.get_tools"></a> |
| 204 | + |
| 205 | +### get_tools |
| 206 | + |
| 207 | +```python |
| 208 | +def get_tools(self): |
| 209 | +``` |
| 210 | + |
| 211 | +**Returns:** |
| 212 | + |
| 213 | + List[FunctionTool]: A list of FunctionTool objects representing the |
| 214 | +available functions in the toolkit. |
| 215 | + |
| 216 | +<a id="camel.toolkits.sql_toolkit.SQLToolkit.__del__"></a> |
| 217 | + |
| 218 | +### __del__ |
| 219 | + |
| 220 | +```python |
| 221 | +def __del__(self): |
| 222 | +``` |
| 223 | + |
| 224 | +Clean up database connection on deletion. |
0 commit comments