|
| 1 | +package db |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/gogf/gf/v2/container/gmap" |
| 9 | + "github.com/gogf/gf/v2/database/gdb" |
| 10 | +) |
| 11 | + |
| 12 | +// func getSQLiteSchemaByCli(tableName string, db gdb.DB) (string, error) { |
| 13 | +// dbConf := db.GetConfig() |
| 14 | +// cmd := exec.Command("sqlite3", dbConf.Name, fmt.Sprintf(".schema %s", tableName)) |
| 15 | +// glog.Info(context.TODO(), "sqlite3", dbConf.Name, fmt.Sprintf("'.schema %s'", tableName)) |
| 16 | +// output, err := cmd.CombinedOutput() |
| 17 | +// if err != nil { |
| 18 | +// return "", err |
| 19 | +// } |
| 20 | +// return strings.TrimSpace(string(output)), nil |
| 21 | +// } |
| 22 | + |
| 23 | +func getSQLiteSchemaBySql(ctx context.Context, tableName string, db gdb.DB) (string, error) { |
| 24 | + schemaRes, err := db.GetValue(ctx, fmt.Sprintf(`SELECT sql FROM sqlite_master WHERE type='table' AND name='%s';`, tableName)) |
| 25 | + if err != nil { |
| 26 | + return "", err |
| 27 | + } |
| 28 | + return schemaRes.String(), nil |
| 29 | +} |
| 30 | + |
| 31 | +func getSQliteTableComments(createTableSql string) (tableComment, tableName string) { |
| 32 | + // 按照换行符分割文本 |
| 33 | + lines := strings.Split(createTableSql, "\n") |
| 34 | + |
| 35 | + // 循环输出每一行 |
| 36 | + for _, line := range lines { |
| 37 | + // 检查 createTableSql 是否包含 comment |
| 38 | + if strings.Contains(line, "--") { |
| 39 | + if strings.Contains(line, "CREATE TABLE") { |
| 40 | + tableName = getLastWord(strings.Split(line, "(")[0]) |
| 41 | + tableComment = strings.Split(line, "--")[1] |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + return |
| 46 | +} |
| 47 | + |
| 48 | +func getSQliteFieldsComments(createTableSql string) (fieldCommentMap gmap.Map, tableComment, tableName string) { |
| 49 | + // 按照换行符分割文本 |
| 50 | + lines := strings.Split(createTableSql, "\n") |
| 51 | + |
| 52 | + // 循环输出每一行 |
| 53 | + for _, line := range lines { |
| 54 | + // 检查 createTableSql 是否包含 comment |
| 55 | + if strings.Contains(line, "--") { |
| 56 | + if strings.Contains(line, "CREATE TABLE") { |
| 57 | + tableName = getLastWord(strings.Split(line, "(")[0]) |
| 58 | + tableComment = strings.Split(line, "--")[1] |
| 59 | + } else { |
| 60 | + firstWord := getFirstWord(line) |
| 61 | + lastWord := getLastWord(line) |
| 62 | + fieldCommentMap.Set(firstWord, lastWord) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + return |
| 67 | +} |
| 68 | + |
| 69 | +func transSqliteTablesComment(ctx context.Context, tableNames []string, db gdb.DB) (tables []*TableComment, err error) { |
| 70 | + schemaStr := "" |
| 71 | + eleIgnore := "sqlite_sequence" |
| 72 | + for _, v := range tableNames { |
| 73 | + if v != eleIgnore { |
| 74 | + schemaStr, err = getSQLiteSchemaBySql(ctx, v, db) |
| 75 | + if err != nil { |
| 76 | + return |
| 77 | + } |
| 78 | + comment, _ := getSQliteTableComments(schemaStr) |
| 79 | + tables = append(tables, &TableComment{ |
| 80 | + Name: v, |
| 81 | + Comment: comment, |
| 82 | + }) |
| 83 | + } |
| 84 | + } |
| 85 | + return |
| 86 | +} |
| 87 | + |
| 88 | +func fixSqliteFieldsComment(ctx context.Context, tableName string, db gdb.DB, fields map[string]*gdb.TableField) (map[string]*gdb.TableField, error) { |
| 89 | + // 记录: db.DoSelect 无法执行 .开头的命令 |
| 90 | + // schemaRes, err := db.DoSelect(ctx, dbConf.Link, fmt.Sprintf(`.schema %s`, d.QuoteWord(table))) |
| 91 | + // 记录: 查询 sqlite_master 不响应任何结果 |
| 92 | + // s, err := db.Query(ctx, `select * from sqlite_master WHERE name="%s";`, tableName) |
| 93 | + // schemaStr, err := getSQLiteSchemaBySql(tableName, db) |
| 94 | + schemaStr, err := getSQLiteSchemaBySql(ctx, tableName, db) |
| 95 | + if err != nil { |
| 96 | + return fields, err |
| 97 | + } |
| 98 | + comments, _, _ := getSQliteFieldsComments(schemaStr) |
| 99 | + for i := range fields { |
| 100 | + if comments.Contains(i) { |
| 101 | + fields[i].Comment = comments.Get(i).(string) |
| 102 | + } |
| 103 | + } |
| 104 | + return fields, nil |
| 105 | +} |
0 commit comments