mirror of
				https://gitee.com/dromara/mayfly-go
				synced 2025-11-04 16:30:25 +08:00 
			
		
		
		
	* feat: 优化数据库 BINLOG 同步机制 * feat: 删除数据库实例前需删除关联的数据库备份与恢复任务 * refactor: 重构数据库备份与恢复模块 * feat: 定时清理数据库备份历史和本地 Binlog 文件 * feat: 压缩数据库备份文件
		
			
				
	
	
		
			339 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			339 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package postgres
 | 
						||
 | 
						||
import (
 | 
						||
	"database/sql"
 | 
						||
	"fmt"
 | 
						||
	"mayfly-go/internal/db/dbm/dbi"
 | 
						||
	"mayfly-go/pkg/errorx"
 | 
						||
	"mayfly-go/pkg/utils/anyx"
 | 
						||
	"mayfly-go/pkg/utils/collx"
 | 
						||
	"regexp"
 | 
						||
	"strings"
 | 
						||
	"time"
 | 
						||
)
 | 
						||
 | 
						||
const (
 | 
						||
	PGSQL_META_FILE      = "metasql/pgsql_meta.sql"
 | 
						||
	PGSQL_DB_SCHEMAS     = "PGSQL_DB_SCHEMAS"
 | 
						||
	PGSQL_TABLE_INFO_KEY = "PGSQL_TABLE_INFO"
 | 
						||
	PGSQL_INDEX_INFO_KEY = "PGSQL_INDEX_INFO"
 | 
						||
	PGSQL_COLUMN_MA_KEY  = "PGSQL_COLUMN_MA"
 | 
						||
	PGSQL_TABLE_DDL_KEY  = "PGSQL_TABLE_DDL_FUNC"
 | 
						||
)
 | 
						||
 | 
						||
type PgsqlDialect struct {
 | 
						||
	dc *dbi.DbConn
 | 
						||
}
 | 
						||
 | 
						||
func (md *PgsqlDialect) GetDbServer() (*dbi.DbServer, error) {
 | 
						||
	_, res, err := md.dc.Query("SHOW server_version")
 | 
						||
	if err != nil {
 | 
						||
		return nil, err
 | 
						||
	}
 | 
						||
	ds := &dbi.DbServer{
 | 
						||
		Version: anyx.ConvString(res[0]["server_version"]),
 | 
						||
	}
 | 
						||
	return ds, nil
 | 
						||
}
 | 
						||
 | 
						||
func (md *PgsqlDialect) GetDbNames() ([]string, error) {
 | 
						||
	_, res, err := md.dc.Query("SELECT datname AS dbname FROM pg_database WHERE datistemplate = false AND has_database_privilege(datname, 'CONNECT')")
 | 
						||
	if err != nil {
 | 
						||
		return nil, err
 | 
						||
	}
 | 
						||
 | 
						||
	databases := make([]string, 0)
 | 
						||
	for _, re := range res {
 | 
						||
		databases = append(databases, anyx.ConvString(re["dbname"]))
 | 
						||
	}
 | 
						||
 | 
						||
	return databases, nil
 | 
						||
}
 | 
						||
 | 
						||
// 获取表基础元信息, 如表名等
 | 
						||
func (md *PgsqlDialect) GetTables() ([]dbi.Table, error) {
 | 
						||
	_, res, err := md.dc.Query(dbi.GetLocalSql(PGSQL_META_FILE, PGSQL_TABLE_INFO_KEY))
 | 
						||
	if err != nil {
 | 
						||
		return nil, err
 | 
						||
	}
 | 
						||
 | 
						||
	tables := make([]dbi.Table, 0)
 | 
						||
	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"]),
 | 
						||
		})
 | 
						||
	}
 | 
						||
	return tables, nil
 | 
						||
}
 | 
						||
 | 
						||
// 获取列元信息, 如列名等
 | 
						||
