mirror of
https://gitee.com/dromara/mayfly-go
synced 2026-03-09 19:45:39 +08:00
refactor: dbms与标签管理优化
This commit is contained in:
@@ -53,7 +53,6 @@ type DbServer struct {
|
||||
// 表信息
|
||||
type Table struct {
|
||||
TableName string `json:"tableName"` // 表名
|
||||
TableNewName string `json:"tableNewName"` // 新表名,复制表生成ddl时,需要传入新表名
|
||||
TableComment string `json:"tableComment"` // 表备注
|
||||
CreateTime string `json:"createTime"` // 创建时间
|
||||
TableRows int `json:"tableRows"`
|
||||
@@ -75,38 +74,22 @@ type Column struct {
|
||||
NumPrecision int `json:"numPrecision"` // 精度(总数字位数)
|
||||
NumScale int `json:"numScale"` // 小数点位数
|
||||
Extra collx.M `json:"extra"` // 其他额外信息
|
||||
|
||||
ShowLength int `json:"showLength"`
|
||||
ShowScale int `json:"showScale"`
|
||||
ShowDataType string `json:"showDataType"` // 显示数据类型
|
||||
}
|
||||
|
||||
// 初始化列显示类型,拼接数据类型与长度等。如varchar(2000),decimal(20,2)
|
||||
func (c *Column) InitShowNum() string {
|
||||
// 拼接数据类型与长度等。如varchar(2000),decimal(20,2)
|
||||
func (c *Column) GetColumnType() string {
|
||||
if c.CharMaxLength > 0 {
|
||||
c.ShowDataType = fmt.Sprintf("%s(%d)", c.DataType, c.CharMaxLength)
|
||||
c.ShowLength = c.CharMaxLength
|
||||
c.ShowScale = 0
|
||||
return c.ShowDataType
|
||||
return fmt.Sprintf("%s(%d)", c.DataType, c.CharMaxLength)
|
||||
}
|
||||
if c.NumPrecision > 0 {
|
||||
if c.NumScale > 0 {
|
||||
c.ShowDataType = fmt.Sprintf("%s(%d,%d)", c.DataType, c.NumPrecision, c.NumScale)
|
||||
c.ShowScale = c.NumScale
|
||||
return fmt.Sprintf("%s(%d,%d)", c.DataType, c.NumPrecision, c.NumScale)
|
||||
} else {
|
||||
c.ShowDataType = fmt.Sprintf("%s(%d)", c.DataType, c.NumPrecision)
|
||||
c.ShowScale = 0
|
||||
return fmt.Sprintf("%s(%d)", c.DataType, c.NumPrecision)
|
||||
}
|
||||
c.ShowLength = c.NumPrecision
|
||||
|
||||
return c.ShowDataType
|
||||
}
|
||||
|
||||
c.ShowDataType = string(c.DataType)
|
||||
c.ShowLength = 0
|
||||
c.ShowScale = 0
|
||||
|
||||
return c.ShowDataType
|
||||
return string(c.DataType)
|
||||
}
|
||||
|
||||
// 表索引信息
|
||||
|
||||
@@ -113,7 +113,7 @@ func baseType(t reflect.Type, expected reflect.Kind) (reflect.Type, error) {
|
||||
// struct is expected but something else is given
|
||||
func structOnlyError(t reflect.Type) error {
|
||||
isStruct := t.Kind() == reflect.Struct
|
||||
isScanner := reflect.PtrTo(t).Implements(_scannerInterface)
|
||||
isScanner := reflect.PointerTo(t).Implements(_scannerInterface)
|
||||
if !isStruct {
|
||||
return fmt.Errorf("expected %s but got %s", reflect.Struct, t.Kind())
|
||||
}
|
||||
|
||||
@@ -77,17 +77,15 @@ func (dd *DMDialect) batchInsertMerge(tx *sql.Tx, tableName string, columns []st
|
||||
}
|
||||
// 查询唯一索引涉及到的字段,并组装到match条件内
|
||||
indexs, _ := metadata.GetTableIndex(tableName)
|
||||
if indexs != nil {
|
||||
for _, index := range indexs {
|
||||
if index.IsUnique {
|
||||
cols := strings.Split(index.ColumnName, ",")
|
||||
tmp := make([]string, 0)
|
||||
for _, col := range cols {
|
||||
uniqueCols = append(uniqueCols, col)
|
||||
tmp = append(tmp, fmt.Sprintf(" T1.%s = T2.%s ", metadata.QuoteIdentifier(col), metadata.QuoteIdentifier(col)))
|
||||
}
|
||||
caseSqls = append(caseSqls, fmt.Sprintf("( %s )", strings.Join(tmp, " AND ")))
|
||||
for _, index := range indexs {
|
||||
if index.IsUnique {
|
||||
cols := strings.Split(index.ColumnName, ",")
|
||||
tmp := make([]string, 0)
|
||||
for _, col := range cols {
|
||||
uniqueCols = append(uniqueCols, col)
|
||||
tmp = append(tmp, fmt.Sprintf(" T1.%s = T2.%s ", metadata.QuoteIdentifier(col), metadata.QuoteIdentifier(col)))
|
||||
}
|
||||
caseSqls = append(caseSqls, fmt.Sprintf("( %s )", strings.Join(tmp, " AND ")))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +100,7 @@ func (dd *DMDialect) batchInsertMerge(tx *sql.Tx, tableName string, columns []st
|
||||
upds = append(upds, fmt.Sprintf("T1.%s = T2.%s", column, column))
|
||||
}
|
||||
if !collx.ArrayContains(identityCols, column) {
|
||||
insertCols = append(insertCols, fmt.Sprintf("%s", column))
|
||||
insertCols = append(insertCols, column)
|
||||
insertVals = append(insertVals, fmt.Sprintf("T2.%s", column))
|
||||
}
|
||||
|
||||
@@ -218,7 +216,3 @@ func (dd *DMDialect) CreateIndex(tableInfo dbi.Table, indexs []dbi.Index) error
|
||||
_, err := dd.dc.Exec(strings.Join(sqls, ";"))
|
||||
return err
|
||||
}
|
||||
|
||||
func (dd *DMDialect) UpdateSequence(tableName string, columns []dbi.Column) {
|
||||
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/may-fly/cast"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -33,7 +35,7 @@ func (dd *DMMetaData) GetDbServer() (*dbi.DbServer, error) {
|
||||
return nil, err
|
||||
}
|
||||
ds := &dbi.DbServer{
|
||||
Version: anyx.ConvString(res[0]["SVR_VERSION"]),
|
||||
Version: cast.ToString(res[0]["SVR_VERSION"]),
|
||||
}
|
||||
return ds, nil
|
||||
}
|
||||
@@ -46,7 +48,7 @@ func (dd *DMMetaData) GetDbNames() ([]string, error) {
|
||||
|
||||
databases := make([]string, 0)
|
||||
for _, re := range res {
|
||||
databases = append(databases, anyx.ConvString(re["DBNAME"]))
|
||||
databases = append(databases, cast.ToString(re["DBNAME"]))
|
||||
}
|
||||
|
||||
return databases, nil
|
||||
@@ -73,12 +75,12 @@ func (dd *DMMetaData) GetTables(tableNames ...string) ([]dbi.Table, error) {
|
||||
tables := make([]dbi.Table, 0)
|
||||
for _, re := range res {
|
||||
tables = append(tables, dbi.Table{
|
||||
TableName: anyx.ConvString(re["TABLE_NAME"]),
|
||||
TableComment: anyx.ConvString(re["TABLE_COMMENT"]),
|
||||
CreateTime: anyx.ConvString(re["CREATE_TIME"]),
|
||||
TableRows: anyx.ConvInt(re["TABLE_ROWS"]),
|
||||
DataLength: anyx.ConvInt64(re["DATA_LENGTH"]),
|
||||
IndexLength: anyx.ConvInt64(re["INDEX_LENGTH"]),
|
||||
TableName: cast.ToString(re["TABLE_NAME"]),
|
||||
TableComment: cast.ToString(re["TABLE_COMMENT"]),
|
||||
CreateTime: cast.ToString(re["CREATE_TIME"]),
|
||||
TableRows: cast.ToInt(re["TABLE_ROWS"]),
|
||||
DataLength: cast.ToInt64(re["DATA_LENGTH"]),
|
||||
IndexLength: cast.ToInt64(re["INDEX_LENGTH"]),
|
||||
})
|
||||
}
|
||||
return tables, nil
|
||||
@@ -98,21 +100,18 @@ func (dd *DMMetaData) GetColumns(tableNames ...string) ([]dbi.Column, error) {
|
||||
columns := make([]dbi.Column, 0)
|
||||
for _, re := range res {
|
||||
column := dbi.Column{
|
||||
TableName: anyx.ConvString(re["TABLE_NAME"]),
|
||||
ColumnName: anyx.ConvString(re["COLUMN_NAME"]),
|
||||
TableName: cast.ToString(re["TABLE_NAME"]),
|
||||
ColumnName: cast.ToString(re["COLUMN_NAME"]),
|
||||
DataType: dbi.ColumnDataType(anyx.ToString(re["DATA_TYPE"])),
|
||||
CharMaxLength: anyx.ConvInt(re["CHAR_MAX_LENGTH"]),
|
||||
ColumnComment: anyx.ConvString(re["COLUMN_COMMENT"]),
|
||||
Nullable: anyx.ConvString(re["NULLABLE"]),
|
||||
IsPrimaryKey: anyx.ConvInt(re["IS_PRIMARY_KEY"]) == 1,
|
||||
IsIdentity: anyx.ConvInt(re["IS_IDENTITY"]) == 1,
|
||||
ColumnDefault: anyx.ConvString(re["COLUMN_DEFAULT"]),
|
||||
NumPrecision: anyx.ConvInt(re["NUM_PRECISION"]),
|
||||
NumScale: anyx.ConvInt(re["NUM_SCALE"]),
|
||||
CharMaxLength: cast.ToInt(re["CHAR_MAX_LENGTH"]),
|
||||
ColumnComment: cast.ToString(re["COLUMN_COMMENT"]),
|
||||
Nullable: cast.ToString(re["NULLABLE"]),
|
||||
IsPrimaryKey: cast.ToInt(re["IS_PRIMARY_KEY"]) == 1,
|
||||
IsIdentity: cast.ToInt(re["IS_IDENTITY"]) == 1,
|
||||
ColumnDefault: cast.ToString(re["COLUMN_DEFAULT"]),
|
||||
NumPrecision: cast.ToInt(re["NUM_PRECISION"]),
|
||||
NumScale: cast.ToInt(re["NUM_SCALE"]),
|
||||
}
|
||||
|
||||
// 初始化列展示的长度,精度
|
||||
column.InitShowNum()
|
||||
columns = append(columns, column)
|
||||
}
|
||||
return columns, nil
|
||||
@@ -145,12 +144,12 @@ func (dd *DMMetaData) GetTableIndex(tableName string) ([]dbi.Index, error) {
|
||||
indexs := make([]dbi.Index, 0)
|
||||
for _, re := range res {
|
||||
indexs = append(indexs, dbi.Index{
|
||||
IndexName: anyx.ConvString(re["INDEX_NAME"]),
|
||||
ColumnName: anyx.ConvString(re["COLUMN_NAME"]),
|
||||
IndexType: anyx.ConvString(re["INDEX_TYPE"]),
|
||||
IndexComment: anyx.ConvString(re["INDEX_COMMENT"]),
|
||||
IsUnique: anyx.ConvInt(re["IS_UNIQUE"]) == 1,
|
||||
SeqInIndex: anyx.ConvInt(re["SEQ_IN_INDEX"]),
|
||||
IndexName: cast.ToString(re["INDEX_NAME"]),
|
||||
ColumnName: cast.ToString(re["COLUMN_NAME"]),
|
||||
IndexType: cast.ToString(re["INDEX_TYPE"]),
|
||||
IndexComment: cast.ToString(re["INDEX_COMMENT"]),
|
||||
IsUnique: cast.ToInt(re["IS_UNIQUE"]) == 1,
|
||||
SeqInIndex: cast.ToInt(re["SEQ_IN_INDEX"]),
|
||||
})
|
||||
}
|
||||
// 把查询结果以索引名分组,索引字段以逗号连接
|
||||
@@ -207,7 +206,7 @@ func (dd *DMMetaData) genColumnBasicSql(column dbi.Column) string {
|
||||
}
|
||||
}
|
||||
|
||||
columnSql := fmt.Sprintf(" %s %s %s %s %s", colName, column.ShowDataType, incr, nullAble, defVal)
|
||||
columnSql := fmt.Sprintf(" %s %s %s %s %s", colName, column.GetColumnType(), incr, nullAble, defVal)
|
||||
return columnSql
|
||||
|
||||
}
|
||||
@@ -328,7 +327,7 @@ func (dd *DMMetaData) GetSchemas() ([]string, error) {
|
||||
}
|
||||
schemaNames := make([]string, 0)
|
||||
for _, re := range res {
|
||||
schemaNames = append(schemaNames, anyx.ConvString(re["SCHEMA_NAME"]))
|
||||
schemaNames = append(schemaNames, cast.ToString(re["SCHEMA_NAME"]))
|
||||
}
|
||||
return schemaNames, nil
|
||||
}
|
||||
@@ -346,8 +345,6 @@ var (
|
||||
dateRegexp = regexp.MustCompile(`(?i)date`)
|
||||
// 时间类型
|
||||
timeRegexp = regexp.MustCompile(`(?i)time`)
|
||||
// 定义正则表达式,匹配括号内的数字
|
||||
bracketsRegexp = regexp.MustCompile(`\((\d+)\)`)
|
||||
|
||||
converter = new(DataConverter)
|
||||
|
||||
|
||||
@@ -16,22 +16,6 @@ type MssqlDialect struct {
|
||||
dc *dbi.DbConn
|
||||
}
|
||||
|
||||
// 从连接信息中获取数据库和schema信息
|
||||
func (md *MssqlDialect) currentSchema() string {
|
||||
dbName := md.dc.Info.Database
|
||||
schema := ""
|
||||
arr := strings.Split(dbName, "/")
|
||||
if len(arr) == 2 {
|
||||
schema = arr[1]
|
||||
}
|
||||
return schema
|
||||
}
|
||||
|
||||
// GetDbProgram 获取数据库程序模块,用于数据库备份与恢复
|
||||
func (md *MssqlDialect) GetDbProgram() (dbi.DbProgram, error) {
|
||||
return nil, fmt.Errorf("该数据库类型不支持数据库备份与恢复: %v", md.dc.Info.Type)
|
||||
}
|
||||
|
||||
func (md *MssqlDialect) BatchInsert(tx *sql.Tx, tableName string, columns []string, values [][]any, duplicateStrategy int) (int64, error) {
|
||||
|
||||
if duplicateStrategy == dbi.DuplicateStrategyUpdate {
|
||||
@@ -143,6 +127,9 @@ func (md *MssqlDialect) batchInsertMerge(tx *sql.Tx, tableName string, columns [
|
||||
// 查询取出自增列字段, merge update不能修改自增列
|
||||
identityCols := make([]string, 0)
|
||||
cols, err := msMetadata.GetColumns(tableName)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
for _, col := range cols {
|
||||
if col.IsIdentity {
|
||||
identityCols = append(identityCols, col.ColumnName)
|
||||
@@ -166,7 +153,7 @@ func (md *MssqlDialect) batchInsertMerge(tx *sql.Tx, tableName string, columns [
|
||||
if !collx.ArrayContains(identityCols, msMetadata.RemoveQuote(column)) {
|
||||
upds = append(upds, fmt.Sprintf("T1.%s = T2.%s", column, column))
|
||||
}
|
||||
insertCols = append(insertCols, fmt.Sprintf("%s", column))
|
||||
insertCols = append(insertCols, column)
|
||||
insertVals = append(insertVals, fmt.Sprintf("T2.%s", column))
|
||||
phs = append(phs, fmt.Sprintf("? %s", column))
|
||||
}
|
||||
@@ -309,7 +296,3 @@ func (md *MssqlDialect) CreateIndex(tableInfo dbi.Table, indexs []dbi.Index) err
|
||||
_, err := md.dc.Exec(strings.Join(sqlArr, ";"))
|
||||
return err
|
||||
}
|
||||
|
||||
func (md *MssqlDialect) UpdateSequence(tableName string, columns []dbi.Column) {
|
||||
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/may-fly/cast"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -34,7 +36,7 @@ func (md *MssqlMetaData) GetDbServer() (*dbi.DbServer, error) {
|
||||
return nil, err
|
||||
}
|
||||
ds := &dbi.DbServer{
|
||||
Version: anyx.ConvString(res[0]["version"]),
|
||||
Version: cast.ToString(res[0]["version"]),
|
||||
}
|
||||
return ds, nil
|
||||
}
|
||||
@@ -47,7 +49,7 @@ func (md *MssqlMetaData) GetDbNames() ([]string, error) {
|
||||
|
||||
databases := make([]string, 0)
|
||||
for _, re := range res {
|
||||
databases = append(databases, anyx.ConvString(re["dbname"]))
|
||||
databases = append(databases, cast.ToString(re["dbname"]))
|
||||
}
|
||||
|
||||
return databases, nil
|
||||
@@ -63,9 +65,6 @@ func (md *MssqlMetaData) GetTables(tableNames ...string) ([]dbi.Table, error) {
|
||||
|
||||
var res []map[string]any
|
||||
var err error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sql, err := stringx.TemplateParse(dbi.GetLocalSql(MSSQL_META_FILE, MSSQL_TABLE_INFO_KEY), collx.M{"tableNames": names})
|
||||
if err != nil {
|
||||
@@ -80,12 +79,12 @@ func (md *MssqlMetaData) GetTables(tableNames ...string) ([]dbi.Table, error) {
|
||||
tables := make([]dbi.Table, 0)
|
||||
for _, re := range res {
|
||||
tables = append(tables, dbi.Table{
|
||||
TableName: anyx.ConvString(re["tableName"]),
|
||||
TableComment: anyx.ConvString(re["tableComment"]),
|
||||
CreateTime: anyx.ConvString(re["createTime"]),
|
||||
TableRows: anyx.ConvInt(re["tableRows"]),
|
||||
DataLength: anyx.ConvInt64(re["dataLength"]),
|
||||
IndexLength: anyx.ConvInt64(re["indexLength"]),
|
||||
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"]),
|
||||
})
|
||||
}
|
||||
return tables, nil
|
||||
@@ -110,14 +109,14 @@ func (md *MssqlMetaData) GetColumns(tableNames ...string) ([]dbi.Column, error)
|
||||
TableName: anyx.ToString(re["TABLE_NAME"]),
|
||||
ColumnName: anyx.ToString(re["COLUMN_NAME"]),
|
||||
DataType: dbi.ColumnDataType(anyx.ToString(re["DATA_TYPE"])),
|
||||
CharMaxLength: anyx.ConvInt(re["CHAR_MAX_LENGTH"]),
|
||||
CharMaxLength: cast.ToInt(re["CHAR_MAX_LENGTH"]),
|
||||
ColumnComment: anyx.ToString(re["COLUMN_COMMENT"]),
|
||||
Nullable: anyx.ToString(re["NULLABLE"]),
|
||||
IsPrimaryKey: anyx.ConvInt(re["IS_PRIMARY_KEY"]) == 1,
|
||||
IsIdentity: anyx.ConvInt(re["IS_IDENTITY"]) == 1,
|
||||
ColumnDefault: anyx.ConvString(re["COLUMN_DEFAULT"]),
|
||||
NumPrecision: anyx.ConvInt(re["NUM_PRECISION"]),
|
||||
NumScale: anyx.ConvInt(re["NUM_SCALE"]),
|
||||
IsPrimaryKey: cast.ToInt(re["IS_PRIMARY_KEY"]) == 1,
|
||||
IsIdentity: cast.ToInt(re["IS_IDENTITY"]) == 1,
|
||||
ColumnDefault: cast.ToString(re["COLUMN_DEFAULT"]),
|
||||
NumPrecision: cast.ToInt(re["NUM_PRECISION"]),
|
||||
NumScale: cast.ToInt(re["NUM_SCALE"]),
|
||||
}
|
||||
|
||||
dataType := strings.ToLower(string(column.DataType))
|
||||
@@ -137,8 +136,6 @@ func (md *MssqlMetaData) GetColumns(tableNames ...string) ([]dbi.Column, error)
|
||||
column.CharMaxLength = column.CharMaxLength / 2
|
||||
}
|
||||
|
||||
// 初始化列展示的长度,精度
|
||||
column.InitShowNum()
|
||||
columns = append(columns, column)
|
||||
}
|
||||
return columns, nil
|
||||
@@ -171,12 +168,12 @@ func (md *MssqlMetaData) getTableIndexWithPK(tableName string) ([]dbi.Index, err
|
||||
indexs := make([]dbi.Index, 0)
|
||||
for _, re := range res {
|
||||
indexs = append(indexs, dbi.Index{
|
||||
IndexName: anyx.ConvString(re["indexName"]),
|
||||
ColumnName: anyx.ConvString(re["columnName"]),
|
||||
IndexType: anyx.ConvString(re["indexType"]),
|
||||
IndexComment: anyx.ConvString(re["indexComment"]),
|
||||
IsUnique: anyx.ConvInt(re["isUnique"]) == 1,
|
||||
SeqInIndex: anyx.ConvInt(re["seqInIndex"]),
|
||||
IndexName: cast.ToString(re["indexName"]),
|
||||
ColumnName: cast.ToString(re["columnName"]),
|
||||
IndexType: cast.ToString(re["indexType"]),
|
||||
IndexComment: cast.ToString(re["indexComment"]),
|
||||
IsUnique: cast.ToInt(re["isUnique"]) == 1,
|
||||
SeqInIndex: cast.ToInt(re["seqInIndex"]),
|
||||
})
|
||||
}
|
||||
// 把查询结果以索引名分组,多个索引字段以逗号连接
|
||||
@@ -208,6 +205,7 @@ func (md *MssqlMetaData) GetTableIndex(tableName string) ([]dbi.Index, error) {
|
||||
if strings.HasPrefix(in, "PK__") {
|
||||
continue
|
||||
}
|
||||
result = append(result, v)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -224,9 +222,8 @@ func (md *MssqlMetaData) CopyTableDDL(tableName string, newTableName string) (st
|
||||
return "", err
|
||||
}
|
||||
tabInfo := &dbi.Table{
|
||||
TableName: tableName,
|
||||
TableName: newTableName,
|
||||
TableComment: tbs[0].TableComment,
|
||||
TableNewName: newTableName,
|
||||
}
|
||||
|
||||
// 查询列信息
|
||||
@@ -250,12 +247,7 @@ func (md *MssqlMetaData) CopyTableDDL(tableName string, newTableName string) (st
|
||||
// 获取建索引ddl
|
||||
|
||||
func (md *MssqlMetaData) GenerateIndexDDL(indexs []dbi.Index, tableInfo dbi.Table) []string {
|
||||
|
||||
tbName := tableInfo.TableName
|
||||
if tableInfo.TableNewName != "" {
|
||||
tbName = tableInfo.TableNewName
|
||||
}
|
||||
|
||||
sqls := make([]string, 0)
|
||||
comments := make([]string, 0)
|
||||
for _, index := range indexs {
|
||||
@@ -277,6 +269,10 @@ func (md *MssqlMetaData) GenerateIndexDDL(indexs []dbi.Index, tableInfo dbi.Tabl
|
||||
comments = append(comments, fmt.Sprintf("EXECUTE sp_addextendedproperty N'MS_Description', N'%s', N'SCHEMA', N'%s', N'TABLE', N'%s', N'INDEX', N'%s'", index.IndexComment, md.dc.Info.CurrentSchema(), tbName, indexName))
|
||||
}
|
||||
}
|
||||
if len(comments) > 0 {
|
||||
sqls = append(sqls, comments...)
|
||||
}
|
||||
|
||||
return sqls
|
||||
}
|
||||
|
||||
@@ -316,18 +312,13 @@ func (md *MssqlMetaData) genColumnBasicSql(column dbi.Column) string {
|
||||
}
|
||||
}
|
||||
|
||||
columnSql := fmt.Sprintf(" %s %s %s %s %s", colName, column.ShowDataType, incr, nullAble, defVal)
|
||||
columnSql := fmt.Sprintf(" %s %s %s %s %s", colName, column.GetColumnType(), incr, nullAble, defVal)
|
||||
return columnSql
|
||||
}
|
||||
|
||||
// 获取建表ddl
|
||||
func (md *MssqlMetaData) GenerateTableDDL(columns []dbi.Column, tableInfo dbi.Table, dropBeforeCreate bool) []string {
|
||||
|
||||
tbName := tableInfo.TableName
|
||||
if tableInfo.TableNewName != "" {
|
||||
tbName = tableInfo.TableNewName
|
||||
}
|
||||
|
||||
meta := md.dc.GetMetaData()
|
||||
replacer := strings.NewReplacer(";", "", "'", "")
|
||||
|
||||
@@ -424,7 +415,7 @@ func (md *MssqlMetaData) GetSchemas() ([]string, error) {
|
||||
|
||||
schemas := make([]string, 0)
|
||||
for _, re := range res {
|
||||
schemas = append(schemas, anyx.ConvString(re["SCHEMA_NAME"]))
|
||||
schemas = append(schemas, cast.ToString(re["SCHEMA_NAME"]))
|
||||
}
|
||||
return schemas, nil
|
||||
}
|
||||
@@ -448,8 +439,7 @@ var (
|
||||
timeRegexp = regexp.MustCompile(`(?i)time`)
|
||||
|
||||
converter = new(DataConverter)
|
||||
// 定义正则表达式,匹配括号内的数字
|
||||
bracketsRegexp = regexp.MustCompile(`\((\d+)\)`)
|
||||
|
||||
// mssql数据类型 对应 公共数据类型
|
||||
commonColumnTypeMap = map[string]dbi.ColumnDataType{
|
||||
"bigint": dbi.CommonTypeBigint,
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kanzihuang/vitess/go/vt/sqlparser"
|
||||
"github.com/may-fly/cast"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -36,7 +37,7 @@ func (md *MysqlMetaData) GetDbServer() (*dbi.DbServer, error) {
|
||||
return nil, err
|
||||
}
|
||||
ds := &dbi.DbServer{
|
||||
Version: anyx.ConvString(res[0]["version"]),
|
||||
Version: cast.ToString(res[0]["version"]),
|
||||
}
|
||||
return ds, nil
|
||||
}
|
||||
@@ -49,7 +50,7 @@ func (md *MysqlMetaData) GetDbNames() ([]string, error) {
|
||||
|
||||
databases := make([]string, 0)
|
||||
for _, re := range res {
|
||||
databases = append(databases, anyx.ConvString(re["dbname"]))
|
||||
databases = append(databases, cast.ToString(re["dbname"]))
|
||||
}
|
||||
return databases, nil
|
||||
}
|
||||
@@ -76,12 +77,12 @@ func (md *MysqlMetaData) GetTables(tableNames ...string) ([]dbi.Table, error) {
|
||||
tables := make([]dbi.Table, 0)
|
||||
for _, re := range res {
|
||||
tables = append(tables, dbi.Table{
|
||||
TableName: anyx.ConvString(re["tableName"]),
|
||||
TableComment: anyx.ConvString(re["tableComment"]),
|
||||
CreateTime: anyx.ConvString(re["createTime"]),
|
||||
TableRows: anyx.ConvInt(re["tableRows"]),
|
||||
DataLength: anyx.ConvInt64(re["dataLength"]),
|
||||
IndexLength: anyx.ConvInt64(re["indexLength"]),
|
||||
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"]),
|
||||
})
|
||||
}
|
||||
return tables, nil
|
||||
@@ -103,21 +104,19 @@ func (md *MysqlMetaData) GetColumns(tableNames ...string) ([]dbi.Column, error)
|
||||
for _, re := range res {
|
||||
|
||||
column := dbi.Column{
|
||||
TableName: anyx.ConvString(re["tableName"]),
|
||||
ColumnName: anyx.ConvString(re["columnName"]),
|
||||
DataType: dbi.ColumnDataType(anyx.ConvString(re["dataType"])),
|
||||
ColumnComment: anyx.ConvString(re["columnComment"]),
|
||||
Nullable: anyx.ConvString(re["nullable"]),
|
||||
IsPrimaryKey: anyx.ConvInt(re["isPrimaryKey"]) == 1,
|
||||
IsIdentity: anyx.ConvInt(re["isIdentity"]) == 1,
|
||||
ColumnDefault: anyx.ConvString(re["columnDefault"]),
|
||||
CharMaxLength: anyx.ConvInt(re["charMaxLength"]),
|
||||
NumPrecision: anyx.ConvInt(re["numPrecision"]),
|
||||
NumScale: anyx.ConvInt(re["numScale"]),
|
||||
TableName: cast.ToString(re["tableName"]),
|
||||
ColumnName: cast.ToString(re["columnName"]),
|
||||
DataType: dbi.ColumnDataType(cast.ToString(re["dataType"])),
|
||||
ColumnComment: cast.ToString(re["columnComment"]),
|
||||
Nullable: cast.ToString(re["nullable"]),
|
||||
IsPrimaryKey: cast.ToInt(re["isPrimaryKey"]) == 1,
|
||||
IsIdentity: cast.ToInt(re["isIdentity"]) == 1,
|
||||
ColumnDefault: cast.ToString(re["columnDefault"]),
|
||||
CharMaxLength: cast.ToInt(re["charMaxLength"]),
|
||||
NumPrecision: cast.ToInt(re["numPrecision"]),
|
||||
NumScale: cast.ToInt(re["numScale"]),
|
||||
}
|
||||
|
||||
// 初始化列展示的长度,精度
|
||||
column.InitShowNum()
|
||||
columns = append(columns, column)
|
||||
}
|
||||
return columns, nil
|
||||
@@ -152,12 +151,12 @@ func (md *MysqlMetaData) GetTableIndex(tableName string) ([]dbi.Index, error) {
|
||||
indexs := make([]dbi.Index, 0)
|
||||
for _, re := range res {
|
||||
indexs = append(indexs, dbi.Index{
|
||||
IndexName: anyx.ConvString(re["indexName"]),
|
||||
ColumnName: anyx.ConvString(re["columnName"]),
|
||||
IndexType: anyx.ConvString(re["indexType"]),
|
||||
IndexComment: anyx.ConvString(re["indexComment"]),
|
||||
IsUnique: anyx.ConvInt(re["isUnique"]) == 1,
|
||||
SeqInIndex: anyx.ConvInt(re["seqInIndex"]),
|
||||
IndexName: cast.ToString(re["indexName"]),
|
||||
ColumnName: cast.ToString(re["columnName"]),
|
||||
IndexType: cast.ToString(re["indexType"]),
|
||||
IndexComment: cast.ToString(re["indexComment"]),
|
||||
IsUnique: cast.ToInt(re["isUnique"]) == 1,
|
||||
SeqInIndex: cast.ToInt(re["seqInIndex"]),
|
||||
})
|
||||
}
|
||||
// 把查询结果以索引名分组,索引字段以逗号连接
|
||||
@@ -243,7 +242,7 @@ func (md *MysqlMetaData) genColumnBasicSql(column dbi.Column) string {
|
||||
comment = fmt.Sprintf(" COMMENT '%s'", commentStr)
|
||||
}
|
||||
|
||||
columnSql := fmt.Sprintf(" %s %s %s %s %s %s", md.dc.GetMetaData().QuoteIdentifier(column.ColumnName), column.ShowDataType, nullAble, incr, defVal, comment)
|
||||
columnSql := fmt.Sprintf(" %s %s %s %s %s %s", md.dc.GetMetaData().QuoteIdentifier(column.ColumnName), column.GetColumnType(), nullAble, incr, defVal, comment)
|
||||
return columnSql
|
||||
}
|
||||
|
||||
@@ -260,7 +259,7 @@ func (md *MysqlMetaData) GenerateTableDDL(columns []dbi.Column, tableInfo dbi.Ta
|
||||
createSql := fmt.Sprintf("CREATE TABLE %s (\n", meta.QuoteIdentifier(tableInfo.TableName))
|
||||
fields := make([]string, 0)
|
||||
pks := make([]string, 0)
|
||||
// 把通用类型转换为达梦类型
|
||||
|
||||
for _, column := range columns {
|
||||
if column.IsPrimaryKey {
|
||||
pks = append(pks, column.ColumnName)
|
||||
|
||||
@@ -211,7 +211,3 @@ func (od *OracleDialect) CreateIndex(tableInfo dbi.Table, indexs []dbi.Index) er
|
||||
_, err := od.dc.Exec(strings.Join(sqlArr, ";"))
|
||||
return err
|
||||
}
|
||||
|
||||
func (od *OracleDialect) UpdateSequence(tableName string, columns []dbi.Column) {
|
||||
|
||||
}
|
||||
|
||||
@@ -4,10 +4,10 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"mayfly-go/internal/db/dbm/dbi"
|
||||
"mayfly-go/pkg/utils/anyx"
|
||||
"mayfly-go/pkg/utils/jsonx"
|
||||
"strings"
|
||||
|
||||
"github.com/may-fly/cast"
|
||||
go_ora "github.com/sijms/go-ora/v2"
|
||||
)
|
||||
|
||||
@@ -56,8 +56,8 @@ func (md *OraMeta) GetSqlDb(d *dbi.DbInfo) (*sql.DB, error) {
|
||||
serviceName := ""
|
||||
if d.Extra != "" {
|
||||
extraMap := jsonx.ToMap(d.Extra)
|
||||
serviceName = anyx.ConvString(extraMap["serviceName"])
|
||||
if sid := anyx.ConvString(extraMap["sid"]); sid != "" {
|
||||
serviceName = cast.ToString(extraMap["serviceName"])
|
||||
if sid := cast.ToString(extraMap["sid"]); sid != "" {
|
||||
urlOptions["SID"] = sid
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/may-fly/cast"
|
||||
)
|
||||
|
||||
// ---------------------------------- DM元数据 -----------------------------------
|
||||
@@ -34,7 +36,7 @@ func (od *OracleMetaData) GetDbServer() (*dbi.DbServer, error) {
|
||||
return nil, err
|
||||
}
|
||||
ds := &dbi.DbServer{
|
||||
Version: anyx.ConvString(res[0]["VERSION"]),
|
||||
Version: cast.ToString(res[0]["VERSION"]),
|
||||
}
|
||||
return ds, nil
|
||||
}
|
||||
@@ -47,7 +49,7 @@ func (od *OracleMetaData) GetDbNames() ([]string, error) {
|
||||
|
||||
databases := make([]string, 0)
|
||||
for _, re := range res {
|
||||
databases = append(databases, anyx.ConvString(re["DBNAME"]))
|
||||
databases = append(databases, cast.ToString(re["DBNAME"]))
|
||||
}
|
||||
|
||||
return databases, nil
|
||||
@@ -75,12 +77,12 @@ func (od *OracleMetaData) GetTables(tableNames ...string) ([]dbi.Table, error) {
|
||||
tables := make([]dbi.Table, 0)
|
||||
for _, re := range res {
|
||||
tables = append(tables, dbi.Table{
|
||||
TableName: anyx.ConvString(re["TABLE_NAME"]),
|
||||
TableComment: anyx.ConvString(re["TABLE_COMMENT"]),
|
||||
CreateTime: anyx.ConvString(re["CREATE_TIME"]),
|
||||
TableRows: anyx.ConvInt(re["TABLE_ROWS"]),
|
||||
DataLength: anyx.ConvInt64(re["DATA_LENGTH"]),
|
||||
IndexLength: anyx.ConvInt64(re["INDEX_LENGTH"]),
|
||||
TableName: cast.ToString(re["TABLE_NAME"]),
|
||||
TableComment: cast.ToString(re["TABLE_COMMENT"]),
|
||||
CreateTime: cast.ToString(re["CREATE_TIME"]),
|
||||
TableRows: cast.ToInt(re["TABLE_ROWS"]),
|
||||
DataLength: cast.ToInt64(re["DATA_LENGTH"]),
|
||||
IndexLength: cast.ToInt64(re["INDEX_LENGTH"]),
|
||||
})
|
||||
}
|
||||
return tables, nil
|
||||
@@ -118,25 +120,23 @@ func (od *OracleMetaData) GetColumns(tableNames ...string) ([]dbi.Column, error)
|
||||
|
||||
columns := make([]dbi.Column, 0)
|
||||
for _, re := range res {
|
||||
defaultVal := anyx.ConvString(re["COLUMN_DEFAULT"])
|
||||
defaultVal := cast.ToString(re["COLUMN_DEFAULT"])
|
||||
// 如果默认值包含.nextval,说明是序列,默认值为null
|
||||
if strings.Contains(defaultVal, ".nextval") {
|
||||
defaultVal = ""
|
||||
}
|
||||
column := dbi.Column{
|
||||
TableName: anyx.ConvString(re["TABLE_NAME"]),
|
||||
ColumnName: anyx.ConvString(re["COLUMN_NAME"]),
|
||||
DataType: dbi.ColumnDataType(anyx.ConvString(re["DATA_TYPE"])),
|
||||
ColumnComment: anyx.ConvString(re["COLUMN_COMMENT"]),
|
||||
Nullable: anyx.ConvString(re["NULLABLE"]),
|
||||
IsPrimaryKey: anyx.ConvInt(re["IS_PRIMARY_KEY"]) == 1,
|
||||
IsIdentity: anyx.ConvInt(re["IS_IDENTITY"]) == 1,
|
||||
TableName: cast.ToString(re["TABLE_NAME"]),
|
||||
ColumnName: cast.ToString(re["COLUMN_NAME"]),
|
||||
DataType: dbi.ColumnDataType(cast.ToString(re["DATA_TYPE"])),
|
||||
ColumnComment: cast.ToString(re["COLUMN_COMMENT"]),
|
||||
Nullable: cast.ToString(re["NULLABLE"]),
|
||||
IsPrimaryKey: cast.ToInt(re["IS_PRIMARY_KEY"]) == 1,
|
||||
IsIdentity: cast.ToInt(re["IS_IDENTITY"]) == 1,
|
||||
ColumnDefault: defaultVal,
|
||||
NumScale: anyx.ConvInt(re["NUM_SCALE"]),
|
||||
NumScale: cast.ToInt(re["NUM_SCALE"]),
|
||||
}
|
||||
|
||||
// 初始化列展示的长度,精度
|
||||
column.InitShowNum()
|
||||
columns = append(columns, column)
|
||||
}
|
||||
return columns, nil
|
||||
@@ -169,12 +169,12 @@ func (od *OracleMetaData) GetTableIndex(tableName string) ([]dbi.Index, error) {
|
||||
indexs := make([]dbi.Index, 0)
|
||||
for _, re := range res {
|
||||
indexs = append(indexs, dbi.Index{
|
||||
IndexName: anyx.ConvString(re["INDEX_NAME"]),
|
||||
ColumnName: anyx.ConvString(re["COLUMN_NAME"]),
|
||||
IndexType: anyx.ConvString(re["INDEX_TYPE"]),
|
||||
IndexComment: anyx.ConvString(re["INDEX_COMMENT"]),
|
||||
IsUnique: anyx.ConvInt(re["IS_UNIQUE"]) == 1,
|
||||
SeqInIndex: anyx.ConvInt(re["SEQ_IN_INDEX"]),
|
||||
IndexName: cast.ToString(re["INDEX_NAME"]),
|
||||
ColumnName: cast.ToString(re["COLUMN_NAME"]),
|
||||
IndexType: cast.ToString(re["INDEX_TYPE"]),
|
||||
IndexComment: cast.ToString(re["INDEX_COMMENT"]),
|
||||
IsUnique: cast.ToInt(re["IS_UNIQUE"]) == 1,
|
||||
SeqInIndex: cast.ToInt(re["SEQ_IN_INDEX"]),
|
||||
})
|
||||
}
|
||||
// 把查询结果以索引名分组,索引字段以逗号连接
|
||||
@@ -271,7 +271,7 @@ func (od *OracleMetaData) genColumnBasicSql(column dbi.Column) string {
|
||||
if collx.ArrayAnyMatches([]string{"NUM", "INT"}, dataType) {
|
||||
match := bracketsRegexp.FindStringSubmatch(dataType)
|
||||
if len(match) > 1 {
|
||||
length := anyx.ConvInt(match[1])
|
||||
length := cast.ToInt(match[1])
|
||||
defVal = fmt.Sprintf(" DEFAULT %d", length)
|
||||
} else {
|
||||
defVal = fmt.Sprintf(" DEFAULT 0")
|
||||
@@ -282,7 +282,7 @@ func (od *OracleMetaData) genColumnBasicSql(column dbi.Column) string {
|
||||
}
|
||||
}
|
||||
|
||||
columnSql := fmt.Sprintf(" %s %s %s %s", colName, column.ShowDataType, defVal, nullAble)
|
||||
columnSql := fmt.Sprintf(" %s %s %s %s", colName, column.GetColumnType(), defVal, nullAble)
|
||||
return columnSql
|
||||
}
|
||||
|
||||
@@ -389,7 +389,7 @@ func (od *OracleMetaData) GetSchemas() ([]string, error) {
|
||||
}
|
||||
schemaNames := make([]string, 0)
|
||||
for _, re := range res {
|
||||
schemaNames = append(schemaNames, anyx.ConvString(re["USERNAME"]))
|
||||
schemaNames = append(schemaNames, cast.ToString(re["USERNAME"]))
|
||||
}
|
||||
return schemaNames, nil
|
||||
}
|
||||
@@ -485,7 +485,7 @@ func (dc *DataConverter) FormatData(dbColumnValue any, dataType dbi.DataType) st
|
||||
func (dc *DataConverter) ParseData(dbColumnValue any, dataType dbi.DataType) any {
|
||||
// oracle把日期类型的数据转化为time类型
|
||||
if dataType == dbi.DataTypeDateTime {
|
||||
res, _ := time.Parse(time.RFC3339, anyx.ConvString(dbColumnValue))
|
||||
res, _ := time.Parse(time.RFC3339, cast.ToString(dbColumnValue))
|
||||
return res
|
||||
}
|
||||
return dbColumnValue
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/may-fly/cast"
|
||||
)
|
||||
|
||||
type PgsqlDialect struct {
|
||||
@@ -141,7 +143,7 @@ func (pd *PgsqlDialect) CopyTable(copy *dbi.DbCopyTable) error {
|
||||
}
|
||||
|
||||
for _, re := range res {
|
||||
colName := anyx.ConvString(re["column_name"])
|
||||
colName := cast.ToString(re["column_name"])
|
||||
if colName != "" {
|
||||
|
||||
// 查询自增列当前最大值
|
||||
@@ -149,7 +151,7 @@ func (pd *PgsqlDialect) CopyTable(copy *dbi.DbCopyTable) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
maxVal := anyx.ConvInt(maxRes[0]["max_val"])
|
||||
maxVal := cast.ToInt(maxRes[0]["max_val"])
|
||||
// 序列起始值为1或当前最大值+1
|
||||
if maxVal <= 0 {
|
||||
maxVal = 1
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/may-fly/cast"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -33,7 +35,7 @@ func (pd *PgsqlMetaData) GetDbServer() (*dbi.DbServer, error) {
|
||||
return nil, err
|
||||
}
|
||||
ds := &dbi.DbServer{
|
||||
Version: anyx.ConvString(res[0]["server_version"]),
|
||||
Version: cast.ToString(res[0]["server_version"]),
|
||||
}
|
||||
return ds, nil
|
||||
}
|
||||
@@ -46,7 +48,7 @@ func (pd *PgsqlMetaData) GetDbNames() ([]string, error) {
|
||||
|
||||
databases := make([]string, 0)
|
||||
for _, re := range res {
|
||||
databases = append(databases, anyx.ConvString(re["dbname"]))
|
||||
databases = append(databases, cast.ToString(re["dbname"]))
|
||||
}
|
||||
|
||||
return databases, nil
|
||||
@@ -75,11 +77,11 @@ func (pd *PgsqlMetaData) GetTables(tableNames ...string) ([]dbi.Table, error) {
|
||||
for _, re := range res {
|
||||
tables = append(tables, dbi.Table{
|
||||
TableName: re["tableName"].(string),
|
||||
TableComment: anyx.ConvString(re["tableComment"]),
|
||||
CreateTime: anyx.ConvString(re["createTime"]),
|
||||
TableRows: anyx.ConvInt(re["tableRows"]),
|
||||
DataLength: anyx.ConvInt64(re["dataLength"]),
|
||||
IndexLength: anyx.ConvInt64(re["indexLength"]),
|
||||
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"]),
|
||||
})
|
||||
}
|
||||
return tables, nil
|
||||
@@ -100,21 +102,19 @@ func (pd *PgsqlMetaData) GetColumns(tableNames ...string) ([]dbi.Column, error)
|
||||
columns := make([]dbi.Column, 0)
|
||||
for _, re := range res {
|
||||
column := dbi.Column{
|
||||
TableName: anyx.ConvString(re["tableName"]),
|
||||
ColumnName: anyx.ConvString(re["columnName"]),
|
||||
DataType: dbi.ColumnDataType(anyx.ConvString(re["dataType"])),
|
||||
CharMaxLength: anyx.ConvInt(re["charMaxLength"]),
|
||||
ColumnComment: anyx.ConvString(re["columnComment"]),
|
||||
Nullable: anyx.ConvString(re["nullable"]),
|
||||
IsPrimaryKey: anyx.ConvInt(re["isPrimaryKey"]) == 1,
|
||||
IsIdentity: anyx.ConvInt(re["isIdentity"]) == 1,
|
||||
ColumnDefault: anyx.ConvString(re["columnDefault"]),
|
||||
NumPrecision: anyx.ConvInt(re["numPrecision"]),
|
||||
NumScale: anyx.ConvInt(re["numScale"]),
|
||||
TableName: cast.ToString(re["tableName"]),
|
||||
ColumnName: cast.ToString(re["columnName"]),
|
||||
DataType: dbi.ColumnDataType(cast.ToString(re["dataType"])),
|
||||
CharMaxLength: cast.ToInt(re["charMaxLength"]),
|
||||
ColumnComment: cast.ToString(re["columnComment"]),
|
||||
Nullable: cast.ToString(re["nullable"]),
|
||||
IsPrimaryKey: cast.ToInt(re["isPrimaryKey"]) == 1,
|
||||
IsIdentity: cast.ToInt(re["isIdentity"]) == 1,
|
||||
ColumnDefault: cast.ToString(re["columnDefault"]),
|
||||
NumPrecision: cast.ToInt(re["numPrecision"]),
|
||||
NumScale: cast.ToInt(re["numScale"]),
|
||||
}
|
||||
|
||||
// 初始化列展示的长度,精度
|
||||
column.InitShowNum()
|
||||
columns = append(columns, column)
|
||||
}
|
||||
return columns, nil
|
||||
@@ -147,12 +147,12 @@ func (pd *PgsqlMetaData) GetTableIndex(tableName string) ([]dbi.Index, error) {
|
||||
indexs := make([]dbi.Index, 0)
|
||||
for _, re := range res {
|
||||
indexs = append(indexs, dbi.Index{
|
||||
IndexName: anyx.ConvString(re["indexName"]),
|
||||
ColumnName: anyx.ConvString(re["columnName"]),
|
||||
IndexType: anyx.ConvString(re["IndexType"]),
|
||||
IndexComment: anyx.ConvString(re["indexComment"]),
|
||||
IsUnique: anyx.ConvInt(re["isUnique"]) == 1,
|
||||
SeqInIndex: anyx.ConvInt(re["seqInIndex"]),
|
||||
IndexName: cast.ToString(re["indexName"]),
|
||||
ColumnName: cast.ToString(re["columnName"]),
|
||||
IndexType: cast.ToString(re["IndexType"]),
|
||||
IndexComment: cast.ToString(re["indexComment"]),
|
||||
IsUnique: cast.ToInt(re["isUnique"]) == 1,
|
||||
SeqInIndex: cast.ToInt(re["seqInIndex"]),
|
||||
})
|
||||
}
|
||||
// 把查询结果以索引名分组,索引字段以逗号连接
|
||||
@@ -234,7 +234,7 @@ func (pd *PgsqlMetaData) genColumnBasicSql(column dbi.Column) string {
|
||||
column.DataType = "bigserial"
|
||||
}
|
||||
|
||||
return fmt.Sprintf(" %s %s NOT NULL", colName, column.ShowDataType)
|
||||
return fmt.Sprintf(" %s %s NOT NULL", colName, column.GetColumnType())
|
||||
}
|
||||
|
||||
nullAble := ""
|
||||
@@ -284,7 +284,7 @@ func (pd *PgsqlMetaData) genColumnBasicSql(column dbi.Column) string {
|
||||
}
|
||||
}
|
||||
|
||||
columnSql := fmt.Sprintf(" %s %s %s %s ", colName, column.ShowDataType, nullAble, defVal)
|
||||
columnSql := fmt.Sprintf(" %s %s %s %s ", colName, column.GetColumnType(), nullAble, defVal)
|
||||
return columnSql
|
||||
}
|
||||
|
||||
@@ -386,7 +386,7 @@ func (pd *PgsqlMetaData) GetSchemas() ([]string, error) {
|
||||
}
|
||||
schemaNames := make([]string, 0)
|
||||
for _, re := range res {
|
||||
schemaNames = append(schemaNames, anyx.ConvString(re["schemaName"]))
|
||||
schemaNames = append(schemaNames, cast.ToString(re["schemaName"]))
|
||||
}
|
||||
return schemaNames, nil
|
||||
}
|
||||
@@ -506,7 +506,7 @@ func (dc *DataConverter) FormatData(dbColumnValue any, dataType dbi.DataType) st
|
||||
res, _ := time.Parse(time.RFC3339, str)
|
||||
return res.Format(time.TimeOnly)
|
||||
}
|
||||
return anyx.ConvString(dbColumnValue)
|
||||
return cast.ToString(dbColumnValue)
|
||||
}
|
||||
|
||||
func (dc *DataConverter) ParseData(dbColumnValue any, dataType dbi.DataType) any {
|
||||
|
||||
@@ -127,7 +127,3 @@ func (sd *SqliteDialect) CreateIndex(tableInfo dbi.Table, indexs []dbi.Index) er
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sd *SqliteDialect) UpdateSequence(tableName string, columns []dbi.Column) {
|
||||
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/may-fly/cast"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -31,7 +33,7 @@ func (sd *SqliteMetaData) GetDbServer() (*dbi.DbServer, error) {
|
||||
return nil, err
|
||||
}
|
||||
ds := &dbi.DbServer{
|
||||
Version: anyx.ConvString(res[0]["version"]),
|
||||
Version: cast.ToString(res[0]["version"]),
|
||||
}
|
||||
return ds, nil
|
||||
}
|
||||
@@ -43,7 +45,7 @@ func (sd *SqliteMetaData) GetDbNames() ([]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
for _, re := range res {
|
||||
databases = append(databases, anyx.ConvString(re["name"]))
|
||||
databases = append(databases, cast.ToString(re["name"]))
|
||||
}
|
||||
|
||||
return databases, nil
|
||||
@@ -71,12 +73,12 @@ func (sd *SqliteMetaData) GetTables(tableNames ...string) ([]dbi.Table, error) {
|
||||
tables := make([]dbi.Table, 0)
|
||||
for _, re := range res {
|
||||
tables = append(tables, dbi.Table{
|
||||
TableName: anyx.ConvString(re["tableName"]),
|
||||
TableComment: anyx.ConvString(re["tableComment"]),
|
||||
CreateTime: anyx.ConvString(re["createTime"]),
|
||||
TableRows: anyx.ConvInt(re["tableRows"]),
|
||||
DataLength: anyx.ConvInt64(re["dataLength"]),
|
||||
IndexLength: anyx.ConvInt64(re["indexLength"]),
|
||||
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"]),
|
||||
})
|
||||
}
|
||||
return tables, nil
|
||||
@@ -108,40 +110,38 @@ func (sd *SqliteMetaData) GetColumns(tableNames ...string) ([]dbi.Column, error)
|
||||
}
|
||||
for _, re := range res {
|
||||
nullable := "YES"
|
||||
if anyx.ConvInt(re["notnull"]) == 1 {
|
||||
if cast.ToInt(re["notnull"]) == 1 {
|
||||
nullable = "NO"
|
||||
}
|
||||
// 去掉默认值的引号
|
||||
defaultValue := anyx.ConvString(re["dflt_value"])
|
||||
defaultValue := cast.ToString(re["dflt_value"])
|
||||
if strings.Contains(defaultValue, "'") {
|
||||
defaultValue = strings.ReplaceAll(defaultValue, "'", "")
|
||||
}
|
||||
|
||||
column := dbi.Column{
|
||||
TableName: tableName,
|
||||
ColumnName: anyx.ConvString(re["name"]),
|
||||
ColumnName: cast.ToString(re["name"]),
|
||||
ColumnComment: "",
|
||||
Nullable: nullable,
|
||||
IsPrimaryKey: anyx.ConvInt(re["pk"]) == 1,
|
||||
IsIdentity: anyx.ConvInt(re["pk"]) == 1,
|
||||
IsPrimaryKey: cast.ToInt(re["pk"]) == 1,
|
||||
IsIdentity: cast.ToInt(re["pk"]) == 1,
|
||||
ColumnDefault: defaultValue,
|
||||
NumScale: 0,
|
||||
}
|
||||
|
||||
// 切割类型和长度,如果长度内有逗号,则说明是decimal类型
|
||||
columnType := anyx.ConvString(re["type"])
|
||||
columnType := cast.ToString(re["type"])
|
||||
dataType, length, scale := sd.getDataTypes(columnType)
|
||||
if scale != "0" && scale != "" {
|
||||
column.NumPrecision = anyx.ConvInt(length)
|
||||
column.NumScale = anyx.ConvInt(scale)
|
||||
column.NumPrecision = cast.ToInt(length)
|
||||
column.NumScale = cast.ToInt(scale)
|
||||
column.CharMaxLength = 0
|
||||
} else {
|
||||
column.CharMaxLength = anyx.ConvInt(length)
|
||||
column.CharMaxLength = cast.ToInt(length)
|
||||
}
|
||||
column.DataType = dbi.ColumnDataType(dataType)
|
||||
|
||||
// 初始化列展示的长度,精度
|
||||
column.InitShowNum()
|
||||
columns = append(columns, column)
|
||||
}
|
||||
}
|
||||
@@ -154,8 +154,8 @@ func (sd *SqliteMetaData) GetPrimaryKey(tableName string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
for _, re := range res {
|
||||
if anyx.ConvInt(re["pk"]) == 1 {
|
||||
return anyx.ConvString(re["name"]), nil
|
||||
if cast.ToInt(re["pk"]) == 1 {
|
||||
return cast.ToString(re["name"]), nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,14 +187,14 @@ func (sd *SqliteMetaData) GetTableIndex(tableName string) ([]dbi.Index, error) {
|
||||
|
||||
indexs := make([]dbi.Index, 0)
|
||||
for _, re := range res {
|
||||
indexSql := anyx.ConvString(re["indexSql"])
|
||||
indexSql := cast.ToString(re["indexSql"])
|
||||
isUnique := strings.Contains(indexSql, "CREATE UNIQUE INDEX")
|
||||
|
||||
indexs = append(indexs, dbi.Index{
|
||||
IndexName: anyx.ConvString(re["indexName"]),
|
||||
IndexName: cast.ToString(re["indexName"]),
|
||||
ColumnName: extractIndexFields(indexSql),
|
||||
IndexType: anyx.ConvString(re["indexType"]),
|
||||
IndexComment: anyx.ConvString(re["indexComment"]),
|
||||
IndexType: cast.ToString(re["indexType"]),
|
||||
IndexComment: cast.ToString(re["indexComment"]),
|
||||
IsUnique: isUnique,
|
||||
SeqInIndex: 1,
|
||||
})
|
||||
@@ -262,7 +262,7 @@ func (sd *SqliteMetaData) genColumnBasicSql(column dbi.Column) string {
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Sprintf(" %s %s %s %s", sd.dc.GetMetaData().QuoteIdentifier(column.ColumnName), column.ShowDataType, nullAble, defVal)
|
||||
return fmt.Sprintf(" %s %s %s %s", sd.dc.GetMetaData().QuoteIdentifier(column.ColumnName), column.GetColumnType(), nullAble, defVal)
|
||||
}
|
||||
|
||||
// 获取建表ddl
|
||||
@@ -296,7 +296,7 @@ func (sd *SqliteMetaData) GetTableDDL(tableName string) (string, error) {
|
||||
}
|
||||
var builder strings.Builder
|
||||
for _, re := range res {
|
||||
builder.WriteString(anyx.ConvString(re["sql"]) + "; \n\n")
|
||||
builder.WriteString(cast.ToString(re["sql"]) + "; \n\n")
|
||||
}
|
||||
|
||||
return builder.String(), nil
|
||||
|
||||
Reference in New Issue
Block a user