Skip to content

Commit 53412a9

Browse files
committed
feat(database): add support for parsing and verifying SQL scripts before inserting them into the editor.
This commit introduces a new feature to the devins-lang project that enhances the user experience when working with SQL scripts. The commit enables the parsing and verification of SQL scripts before they are inserted into the editor, ensuring that the scripts are syntactically correct and free from errors. This feature significantly improves the quality of the generated SQL code and reduces the likelihood of runtime issues. The commit also includes a new method `fix` in the `TaskFlow` class, which is used to address any errors found in the SQL scripts. This method is designed to be flexible and can be extended to handle a variety of error-fixing scenarios. By implementing these changes, the devins-lang project now provides a more robust and user-friendly environment for developers to work with SQL scripts.
1 parent a9b9b77 commit 53412a9

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

exts/database/src/main/kotlin/cc/unitmesh/database/flow/AutoSqlBackgroundTask.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,31 @@ class AutoSqlBackgroundTask(
4444
indicator.text = AutoDevBundle.message("autosql.generate.generate")
4545
val sqlScript = flow.design(tableNames)[0]
4646

47-
logger.info("SQL Script: $sqlScript")
48-
// verify sql script with parser
4947
try {
5048
val sqlFile =
5149
PsiFileFactory.getInstance(project).createFileFromText("temp.sql", language, sqlScript)
5250
as SqlFile
5351

5452
val errors = sqlFile.verifySqlElement()
5553
if (errors.isNotEmpty()) {
56-
logger.error("SQL Script parse error: ${errors[0]}")
54+
val response = flow.fix(errors.joinToString("\n"))
55+
val code = parseCodeFromString(response).last()
56+
writeToFile(code, indicator)
5757
}
5858
} catch (e: Exception) {
5959
logger.error("SQL Script parse error: $e")
6060
}
6161

62+
writeToFile(sqlScript, indicator)
63+
indicator.fraction = 1.0
64+
}
65+
66+
private fun writeToFile(sqlScript: String, indicator: ProgressIndicator) {
6267
WriteCommandAction.runWriteCommandAction(project, "Gen SQL", "cc.unitmesh.livingDoc", {
6368
editor.document.insertString(editor.caretModel.offset, "\n")
6469
val code = parseCodeFromString(sqlScript).first()
6570
editor.document.insertString(editor.caretModel.offset + "\n".length, code)
6671
})
67-
68-
indicator.fraction = 1.0
6972
}
7073
}
7174

exts/database/src/main/kotlin/cc/unitmesh/database/flow/AutoSqlFlow.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ class AutoSqlFlow(
7474
return prompter
7575
}
7676

77+
override fun fix(errors: String): String {
78+
panel.addMessage(errors, true, errors)
79+
panel.addMessage(AutoDevBundle.message("autodev.loading"))
80+
81+
return runBlocking {
82+
val prompt = llm.stream(errors, "")
83+
return@runBlocking panel.updateMessage(prompt)
84+
}
85+
}
86+
7787
fun getAllTables(): List<String> {
7888
return actions.dasTables.map { it.name }
7989
}

src/main/kotlin/cc/unitmesh/devti/flow/TaskFlow.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,14 @@ interface TaskFlow<Tasking> {
3838
fun execute(context: Any): String {
3939
return ""
4040
}
41+
42+
/**
43+
* This method is used to fix the errors in the task flow.
44+
*
45+
* @param errors The errors that need to be fixed.
46+
* @return A string representing the fixed errors.
47+
*/
48+
fun fix(errors: String): String {
49+
return ""
50+
}
4151
}

0 commit comments

Comments
 (0)