func (md *PgsqlDialect) GetColumns(tableNames ...string) ([]dbi.Column, error) {
 | 
						||
	dbType := md.dc.Info.Type
 | 
						||
	tableName := strings.Join(collx.ArrayMap[string, string](tableNames, func(val string) string {
 | 
						||
		return fmt.Sprintf("'%s'", dbType.RemoveQuote(val))
 | 
						||
	}), ",")
 | 
						||
 | 
						||
	_, res, err := md.dc.Query(fmt.Sprintf(dbi.GetLocalSql(PGSQL_META_FILE, PGSQL_COLUMN_MA_KEY), tableName))
 | 
						||
	if err != nil {
 | 
						||
		return nil, err
 | 
						||
	}
 | 
						||
 | 
						||
	columns := make([]dbi.Column, 0)
 | 
						||
	for _, re := range res {
 | 
						||
		columns = append(columns, dbi.Column{
 | 
						||
			TableName:     anyx.ConvString(re["tableName"]),
 | 
						||
			ColumnName:    anyx.ConvString(re["columnName"]),
 | 
						||
			ColumnType:    anyx.ConvString(re["columnType"]),
 | 
						||
			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"]),
 | 
						||
			NumScale:      anyx.ConvString(re["numScale"]),
 | 
						||
		})
 | 
						||
	}
 | 
						||
	return columns, nil
 | 
						||
}
 | 
						||
 | 
						||
func (md *PgsqlDialect) GetPrimaryKey(tablename string) (string, error) {
 | 
						||
	columns, err := md.GetColumns(tablename)
 | 
						||
	if err != nil {
 | 
						||
		return "", err
 | 
						||
	}
 | 
						||
	if len(columns) == 0 {
 | 
						||
		return "", errorx.NewBiz("[%s] 表不存在", tablename)
 | 
						||
	}
 | 
						||
	for _, v := range columns {
 | 
						||
		if v.IsPrimaryKey {
 | 
						||
			return v.ColumnName, nil
 | 
						||
		}
 | 
						||
	}
 | 
						||
 | 
						||
	return columns[0].ColumnName, nil
 | 
						||
}
 | 
						||
 | 
						||
// 获取表索引信息
 | 
						||
func (md *PgsqlDialect) GetTableIndex(tableName string) ([]dbi.Index, error) {
 | 
						||
	_, res, err := md.dc.Query(fmt.Sprintf(dbi.GetLocalSql(PGSQL_META_FILE, PGSQL_INDEX_INFO_KEY), tableName))
 | 
						||
	if err != nil {
 | 
						||
		return nil, 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"]),
 | 
						||
		})
 | 
						||
	}
 | 
						||
	// 把查询结果以索引名分组,索引字段以逗号连接
 | 
						||
	result := make([]dbi.Index, 0)
 | 
						||
	key := ""
 | 
						||
	for _, v := range indexs {
 | 
						||
		// 当前的索引名
 | 
						||
		in := v.IndexName
 | 
						||
		if key == in {
 | 
						||
			// 索引字段已根据名称和顺序排序,故取最后一个即可
 | 
						||
			i := len(result) - 1
 | 
						||
			// 同索引字段以逗号连接
 | 
						||
			result[i].ColumnName = result[i].ColumnName + "," + v.ColumnName
 | 
						||
		} else {
 | 
						||
			key = in
 | 
						||
			result = append(result, v)
 | 
						||
		}
 | 
						||
	}
 | 
						||
	return result, nil
 | 
						||
}
 | 
						||
 | 
						||
// 获取建表ddl
 | 
						||
func (md *PgsqlDialect) GetTableDDL(tableName string) (string, error) {
 | 
						||
	_, err := md.dc.Exec(dbi.GetLocalSql(PGSQL_META_FILE, PGSQL_TABLE_DDL_KEY))
 | 
						||
	if err != nil {
 | 
						||
		return "", err
 | 
						||
	}
 | 
						||
 | 
						||
	_, schemaRes, _ := md.dc.Query("select current_schema() as schema")
 | 
						||
	schemaName := schemaRes[0]["schema"].(string)
 | 
						||
 | 
						||
	ddlSql := fmt.Sprintf("select showcreatetable('%s','%s') as sql", schemaName, tableName)
 | 
						||
	_, res, err := md.dc.Query(ddlSql)
 | 
						||
	if err != nil {
 | 
						||
		return "", err
 | 
						||
	}
 | 
						||
 | 
						||
	return res[0]["sql"].(string), nil
 | 
						||
}
 | 
						||
 | 
						||
// 获取pgsql当前连接的库可访问的schemaNames
 | 
						||
func (md *PgsqlDialect) GetSchemas() ([]string, error) {
 | 
						||
	sql := dbi.GetLocalSql(PGSQL_META_FILE, PGSQL_DB_SCHEMAS)
 | 
						||
	_, res, err := md.dc.Query(sql)
 | 
						||
	if err != nil {
 | 
						||
		return nil, err
 | 
						||
	}
 | 
						||
	schemaNames := make([]string, 0)
 | 
						||
	for _, re := range res {
 | 
						||
		schemaNames = append(schemaNames, anyx.ConvString(re["schemaName"]))
 | 
						||
	}
 | 
						||
	return schemaNames, nil
 | 
						||
}
 | 
						||
 | 
						||
