mirror of
https://gitee.com/dromara/mayfly-go
synced 2026-01-03 05:06:36 +08:00
refactor: dbm重构、调整metadata与dialect接口
This commit is contained in:
@@ -40,7 +40,7 @@ func (dd *DMDialect) batchInsertSimple(tx *sql.Tx, tableName string, columns []s
|
||||
|
||||
identityInsert := fmt.Sprintf("set identity_insert \"%s\" on;", tableName)
|
||||
|
||||
sqlTemp := fmt.Sprintf("%s insert into %s (%s) values %s", identityInsert, dd.dc.GetMetaData().QuoteIdentifier(tableName), strings.Join(columns, ","), placeholder)
|
||||
sqlTemp := fmt.Sprintf("%s insert into %s (%s) values %s", identityInsert, dd.QuoteIdentifier(tableName), strings.Join(columns, ","), placeholder)
|
||||
effRows := 0
|
||||
// 设置允许填充自增列之后,显示指定列名可以插入自增列
|
||||
for _, value := range values {
|
||||
@@ -60,17 +60,17 @@ func (dd *DMDialect) batchInsertMerge(tx *sql.Tx, tableName string, columns []st
|
||||
// 查询主键字段
|
||||
uniqueCols := make([]string, 0)
|
||||
caseSqls := make([]string, 0)
|
||||
metadata := dd.dc.GetMetaData()
|
||||
metadata := dd.dc.GetMetadata()
|
||||
tableCols, _ := metadata.GetColumns(tableName)
|
||||
identityCols := make([]string, 0)
|
||||
for _, col := range tableCols {
|
||||
if col.IsPrimaryKey {
|
||||
uniqueCols = append(uniqueCols, col.ColumnName)
|
||||
caseSqls = append(caseSqls, fmt.Sprintf("( T1.%s = T2.%s )", metadata.QuoteIdentifier(col.ColumnName), metadata.QuoteIdentifier(col.ColumnName)))
|
||||
caseSqls = append(caseSqls, fmt.Sprintf("( T1.%s = T2.%s )", dd.QuoteIdentifier(col.ColumnName), dd.QuoteIdentifier(col.ColumnName)))
|
||||
}
|
||||
if col.IsIdentity {
|
||||
// 自增字段不放入insert内,即使是设置了identity_insert on也不起作用
|
||||
identityCols = append(identityCols, metadata.QuoteIdentifier(col.ColumnName))
|
||||
identityCols = append(identityCols, dd.QuoteIdentifier(col.ColumnName))
|
||||
}
|
||||
}
|
||||
// 查询唯一索引涉及到的字段,并组装到match条件内
|
||||
@@ -81,7 +81,7 @@ func (dd *DMDialect) batchInsertMerge(tx *sql.Tx, tableName string, columns []st
|
||||
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)))
|
||||
tmp = append(tmp, fmt.Sprintf(" T1.%s = T2.%s ", dd.QuoteIdentifier(col), dd.QuoteIdentifier(col)))
|
||||
}
|
||||
caseSqls = append(caseSqls, fmt.Sprintf("( %s )", strings.Join(tmp, " AND ")))
|
||||
}
|
||||
@@ -94,7 +94,7 @@ func (dd *DMDialect) batchInsertMerge(tx *sql.Tx, tableName string, columns []st
|
||||
insertCols := make([]string, 0)
|
||||
for _, column := range columns {
|
||||
phs = append(phs, fmt.Sprintf("? %s", column))
|
||||
if !collx.ArrayContains(uniqueCols, metadata.RemoveQuote(column)) {
|
||||
if !collx.ArrayContains(uniqueCols, dd.RemoveQuote(column)) {
|
||||
upds = append(upds, fmt.Sprintf("T1.%s = T2.%s", column, column))
|
||||
}
|
||||
if !collx.ArrayContains(identityCols, column) {
|
||||
@@ -109,7 +109,7 @@ func (dd *DMDialect) batchInsertMerge(tx *sql.Tx, tableName string, columns []st
|
||||
}
|
||||
t2 := strings.Join(t2s, " UNION ALL ")
|
||||
|
||||
sqlTemp := "MERGE INTO " + metadata.QuoteIdentifier(tableName) + " T1 USING (" + t2 + ") T2 ON " + strings.Join(caseSqls, " OR ")
|
||||
sqlTemp := "MERGE INTO " + dd.QuoteIdentifier(tableName) + " T1 USING (" + t2 + ") T2 ON " + strings.Join(caseSqls, " OR ")
|
||||
sqlTemp += "WHEN NOT MATCHED THEN INSERT (" + strings.Join(insertCols, ",") + ") VALUES (" + strings.Join(insertVals, ",") + ")"
|
||||
sqlTemp += "WHEN MATCHED THEN UPDATE SET " + strings.Join(upds, ",")
|
||||
|
||||
@@ -124,7 +124,7 @@ func (dd *DMDialect) batchInsertMerge(tx *sql.Tx, tableName string, columns []st
|
||||
|
||||
func (dd *DMDialect) CopyTable(copy *dbi.DbCopyTable) error {
|
||||
tableName := copy.TableName
|
||||
metadata := dd.dc.GetMetaData()
|
||||
metadata := dd.dc.GetMetadata()
|
||||
ddl, err := metadata.GetTableDDL(tableName, false)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -162,32 +162,119 @@ func (dd *DMDialect) CopyTable(copy *dbi.DbCopyTable) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (dd *DMDialect) CreateTable(columns []dbi.Column, tableInfo dbi.Table, dropOldTable bool) (int, error) {
|
||||
func (dd *DMDialect) GenerateTableDDL(columns []dbi.Column, tableInfo dbi.Table, dropBeforeCreate bool) []string {
|
||||
tbName := dd.QuoteIdentifier(tableInfo.TableName)
|
||||
sqlArr := make([]string, 0)
|
||||
|
||||
sqlArr := dd.dc.GetMetaData().GenerateTableDDL(columns, tableInfo, dropOldTable)
|
||||
// 达梦需要分开执行sql
|
||||
if len(sqlArr) > 0 {
|
||||
for _, sqlStr := range sqlArr {
|
||||
_, err := dd.dc.Exec(sqlStr)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
if dropBeforeCreate {
|
||||
sqlArr = append(sqlArr, fmt.Sprintf("drop table if exists %s", tbName))
|
||||
}
|
||||
// 组装建表语句
|
||||
createSql := fmt.Sprintf("create table %s (", tbName)
|
||||
fields := make([]string, 0)
|
||||
pks := make([]string, 0)
|
||||
columnComments := make([]string, 0)
|
||||
|
||||
for _, column := range columns {
|
||||
if column.IsPrimaryKey {
|
||||
pks = append(pks, dd.QuoteIdentifier(column.ColumnName))
|
||||
}
|
||||
fields = append(fields, dd.genColumnBasicSql(column))
|
||||
if column.ColumnComment != "" {
|
||||
comment := dd.QuoteEscape(column.ColumnComment)
|
||||
columnComments = append(columnComments, fmt.Sprintf("comment on column %s.%s is '%s'", tbName, dd.QuoteIdentifier(column.ColumnName), comment))
|
||||
}
|
||||
}
|
||||
createSql += strings.Join(fields, ",\n")
|
||||
if len(pks) > 0 {
|
||||
createSql += fmt.Sprintf(",\n PRIMARY KEY (%s)", strings.Join(pks, ","))
|
||||
}
|
||||
createSql += "\n)"
|
||||
|
||||
tableCommentSql := ""
|
||||
if tableInfo.TableComment != "" {
|
||||
comment := dd.QuoteEscape(tableInfo.TableComment)
|
||||
tableCommentSql = fmt.Sprintf("comment on table %s is '%s'", tbName, comment)
|
||||
}
|
||||
|
||||
sqlArr = append(sqlArr, createSql)
|
||||
if tableCommentSql != "" {
|
||||
sqlArr = append(sqlArr, tableCommentSql)
|
||||
}
|
||||
|
||||
if len(columnComments) > 0 {
|
||||
sqlArr = append(sqlArr, columnComments...)
|
||||
}
|
||||
|
||||
return sqlArr
|
||||
}
|
||||
|
||||
func (dd *DMDialect) GenerateIndexDDL(indexs []dbi.Index, tableInfo dbi.Table) []string {
|
||||
sqls := make([]string, 0)
|
||||
for _, index := range indexs {
|
||||
unique := ""
|
||||
if index.IsUnique {
|
||||
unique = "unique"
|
||||
}
|
||||
|
||||
// 取出列名,添加引号
|
||||
cols := strings.Split(index.ColumnName, ",")
|
||||
colNames := make([]string, len(cols))
|
||||
for i, name := range cols {
|
||||
colNames[i] = dd.QuoteIdentifier(name)
|
||||
}
|
||||
|
||||
sqls = append(sqls, fmt.Sprintf("create %s index %s on %s(%s)", unique, dd.QuoteIdentifier(index.IndexName), dd.QuoteIdentifier(tableInfo.TableName), strings.Join(colNames, ",")))
|
||||
}
|
||||
return sqls
|
||||
}
|
||||
|
||||
func (dd *DMDialect) GetDataHelper() dbi.DataHelper {
|
||||
return new(DataHelper)
|
||||
}
|
||||
|
||||
func (dd *DMDialect) GetColumnHelper() dbi.ColumnHelper {
|
||||
return new(ColumnHelper)
|
||||
}
|
||||
|
||||
func (dd *DMDialect) GetDumpHelper() dbi.DumpHelper {
|
||||
return new(DumpHelper)
|
||||
}
|
||||
|
||||
func (dd *DMDialect) genColumnBasicSql(column dbi.Column) string {
|
||||
colName := dd.QuoteIdentifier(column.ColumnName)
|
||||
dataType := string(column.DataType)
|
||||
|
||||
incr := ""
|
||||
if column.IsIdentity {
|
||||
incr = " IDENTITY"
|
||||
}
|
||||
|
||||
nullAble := ""
|
||||
if !column.Nullable {
|
||||
nullAble = " NOT NULL"
|
||||
}
|
||||
|
||||
defVal := "" // 默认值需要判断引号,如函数是不需要引号的 // 为了防止跨源函数不支持 当默认值是函数时,不需要设置默认值
|
||||
if column.ColumnDefault != "" && !strings.Contains(column.ColumnDefault, "(") {
|
||||
// 哪些字段类型默认值需要加引号
|
||||
mark := false
|
||||
if collx.ArrayAnyMatches([]string{"char", "text", "date", "time", "lob"}, strings.ToLower(dataType)) {
|
||||
// 当数据类型是日期时间,默认值是日期时间函数时,默认值不需要引号
|
||||
if collx.ArrayAnyMatches([]string{"date", "time"}, strings.ToLower(dataType)) &&
|
||||
collx.ArrayAnyMatches([]string{"DATE", "TIME"}, strings.ToUpper(column.ColumnDefault)) {
|
||||
mark = false
|
||||
} else {
|
||||
mark = true
|
||||
}
|
||||
}
|
||||
if mark {
|
||||
defVal = fmt.Sprintf(" DEFAULT '%s'", column.ColumnDefault)
|
||||
} else {
|
||||
defVal = fmt.Sprintf(" DEFAULT %s", column.ColumnDefault)
|
||||
}
|
||||
}
|
||||
|
||||
return len(sqlArr), nil
|
||||
}
|
||||
|
||||
func (dd *DMDialect) CreateIndex(tableInfo dbi.Table, indexs []dbi.Index) error {
|
||||
sqlArr := dd.dc.GetMetaData().GenerateIndexDDL(indexs, tableInfo)
|
||||
// 达梦需要分开执行sql
|
||||
if len(sqlArr) > 0 {
|
||||
for _, sqlStr := range sqlArr {
|
||||
_, err := dd.dc.Exec(sqlStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
columnSql := fmt.Sprintf(" %s %s%s%s%s", colName, column.GetColumnType(), incr, nullAble, defVal)
|
||||
return columnSql
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user