2023-02-06 09:58:24 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-02-21 10:25:52 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
2023-02-06 09:58:24 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-02-22 08:53:53 +00:00
|
|
|
type SqlxQuery[T any] struct {
|
2023-02-06 09:58:24 +00:00
|
|
|
sqlx *sqlx.DB
|
2023-02-22 08:53:53 +00:00
|
|
|
UniversalDb[T]
|
2023-02-06 09:58:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 08:53:53 +00:00
|
|
|
func NewSqlxQuery[T any](sqlx *sqlx.DB, u UniversalDb[T]) *SqlxQuery[T] {
|
2023-02-20 17:07:32 +00:00
|
|
|
|
2023-02-22 08:53:53 +00:00
|
|
|
s := &SqlxQuery[T]{sqlx: sqlx, UniversalDb: u}
|
2023-02-20 17:07:32 +00:00
|
|
|
if u.selects == nil {
|
|
|
|
s.UniversalDb.selects = s.Selects
|
|
|
|
}
|
|
|
|
if u.gets == nil {
|
|
|
|
s.UniversalDb.gets = s.Gets
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-02-22 08:53:53 +00:00
|
|
|
func SetSelect[T any](db *SqlxQuery[T], fn QuerySelect[T]) {
|
2023-02-20 17:07:32 +00:00
|
|
|
db.selects = fn
|
|
|
|
}
|
2023-02-22 08:53:53 +00:00
|
|
|
func SetGet[T any](db *SqlxQuery[T], fn QueryGet[T]) {
|
2023-02-20 17:07:32 +00:00
|
|
|
db.gets = fn
|
2023-02-06 09:58:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 08:53:53 +00:00
|
|
|
func (s *SqlxQuery[T]) Selects(ctx context.Context, sql string, params ...any) (r []T, err error) {
|
2023-02-21 11:49:13 +00:00
|
|
|
v := ctx.Value("handle=>")
|
2023-02-20 17:07:32 +00:00
|
|
|
if v != nil {
|
2023-02-21 10:25:52 +00:00
|
|
|
vv, ok := v.(string)
|
|
|
|
if ok && vv != "" {
|
|
|
|
switch vv {
|
|
|
|
case "string":
|
2023-02-22 08:53:53 +00:00
|
|
|
//return ToMapSlice(r.sqlx, dest.(*[]map[string]string), sql, params...)
|
2023-02-21 11:49:13 +00:00
|
|
|
case "scanner":
|
|
|
|
fn := ctx.Value("fn")
|
2023-02-22 08:53:53 +00:00
|
|
|
return nil, Scanner[T](s.sqlx, sql, params...)(fn.(func(T)))
|
2023-02-20 17:07:32 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-06 09:58:24 +00:00
|
|
|
}
|
2023-02-22 08:53:53 +00:00
|
|
|
//var a T
|
|
|
|
err = s.sqlx.Select(&r, sql, params...)
|
|
|
|
return
|
2023-02-06 09:58:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 08:53:53 +00:00
|
|
|
func (s *SqlxQuery[T]) Gets(ctx context.Context, sql string, params ...any) (r T, err error) {
|
2023-02-21 11:49:13 +00:00
|
|
|
v := ctx.Value("handle=>")
|
2023-02-20 17:07:32 +00:00
|
|
|
if v != nil {
|
2023-02-21 10:25:52 +00:00
|
|
|
vv, ok := v.(string)
|
|
|
|
if ok && vv != "" {
|
|
|
|
switch vv {
|
|
|
|
case "string":
|
2023-02-22 08:53:53 +00:00
|
|
|
//return GetToMap(r.sqlx, dest.(*map[string]string), sql, params...)
|
2023-02-20 17:07:32 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-06 09:58:24 +00:00
|
|
|
}
|
2023-02-22 08:53:53 +00:00
|
|
|
err = s.sqlx.Get(&r, sql, params...)
|
|
|
|
return
|
2023-02-06 09:58:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 08:53:53 +00:00
|
|
|
func Scanner[T any](db *sqlx.DB, s string, params ...any) func(func(T)) error {
|
|
|
|
var v T
|
2023-02-21 11:49:13 +00:00
|
|
|
return func(fn func(T)) error {
|
|
|
|
rows, err := db.Queryx(s, params...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for rows.Next() {
|
2023-02-22 08:53:53 +00:00
|
|
|
err = rows.StructScan(&v)
|
2023-02-21 11:49:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fn(v)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 10:25:52 +00:00
|
|
|
func ToMapSlice[V any](db *sqlx.DB, dest *[]map[string]V, sql string, params ...any) (err error) {
|
|
|
|
rows, err := db.Query(sql, params...)
|
2023-02-20 17:07:32 +00:00
|
|
|
columns, err := rows.Columns()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-21 10:25:52 +00:00
|
|
|
defer rows.Close()
|
2023-02-20 17:07:32 +00:00
|
|
|
columnLen := len(columns)
|
2023-02-21 10:25:52 +00:00
|
|
|
c := make([]*V, columnLen)
|
2023-02-20 17:07:32 +00:00
|
|
|
for i, _ := range c {
|
2023-02-21 10:25:52 +00:00
|
|
|
var a V
|
2023-02-20 17:07:32 +00:00
|
|
|
c[i] = &a
|
|
|
|
}
|
2023-02-21 11:49:13 +00:00
|
|
|
args := slice.ToAnySlice(c)
|
2023-02-21 10:25:52 +00:00
|
|
|
var m []map[string]V
|
|
|
|
for rows.Next() {
|
2023-02-21 11:49:13 +00:00
|
|
|
err = rows.Scan(args...)
|
2023-02-21 10:25:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v := make(map[string]V)
|
|
|
|
for i, data := range c {
|
|
|
|
v[columns[i]] = *data
|
2023-02-20 17:07:32 +00:00
|
|
|
}
|
2023-02-21 10:25:52 +00:00
|
|
|
m = append(m, v)
|
2023-02-20 17:07:32 +00:00
|
|
|
}
|
2023-02-21 10:25:52 +00:00
|
|
|
*dest = m
|
2023-02-20 17:07:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-21 10:25:52 +00:00
|
|
|
func GetToMap[V any](db *sqlx.DB, dest *map[string]V, sql string, params ...any) (err error) {
|
|
|
|
rows := db.QueryRowx(sql, params...)
|
2023-02-20 17:07:32 +00:00
|
|
|
columns, err := rows.Columns()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
columnLen := len(columns)
|
2023-02-21 10:25:52 +00:00
|
|
|
c := make([]*V, columnLen)
|
2023-02-20 17:07:32 +00:00
|
|
|
for i, _ := range c {
|
2023-02-21 10:25:52 +00:00
|
|
|
var a V
|
2023-02-20 17:07:32 +00:00
|
|
|
c[i] = &a
|
|
|
|
}
|
2023-02-21 10:25:52 +00:00
|
|
|
err = rows.Scan(slice.ToAnySlice(c)...)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2023-02-20 17:07:32 +00:00
|
|
|
}
|
2023-02-21 10:25:52 +00:00
|
|
|
v := make(map[string]V)
|
|
|
|
for i, data := range c {
|
|
|
|
v[columns[i]] = *data
|
|
|
|
}
|
|
|
|
*dest = v
|
2023-02-20 17:07:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-10 13:23:30 +00:00
|
|
|
func FormatSql(sql string, params ...any) string {
|
2023-02-06 09:58:24 +00:00
|
|
|
for _, param := range params {
|
|
|
|
switch param.(type) {
|
|
|
|
case string:
|
|
|
|
sql = strings.Replace(sql, "?", fmt.Sprintf("'%s'", param.(string)), 1)
|
|
|
|
case int64:
|
|
|
|
sql = strings.Replace(sql, "?", strconv.FormatInt(param.(int64), 10), 1)
|
|
|
|
case int:
|
|
|
|
sql = strings.Replace(sql, "?", strconv.Itoa(param.(int)), 1)
|
|
|
|
case uint64:
|
|
|
|
sql = strings.Replace(sql, "?", strconv.FormatUint(param.(uint64), 10), 1)
|
|
|
|
case float64:
|
|
|
|
sql = strings.Replace(sql, "?", fmt.Sprintf("%f", param.(float64)), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sql
|
|
|
|
}
|