// GetDbProgram 获取数据库程序模块,用于数据库备份与恢复
 | 
						||
func (md *PgsqlDialect) GetDbProgram() (dbi.DbProgram, error) {
 | 
						||
	return nil, fmt.Errorf("该数据库类型不支持数据库备份与恢复: %v", md.dc.Info.Type)
 | 
						||
}
 | 
						||
 | 
						||
func (md *PgsqlDialect) BatchInsert(tx *sql.Tx, tableName string, columns []string, values [][]any) (int64, error) {
 | 
						||
	// 执行批量insert sql,跟mysql一样  pg或高斯支持批量insert语法
 | 
						||
	// insert into table_name (column1, column2, ...) values (value1, value2, ...), (value1, value2, ...), ...
 | 
						||
 | 
						||
	// 把二维数组转为一维数组
 | 
						||
	var args []any
 | 
						||
	for _, v := range values {
 | 
						||
		args = append(args, v...)
 | 
						||
	}
 | 
						||
 | 
						||
	// 构建占位符字符串 "($1, $2, $3), ($4, $5, $6), ..." 用于指定参数
 | 
						||
	var placeholders []string
 | 
						||
	for i := 0; i < len(args); i += len(columns) {
 | 
						||
		var placeholder []string
 | 
						||
		for j := 0; j < len(columns); j++ {
 | 
						||
			placeholder = append(placeholder, fmt.Sprintf("$%d", i+j+1))
 | 
						||
		}
 | 
						||
		placeholders = append(placeholders, "("+strings.Join(placeholder, ", ")+")")
 | 
						||
	}
 | 
						||
 | 
						||
	sqlStr := fmt.Sprintf("insert into %s (%s) values %s", md.dc.Info.Type.QuoteIdentifier(tableName), strings.Join(columns, ","), strings.Join(placeholders, ", "))
 | 
						||
	// 执行批量insert sql
 | 
						||
 | 
						||
	return md.dc.TxExec(tx, sqlStr, args...)
 | 
						||
}
 | 
						||
 | 
						||
func (md *PgsqlDialect) GetDataConverter() dbi.DataConverter {
 | 
						||
	return new(DataConverter)
 | 
						||
}
 | 
						||
 | 
						||
var (
 | 
						||
	// 数字类型
 | 
						||
	numberRegexp = regexp.MustCompile(`(?i)int|double|float|number|decimal|byte|bit`)
 | 
						||
	// 日期时间类型
 | 
						||
	datetimeRegexp = regexp.MustCompile(`(?i)datetime|timestamp`)
 | 
						||
	// 日期类型
 | 
						||
	dateRegexp = regexp.MustCompile(`(?i)date`)
 | 
						||
	// 时间类型
 | 
						||
	timeRegexp = regexp.MustCompile(`(?i)time`)
 | 
						||
)
 | 
						||
 | 
						||
type DataConverter struct {
 | 
						||
}
 | 
						||
 | 
						||
func (dc *DataConverter) GetDataType(dbColumnType string) dbi.DataType {
 | 
						||
	if numberRegexp.MatchString(dbColumnType) {
 | 
						||
		return dbi.DataTypeNumber
 | 
						||
	}
 | 
						||
	// 日期时间类型
 | 
						||
	if datetimeRegexp.MatchString(dbColumnType) {
 | 
						||
		return dbi.DataTypeDateTime
 | 
						||
	}
 | 
						||
	// 日期类型
 | 
						||
	if dateRegexp.MatchString(dbColumnType) {
 | 
						||
		return dbi.DataTypeDate
 | 
						||
	}
 | 
						||
	// 时间类型
 | 
						||
	if timeRegexp.MatchString(dbColumnType) {
 | 
						||
		return dbi.DataTypeTime
 | 
						||
	}
 | 
						||
	return dbi.DataTypeString
 | 
						||
}
 | 
						||
 | 
						||
