From 8397bcd31457147b7ce7509abf0b359c606d7b43 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Fri, 31 Mar 2023 07:22:29 +0800 Subject: [PATCH] add NewPrepareBuilder(...)/NewPreparexBuilder(...) help function --- examples/auto/yesql.go | 40 ++++++++++++------------- generator.go | 67 ++++++++++++++++++++++++++++++++++++++++++ version.go | 2 +- 3 files changed, 88 insertions(+), 21 deletions(-) diff --git a/examples/auto/yesql.go b/examples/auto/yesql.go index a8c937a..7a157d3 100644 --- a/examples/auto/yesql.go +++ b/examples/auto/yesql.go @@ -1,6 +1,6 @@ // Code generated by go-mir. DO NOT EDIT. // versions: -// - Yesql v1.1.0 +// - Yesql v1.1.1 package yesql @@ -12,45 +12,45 @@ import ( ) const ( + _LoginInfoB = `SELECT * FROM @user WHERE username=?` + _LoginInfoE = `SELECT * FROM @user WHERE username=?` + _UserInfoC = `SELECT "username" FROM @user WHERE username=?` _LoginInfoA = `SELECT * FROM @user WHERE username=?` + _UserInfoD = `SELECT "username", "nickname" FROM @user WHERE username=?` _LogoutInfoC = `SELECT * FROM @user WHERE username=?` + _LoginInfoD = `SELECT * FROM @user WHERE username=?` _UserInfoA = `SELECT ` + "`" + `username` + "`" + ` FROM @user WHERE username=?` - _UserInfoC = `SELECT "username" FROM @user WHERE username=?` - _UserInfoD = `SELECT "username", "nickname" FROM @user WHERE username=?` - _LoginInfoE = `SELECT * FROM @user WHERE username=?` _UserInfoB = `SELECT ` + "`" + `username` + "`" + `, ` + "`" + `nickname` + "`" + ` FROM @user WHERE username=?` - _LoginInfoB = `SELECT * FROM @user WHERE username=?` - _LoginInfoD = `SELECT * FROM @user WHERE username=?` _InsertTag_Topic = `INSERT INTO @tag (user_id, tag, created_on, modified_on, quote_num) VALUES (?, ?, ?, ?, 1);` + _TagsByIdB_Topic = `SELECT id, user_id, tag, quote_num FROM @tag WHERE id IN (?);` _DecrTagsById_Topic = `UPDATE @tag SET quote_num=quote_num-1, modified_on=? WHERE id IN (?);` _IncrTagsById_Topic = `UPDATE @tag SET quote_num=quote_num+1, is_del=0, modified_on=? WHERE id IN (?);` _NewestTags_Topic = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id, u.nickname, u.username, u.status, u.avatar, u.is_admin FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.id DESC LIMIT ? OFFSET ?;` - _HotTags_Topic = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id, u.nickname, u.username, u.status, u.avatar, u.is_admin FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.quote_num DESC LIMIT ? OFFSET ?;` - _TagsByKeywordB_Topic = `SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 AND tag LIKE ? ORDER BY quote_num DESC LIMIT 6;` - _TagsForIncr_Topic = `SELECT id, user_id, tag, quote_num FROM @tag WHERE tag IN (?);` _TagsByKeywordA_Topic = `SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 ORDER BY quote_num DESC LIMIT 6;` _TagsByIdA_Topic = `SELECT id FROM @tag WHERE id IN (?) AND is_del = 0 AND quote_num > 0;` - _TagsByIdB_Topic = `SELECT id, user_id, tag, quote_num FROM @tag WHERE id IN (?);` - _TagsByIdA_Shutter = `SELECT id FROM @tag WHERE id IN (?) AND is_del = 0 AND quote_num > 0;` + _TagsForIncr_Topic = `SELECT id, user_id, tag, quote_num FROM @tag WHERE tag IN (?);` + _HotTags_Topic = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id, u.nickname, u.username, u.status, u.avatar, u.is_admin FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.quote_num DESC LIMIT ? OFFSET ?;` + _TagsByKeywordB_Topic = `SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 AND tag LIKE ? ORDER BY quote_num DESC LIMIT 6;` _HotTags_Shutter = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id, u.nickname, u.username, u.status, u.avatar, u.is_admin FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.quote_num DESC LIMIT ? OFFSET ?;` _TagsByKeywordA_Shutter = `SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 ORDER BY quote_num DESC LIMIT 6;` - _TagsByKeywordB_Shutter = `SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 AND tag LIKE ? ORDER BY quote_num DESC LIMIT 6;` - _InsertTag_Shutter = `INSERT INTO @tag (user_id, tag, created_on, modified_on, quote_num) VALUES (?, ?, ?, ?, 1);` + _TagsByIdA_Shutter = `SELECT id FROM @tag WHERE id IN (?) AND is_del = 0 AND quote_num > 0;` _TagsByIdB_Shutter = `SELECT id, user_id, tag, quote_num FROM @tag WHERE id IN (?);` _DecrTagsById_Shutter = `UPDATE @tag SET quote_num=quote_num-1, modified_on=? WHERE id IN (?);` - _TagsForIncr_Shutter = `SELECT id, user_id, tag, quote_num FROM @tag WHERE tag IN (?);` _IncrTagsById_Shutter = `UPDATE @tag SET quote_num=quote_num+1, is_del=0, modified_on=? WHERE id IN (?);` _NewestTags_Shutter = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id, u.nickname, u.username, u.status, u.avatar, u.is_admin FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.id DESC LIMIT ? OFFSET ?;` - _TagsByKeywordB_TagsInfo = `SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 AND tag LIKE ? ORDER BY quote_num DESC LIMIT 6;` + _InsertTag_Shutter = `INSERT INTO @tag (user_id, tag, created_on, modified_on, quote_num) VALUES (?, ?, ?, ?, 1);` + _TagsForIncr_Shutter = `SELECT id, user_id, tag, quote_num FROM @tag WHERE tag IN (?);` + _TagsByKeywordB_Shutter = `SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 AND tag LIKE ? ORDER BY quote_num DESC LIMIT 6;` + _NewestTags_TagsInfo = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id, u.nickname, u.username, u.status, u.avatar, u.is_admin FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.id DESC LIMIT ? OFFSET ?;` + _TagsByKeywordA_TagsInfo = `SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 ORDER BY quote_num DESC LIMIT 6;` _InsertTag_TagsInfo = `INSERT INTO @tag (user_id, tag, created_on, modified_on, quote_num) VALUES (?, ?, ?, ?, 1);` _TagsByIdA_TagsInfo = `SELECT id FROM @tag WHERE id IN (?) AND is_del = 0 AND quote_num > 0;` + _DecrTagsById_TagsInfo = `UPDATE @tag SET quote_num=quote_num-1, modified_on=? WHERE id IN (?);` + _IncrTagsById_TagsInfo = `UPDATE @tag SET quote_num=quote_num+1, is_del=0, modified_on=? WHERE id IN (?);` + _HotTags_TagsInfo = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id, u.nickname, u.username, u.status, u.avatar, u.is_admin FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.quote_num DESC LIMIT ? OFFSET ?;` + _TagsByKeywordB_TagsInfo = `SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 AND tag LIKE ? ORDER BY quote_num DESC LIMIT 6;` _TagsByIdB_TagsInfo = `SELECT id, user_id, tag, quote_num FROM @tag WHERE id IN (?);` _TagsForIncr_TagsInfo = `SELECT id, user_id, tag, quote_num FROM @tag WHERE tag IN (?);` - _HotTags_TagsInfo = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id, u.nickname, u.username, u.status, u.avatar, u.is_admin FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.quote_num DESC LIMIT ? OFFSET ?;` - _TagsByKeywordA_TagsInfo = `SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 ORDER BY quote_num DESC LIMIT 6;` - _IncrTagsById_TagsInfo = `UPDATE @tag SET quote_num=quote_num+1, is_del=0, modified_on=? WHERE id IN (?);` - _NewestTags_TagsInfo = `SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id, u.nickname, u.username, u.status, u.avatar, u.is_admin FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.id DESC LIMIT ? OFFSET ?;` - _DecrTagsById_TagsInfo = `UPDATE @tag SET quote_num=quote_num-1, modified_on=? WHERE id IN (?);` ) type Yesql struct { diff --git a/generator.go b/generator.go index 53f0f72..eff681e 100644 --- a/generator.go +++ b/generator.go @@ -1,12 +1,15 @@ package yesql import ( + "context" + "database/sql" "os" "path/filepath" stdTmpl "text/template" "github.com/alimy/yesql/naming" "github.com/alimy/yesql/template" + "github.com/jmoiron/sqlx" ) var ( @@ -22,10 +25,50 @@ type tmplCtx struct { YesqlVer string } +type simplePrepareBuilder struct { + p PrepareContext + hook func(string) string +} + +type simplePreparexBuilder struct { + p PreparexContext + hook func(string) string +} + type sqlGenerator struct { tmpl *stdTmpl.Template } +func (s *simplePrepareBuilder) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) { + return s.p.PrepareContext(ctx, query) +} + +func (s *simplePrepareBuilder) QueryHook(query string) string { + if s.hook != nil { + return s.hook(query) + } + return query +} + +func (s *simplePreparexBuilder) PreparexContext(ctx context.Context, query string) (*sqlx.Stmt, error) { + return s.p.PreparexContext(ctx, query) +} + +func (s *simplePreparexBuilder) PrepareNamedContext(ctx context.Context, query string) (*sqlx.NamedStmt, error) { + return s.p.PrepareNamedContext(ctx, query) +} + +func (s *simplePreparexBuilder) Rebind(query string) string { + return s.p.Rebind(query) +} + +func (s *simplePreparexBuilder) QueryHook(query string) string { + if s.hook != nil { + return s.hook(query) + } + return query +} + func (t *tmplCtx) DefaultQueryMapNotEmpty() bool { return len(t.DefaultQueryMap) != 0 } @@ -70,12 +113,36 @@ func (s *sqlGenerator) Generate(dstPath string, pkgName string, query SQLQuery, return s.tmpl.Execute(file, data) } +// NewPrepareBuilder create a simple prepare builder instance +func NewPrepareBuilder(p PrepareContext, hook ...func(string) string) PrepareBuilder { + obj := &simplePrepareBuilder{ + p: p, + } + if len(hook) > 0 && hook[0] != nil { + obj.hook = hook[0] + } + return obj +} + +// NewPreprarexBuilder create a simple preparex builder instance +func NewPreparexBuilder(p PreparexContext, hook ...func(string) string) PreparexBuilder { + obj := &simplePreparexBuilder{ + p: p, + } + if len(hook) > 0 && hook[0] != nil { + obj.hook = hook[0] + } + return obj +} + +// NewSqlGenerator create a sql generator use std sql func NewSqlGenerator() Generator { return &sqlGenerator{ tmpl: template.NewSqlTemplate(), } } +// NewSqlxGenerator create a sqlx generator use sqlx func NewSqlxGenerator() Generator { return &sqlGenerator{ tmpl: template.NewSqlxTemplate(), diff --git a/version.go b/version.go index 9a544a2..9816bb8 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package yesql -var Version = "v1.1.0" +var Version = "v1.1.1"