Skip to content

Commit 62d3e13

Browse files
committed
feat(database): add SQL generation templates #80
This commit adds two new SQL generation templates for the Database Administrator. The first template, `sql-gen-clarify.vm`, prompts the user to choose the best tables based on their requirements. The second template, `sql-gen-design.vm`, prompts the user to write SQL queries based on their requirements and table information. Both templates provide clear instructions and examples to guide the user.
1 parent 6017221 commit 62d3e13

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

exts/database/src/main/kotlin/cc/unitmesh/database/actions/GenSqlScriptBySelection.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,24 @@ class GenSqlScriptBySelection : AbstractChatIntention() {
4343

4444
val tableColumns = DbContextProvider(dasTables).getTableColumns(dasTables.map { it.name })
4545

46-
val prompt = """
47-
|Database: $databaseVersion
48-
|Requirement: $selectedText
49-
|Tables: ${dasTables.joinToString { it.name }}
50-
|Columns: ${tableColumns.joinToString { it }}
51-
""".trimMargin()
52-
53-
println(prompt)
46+
DbContext(
47+
requirement = selectedText ?: "",
48+
databaseVersion = databaseVersion.let {
49+
"name: ${it.name}, version: ${it.version}"
50+
},
51+
schemaName = schemaName,
52+
tableNames = dasTables.map { it.name },
53+
)
5454
}
5555
}
5656

5757
data class DbContext(
58+
val requirement: String,
5859
val databaseVersion: String,
5960
val schemaName: String,
6061
val tableNames: List<String>,
62+
// for step 2
63+
val tableInfos: List<String> = emptyList(),
6164
) {
6265
}
6366

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
You are a professional Database Administrator.
2+
According to the user's requirements, you should choose the best Tables for the user in List.
3+
4+
— User use database: ${context.databaseVersion}
5+
- User schema name: ${context.schemaName}
6+
- User tables: ${context.tableNames}
7+
8+
For example:
9+
10+
- Question(requirements): calculate the average trip length by subscriber type.// User tables: trips, users, subscriber_type
11+
- Answer: [trips, subscriber_type]
12+
13+
----
14+
15+
Here are the requirements:
16+
17+
```
18+
${context.requirement}
19+
```
20+
21+
Please choose the best Tables for the user, just return the table names in a list, no explain.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
You are a professional Database Administrator.
2+
According to the user's requirements, and Tables info, write SQL for the user.
3+
4+
— User use database: ${context.databaseVersion}
5+
- User schema name: ${context.schemaName}
6+
- User tableInfos: ${context.tableInfos}
7+
8+
For example:
9+
10+
- Question(requirements): calculate the average trip length by subscriber type.
11+
// table `subscriber_type`: average_trip_length: int, subscriber_type: string
12+
- Answer:
13+
```sql
14+
select average_trip_length from subscriber_type where subscriber_type = 'subscriber'
15+
```
16+
17+
----
18+
19+
Here are the requirements:
20+
21+
```
22+
${context.requirement}
23+
```
24+
25+
Please write your SQL here:
26+
27+
```sql

0 commit comments

Comments
 (0)