From ee108474ad74d43adda58160ece80db938d41a1b Mon Sep 17 00:00:00 2001 From: E99p1ant Date: Thu, 11 Jan 2024 00:54:28 +0800 Subject: [PATCH] dbutil: add censor pass type --- internal/cmd/censor.go | 2 +- internal/cmd/web.go | 2 +- internal/db/questions.go | 24 ++++++++++++------------ internal/dbutil/type.go | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 14 deletions(-) create mode 100644 internal/dbutil/type.go diff --git a/internal/cmd/censor.go b/internal/cmd/censor.go index 5b8490e..ea3a4e0 100644 --- a/internal/cmd/censor.go +++ b/internal/cmd/censor.go @@ -31,7 +31,7 @@ func runCensor(ctx *cli.Context) error { return errors.New("text censor is disabled") } - dsn := fmt.Sprintf("%s:%s@%s:%s/%s?charset=utf8mb4&parseTime=True&loc=Local", + dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", conf.Database.User, conf.Database.Password, conf.Database.Host, diff --git a/internal/cmd/web.go b/internal/cmd/web.go index f8442de..7b2d4e0 100644 --- a/internal/cmd/web.go +++ b/internal/cmd/web.go @@ -51,7 +51,7 @@ func runWeb(ctx *cli.Context) error { var dsn string switch dbType { case "mysql", "": - dsn = fmt.Sprintf("%s:%s@%s:%s/%s?charset=utf8mb4&parseTime=True&loc=Local", + dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", conf.Database.User, conf.Database.Password, conf.Database.Host, diff --git a/internal/db/questions.go b/internal/db/questions.go index 0fffe81..f3c8ce2 100644 --- a/internal/db/questions.go +++ b/internal/db/questions.go @@ -43,18 +43,18 @@ type questions struct { type Question struct { dbutil.Model - FromIP string `json:"-"` - UserID uint `gorm:"index:idx_question_user_id" json:"-"` - Content string `json:"content"` - ContentCensorMetadata datatypes.JSON `json:"-"` - ContentCensorPass bool `gorm:"->;type:boolean GENERATED ALWAYS AS (IFNULL(content_censor_metadata->'$.pass' = true, false)) STORED NOT NULL" json:"-"` - Token string `json:"-"` - Answer string `json:"answer"` - AnswerCensorMetadata datatypes.JSON `json:"-"` - AnswerCensorPass bool `gorm:"->;type:boolean GENERATED ALWAYS AS (IFNULL(answer_censor_metadata->'$.pass' = true, false)) STORED NOT NULL" json:"-"` - ReceiveReplyEmail string `json:"-"` - AskerUserID uint `json:"-"` - IsPrivate bool `gorm:"default: FALSE; NOT NULL" json:"-"` + FromIP string `json:"-"` + UserID uint `gorm:"index:idx_question_user_id" json:"-"` + Content string `json:"content"` + ContentCensorMetadata datatypes.JSON `json:"-"` + ContentCensorPass dbutil.ContentCensorPass `json:"-"` + Token string `json:"-"` + Answer string `json:"answer"` + AnswerCensorMetadata datatypes.JSON `json:"-"` + AnswerCensorPass dbutil.AnswerCensorPass `json:"-"` + ReceiveReplyEmail string `json:"-"` + AskerUserID uint `json:"-"` + IsPrivate bool `gorm:"default: FALSE; NOT NULL" json:"-"` } type CreateQuestionOptions struct { diff --git a/internal/dbutil/type.go b/internal/dbutil/type.go new file mode 100644 index 0000000..deac0c2 --- /dev/null +++ b/internal/dbutil/type.go @@ -0,0 +1,34 @@ +// Copyright 2024 E99p1ant. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package dbutil + +import ( + "gorm.io/gorm" + "gorm.io/gorm/schema" +) + +type ContentCensorPass bool + +func (ContentCensorPass) GormDBDataType(db *gorm.DB, field *schema.Field) string { + switch db.Dialector.Name() { + case "mysql": + return "boolean GENERATED ALWAYS AS (IFNULL(content_censor_metadata->'$.pass' = true, false)) STORED NOT NULL" + case "postgres": + return "BOOLEAN GENERATED ALWAYS AS (COALESCE(content_censor_metadata->>'$.pass' = 'true', false)) STORED" + } + return "" +} + +type AnswerCensorPass bool + +func (AnswerCensorPass) GormDBDataType(db *gorm.DB, field *schema.Field) string { + switch db.Dialector.Name() { + case "mysql": + return "boolean GENERATED ALWAYS AS (IFNULL(answer_censor_metadata->'$.pass' = true, false)) STORED NOT NULL" + case "postgres": + return "BOOLEAN GENERATED ALWAYS AS (COALESCE(answer_censor_metadata->>'$.pass' = 'true', false)) STORED" + } + return "" +}