2024-03-11 20:04:20 +08:00
|
|
|
|
package sqlite
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"mayfly-go/internal/db/dbm/dbi"
|
|
|
|
|
|
"mayfly-go/pkg/logx"
|
2024-03-15 09:01:51 +00:00
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
2024-03-18 12:25:40 +08:00
|
|
|
|
"mayfly-go/pkg/utils/stringx"
|
2024-03-11 20:04:20 +08:00
|
|
|
|
"regexp"
|
|
|
|
|
|
"strings"
|
2024-03-21 17:15:52 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/may-fly/cast"
|
2024-03-11 20:04:20 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
2024-03-18 12:25:40 +08:00
|
|
|
|
SQLITE_META_FILE = "metasql/sqlite_meta.sql"
|
|
|
|
|
|
SQLITE_TABLE_INFO_KEY = "SQLITE_TABLE_INFO"
|
|
|
|
|
|
SQLITE_INDEX_INFO_KEY = "SQLITE_INDEX_INFO"
|
2024-03-11 20:04:20 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2024-11-01 17:27:22 +08:00
|
|
|
|
type SqliteMetadata struct {
|
|
|
|
|
|
dbi.DefaultMetadata
|
2024-03-15 13:31:53 +08:00
|
|
|
|
|
2024-03-11 20:04:20 +08:00
|
|
|
|
dc *dbi.DbConn
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-01 17:27:22 +08:00
|
|
|
|
func (sd *SqliteMetadata) GetDbServer() (*dbi.DbServer, error) {
|
2024-03-11 20:04:20 +08:00
|
|
|
|
_, res, err := sd.dc.Query("SELECT SQLITE_VERSION() as version")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
ds := &dbi.DbServer{
|
2024-03-21 17:15:52 +08:00
|
|
|
|
Version: cast.ToString(res[0]["version"]),
|
2024-03-11 20:04:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
return ds, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-01 17:27:22 +08:00
|
|
|
|
func (sd *SqliteMetadata) GetDbNames() ([]string, error) {
|
2024-03-11 20:04:20 +08:00
|
|
|
|
databases := make([]string, 0)
|
|
|
|
|
|
_, res, err := sd.dc.Query("PRAGMA database_list")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, re := range res {
|
2024-03-21 17:15:52 +08:00
|
|
|
|
databases = append(databases, cast.ToString(re["name"]))
|
2024-03-11 20:04:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return databases, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取表基础元信息, 如表名等
|
2024-11-01 17:27:22 +08:00
|
|
|
|
func (sd *SqliteMetadata) GetTables(tableNames ...string) ([]dbi.Table, error) {
|
|
|
|
|
|
dialect := sd.dc.GetDialect()
|
2024-03-15 09:01:51 +00:00
|
|
|
|
names := strings.Join(collx.ArrayMap[string, string](tableNames, func(val string) string {
|
2024-11-01 17:27:22 +08:00
|
|
|
|
return fmt.Sprintf("'%s'", dialect.RemoveQuote(val))
|
2024-03-15 09:01:51 +00:00
|
|
|
|
}), ",")
|
|
|
|
|
|
|
|
|
|
|
|
var res []map[string]any
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
2024-03-18 12:25:40 +08:00
|
|
|
|
sql, err := stringx.TemplateParse(dbi.GetLocalSql(SQLITE_META_FILE, SQLITE_TABLE_INFO_KEY), collx.M{"tableNames": names})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
2024-03-15 09:01:51 +00:00
|
|
|
|
}
|
2024-03-18 12:25:40 +08:00
|
|
|
|
|
|
|
|
|
|
_, res, err = sd.dc.Query(sql)
|
2024-03-11 20:04:20 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2024-03-18 12:25:40 +08:00
|
|
|
|
|
2024-03-11 20:04:20 +08:00
|
|
|
|
tables := make([]dbi.Table, 0)
|
|
|
|
|
|
for _, re := range res {
|
|
|
|
|
|
tables = append(tables, dbi.Table{
|
2024-03-21 17:15:52 +08:00
|
|
|
|
TableName: cast.ToString(re["tableName"]),
|
|
|
|
|
|
TableComment: cast.ToString(re["tableComment"]),
|
|
|
|
|
|
CreateTime: cast.ToString(re["createTime"]),
|
|
|
|
|
|
TableRows: cast.ToInt(re["tableRows"]),
|
|
|
|
|
|
DataLength: cast.ToInt64(re["dataLength"]),
|
|
|
|
|
|
IndexLength: cast.ToInt64(re["indexLength"]),
|
2024-03-11 20:04:20 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
return tables, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-21 03:35:18 +00:00
|
|
|
|
// GetDataTypes 正则提取字段类型中的关键字,
|
|
|
|
|
|
// 如 decimal(10,2) 提取decimal, 10 ,2
|
|
|
|
|
|
// 如:text 提取text,null,null
|
|
|
|
|
|
// 如:varchar(100) 提取varchar, 100
|
2024-11-01 17:27:22 +08:00
|
|
|
|
func (sd *SqliteMetadata) getDataTypes(dataType string) (string, string, string) {
|
2024-03-21 03:35:18 +00:00
|
|
|
|
matches := dataTypeRegexp.FindStringSubmatch(dataType)
|
|
|
|
|
|
if len(matches) == 0 {
|
2024-10-22 20:39:44 +08:00
|
|
|
|
return dataType, "", ""
|
2024-03-21 03:35:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
return matches[1], matches[2], matches[3]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-11 20:04:20 +08:00
|
|
|
|
// 获取列元信息, 如列名等
|
2024-11-01 17:27:22 +08:00
|
|
|
|
func (sd *SqliteMetadata) GetColumns(tableNames ...string) ([]dbi.Column, error) {
|
2024-03-11 20:04:20 +08:00
|
|
|
|
columns := make([]dbi.Column, 0)
|
2024-11-01 17:27:22 +08:00
|
|
|
|
columnHelper := sd.dc.GetDialect().GetColumnHelper()
|
2024-03-11 20:04:20 +08:00
|
|
|
|
for i := 0; i < len(tableNames); i++ {
|
|
|
|
|
|
tableName := tableNames[i]
|
|
|
|
|
|
_, res, err := sd.dc.Query(fmt.Sprintf("PRAGMA table_info(%s)", tableName))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
logx.Error("获取数据库表字段结构出错", err.Error())
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, re := range res {
|
|
|
|
|
|
// 去掉默认值的引号
|
2024-03-21 17:15:52 +08:00
|
|
|
|
defaultValue := cast.ToString(re["dflt_value"])
|
2024-03-11 20:04:20 +08:00
|
|
|
|
if strings.Contains(defaultValue, "'") {
|
|
|
|
|
|
defaultValue = strings.ReplaceAll(defaultValue, "'", "")
|
|
|
|
|
|
}
|
2024-03-21 03:35:18 +00:00
|
|
|
|
|
|
|
|
|
|
column := dbi.Column{
|
2024-03-11 20:04:20 +08:00
|
|
|
|
TableName: tableName,
|
2024-03-21 17:15:52 +08:00
|
|
|
|
ColumnName: cast.ToString(re["name"]),
|
2024-03-11 20:04:20 +08:00
|
|
|
|
ColumnComment: "",
|
2024-03-21 20:28:24 +08:00
|
|
|
|
Nullable: cast.ToInt(re["notnull"]) != 1,
|
2024-03-21 17:15:52 +08:00
|
|
|
|
IsPrimaryKey: cast.ToInt(re["pk"]) == 1,
|
|
|
|
|
|
IsIdentity: cast.ToInt(re["pk"]) == 1,
|
2024-03-11 20:04:20 +08:00
|
|
|
|
ColumnDefault: defaultValue,
|
2024-03-18 12:25:40 +08:00
|
|
|
|
NumScale: 0,
|
2024-03-21 03:35:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 切割类型和长度,如果长度内有逗号,则说明是decimal类型
|
2024-03-21 17:15:52 +08:00
|
|
|
|
columnType := cast.ToString(re["type"])
|
2024-03-21 03:35:18 +00:00
|
|
|
|
dataType, length, scale := sd.getDataTypes(columnType)
|
|
|
|
|
|
if scale != "0" && scale != "" {
|
2024-03-21 17:15:52 +08:00
|
|
|
|
column.NumPrecision = cast.ToInt(length)
|
|
|
|
|
|
column.NumScale = cast.ToInt(scale)
|
2024-03-21 03:35:18 +00:00
|
|
|
|
column.CharMaxLength = 0
|
|
|
|
|
|
} else {
|
2024-03-21 17:15:52 +08:00
|
|
|
|
column.CharMaxLength = cast.ToInt(length)
|
2024-03-21 03:35:18 +00:00
|
|
|
|
}
|
2024-10-22 20:39:44 +08:00
|
|
|
|
column.DataType = dbi.ColumnDataType(strings.ToLower(dataType))
|
2024-03-26 21:46:03 +08:00
|
|
|
|
columnHelper.FixColumn(&column)
|
2024-03-26 09:05:28 +00:00
|
|
|
|
|
2024-03-21 03:35:18 +00:00
|
|
|
|
columns = append(columns, column)
|
2024-03-11 20:04:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return columns, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-01 17:27:22 +08:00
|
|
|
|
func (sd *SqliteMetadata) GetPrimaryKey(tableName string) (string, error) {
|
2024-03-11 20:04:20 +08:00
|
|
|
|
_, res, err := sd.dc.Query(fmt.Sprintf("PRAGMA table_info(%s)", tableName))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, re := range res {
|
2024-03-21 17:15:52 +08:00
|
|
|
|
if cast.ToInt(re["pk"]) == 1 {
|
|
|
|
|
|
return cast.ToString(re["name"]), nil
|
2024-03-11 20:04:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return "", errors.New("不存在主键")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 解析索引创建语句以获取字段信息
|
|
|
|
|
|
func extractIndexFields(indexSQL string) string {
|
|
|
|
|
|
// 使用正则表达式提取字段信息
|
|
|
|
|
|
re := regexp.MustCompile(`\((.*?)\)`)
|
|
|
|
|
|
match := re.FindStringSubmatch(indexSQL)
|
|
|
|
|
|
if len(match) > 1 {
|
|
|
|
|
|
fields := strings.Split(match[1], ",")
|
|
|
|
|
|
for i, field := range fields {
|
|
|
|
|
|
// 去除空格
|
|
|
|
|
|
fields[i] = strings.TrimSpace(field)
|
|
|
|
|
|
}
|
|
|
|
|
|
return strings.Join(fields, ",")
|
|
|
|
|
|
}
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取表索引信息
|
2024-11-01 17:27:22 +08:00
|
|
|
|
func (sd *SqliteMetadata) GetTableIndex(tableName string) ([]dbi.Index, error) {
|
2024-03-11 20:04:20 +08:00
|
|
|
|
_, res, err := sd.dc.Query(fmt.Sprintf(dbi.GetLocalSql(SQLITE_META_FILE, SQLITE_INDEX_INFO_KEY), tableName))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
indexs := make([]dbi.Index, 0)
|
|
|
|
|
|
for _, re := range res {
|
2024-03-21 17:15:52 +08:00
|
|
|
|
indexSql := cast.ToString(re["indexSql"])
|
2024-03-11 20:04:20 +08:00
|
|
|
|
isUnique := strings.Contains(indexSql, "CREATE UNIQUE INDEX")
|
|
|
|
|
|
|
|
|
|
|
|
indexs = append(indexs, dbi.Index{
|
2024-03-21 17:15:52 +08:00
|
|
|
|
IndexName: cast.ToString(re["indexName"]),
|
2024-03-11 20:04:20 +08:00
|
|
|
|
ColumnName: extractIndexFields(indexSql),
|
2024-03-21 17:15:52 +08:00
|
|
|
|
IndexType: cast.ToString(re["indexType"]),
|
|
|
|
|
|
IndexComment: cast.ToString(re["indexComment"]),
|
2024-03-11 20:04:20 +08:00
|
|
|
|
IsUnique: isUnique,
|
|
|
|
|
|
SeqInIndex: 1,
|
2024-03-26 09:05:28 +00:00
|
|
|
|
IsPrimaryKey: false,
|
2024-03-11 20:04:20 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
// 把查询结果以索引名分组,索引字段以逗号连接
|
|
|
|
|
|
return indexs, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取建表ddl
|
2024-11-01 17:27:22 +08:00
|
|
|
|
func (sd *SqliteMetadata) GetTableDDL(tableName string, dropBeforeCreate bool) (string, error) {
|
2024-03-26 09:05:28 +00:00
|
|
|
|
var builder strings.Builder
|
|
|
|
|
|
|
|
|
|
|
|
if dropBeforeCreate {
|
|
|
|
|
|
builder.WriteString(fmt.Sprintf("DROP TABLE IF EXISTS %s; \n\n", tableName))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-11 20:04:20 +08:00
|
|
|
|
_, res, err := sd.dc.Query("select sql from sqlite_master WHERE tbl_name=? order by type desc", tableName)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
2024-03-26 09:05:28 +00:00
|
|
|
|
|
2024-03-11 20:04:20 +08:00
|
|
|
|
for _, re := range res {
|
2024-03-21 17:15:52 +08:00
|
|
|
|
builder.WriteString(cast.ToString(re["sql"]) + "; \n\n")
|
2024-03-11 20:04:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return builder.String(), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-01 17:27:22 +08:00
|
|
|
|
func (sd *SqliteMetadata) GetSchemas() ([]string, error) {
|
2024-03-11 20:04:20 +08:00
|
|
|
|
return nil, nil
|
|
|
|
|
|
}
|