mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-06-08 15:39:51 +08:00
fix(mysql): 修复视图编辑时的DDL头部兼容
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -29,6 +30,8 @@ import (
|
||||
const minExportQueryTimeout = 5 * time.Minute
|
||||
const minClickHouseExportQueryTimeout = 2 * time.Hour
|
||||
|
||||
var mysqlCreateViewPrefixPattern = regexp.MustCompile(`(?is)^\s*create\s+(?:algorithm\s*=\s*\w+\s+)?(?:definer\s*=\s*(?:` + "`[^`]+`" + `|\S+)\s*@\s*(?:` + "`[^`]+`" + `|\S+)\s+)?(?:sql\s+security\s+(?:definer|invoker)\s+)?view\s+`)
|
||||
|
||||
func (a *App) OpenSQLFile() connection.QueryResult {
|
||||
selection, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
|
||||
Title: "Select SQL File",
|
||||
@@ -1601,7 +1604,7 @@ func extractViewCreateSQL(row map[string]interface{}) string {
|
||||
}
|
||||
ddl := exportRowValueCI(row, "create view", "create_statement", "create_sql", "ddl", "sql", "view_definition", "definition")
|
||||
if ddl != "" {
|
||||
return ddl
|
||||
return normalizeMySQLViewCreateSQL(ddl)
|
||||
}
|
||||
for _, value := range row {
|
||||
if value == nil {
|
||||
@@ -1613,12 +1616,23 @@ func extractViewCreateSQL(row map[string]interface{}) string {
|
||||
}
|
||||
lower := strings.ToLower(text)
|
||||
if strings.HasPrefix(lower, "create ") || strings.HasPrefix(lower, "select ") || strings.HasPrefix(lower, "with ") {
|
||||
return text
|
||||
return normalizeMySQLViewCreateSQL(text)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func normalizeMySQLViewCreateSQL(sql string) string {
|
||||
trimmed := strings.TrimSpace(strings.TrimSuffix(strings.TrimSpace(sql), ";"))
|
||||
if trimmed == "" {
|
||||
return ""
|
||||
}
|
||||
if mysqlCreateViewPrefixPattern.MatchString(trimmed) {
|
||||
return mysqlCreateViewPrefixPattern.ReplaceAllString(trimmed, "CREATE OR REPLACE VIEW ")
|
||||
}
|
||||
return trimmed
|
||||
}
|
||||
|
||||
func exportRowValueCI(row map[string]interface{}, candidates ...string) string {
|
||||
if len(row) == 0 || len(candidates) == 0 {
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user