Skip to content

Commit

Permalink
dbutil: add censor pass type
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhan005 committed Jan 10, 2024
1 parent d038028 commit ee10847
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/censor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 12 additions & 12 deletions internal/db/questions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions internal/dbutil/type.go
Original file line number Diff line number Diff line change
@@ -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 ""
}

0 comments on commit ee10847

Please sign in to comment.