feat: support PG-SQL

This commit is contained in:
ckt1031
2023-07-27 00:10:08 +08:00
parent 7419ca511e
commit 0d6163a9fb
3 changed files with 27 additions and 4 deletions

View File

@@ -1,11 +1,13 @@
package model
import (
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"one-api/common"
"os"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
var DB *gorm.DB
@@ -40,7 +42,13 @@ func CountTable(tableName string) (num int64) {
func InitDB() (err error) {
var db *gorm.DB
if os.Getenv("SQL_DSN") != "" {
if os.Getenv("POSTGRES_DSN") != "" {
// Use PostgreSQL
common.SysLog("using PostgreSQL as database")
db, err = gorm.Open(postgres.Open(os.Getenv("POSTGRES_DSN")), &gorm.Config{
PrepareStmt: true, // precompile SQL
})
} else if os.Getenv("SQL_DSN") != "" {
// Use MySQL
common.SysLog("using MySQL as database")
db, err = gorm.Open(mysql.Open(os.Getenv("SQL_DSN")), &gorm.Config{