From 513bdc75faba6715036cda24eec5ebeef3433f24 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Sat, 19 Aug 2023 22:50:26 +0800 Subject: [PATCH] sqlx: add pgsql special implement for tweet create --- internal/dao/sakila/auto/pg/yesql.go | 60 ++++++++++++++++++++++++++ internal/dao/sakila/sqlx.go | 22 ++++++++++ internal/dao/sakila/tweets.go | 14 +++++- internal/dao/sakila/tweets_pg.go | 37 ++++++++++++++++ internal/dao/sakila/yesql/gen.go | 1 + internal/dao/sakila/yesql/yesql_pg.sql | 11 +++++ 6 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 internal/dao/sakila/auto/pg/yesql.go create mode 100644 internal/dao/sakila/tweets_pg.go create mode 100644 internal/dao/sakila/yesql/yesql_pg.sql diff --git a/internal/dao/sakila/auto/pg/yesql.go b/internal/dao/sakila/auto/pg/yesql.go new file mode 100644 index 00000000..42942c70 --- /dev/null +++ b/internal/dao/sakila/auto/pg/yesql.go @@ -0,0 +1,60 @@ +// Code generated by Yesql. DO NOT EDIT. +// versions: +// - Yesql v1.8.5 + +package pg + +import ( + "context" + "fmt" + + "github.com/alimy/yesql" + "github.com/bitbus/sqlx" +) + +var ( + _ = fmt.Errorf("error for placeholder") +) + +const ( + _TweetManage_AddPost = `INSERT INTO @post (user_id, tags, ip, ip_loc, attachment_price, visibility, latest_replied_on, created_on) VALUES (:user_id, :tags, :ip, :ip_loc, :attachment_price, :visibility, :latest_replied_on, :created_on)` +) + +// PreparexContext enhances the Conn interface with context. +type PreparexContext interface { + // PrepareContext prepares a statement. + // The provided context is used for the preparation of the statement, not for + // the execution of the statement. + PreparexContext(ctx context.Context, query string) (*sqlx.Stmt, error) + + // PrepareNamedContext returns an sqlx.NamedStmt + PrepareNamedContext(ctx context.Context, query string) (*sqlx.NamedStmt, error) + + // Rebind rebind query to adapte SQL Driver + Rebind(query string) string +} + +// PreparexBuilder preparex builder interface for sqlx +type PreparexBuilder interface { + PreparexContext + QueryHook(query string) string +} + +type TweetManage struct { + yesql.Namespace `yesql:"tweet_manage"` + AddPost *sqlx.NamedStmt `yesql:"add_post"` +} + +func BuildTweetManage(p PreparexBuilder, ctx ...context.Context) (obj *TweetManage, err error) { + var c context.Context + if len(ctx) > 0 && ctx[0] != nil { + c = ctx[0] + } else { + c = context.Background() + } + obj = &TweetManage{} + if obj.AddPost, err = p.PrepareNamedContext(c, p.Rebind(p.QueryHook(_TweetManage_AddPost))); err != nil { + return nil, fmt.Errorf("prepare _TweetManage_AddPost error: %w", err) + } + return +} diff --git a/internal/dao/sakila/sqlx.go b/internal/dao/sakila/sqlx.go index 9877d51e..b1fcde49 100644 --- a/internal/dao/sakila/sqlx.go +++ b/internal/dao/sakila/sqlx.go @@ -14,6 +14,7 @@ import ( yc "github.com/rocboss/paopao-ce/internal/dao/sakila/auto" "github.com/rocboss/paopao-ce/internal/dao/sakila/auto/ac" "github.com/rocboss/paopao-ce/internal/dao/sakila/auto/cc" + "github.com/rocboss/paopao-ce/internal/dao/sakila/auto/pg" "github.com/sirupsen/logrus" ) @@ -103,6 +104,7 @@ func mustBuild[T any](db *sqlx.DB, fn func(yc.PreparexBuilder, ...context.Contex return obj } +//lint:ignore U1000 mustBuildFn func mustBuildFn[T any](db *sqlx.DB, fn func(yc.PreparexBuilder) (T, error)) T { p := yesql.NewPreparexBuilder(db, t) obj, err := fn(p) @@ -148,6 +150,26 @@ func ccBuildFn[T any](db *sqlx.DB, fn func(cc.PreparexBuilder) (T, error)) T { return obj } +//lint:ignore U1000 pgBuild +func pgBuild[T any](db *sqlx.DB, fn func(pg.PreparexBuilder, ...context.Context) (T, error)) T { + p := yesql.NewPreparexBuilder(db, t) + obj, err := fn(p) + if err != nil { + logrus.Fatalf("build object failure: %s", err) + } + return obj +} + +//lint:ignore U1000 pgBuildFn +func pgBuildFn[T any](db *sqlx.DB, fn func(pg.PreparexBuilder) (T, error)) T { + p := yesql.NewPreparexBuilder(db, t) + obj, err := fn(p) + if err != nil { + logrus.Fatalf("build object failure: %s", err) + } + return obj +} + func initSqlxDB() { _db = conf.MustSqlxDB() _tablePrefix = conf.DatabaseSetting.TablePrefix diff --git a/internal/dao/sakila/tweets.go b/internal/dao/sakila/tweets.go index 7a253ebf..c0e16ff6 100644 --- a/internal/dao/sakila/tweets.go +++ b/internal/dao/sakila/tweets.go @@ -8,12 +8,14 @@ import ( "strings" "time" + "github.com/alimy/cfg" "github.com/bitbus/sqlx" "github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/internal/core/cs" "github.com/rocboss/paopao-ce/internal/core/ms" "github.com/rocboss/paopao-ce/internal/dao/jinzhu/dbr" "github.com/rocboss/paopao-ce/internal/dao/sakila/auto/cc" + "github.com/rocboss/paopao-ce/internal/dao/sakila/auto/pg" ) var ( @@ -477,12 +479,20 @@ func newTweetService(db *sqlx.DB) core.TweetService { } } -func newTweetManageService(db *sqlx.DB, cacheIndex core.CacheIndexService) core.TweetManageService { - return &tweetManageSrv{ +func newTweetManageService(db *sqlx.DB, cacheIndex core.CacheIndexService) (s core.TweetManageService) { + tms := &tweetManageSrv{ sqlxSrv: newSqlxSrv(db), cis: cacheIndex, q: ccBuild(db, cc.BuildTweetManage), } + s = tms + if cfg.Any("PostgreSQL", "PgSQL", "Postgres") { + s = &pgTweetManageSrv{ + tweetManageSrv: tms, + p: pgBuild(db, pg.BuildTweetManage), + } + } + return } func newTweetHelpService(db *sqlx.DB) core.TweetHelpService { diff --git a/internal/dao/sakila/tweets_pg.go b/internal/dao/sakila/tweets_pg.go new file mode 100644 index 00000000..4066f19e --- /dev/null +++ b/internal/dao/sakila/tweets_pg.go @@ -0,0 +1,37 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package sakila + +import ( + "time" + + "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/internal/core/ms" + "github.com/rocboss/paopao-ce/internal/dao/jinzhu/dbr" + "github.com/rocboss/paopao-ce/internal/dao/sakila/auto/pg" +) + +var ( + _ core.TweetManageService = (*pgTweetManageSrv)(nil) +) + +type pgTweetManageSrv struct { + *tweetManageSrv + p *pg.TweetManage +} + +func (s *pgTweetManageSrv) CreatePost(r *ms.Post) (*ms.Post, error) { + now := time.Now().Unix() + r.Model = &dbr.Model{CreatedOn: now} + r.LatestRepliedOn = now + var postId int64 + err := s.p.AddPost.Get(&postId, r) + if err != nil { + return nil, err + } + r.ID = postId + s.cis.SendAction(core.IdxActCreatePost, r) + return r, nil +} diff --git a/internal/dao/sakila/yesql/gen.go b/internal/dao/sakila/yesql/gen.go index 4bda5dc1..89eff8cd 100644 --- a/internal/dao/sakila/yesql/gen.go +++ b/internal/dao/sakila/yesql/gen.go @@ -26,6 +26,7 @@ func main() { yesql.NewSqlInfo("yesql.sql", "../auto", "yesql"), yesql.NewSqlInfo("yesql_ac.sql", "../auto/ac", "ac"), yesql.NewSqlInfo("yesql_cc.sql", "../auto/cc", "cc"), + yesql.NewSqlInfo("yesql_pg.sql", "../auto/pg", "pg"), } if err := yesql.GenerateFrom(sqlInfos, opt); err != nil { log.Fatalf("generate code occurs error: %s", err) diff --git a/internal/dao/sakila/yesql/yesql_pg.sql b/internal/dao/sakila/yesql/yesql_pg.sql new file mode 100644 index 00000000..b7c84681 --- /dev/null +++ b/internal/dao/sakila/yesql/yesql_pg.sql @@ -0,0 +1,11 @@ +-------------------------------------------------------------------------------- +-- file yesql.sql +-- this sql file contain some PostgreSQL special sql dml +-------------------------------------------------------------------------------- + +-- name: add_post@tweet_manage +-- prepare: named_stmt +INSERT INTO @post (user_id, tags, ip, ip_loc, attachment_price, visibility, latest_replied_on, created_on) +VALUES (:user_id, :tags, :ip, :ip_loc, :attachment_price, :visibility, :latest_replied_on, :created_on) +RETURNING id; +