|
| 1 | +--- |
| 2 | +title: "tidb-sql" |
| 3 | +type: docs |
| 4 | +weight: 1 |
| 5 | +description: > |
| 6 | + A "tidb-sql" tool executes a pre-defined SQL statement against a TiDB |
| 7 | + database. |
| 8 | +aliases: |
| 9 | +- /resources/tools/tidb-sql |
| 10 | +--- |
| 11 | + |
| 12 | +## About |
| 13 | + |
| 14 | +A `tidb-sql` tool executes a pre-defined SQL statement against a TiDB |
| 15 | +database. It's compatible with the following source: |
| 16 | + |
| 17 | +- [tidb](../sources/tidb.md) |
| 18 | + |
| 19 | +The specified SQL statement is executed as a [prepared statement][tidb-prepare], |
| 20 | +and expects parameters in the SQL query to be in the form of placeholders `?`. |
| 21 | + |
| 22 | +[tidb-prepare]: https://docs.pingcap.com/tidb/stable/sql-prepared-plan-cache |
| 23 | + |
| 24 | +## Example |
| 25 | + |
| 26 | +> **Note:** This tool uses parameterized queries to prevent SQL injections. |
| 27 | +> Query parameters can be used as substitutes for arbitrary expressions. |
| 28 | +> Parameters cannot be used as substitutes for identifiers, column names, table |
| 29 | +> names, or other parts of the query. |
| 30 | +
|
| 31 | +```yaml |
| 32 | +tools: |
| 33 | + search_flights_by_number: |
| 34 | + kind: tidb-sql |
| 35 | + source: my-tidb-instance |
| 36 | + statement: | |
| 37 | + SELECT * FROM flights |
| 38 | + WHERE airline = ? |
| 39 | + AND flight_number = ? |
| 40 | + LIMIT 10 |
| 41 | + description: | |
| 42 | + Use this tool to get information for a specific flight. |
| 43 | + Takes an airline code and flight number and returns info on the flight. |
| 44 | + Do NOT use this tool with a flight id. Do NOT guess an airline code or flight number. |
| 45 | + A airline code is a code for an airline service consisting of two-character |
| 46 | + airline designator and followed by flight number, which is 1 to 4 digit number. |
| 47 | + For example, if given CY 0123, the airline is "CY", and flight_number is "123". |
| 48 | + Another example for this is DL 1234, the airline is "DL", and flight_number is "1234". |
| 49 | + If the tool returns more than one option choose the date closes to today. |
| 50 | + Example: |
| 51 | + {{ |
| 52 | + "airline": "CY", |
| 53 | + "flight_number": "888", |
| 54 | + }} |
| 55 | + Example: |
| 56 | + {{ |
| 57 | + "airline": "DL", |
| 58 | + "flight_number": "1234", |
| 59 | + }} |
| 60 | + parameters: |
| 61 | + - name: airline |
| 62 | + type: string |
| 63 | + description: Airline unique 2 letter identifier |
| 64 | + - name: flight_number |
| 65 | + type: string |
| 66 | + description: 1 to 4 digit number |
| 67 | +``` |
| 68 | +
|
| 69 | +### Example with Template Parameters |
| 70 | +
|
| 71 | +> **Note:** This tool allows direct modifications to the SQL statement, |
| 72 | +> including identifiers, column names, and table names. **This makes it more |
| 73 | +> vulnerable to SQL injections**. Using basic parameters only (see above) is |
| 74 | +> recommended for performance and safety reasons. For more details, please check |
| 75 | +> [templateParameters](_index#template-parameters). |
| 76 | +
|
| 77 | +```yaml |
| 78 | +tools: |
| 79 | + list_table: |
| 80 | + kind: tidb-sql |
| 81 | + source: my-tidb-instance |
| 82 | + statement: | |
| 83 | + SELECT * FROM {{.tableName}}; |
| 84 | + description: | |
| 85 | + Use this tool to list all information from a specific table. |
| 86 | + Example: |
| 87 | + {{ |
| 88 | + "tableName": "flights", |
| 89 | + }} |
| 90 | + templateParameters: |
| 91 | + - name: tableName |
| 92 | + type: string |
| 93 | + description: Table to select from |
| 94 | +``` |
| 95 | +
|
| 96 | +## Reference |
| 97 | +
|
| 98 | +| **field** | **type** | **required** | **description** | |
| 99 | +|--------------------|:------------------------------------------------:|:------------:|--------------------------------------------------------------------------------------------------------------------------------------------| |
| 100 | +| kind | string | true | Must be "tidb-sql". | |
| 101 | +| source | string | true | Name of the source the SQL should execute on. | |
| 102 | +| description | string | true | Description of the tool that is passed to the LLM. | |
| 103 | +| statement | string | true | SQL statement to execute on. | |
| 104 | +| parameters | [parameters](_index#specifying-parameters) | false | List of [parameters](_index#specifying-parameters) that will be inserted into the SQL statement. | |
| 105 | +| templateParameters | [templateParameters](_index#template-parameters) | false | List of [templateParameters](_index#template-parameters) that will be inserted into the SQL statement before executing prepared statement. | |
0 commit comments