|
|
|
|
|
|
|
|
|
package db |
|
|
|
import ( |
|
"database/sql" |
|
"strconv" |
|
"strings" |
|
|
|
"github.com/GoAdminGroup/go-admin/modules/config" |
|
) |
|
|
|
|
|
type Postgresql struct { |
|
Base |
|
} |
|
|
|
|
|
func GetPostgresqlDB() *Postgresql { |
|
return &Postgresql{ |
|
Base: Base{ |
|
DbList: make(map[string]*sql.DB), |
|
}, |
|
} |
|
} |
|
|
|
|
|
func (db *Postgresql) Name() string { |
|
return "postgresql" |
|
} |
|
|
|
|
|
func (db *Postgresql) GetDelimiter() string { |
|
return `"` |
|
} |
|
|
|
|
|
func (db *Postgresql) GetDelimiter2() string { |
|
return `"` |
|
} |
|
|
|
|
|
func (db *Postgresql) GetDelimiters() []string { |
|
return []string{`"`, `"`} |
|
} |
|
|
|
|
|
func (db *Postgresql) QueryWithConnection(con string, query string, args ...interface{}) ([]map[string]interface{}, error) { |
|
return CommonQuery(db.DbList[con], filterQuery(query), args...) |
|
} |
|
|
|
|
|
func (db *Postgresql) ExecWithConnection(con string, query string, args ...interface{}) (sql.Result, error) { |
|
return CommonExec(db.DbList[con], filterQuery(query), args...) |
|
} |
|
|
|
|
|
func (db *Postgresql) Query(query string, args ...interface{}) ([]map[string]interface{}, error) { |
|
return CommonQuery(db.DbList["default"], filterQuery(query), args...) |
|
} |
|
|
|
|
|
func (db *Postgresql) Exec(query string, args ...interface{}) (sql.Result, error) { |
|
return CommonExec(db.DbList["default"], filterQuery(query), args...) |
|
} |
|
|
|
func (db *Postgresql) QueryWith(tx *sql.Tx, conn, query string, args ...interface{}) ([]map[string]interface{}, error) { |
|
if tx != nil { |
|
return db.QueryWithTx(tx, query, args...) |
|
} |
|
return db.QueryWithConnection(conn, query, args...) |
|
} |
|
|
|
func (db *Postgresql) ExecWith(tx *sql.Tx, conn, query string, args ...interface{}) (sql.Result, error) { |
|
if tx != nil { |
|
return db.ExecWithTx(tx, query, args...) |
|
} |
|
return db.ExecWithConnection(conn, query, args...) |
|
} |
|
|
|
func filterQuery(query string) string { |
|
queCount := strings.Count(query, "?") |
|
for i := 1; i < queCount+1; i++ { |
|
query = strings.Replace(query, "?", "$"+strconv.Itoa(i), 1) |
|
} |
|
query = strings.ReplaceAll(query, "`", "") |
|
|
|
return strings.ReplaceAll(query, "by order ", `by "order" `) |
|
} |
|
|
|
|
|
func (db *Postgresql) InitDB(cfgList map[string]config.Database) Connection { |
|
db.Configs = cfgList |
|
db.Once.Do(func() { |
|
for conn, cfg := range cfgList { |
|
sqlDB, err := sql.Open("postgres", cfg.GetDSN()) |
|
if err != nil { |
|
if sqlDB != nil { |
|
_ = sqlDB.Close() |
|
} |
|
panic(err) |
|
} |
|
|
|
sqlDB.SetMaxIdleConns(cfg.MaxIdleConns) |
|
sqlDB.SetMaxOpenConns(cfg.MaxOpenConns) |
|
sqlDB.SetConnMaxLifetime(cfg.ConnMaxLifetime) |
|
sqlDB.SetConnMaxIdleTime(cfg.ConnMaxIdleTime) |
|
|
|
db.DbList[conn] = sqlDB |
|
|
|
if err := sqlDB.Ping(); err != nil { |
|
panic(err) |
|
} |
|
} |
|
}) |
|
return db |
|
} |
|
|
|
|
|
func (db *Postgresql) BeginTxWithReadUncommitted() *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList["default"], sql.LevelReadUncommitted) |
|
} |
|
|
|
|
|
func (db *Postgresql) BeginTxWithReadCommitted() *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList["default"], sql.LevelReadCommitted) |
|
} |
|
|
|
|
|
func (db *Postgresql) BeginTxWithRepeatableRead() *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList["default"], sql.LevelRepeatableRead) |
|
} |
|
|
|
|
|
func (db *Postgresql) BeginTx() *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList["default"], sql.LevelDefault) |
|
} |
|
|
|
|
|
func (db *Postgresql) BeginTxWithLevel(level sql.IsolationLevel) *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList["default"], level) |
|
} |
|
|
|
|
|
func (db *Postgresql) BeginTxWithReadUncommittedAndConnection(conn string) *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList[conn], sql.LevelReadUncommitted) |
|
} |
|
|
|
|
|
func (db *Postgresql) BeginTxWithReadCommittedAndConnection(conn string) *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList[conn], sql.LevelReadCommitted) |
|
} |
|
|
|
|
|
func (db *Postgresql) BeginTxWithRepeatableReadAndConnection(conn string) *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList[conn], sql.LevelRepeatableRead) |
|
} |
|
|
|
|
|
func (db *Postgresql) BeginTxAndConnection(conn string) *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList[conn], sql.LevelDefault) |
|
} |
|
|
|
|
|
func (db *Postgresql) BeginTxWithLevelAndConnection(conn string, level sql.IsolationLevel) *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList[conn], level) |
|
} |
|
|
|
|
|
func (db *Postgresql) QueryWithTx(tx *sql.Tx, query string, args ...interface{}) ([]map[string]interface{}, error) { |
|
return CommonQueryWithTx(tx, filterQuery(query), args...) |
|
} |
|
|
|
|
|
func (db *Postgresql) ExecWithTx(tx *sql.Tx, query string, args ...interface{}) (sql.Result, error) { |
|
return CommonExecWithTx(tx, filterQuery(query), args...) |
|
} |
|
|