Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
4b55dbaacf
|
|||
c399fa42ae
|
|||
9e586f7706
|
|||
3cc8dccc63
|
|||
7fedfbca81
|
|||
3c439ba428
|
@@ -53,15 +53,11 @@ func (w *GinRoutesWrapper) Group(relativePath string) *GinRoutesWrapper {
|
|||||||
func (w *GinRoutesWrapper) Use(middleware ...gin.HandlerFunc) *GinRoutesWrapper {
|
func (w *GinRoutesWrapper) Use(middleware ...gin.HandlerFunc) *GinRoutesWrapper {
|
||||||
defHandler := langext.ArrCopy(w.defaultHandler)
|
defHandler := langext.ArrCopy(w.defaultHandler)
|
||||||
defHandler = append(defHandler, middleware...)
|
defHandler = append(defHandler, middleware...)
|
||||||
return &GinRoutesWrapper{wrapper: w.wrapper, routes: w.routes, defaultHandler: defHandler}
|
return &GinRoutesWrapper{wrapper: w.wrapper, routes: w.routes, defaultHandler: defHandler, absPath: w.absPath}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *GinRoutesWrapper) WithJSONFilter(filter string) *GinRoutesWrapper {
|
func (w *GinRoutesWrapper) WithJSONFilter(filter string) *GinRoutesWrapper {
|
||||||
defHandler := langext.ArrCopy(w.defaultHandler)
|
return w.Use(func(g *gin.Context) { g.Set("goext.jsonfilter", filter) })
|
||||||
defHandler = append(defHandler, func(g *gin.Context) {
|
|
||||||
g.Set("goext.jsonfilter", filter)
|
|
||||||
})
|
|
||||||
return &GinRoutesWrapper{wrapper: w.wrapper, routes: w.routes, defaultHandler: defHandler}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *GinRoutesWrapper) GET(relativePath string) *GinRouteBuilder {
|
func (w *GinRoutesWrapper) GET(relativePath string) *GinRouteBuilder {
|
||||||
@@ -116,10 +112,7 @@ func (w *GinRouteBuilder) Use(middleware ...gin.HandlerFunc) *GinRouteBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *GinRouteBuilder) WithJSONFilter(filter string) *GinRouteBuilder {
|
func (w *GinRouteBuilder) WithJSONFilter(filter string) *GinRouteBuilder {
|
||||||
w.handlers = append(w.handlers, func(g *gin.Context) {
|
return w.Use(func(g *gin.Context) { g.Set("goext.jsonfilter", filter) })
|
||||||
g.Set("goext.jsonfilter", filter)
|
|
||||||
})
|
|
||||||
return w
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *GinRouteBuilder) Handle(handler WHandlerFunc) {
|
func (w *GinRouteBuilder) Handle(handler WHandlerFunc) {
|
||||||
|
8
go.mod
8
go.mod
@@ -8,14 +8,14 @@ require (
|
|||||||
github.com/jmoiron/sqlx v1.3.5
|
github.com/jmoiron/sqlx v1.3.5
|
||||||
github.com/rs/xid v1.5.0
|
github.com/rs/xid v1.5.0
|
||||||
github.com/rs/zerolog v1.32.0
|
github.com/rs/zerolog v1.32.0
|
||||||
go.mongodb.org/mongo-driver v1.13.1
|
go.mongodb.org/mongo-driver v1.14.0
|
||||||
golang.org/x/crypto v0.19.0
|
golang.org/x/crypto v0.19.0
|
||||||
golang.org/x/sys v0.17.0
|
golang.org/x/sys v0.17.0
|
||||||
golang.org/x/term v0.17.0
|
golang.org/x/term v0.17.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/bytedance/sonic v1.10.2 // indirect
|
github.com/bytedance/sonic v1.11.0 // indirect
|
||||||
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
|
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
|
||||||
github.com/chenzhuoyu/iasm v0.9.1 // indirect
|
github.com/chenzhuoyu/iasm v0.9.1 // indirect
|
||||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
@@ -23,13 +23,13 @@ require (
|
|||||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
github.com/go-playground/locales v0.14.1 // indirect
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
github.com/go-playground/validator/v10 v10.17.0 // indirect
|
github.com/go-playground/validator/v10 v10.18.0 // indirect
|
||||||
github.com/goccy/go-json v0.10.2 // indirect
|
github.com/goccy/go-json v0.10.2 // indirect
|
||||||
github.com/golang/snappy v0.0.4 // indirect
|
github.com/golang/snappy v0.0.4 // indirect
|
||||||
github.com/google/uuid v1.5.0 // indirect
|
github.com/google/uuid v1.5.0 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/klauspost/compress v1.17.6 // indirect
|
github.com/klauspost/compress v1.17.6 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
|
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||||
github.com/leodido/go-urn v1.4.0 // indirect
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
8
go.sum
8
go.sum
@@ -2,6 +2,8 @@ github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1
|
|||||||
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
|
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
|
||||||
github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE=
|
github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE=
|
||||||
github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
|
github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
|
||||||
|
github.com/bytedance/sonic v1.11.0 h1:FwNNv6Vu4z2Onf1++LNzxB/QhitD8wuTdpZzMTGITWo=
|
||||||
|
github.com/bytedance/sonic v1.11.0/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
|
||||||
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
|
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
|
||||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
|
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
|
||||||
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
|
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
|
||||||
@@ -33,6 +35,8 @@ github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqR
|
|||||||
github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
|
github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
|
||||||
github.com/go-playground/validator/v10 v10.17.0 h1:SmVVlfAOtlZncTxRuinDPomC2DkXJ4E5T9gDA0AIH74=
|
github.com/go-playground/validator/v10 v10.17.0 h1:SmVVlfAOtlZncTxRuinDPomC2DkXJ4E5T9gDA0AIH74=
|
||||||
github.com/go-playground/validator/v10 v10.17.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
|
github.com/go-playground/validator/v10 v10.17.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
|
||||||
|
github.com/go-playground/validator/v10 v10.18.0 h1:BvolUXjp4zuvkZ5YN5t7ebzbhlUtPsPm2S9NAZ5nl9U=
|
||||||
|
github.com/go-playground/validator/v10 v10.18.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||||
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
|
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
|
||||||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||||
@@ -62,6 +66,8 @@ github.com/klauspost/compress v1.17.6/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6K
|
|||||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc=
|
github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc=
|
||||||
github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||||
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
|
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
|
||||||
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
|
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
|
||||||
@@ -126,6 +132,8 @@ github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/
|
|||||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
go.mongodb.org/mongo-driver v1.13.1 h1:YIc7HTYsKndGK4RFzJ3covLz1byri52x0IoMB0Pt/vk=
|
go.mongodb.org/mongo-driver v1.13.1 h1:YIc7HTYsKndGK4RFzJ3covLz1byri52x0IoMB0Pt/vk=
|
||||||
go.mongodb.org/mongo-driver v1.13.1/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo=
|
go.mongodb.org/mongo-driver v1.13.1/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo=
|
||||||
|
go.mongodb.org/mongo-driver v1.14.0 h1:P98w8egYRjYe3XDjxhYJagTokP/H6HzlsnojRgZRd80=
|
||||||
|
go.mongodb.org/mongo-driver v1.14.0/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c=
|
||||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||||
golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc=
|
golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc=
|
||||||
golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.385"
|
const GoextVersion = "0.0.391"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2024-02-09T15:40:09+0100"
|
const GoextVersionTimestamp = "2024-02-21T16:18:04+0100"
|
||||||
|
@@ -265,6 +265,15 @@ func ArrFirstIndex[T comparable](arr []T, needle T) int {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ArrFirstIndexFunc[T any](arr []T, comp func(v T) bool) int {
|
||||||
|
for i, v := range arr {
|
||||||
|
if comp(v) {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
func ArrLastIndex[T comparable](arr []T, needle T) int {
|
func ArrLastIndex[T comparable](arr []T, needle T) int {
|
||||||
result := -1
|
result := -1
|
||||||
for i, v := range arr {
|
for i, v := range arr {
|
||||||
@@ -275,6 +284,16 @@ func ArrLastIndex[T comparable](arr []T, needle T) int {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ArrLastIndexFunc[T any](arr []T, comp func(v T) bool) int {
|
||||||
|
result := -1
|
||||||
|
for i, v := range arr {
|
||||||
|
if comp(v) {
|
||||||
|
result = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
func AddToSet[T comparable](set []T, add T) []T {
|
func AddToSet[T comparable](set []T, add T) []T {
|
||||||
for _, v := range set {
|
for _, v := range set {
|
||||||
if v == add {
|
if v == add {
|
||||||
|
49
sq/filter.go
Normal file
49
sq/filter.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package sq
|
||||||
|
|
||||||
|
import ct "gogs.mikescher.com/BlackForestBytes/goext/cursortoken"
|
||||||
|
|
||||||
|
type FilterSort struct {
|
||||||
|
Field string
|
||||||
|
Direction ct.SortDirection
|
||||||
|
}
|
||||||
|
|
||||||
|
type PaginateFilter interface {
|
||||||
|
SQL(params PP) (filterClause string, joinClause string, joinTables []string)
|
||||||
|
Sort() []FilterSort
|
||||||
|
}
|
||||||
|
|
||||||
|
type genericPaginateFilter struct {
|
||||||
|
sql func(params PP) (filterClause string, joinClause string, joinTables []string)
|
||||||
|
sort func() []FilterSort
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g genericPaginateFilter) SQL(params PP) (filterClause string, joinClause string, joinTables []string) {
|
||||||
|
return g.sql(params)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g genericPaginateFilter) Sort() []FilterSort {
|
||||||
|
return g.sort()
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPaginateFilter(sql func(params PP) (filterClause string, joinClause string, joinTables []string), sort []FilterSort) PaginateFilter {
|
||||||
|
return genericPaginateFilter{
|
||||||
|
sql: func(params PP) (filterClause string, joinClause string, joinTables []string) {
|
||||||
|
return sql(params)
|
||||||
|
},
|
||||||
|
sort: func() []FilterSort {
|
||||||
|
return sort
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSimplePaginateFilter(filterClause string, filterParams PP, sort []FilterSort) PaginateFilter {
|
||||||
|
return genericPaginateFilter{
|
||||||
|
sql: func(params PP) (string, string, []string) {
|
||||||
|
params.AddAll(filterParams)
|
||||||
|
return filterClause, "", nil
|
||||||
|
},
|
||||||
|
sort: func() []FilterSort {
|
||||||
|
return sort
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
@@ -3,22 +3,11 @@ package sq
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
ct "gogs.mikescher.com/BlackForestBytes/goext/cursortoken"
|
|
||||||
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
|
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
|
||||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||||
pag "gogs.mikescher.com/BlackForestBytes/goext/pagination"
|
pag "gogs.mikescher.com/BlackForestBytes/goext/pagination"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PaginateFilter interface {
|
|
||||||
SQL(params PP) (filterClause string, joinClause string, joinTables []string)
|
|
||||||
Sort() []FilterSort
|
|
||||||
}
|
|
||||||
|
|
||||||
type FilterSort struct {
|
|
||||||
Field string
|
|
||||||
Direction ct.SortDirection
|
|
||||||
}
|
|
||||||
|
|
||||||
func Paginate[TData any](ctx context.Context, q Queryable, table string, filter PaginateFilter, scanMode StructScanMode, scanSec StructScanSafety, page int, limit *int) ([]TData, pag.Pagination, error) {
|
func Paginate[TData any](ctx context.Context, q Queryable, table string, filter PaginateFilter, scanMode StructScanMode, scanSec StructScanSafety, page int, limit *int) ([]TData, pag.Pagination, error) {
|
||||||
prepParams := PP{}
|
prepParams := PP{}
|
||||||
|
|
||||||
|
@@ -20,6 +20,12 @@ func (pp *PP) Add(v any) string {
|
|||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pp *PP) AddAll(other PP) {
|
||||||
|
for id, v := range other {
|
||||||
|
(*pp)[id] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func PPID() string {
|
func PPID() string {
|
||||||
return "p_" + langext.RandBase62(8)
|
return "p_" + langext.RandBase62(8)
|
||||||
}
|
}
|
||||||
|
@@ -40,6 +40,49 @@ func InsertSingle[TData any](ctx context.Context, q Queryable, tableName string,
|
|||||||
return sqlr, nil
|
return sqlr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func InsertAndQuerySingle[TData any](ctx context.Context, q Queryable, tableName string, v TData, idColumn string, mode StructScanMode, sec StructScanSafety) (TData, error) {
|
||||||
|
|
||||||
|
rval := reflect.ValueOf(v)
|
||||||
|
|
||||||
|
idRVal := fieldByTag(rval, "db", idColumn)
|
||||||
|
if !idRVal.IsValid() || idRVal.IsZero() {
|
||||||
|
return *new(TData), fmt.Errorf("failed to find idColumn '%s' in %T", idColumn, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
idValue, err := convertValueToDB(q, idRVal.Interface())
|
||||||
|
if err != nil {
|
||||||
|
return *new(TData), err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = InsertSingle[TData](ctx, q, tableName, v)
|
||||||
|
if err != nil {
|
||||||
|
return *new(TData), err
|
||||||
|
}
|
||||||
|
|
||||||
|
pp := PP{}
|
||||||
|
|
||||||
|
//goland:noinspection ALL
|
||||||
|
sqlstr := fmt.Sprintf("SELECT * FROM %s WHERE %s = :%s", tableName, idColumn, pp.Add(idValue))
|
||||||
|
|
||||||
|
return QuerySingle[TData](ctx, q, sqlstr, pp, mode, sec)
|
||||||
|
}
|
||||||
|
|
||||||
|
func fieldByTag(rval reflect.Value, tagkey string, tagval string) reflect.Value {
|
||||||
|
rtyp := rval.Type()
|
||||||
|
for i := 0; i < rtyp.NumField(); i++ {
|
||||||
|
rsfield := rtyp.Field(i)
|
||||||
|
|
||||||
|
if !rsfield.IsExported() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if rsfield.Tag.Get(tagkey) == tagval {
|
||||||
|
return rval.Field(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
panic(fmt.Sprintf("tag %s = '%s' not found in %s", tagkey, tagval, rtyp.Name()))
|
||||||
|
}
|
||||||
|
|
||||||
func InsertMultiple[TData any](ctx context.Context, q Queryable, tableName string, vArr []TData, maxBatch int) ([]sql.Result, error) {
|
func InsertMultiple[TData any](ctx context.Context, q Queryable, tableName string, vArr []TData, maxBatch int) ([]sql.Result, error) {
|
||||||
|
|
||||||
if len(vArr) == 0 {
|
if len(vArr) == 0 {
|
||||||
@@ -95,7 +138,7 @@ func UpdateAndQuerySingle[TData any](ctx context.Context, q Queryable, tableName
|
|||||||
|
|
||||||
rval := reflect.ValueOf(v)
|
rval := reflect.ValueOf(v)
|
||||||
|
|
||||||
idRVal := rval.FieldByName(idColumn)
|
idRVal := fieldByTag(rval, "db", idColumn)
|
||||||
if !idRVal.IsValid() || idRVal.IsZero() {
|
if !idRVal.IsValid() || idRVal.IsZero() {
|
||||||
return *new(TData), fmt.Errorf("failed to find idColumn '%s' in %T", idColumn, v)
|
return *new(TData), fmt.Errorf("failed to find idColumn '%s' in %T", idColumn, v)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user