Skip to content

Commit

Permalink
feat(config): add db.prepare_stmt config option (#760) (#762)
Browse files Browse the repository at this point in the history
The Prepared Statement database feature is enabled by default. You can disable it in config file or a env variable `ATK_DB_PREPARE__STMT=0`.
  • Loading branch information
qwqcode authored Feb 3, 2024
1 parent 4012d3e commit 9c3228b
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions conf/artalk.example.simple.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ db:
password: ""
charset: "utf8mb4"
ssl: false
prepare_stmt: true
log:
enabled: true
filename: "./data/artalk.log"
Expand Down
2 changes: 2 additions & 0 deletions conf/artalk.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ db:
charset: "utf8mb4"
# Enable SSL mode
ssl: false
# Prepared Statement
prepare_stmt: true

# Logging
log:
Expand Down
2 changes: 2 additions & 0 deletions conf/artalk.example.zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ db:
table_prefix: ""
# 启用 SSL
ssl: false
# 预编译语句
prepare_stmt: true

# 日志
log:
Expand Down
1 change: 1 addition & 0 deletions docs/docs/guide/backend/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ db:
charset: "utf8mb4" # 编码格式
table_prefix: "" # 表前缀 (例如:"atk_")
ssl: false # 启用 SSL
prepare_stmt: true # 预编译语句
```

数据表将在 Artalk 启动时自动完成创建,无需额外操作。
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type DBConf struct {
TablePrefix string `koanf:"table_prefix" json:"table_prefix"`
Charset string `koanf:"charset" json:"charset"`
SSL bool `koanf:"ssl" json:"ssl"`
PrepareStmt *bool `koanf:"prepare_stmt" json:"prepare_stmt"`
}

type CacheConf struct {
Expand Down
7 changes: 7 additions & 0 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ func NewDB(conf config.DBConf) (*gorm.DB, error) {
},
}

// Enable Prepared Statement by default
if prepareStmt := conf.PrepareStmt; prepareStmt != nil {
gormConfig.PrepareStmt = *prepareStmt
} else {
gormConfig.PrepareStmt = true
}

var dsn string
if conf.Dsn != "" {
dsn = conf.Dsn
Expand Down
9 changes: 8 additions & 1 deletion internal/db/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ func OpenMySql(dsn string, gormConfig *gorm.Config) (*gorm.DB, error) {
}

func OpenPostgreSQL(dsn string, gormConfig *gorm.Config) (*gorm.DB, error) {
return gorm.Open(postgres.Open(dsn), gormConfig)
return gorm.Open(postgres.New(postgres.Config{
DSN: dsn,

// gorm v2 use `pgx` as postgres’s database/sql driver,
// it enables prepared statement cache by default,
// disable it when `PrepareStmt` is false by following code:
PreferSimpleProtocol: !gormConfig.PrepareStmt,
}), gormConfig)
}

func OpenSqlServer(dsn string, gormConfig *gorm.Config) (*gorm.DB, error) {
Expand Down

0 comments on commit 9c3228b

Please sign in to comment.