func (dc *DataConverter) FormatData(dbColumnValue any, dataType dbi.DataType) string {
 | 
						||
	str := fmt.Sprintf("%v", dbColumnValue)
 | 
						||
	switch dataType {
 | 
						||
	case dbi.DataTypeDateTime: // "2024-01-02T22:16:28.545377+08:00"
 | 
						||
		res, _ := time.Parse(time.RFC3339, str)
 | 
						||
		return res.Format(time.DateTime)
 | 
						||
	case dbi.DataTypeDate: //  "2024-01-02T00:00:00Z"
 | 
						||
		res, _ := time.Parse(time.RFC3339, str)
 | 
						||
		return res.Format(time.DateOnly)
 | 
						||
	case dbi.DataTypeTime: // "0000-01-01T22:16:28.545075+08:00"
 | 
						||
		res, _ := time.Parse(time.RFC3339, str)
 | 
						||
		return res.Format(time.TimeOnly)
 | 
						||
	}
 | 
						||
	return anyx.ConvString(dbColumnValue)
 | 
						||
}
 | 
						||
 | 
						||
func (dc *DataConverter) ParseData(dbColumnValue any, dataType dbi.DataType) any {
 | 
						||
	return dbColumnValue
 | 
						||
}
 | 
						||
 | 
						||
func (md *PgsqlDialect) IsGauss() bool {
 | 
						||
	return strings.Contains(md.dc.Info.Params, "gauss")
 | 
						||
}
 | 
						||
 | 
						||
func (md *PgsqlDialect) CopyTable(copy *dbi.DbCopyTable) error {
 | 
						||
	tableName := copy.TableName
 | 
						||
	// 生成新表名,为老表明+_copy_时间戳
 | 
						||
	newTableName := tableName + "_copy_" + time.Now().Format("20060102150405")
 | 
						||
	// 执行根据旧表创建新表
 | 
						||
	_, err := md.dc.Exec(fmt.Sprintf("create table %s (like %s)", newTableName, tableName))
 | 
						||
	if err != nil {
 | 
						||
		return err
 | 
						||
	}
 | 
						||
 | 
						||
	// 复制数据
 | 
						||
	if copy.CopyData {
 | 
						||
		go func() {
 | 
						||
			_, _ = md.dc.Exec(fmt.Sprintf("insert into %s select * from %s", newTableName, tableName))
 | 
						||
		}()
 | 
						||
	}
 | 
						||
 | 
						||
	// 查询旧表的自增字段名 重新设置新表的序列序列器
 | 
						||
	_, res, err := md.dc.Query(fmt.Sprintf("select column_name from information_schema.columns where table_name = '%s' and column_default like 'nextval%%'", tableName))
 | 
						||
	if err != nil {
 | 
						||
		return err
 | 
						||
	}
 | 
						||
 | 
						||
	for _, re := range res {
 | 
						||
		colName := anyx.ConvString(re["column_name"])
 | 
						||
		if colName != "" {
 | 
						||
 | 
						||
			// 查询自增列当前最大值
 | 
						||
			_, maxRes, err := md.dc.Query(fmt.Sprintf("select max(%s) max_val from %s", colName, tableName))
 | 
						||
			if err != nil {
 | 
						||
				return err
 | 
						||
			}
 | 
						||
			maxVal := anyx.ConvInt(maxRes[0]["max_val"])
 | 
						||
			// 序列起始值为1或当前最大值+1
 | 
						||
			if maxVal <= 0 {
 | 
						||
				maxVal = 1
 | 
						||
			} else {
 | 
						||
				maxVal += 1
 | 
						||
			}
 | 
						||
 | 
						||
			// 之所以不用tableName_colName_seq是因为gauss会自动创建同名的序列,且无法修改序列起始值,所以直接使用新序列值
 | 
						||
			newSeqName := fmt.Sprintf("%s_%s_copy_seq", newTableName, colName)
 | 
						||
 | 
						||
			// 创建自增序列,当前最大值为旧表最大值
 | 
						||
			_, err = md.dc.Exec(fmt.Sprintf("CREATE SEQUENCE %s START %d INCREMENT 1", newSeqName, maxVal))
 | 
						||
			if err != nil {
 | 
						||
				return err
 | 
						||
			}
 | 
						||
			// 将新表的自增主键序列与主键列相关联
 | 
						||
			_, err = md.dc.Exec(fmt.Sprintf("alter table %s alter column %s set default nextval('%s')", newTableName, colName, newSeqName))
 | 
						||
			if err != nil {
 | 
						||
				return err
 | 
						||
			}
 | 
						||
		}
 | 
						||
	}
 | 
						||
	return err
 | 
						||
}
 |