2023-10-27 17:41:45 +08:00
|
|
|
|
package dbm
|
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"
|
|
|
|
|
|
"database/sql/driver"
|
2022-10-15 17:38:34 +08:00
|
|
|
|
"fmt"
|
2022-12-22 18:41:34 +08:00
|
|
|
|
machineapp "mayfly-go/internal/machine/application"
|
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"
|
|
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
2023-11-30 15:02:48 +08:00
|
|
|
|
"mayfly-go/pkg/utils/netx"
|
2022-12-22 18:41:34 +08:00
|
|
|
|
"net"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2023-11-28 14:33:53 +08:00
|
|
|
|
pq "gitee.com/liuzongyang/libpq"
|
2022-10-15 17:38:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
|
func getPgsqlDB(d *DbInfo) (*sql.DB, error) {
|
2023-11-29 17:34:54 +08:00
|
|
|
|
driverName := string(d.Type)
|
2022-12-22 18:41:34 +08:00
|
|
|
|
// SSH Conect
|
2023-03-06 16:59:57 +08:00
|
|
|
|
if d.SshTunnelMachineId > 0 {
|
2022-12-22 18:41:34 +08:00
|
|
|
|
// 如果使用了隧道,则使用`postgres:ssh:隧道机器id`注册名
|
|
|
|
|
|
driverName = fmt.Sprintf("postgres:ssh:%d", d.SshTunnelMachineId)
|
2023-07-21 17:07:04 +08:00
|
|
|
|
if !collx.ArrayContains(sql.Drivers(), driverName) {
|
2022-12-22 18:41:34 +08:00
|
|
|
|
sql.Register(driverName, &PqSqlDialer{sshTunnelMachineId: d.SshTunnelMachineId})
|
|
|
|
|
|
}
|
|
|
|
|
|
sql.Drivers()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
|
db := d.Database
|
2023-08-30 22:41:42 +08:00
|
|
|
|
var dbParam string
|
2023-11-12 20:14:44 +08:00
|
|
|
|
exsitSchema := false
|
2023-08-30 22:41:42 +08:00
|
|
|
|
if db != "" {
|
2023-11-12 20:14:44 +08:00
|
|
|
|
// postgres database可以使用db/schema表示,方便连接指定schema, 若不存在schema则使用默认schema
|
|
|
|
|
|
ss := strings.Split(db, "/")
|
|
|
|
|
|
if len(ss) > 1 {
|
|
|
|
|
|
exsitSchema = true
|
|
|
|
|
|
dbParam = fmt.Sprintf("dbname=%s search_path=%s", ss[0], ss[len(ss)-1])
|
|
|
|
|
|
} else {
|
|
|
|
|
|
dbParam = "dbname=" + db
|
|
|
|
|
|
}
|
2023-08-30 22:41:42 +08:00
|
|
|
|
}
|
2023-11-12 20:14:44 +08:00
|
|
|
|
|
2023-11-30 15:02:48 +08:00
|
|
|
|
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s %s sslmode=disable connect_timeout=8", d.Host, d.Port, d.Username, d.Password, dbParam)
|
2023-11-12 20:14:44 +08:00
|
|
|
|
// 存在额外指定参数,则拼接该连接参数
|
2022-12-22 18:41:34 +08:00
|
|
|
|
if d.Params != "" {
|
2023-11-12 20:14:44 +08:00
|
|
|
|
// 存在指定的db,则需要将dbInstance配置中的parmas排除掉dbname和search_path
|
|
|
|
|
|
if db != "" {
|
|
|
|
|
|
paramArr := strings.Split(d.Params, "&")
|
|
|
|
|
|
paramArr = collx.ArrayRemoveFunc(paramArr, func(param string) bool {
|
|
|
|
|
|
if strings.HasPrefix(param, "dbname=") {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
if exsitSchema && strings.HasPrefix(param, "search_path") {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
|
|
|
|
|
})
|
|
|
|
|
|
d.Params = strings.Join(paramArr, " ")
|
|
|
|
|
|
}
|
2022-12-22 18:41:34 +08:00
|
|
|
|
dsn = fmt.Sprintf("%s %s", dsn, strings.Join(strings.Split(d.Params, "&"), " "))
|
|
|
|
|
|
}
|
2023-11-12 20:14:44 +08:00
|
|
|
|
|
2022-12-22 18:41:34 +08:00
|
|
|
|
return sql.Open(driverName, dsn)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// pgsql dialer
|
|
|
|
|
|
type PqSqlDialer struct {
|
2023-03-06 16:59:57 +08:00
|
|
|
|
sshTunnelMachineId int
|
2022-12-22 18:41:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (d *PqSqlDialer) Open(name string) (driver.Conn, error) {
|
|
|
|
|
|
return pq.DialOpen(d, name)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (pd *PqSqlDialer) Dial(network, address string) (net.Conn, error) {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
sshTunnel, err := machineapp.GetMachineApp().GetSshTunnelMachine(pd.sshTunnelMachineId)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if sshConn, err := sshTunnel.GetDialConn("tcp", address); err == nil {
|
2023-11-30 15:02:48 +08:00
|
|
|
|
// 将ssh conn包装,否则会返回错误: ssh: tcpChan: deadline not supported
|
|
|
|
|
|
return &netx.WrapSshConn{Conn: sshConn}, nil
|
2022-12-22 18:41:34 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (pd *PqSqlDialer) DialTimeout(network, address string, timeout time.Duration) (net.Conn, error) {
|
|
|
|
|
|
return pd.Dial(network, address)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-15 17:38:34 +08:00
|
|
|
|
// ---------------------------------- pgsql元数据 -----------------------------------
|
|
|
|
|
|
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 {
|
2023-10-27 17:41:45 +08:00
|
|
|
|
dc *DbConn
|
2022-10-15 17:38:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-20 17:29:16 +08:00
|
|
|
|
func (pd *PgsqlDialect) GetDbServer() (*DbServer, error) {
|
|
|
|
|
|
_, res, err := pd.dc.Query("SHOW server_version")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
ds := &DbServer{
|
|
|
|
|
|
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
|
|
|
|
// 获取表基础元信息, 如表名等
|
2023-11-26 21:21:35 +08:00
|
|
|
|
func (pd *PgsqlDialect) GetTables() ([]Table, error) {
|
|
|
|
|
|
_, res, err := pd.dc.Query(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
|
|
|
|
|
|
|
|
|
|
tables := make([]Table, 0)
|
|
|
|
|
|
for _, re := range res {
|
|
|
|
|
|
tables = append(tables, Table{
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取列元信息, 如列名等
|
2023-11-26 21:21:35 +08:00
|
|
|
|
func (pd *PgsqlDialect) GetColumns(tableNames ...string) ([]Column, error) {
|
2022-10-16 11:01:45 +08:00
|
|
|
|
tableName := ""
|
2022-10-15 17:38:34 +08:00
|
|
|
|
for i := 0; i < len(tableNames); i++ {
|
|
|
|
|
|
if i != 0 {
|
|
|
|
|
|
tableName = tableName + ", "
|
|
|
|
|
|
}
|
|
|
|
|
|
tableName = tableName + "'" + tableNames[i] + "'"
|
|
|
|
|
|
}
|
2023-05-24 12:32:17 +08:00
|
|
|
|
|
2023-11-26 21:21:35 +08:00
|
|
|
|
_, res, err := pd.dc.Query(fmt.Sprintf(GetLocalSql(PGSQL_META_FILE, PGSQL_COLUMN_MA_KEY), tableName))
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-24 12:32:17 +08:00
|
|
|
|
columns := make([]Column, 0)
|
|
|
|
|
|
for _, re := range res {
|
|
|
|
|
|
columns = append(columns, Column{
|
|
|
|
|
|
TableName: re["tableName"].(string),
|
|
|
|
|
|
ColumnName: re["columnName"].(string),
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取表索引信息
|
2023-11-26 21:21:35 +08:00
|
|
|
|
func (pd *PgsqlDialect) GetTableIndex(tableName string) ([]Index, error) {
|
|
|
|
|
|
_, res, err := pd.dc.Query(fmt.Sprintf(GetLocalSql(PGSQL_META_FILE, PGSQL_INDEX_INFO_KEY), tableName))
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-24 12:32:17 +08:00
|
|
|
|
indexs := make([]Index, 0)
|
|
|
|
|
|
for _, re := range res {
|
|
|
|
|
|
indexs = append(indexs, Index{
|
|
|
|
|
|
IndexName: re["indexName"].(string),
|
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
|
|
|
|
// 把查询结果以索引名分组,索引字段以逗号连接
|
|
|
|
|
|
result := make([]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
|
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) {
|
2023-11-26 21:21:35 +08:00
|
|
|
|
_, err := pd.dc.Exec(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
|
|
|
|
|
2023-12-14 21:27:11 +08:00
|
|
|
|
func (pd *PgsqlDialect) GetTableRecord(tableName string, pageNum, pageSize int) ([]*QueryColumn, []map[string]any, error) {
|
2023-11-26 21:21:35 +08:00
|
|
|
|
return pd.dc.Query(fmt.Sprintf("SELECT * FROM %s OFFSET %d LIMIT %d", tableName, (pageNum-1)*pageSize, pageSize))
|
2022-12-17 22:24:21 +08:00
|
|
|
|
}
|
2023-09-07 11:15:11 +08:00
|
|
|
|
|
2023-12-14 21:27:11 +08:00
|
|
|
|
func (pd *PgsqlDialect) WalkTableRecord(tableName string, walk func(record map[string]any, columns []*QueryColumn)) error {
|
2023-12-07 01:07:34 +08:00
|
|
|
|
return pd.dc.WalkTableRecord(context.Background(), fmt.Sprintf("SELECT * FROM %s", tableName), walk)
|
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) {
|
2023-11-12 20:14:44 +08:00
|
|
|
|
sql := 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 获取数据库程序模块,用于数据库备份与恢复
|
|
|
|
|
|
func (pd *PgsqlDialect) GetDbProgram() DbProgram {
|
|
|
|
|
|
panic("implement me")
|
|
|
|
|
|
}
|