2024-01-12 13:15:30 +08:00
|
|
|
|
package postgres
|
2022-10-15 17:38:34 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
2023-12-07 01:07:34 +08:00
|
|
|
|
"context"
|
2022-12-22 18:41:34 +08:00
|
|
|
|
"database/sql"
|
2022-10-15 17:38:34 +08:00
|
|
|
|
"fmt"
|
2024-01-12 13:15:30 +08:00
|
|
|
|
"mayfly-go/internal/db/dbm/dbi"
|
2023-10-26 17:15:49 +08:00
|
|
|
|
"mayfly-go/pkg/errorx"
|
2023-07-21 17:07:04 +08:00
|
|
|
|
"mayfly-go/pkg/utils/anyx"
|
2024-01-18 17:18:17 +08:00
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
2024-01-05 05:31:32 +00:00
|
|
|
|
"regexp"
|
2022-12-22 18:41:34 +08:00
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
2022-10-15 17:38:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
2023-05-24 12:32:17 +08:00
|
|
|
|
PGSQL_META_FILE = "metasql/pgsql_meta.sql"
|
2023-11-12 20:14:44 +08:00
|
|
|
|
PGSQL_DB_SCHEMAS = "PGSQL_DB_SCHEMAS"
|
2023-05-24 12:32:17 +08:00
|
|
|
|
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"
|
2022-10-15 17:38:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-11-26 21:21:35 +08:00
|
|
|
|
type PgsqlDialect struct {
|
2024-01-12 13:15:30 +08:00
|
|
|
|
dc *dbi.DbConn
|
2022-10-15 17:38:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
func (pd *PgsqlDialect) GetDbServer() (*dbi.DbServer, error) {
|
2023-12-20 17:29:16 +08:00
|
|
|
|
_, res, err := pd.dc.Query("SHOW server_version")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2024-01-12 13:15:30 +08:00
|
|
|
|
ds := &dbi.DbServer{
|
2023-12-20 17:29:16 +08:00
|
|
|
|
Version: anyx.ConvString(res[0]["server_version"]),
|
|
|
|
|
|
}
|
|
|
|
|
|
return ds, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-26 21:21:35 +08:00
|
|
|
|
func (pd *PgsqlDialect) GetDbNames() ([]string, error) {
|
|
|
|
|
|
_, res, err := pd.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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-15 17:38:34 +08:00
|
|
|
|
// 获取表基础元信息, 如表名等
|
2024-01-12 13:15:30 +08:00
|
|
|
|
func (pd *PgsqlDialect) GetTables() ([]dbi.Table, error) {
|
|
|
|
|
|
_, res, err := pd.dc.Query(dbi.GetLocalSql(PGSQL_META_FILE, PGSQL_TABLE_INFO_KEY))
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2023-05-24 12:32:17 +08:00
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
tables := make([]dbi.Table, 0)
|
2023-05-24 12:32:17 +08:00
|
|
|
|
for _, re := range res {
|
2024-01-12 13:15:30 +08:00
|
|
|
|
tables = append(tables, dbi.Table{
|
2023-05-24 12:32:17 +08:00
|
|
|
|
TableName: re["tableName"].(string),
|
2023-07-21 17:07:04 +08:00
|
|
|
|
TableComment: anyx.ConvString(re["tableComment"]),
|
2023-11-02 12:46:21 +08:00
|
|
|
|
CreateTime: anyx.ConvString(re["createTime"]),
|
|
|
|
|
|
TableRows: anyx.ConvInt(re["tableRows"]),
|
|
|
|
|
|
DataLength: anyx.ConvInt64(re["dataLength"]),
|
|
|
|
|
|
IndexLength: anyx.ConvInt64(re["indexLength"]),
|
2023-05-24 12:32:17 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return tables, nil
|
2022-10-15 17:38:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取列元信息, 如列名等
|
2024-01-12 13:15:30 +08:00
|
|
|
|
func (pd *PgsqlDialect) GetColumns(tableNames ...string) ([]dbi.Column, error) {
|
2024-01-18 17:18:17 +08:00
|
|
|
|
dbType := pd.dc.Info.Type
|
|
|
|
|
|
tableName := strings.Join(collx.ArrayMap[string, string](tableNames, func(val string) string {
|
|
|
|
|
|
return fmt.Sprintf("'%s'", dbType.RemoveQuote(val))
|
|
|
|
|
|
}), ",")
|
2023-05-24 12:32:17 +08:00
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
_, res, err := pd.dc.Query(fmt.Sprintf(dbi.GetLocalSql(PGSQL_META_FILE, PGSQL_COLUMN_MA_KEY), tableName))
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
columns := make([]dbi.Column, 0)
|
2023-05-24 12:32:17 +08:00
|
|
|
|
for _, re := range res {
|
2024-01-12 13:15:30 +08:00
|
|
|
|
columns = append(columns, dbi.Column{
|
2024-01-18 17:18:17 +08:00
|
|
|
|
TableName: anyx.ConvString(re["tableName"]),
|
|
|
|
|
|
ColumnName: anyx.ConvString(re["columnName"]),
|
2023-07-21 17:07:04 +08:00
|
|
|
|
ColumnType: anyx.ConvString(re["columnType"]),
|
|
|
|
|
|
ColumnComment: anyx.ConvString(re["columnComment"]),
|
|
|
|
|
|
Nullable: anyx.ConvString(re["nullable"]),
|
|
|
|
|
|
ColumnKey: anyx.ConvString(re["columnKey"]),
|
|
|
|
|
|
ColumnDefault: anyx.ConvString(re["columnDefault"]),
|
2023-11-23 10:36:20 +08:00
|
|
|
|
NumScale: anyx.ConvString(re["numScale"]),
|
2023-05-24 12:32:17 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return columns, nil
|
2022-10-15 17:38:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-26 21:21:35 +08:00
|
|
|
|
func (pd *PgsqlDialect) GetPrimaryKey(tablename string) (string, error) {
|
|
|
|
|
|
columns, err := pd.GetColumns(tablename)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(columns) == 0 {
|
|
|
|
|
|
return "", errorx.NewBiz("[%s] 表不存在", tablename)
|
|
|
|
|
|
}
|
2022-11-23 20:48:37 +08:00
|
|
|
|
for _, v := range columns {
|
2023-05-24 12:32:17 +08:00
|
|
|
|
if v.ColumnKey == "PRI" {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return v.ColumnName, nil
|
2022-11-23 20:48:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return columns[0].ColumnName, nil
|
2022-10-15 17:38:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取表索引信息
|
2024-01-12 13:15:30 +08:00
|
|
|
|
func (pd *PgsqlDialect) GetTableIndex(tableName string) ([]dbi.Index, error) {
|
|
|
|
|
|
_, res, err := pd.dc.Query(fmt.Sprintf(dbi.GetLocalSql(PGSQL_META_FILE, PGSQL_INDEX_INFO_KEY), tableName))
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
indexs := make([]dbi.Index, 0)
|
2023-05-24 12:32:17 +08:00
|
|
|
|
for _, re := range res {
|
2024-01-12 13:15:30 +08:00
|
|
|
|
indexs = append(indexs, dbi.Index{
|
2024-01-18 17:18:17 +08:00
|
|
|
|
IndexName: anyx.ConvString(re["indexName"]),
|
2023-07-21 17:07:04 +08:00
|
|
|
|
ColumnName: anyx.ConvString(re["columnName"]),
|
2023-11-23 10:36:20 +08:00
|
|
|
|
IndexType: anyx.ConvString(re["IndexType"]),
|
2023-07-21 17:07:04 +08:00
|
|
|
|
IndexComment: anyx.ConvString(re["indexComment"]),
|
|
|
|
|
|
NonUnique: anyx.ConvInt(re["nonUnique"]),
|
|
|
|
|
|
SeqInIndex: anyx.ConvInt(re["seqInIndex"]),
|
2023-05-24 12:32:17 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2023-11-23 10:36:20 +08:00
|
|
|
|
// 把查询结果以索引名分组,索引字段以逗号连接
|
2024-01-12 13:15:30 +08:00
|
|
|
|
result := make([]dbi.Index, 0)
|
2023-11-23 10:36:20 +08:00
|
|
|
|
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
|
2022-10-15 17:38:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取建表ddl
|
2023-12-20 17:29:16 +08:00
|
|
|
|
func (pd *PgsqlDialect) GetTableDDL(tableName string) (string, error) {
|
2024-01-12 13:15:30 +08:00
|
|
|
|
_, err := pd.dc.Exec(dbi.GetLocalSql(PGSQL_META_FILE, PGSQL_TABLE_DDL_KEY))
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
2023-05-24 12:32:17 +08:00
|
|
|
|
|
2023-11-26 21:21:35 +08:00
|
|
|
|
_, schemaRes, _ := pd.dc.Query("select current_schema() as schema")
|
2023-05-24 12:32:17 +08:00
|
|
|
|
schemaName := schemaRes[0]["schema"].(string)
|
|
|
|
|
|
|
|
|
|
|
|
ddlSql := fmt.Sprintf("select showcreatetable('%s','%s') as sql", schemaName, tableName)
|
2023-11-26 21:21:35 +08:00
|
|
|
|
_, res, err := pd.dc.Query(ddlSql)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
2023-05-24 12:32:17 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return res[0]["sql"].(string), nil
|
2022-10-15 17:38:34 +08:00
|
|
|
|
}
|
2022-12-17 22:24:21 +08:00
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
func (pd *PgsqlDialect) WalkTableRecord(tableName string, walkFn dbi.WalkQueryRowsFunc) error {
|
2024-01-06 22:36:50 +08:00
|
|
|
|
return pd.dc.WalkQueryRows(context.Background(), fmt.Sprintf("SELECT * FROM %s", tableName), walkFn)
|
2023-09-07 11:15:11 +08:00
|
|
|
|
}
|
2023-11-12 20:14:44 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取pgsql当前连接的库可访问的schemaNames
|
2023-11-26 21:21:35 +08:00
|
|
|
|
func (pd *PgsqlDialect) GetSchemas() ([]string, error) {
|
2024-01-12 13:15:30 +08:00
|
|
|
|
sql := dbi.GetLocalSql(PGSQL_META_FILE, PGSQL_DB_SCHEMAS)
|
2023-11-26 21:21:35 +08:00
|
|
|
|
_, res, err := pd.dc.Query(sql)
|
2023-11-12 20:14:44 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
schemaNames := make([]string, 0)
|
|
|
|
|
|
for _, re := range res {
|
|
|
|
|
|
schemaNames = append(schemaNames, anyx.ConvString(re["schemaName"]))
|
|
|
|
|
|
}
|
|
|
|
|
|
return schemaNames, nil
|
|
|
|
|
|
}
|
2024-01-05 08:55:34 +08:00
|
|
|
|
|
|
|
|
|
|
// GetDbProgram 获取数据库程序模块,用于数据库备份与恢复
|
2024-01-12 13:15:30 +08:00
|
|
|
|
func (pd *PgsqlDialect) GetDbProgram() dbi.DbProgram {
|
2024-01-05 08:55:34 +08:00
|
|
|
|
panic("implement me")
|
|
|
|
|
|
}
|
2024-01-05 05:31:32 +00:00
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
func (pd *PgsqlDialect) GetDataType(dbColumnType string) dbi.DataType {
|
2024-01-05 05:31:32 +00:00
|
|
|
|
if regexp.MustCompile(`(?i)int|double|float|number|decimal|byte|bit`).MatchString(dbColumnType) {
|
2024-01-12 13:15:30 +08:00
|
|
|
|
return dbi.DataTypeNumber
|
2024-01-05 05:31:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
// 日期时间类型
|
|
|
|
|
|
if regexp.MustCompile(`(?i)datetime|timestamp`).MatchString(dbColumnType) {
|
2024-01-12 13:15:30 +08:00
|
|
|
|
return dbi.DataTypeDateTime
|
2024-01-05 05:31:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
// 日期类型
|
|
|
|
|
|
if regexp.MustCompile(`(?i)date`).MatchString(dbColumnType) {
|
2024-01-12 13:15:30 +08:00
|
|
|
|
return dbi.DataTypeDate
|
2024-01-05 05:31:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
// 时间类型
|
|
|
|
|
|
if regexp.MustCompile(`(?i)time`).MatchString(dbColumnType) {
|
2024-01-12 13:15:30 +08:00
|
|
|
|
return dbi.DataTypeTime
|
2024-01-05 05:31:32 +00:00
|
|
|
|
}
|
2024-01-12 13:15:30 +08:00
|
|
|
|
return dbi.DataTypeString
|
2024-01-05 05:31:32 +00:00
|
|
|
|
}
|
2024-01-05 22:16:38 +08:00
|
|
|
|
|
2024-01-06 22:36:50 +08:00
|
|
|
|
func (pd *PgsqlDialect) BatchInsert(tx *sql.Tx, tableName string, columns []string, values [][]any) (int64, error) {
|
2024-01-05 05:31:32 +00:00
|
|
|
|
// 执行批量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...)
|
|
|
|
|
|
}
|
2024-01-06 22:36:50 +08:00
|
|
|
|
|
2024-01-08 11:24:37 +08:00
|
|
|
|
// 构建占位符字符串 "($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, ", ")+")")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-11 12:35:44 +08:00
|
|
|
|
sqlStr := fmt.Sprintf("insert into %s (%s) values %s", pd.dc.Info.Type.QuoteIdentifier(tableName), strings.Join(columns, ","), strings.Join(placeholders, ", "))
|
2024-01-08 11:24:37 +08:00
|
|
|
|
// 执行批量insert sql
|
|
|
|
|
|
|
2024-01-06 22:36:50 +08:00
|
|
|
|
return pd.dc.TxExec(tx, sqlStr, args...)
|
2024-01-05 05:31:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
func (pd *PgsqlDialect) FormatStrData(dbColumnValue string, dataType dbi.DataType) string {
|
2024-01-05 05:31:32 +00:00
|
|
|
|
switch dataType {
|
2024-01-12 13:15:30 +08:00
|
|
|
|
case dbi.DataTypeDateTime: // "2024-01-02T22:16:28.545377+08:00"
|
2024-01-05 05:31:32 +00:00
|
|
|
|
res, _ := time.Parse(time.RFC3339, dbColumnValue)
|
|
|
|
|
|
return res.Format(time.DateTime)
|
2024-01-12 13:15:30 +08:00
|
|
|
|
case dbi.DataTypeDate: // "2024-01-02T00:00:00Z"
|
2024-01-05 05:31:32 +00:00
|
|
|
|
res, _ := time.Parse(time.RFC3339, dbColumnValue)
|
|
|
|
|
|
return res.Format(time.DateOnly)
|
2024-01-12 13:15:30 +08:00
|
|
|
|
case dbi.DataTypeTime: // "0000-01-01T22:16:28.545075+08:00"
|
2024-01-05 05:31:32 +00:00
|
|
|
|
res, _ := time.Parse(time.RFC3339, dbColumnValue)
|
|
|
|
|
|
return res.Format(time.TimeOnly)
|
|
|
|
|
|
}
|
|
|
|
|
|
return dbColumnValue
|
|
|
|
|
|
}
|