2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2020-01-06 20:43:59 +08:00
|
|
|
//
|
|
|
|
// Note:
|
|
|
|
// 1. It needs manually import: _ "github.com/lib/pq"
|
|
|
|
// 2. It does not support Save/Replace features.
|
|
|
|
// 3. It does not support LastInsertId.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
package gdb
|
|
|
|
|
|
|
|
import (
|
2021-05-21 13:25:53 +08:00
|
|
|
"context"
|
2019-06-19 09:06:52 +08:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
2021-05-21 13:25:53 +08:00
|
|
|
"strings"
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
|
|
"github.com/gogf/gf/v2/internal/intlog"
|
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
2019-09-02 15:48:25 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/text/gregex"
|
2017-11-23 10:21:28 +08:00
|
|
|
)
|
|
|
|
|
2020-03-08 00:17:42 +08:00
|
|
|
// DriverPgsql is the driver for postgresql database.
|
|
|
|
type DriverPgsql struct {
|
|
|
|
*Core
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates and returns a database object for postgresql.
|
2020-03-08 11:03:18 +08:00
|
|
|
// It implements the interface of gdb.Driver for extra database driver installation.
|
2020-03-08 00:17:42 +08:00
|
|
|
func (d *DriverPgsql) New(core *Core, node *ConfigNode) (DB, error) {
|
|
|
|
return &DriverPgsql{
|
|
|
|
Core: core,
|
|
|
|
}, nil
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
2020-02-29 19:55:53 +08:00
|
|
|
// Open creates and returns a underlying sql.DB object for pgsql.
|
2020-03-08 00:17:42 +08:00
|
|
|
func (d *DriverPgsql) Open(config *ConfigNode) (*sql.DB, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
var source string
|
2021-06-15 19:57:55 +08:00
|
|
|
if config.Link != "" {
|
|
|
|
source = config.Link
|
2019-06-19 09:06:52 +08:00
|
|
|
} else {
|
2020-01-06 20:43:59 +08:00
|
|
|
source = fmt.Sprintf(
|
|
|
|
"user=%s password=%s host=%s port=%s dbname=%s sslmode=disable",
|
|
|
|
config.User, config.Pass, config.Host, config.Port, config.Name,
|
|
|
|
)
|
2021-06-04 18:26:41 +08:00
|
|
|
if config.Timezone != "" {
|
|
|
|
source = fmt.Sprintf("%s timezone=%s", source, config.Timezone)
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2021-06-26 16:23:54 +08:00
|
|
|
intlog.Printf(d.GetCtx(), "Open: %s", source)
|
2019-06-19 09:06:52 +08:00
|
|
|
if db, err := sql.Open("postgres", source); err == nil {
|
|
|
|
return db, nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 19:57:55 +08:00
|
|
|
// FilteredLink retrieves and returns filtered `linkInfo` that can be using for
|
2021-01-25 21:17:32 +08:00
|
|
|
// logging or tracing purpose.
|
2021-06-15 19:57:55 +08:00
|
|
|
func (d *DriverPgsql) FilteredLink() string {
|
|
|
|
linkInfo := d.GetConfig().Link
|
2021-01-25 21:17:32 +08:00
|
|
|
if linkInfo == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
s, _ := gregex.ReplaceString(
|
|
|
|
`(.+?)\s*password=(.+)\s*host=(.+)`,
|
|
|
|
`$1 password=xxx host=$3`,
|
|
|
|
linkInfo,
|
|
|
|
)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-03-08 00:17:42 +08:00
|
|
|
// GetChars returns the security char for this type of database.
|
|
|
|
func (d *DriverPgsql) GetChars() (charLeft string, charRight string) {
|
2019-06-19 09:06:52 +08:00
|
|
|
return "\"", "\""
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:55:55 +08:00
|
|
|
// DoCommit deals with the sql string before commits it to underlying sql driver.
|
2021-06-26 18:20:55 +08:00
|
|
|
func (d *DriverPgsql) DoCommit(ctx context.Context, link Link, sql string, args []interface{}) (newSql string, newArgs []interface{}, err error) {
|
|
|
|
defer func() {
|
|
|
|
newSql, newArgs, err = d.Core.DoCommit(ctx, link, newSql, newArgs)
|
|
|
|
}()
|
|
|
|
|
2020-02-29 19:55:53 +08:00
|
|
|
var index int
|
2020-07-20 19:13:15 +08:00
|
|
|
// Convert place holder char '?' to string "$x".
|
2020-01-07 22:14:32 +08:00
|
|
|
sql, _ = gregex.ReplaceStringFunc("\\?", sql, func(s string) string {
|
2019-06-19 09:06:52 +08:00
|
|
|
index++
|
2020-07-19 23:24:35 +08:00
|
|
|
return fmt.Sprintf("$%d", index)
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2021-06-26 18:20:55 +08:00
|
|
|
newSql, _ = gregex.ReplaceString(` LIMIT (\d+),\s*(\d+)`, ` LIMIT $2 OFFSET $1`, sql)
|
|
|
|
return newSql, args, nil
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-08-12 16:50:57 +08:00
|
|
|
|
2020-02-29 19:55:53 +08:00
|
|
|
// Tables retrieves and returns the tables of current schema.
|
2020-03-08 11:03:18 +08:00
|
|
|
// It's mainly used in cli tool chain for automatically generating the models.
|
2021-05-21 13:25:53 +08:00
|
|
|
func (d *DriverPgsql) Tables(ctx context.Context, schema ...string) (tables []string, err error) {
|
2020-03-02 23:41:33 +08:00
|
|
|
var result Result
|
2021-05-21 15:30:21 +08:00
|
|
|
link, err := d.SlaveLink(schema...)
|
2020-03-02 23:41:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
query := "SELECT TABLENAME FROM PG_TABLES WHERE SCHEMANAME = 'public' ORDER BY TABLENAME"
|
|
|
|
if len(schema) > 0 && schema[0] != "" {
|
|
|
|
query = fmt.Sprintf("SELECT TABLENAME FROM PG_TABLES WHERE SCHEMANAME = '%s' ORDER BY TABLENAME", schema[0])
|
|
|
|
}
|
2021-05-21 13:25:53 +08:00
|
|
|
result, err = d.DoGetAll(ctx, link, query)
|
2020-03-02 23:41:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, m := range result {
|
|
|
|
for _, v := range m {
|
|
|
|
tables = append(tables, v.String())
|
|
|
|
}
|
|
|
|
}
|
2019-09-02 15:48:25 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-29 19:55:53 +08:00
|
|
|
// TableFields retrieves and returns the fields information of specified table of current schema.
|
2021-04-04 12:01:22 +08:00
|
|
|
//
|
|
|
|
// Also see DriverMysql.TableFields.
|
2021-05-29 17:26:08 +08:00
|
|
|
func (d *DriverPgsql) TableFields(ctx context.Context, table string, schema ...string) (fields map[string]*TableField, err error) {
|
2020-04-08 21:26:14 +08:00
|
|
|
charL, charR := d.GetChars()
|
|
|
|
table = gstr.Trim(table, charL+charR)
|
2020-02-22 14:26:36 +08:00
|
|
|
if gstr.Contains(table, " ") {
|
2021-08-24 21:18:59 +08:00
|
|
|
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "function TableFields supports only single table operations")
|
2020-02-22 14:26:36 +08:00
|
|
|
}
|
2019-08-12 16:50:57 +08:00
|
|
|
table, _ = gregex.ReplaceString("\"", "", table)
|
2021-05-21 15:30:21 +08:00
|
|
|
useSchema := d.db.GetSchema()
|
2020-01-07 22:14:32 +08:00
|
|
|
if len(schema) > 0 && schema[0] != "" {
|
2021-05-21 15:30:21 +08:00
|
|
|
useSchema = schema[0]
|
2020-01-07 22:14:32 +08:00
|
|
|
}
|
2021-04-04 12:01:22 +08:00
|
|
|
tableFieldsCacheKey := fmt.Sprintf(
|
|
|
|
`pgsql_table_fields_%s_%s@group:%s`,
|
2021-05-21 15:30:21 +08:00
|
|
|
table, useSchema, d.GetGroup(),
|
2021-04-04 12:01:22 +08:00
|
|
|
)
|
|
|
|
v := tableFieldsMap.GetOrSetFuncLock(tableFieldsCacheKey, func() interface{} {
|
|
|
|
var (
|
|
|
|
result Result
|
2021-05-29 17:26:08 +08:00
|
|
|
link, err = d.SlaveLink(useSchema)
|
2021-04-04 12:01:22 +08:00
|
|
|
structureSql = fmt.Sprintf(`
|
2021-07-15 14:53:21 +08:00
|
|
|
SELECT a.attname AS field, t.typname AS type,a.attnotnull as null,
|
|
|
|
(case when d.contype is not null then 'pri' else '' end) as key
|
|
|
|
,ic.column_default as default_value,b.description as comment
|
|
|
|
,coalesce(character_maximum_length, numeric_precision, -1) as length
|
|
|
|
,numeric_scale as scale
|
|
|
|
FROM pg_attribute a
|
|
|
|
left join pg_class c on a.attrelid = c.oid
|
|
|
|
left join pg_constraint d on d.conrelid = c.oid and a.attnum = d.conkey[1]
|
|
|
|
left join pg_description b ON a.attrelid=b.objoid AND a.attnum = b.objsubid
|
|
|
|
left join pg_type t ON a.atttypid = t.oid
|
|
|
|
left join information_schema.columns ic on ic.column_name = a.attname and ic.table_name = c.relname
|
|
|
|
WHERE c.relname = '%s' and a.attnum > 0
|
2020-10-12 23:22:56 +08:00
|
|
|
ORDER BY a.attnum`,
|
|
|
|
strings.ToLower(table),
|
|
|
|
)
|
2021-04-04 12:01:22 +08:00
|
|
|
)
|
2021-05-29 17:26:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
2021-04-04 12:01:22 +08:00
|
|
|
}
|
2021-05-29 17:26:08 +08:00
|
|
|
structureSql, _ = gregex.ReplaceString(`[\n\r\s]+`, " ", gstr.Trim(structureSql))
|
2021-05-21 13:25:53 +08:00
|
|
|
result, err = d.DoGetAll(ctx, link, structureSql)
|
2021-04-04 12:01:22 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fields = make(map[string]*TableField)
|
|
|
|
for i, m := range result {
|
|
|
|
fields[m["field"].String()] = &TableField{
|
2021-07-15 14:53:21 +08:00
|
|
|
Index: i,
|
|
|
|
Name: m["field"].String(),
|
|
|
|
Type: m["type"].String(),
|
|
|
|
Null: m["null"].Bool(),
|
|
|
|
Key: m["key"].String(),
|
|
|
|
Default: m["default_value"].Val(),
|
|
|
|
Comment: m["comment"].String(),
|
2019-09-02 15:48:25 +08:00
|
|
|
}
|
2021-04-04 12:01:22 +08:00
|
|
|
}
|
|
|
|
return fields
|
|
|
|
})
|
|
|
|
if v != nil {
|
2019-09-02 15:48:25 +08:00
|
|
|
fields = v.(map[string]*TableField)
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-07-06 20:59:09 +08:00
|
|
|
|
|
|
|
// DoInsert is not supported in pgsql.
|
|
|
|
func (d *DriverPgsql) DoInsert(ctx context.Context, link Link, table string, list List, option DoInsertOption) (result sql.Result, err error) {
|
|
|
|
switch option.InsertOption {
|
|
|
|
case insertOptionSave:
|
2021-08-24 21:18:59 +08:00
|
|
|
return nil, gerror.NewCode(gcode.CodeNotSupported, `Save operation is not supported by pgsql driver`)
|
2021-07-06 20:59:09 +08:00
|
|
|
|
|
|
|
case insertOptionReplace:
|
2021-08-24 21:18:59 +08:00
|
|
|
return nil, gerror.NewCode(gcode.CodeNotSupported, `Replace operation is not supported by pgsql driver`)
|
2021-07-06 20:59:09 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
return d.Core.DoInsert(ctx, link, table, list, option)
|
|
|
|
}
|
|
|
|
}
|