wp-go/model/sqxquery.go

52 lines
1.2 KiB
Go
Raw Normal View History

2023-02-06 09:58:24 +00:00
package model
import (
"context"
"fmt"
"github.com/jmoiron/sqlx"
"log"
"os"
"strconv"
"strings"
)
type SqlxQuery struct {
sqlx *sqlx.DB
}
func NewSqlxQuery(sqlx *sqlx.DB) SqlxQuery {
return SqlxQuery{sqlx: sqlx}
}
func (r SqlxQuery) Select(ctx context.Context, dest any, sql string, params ...any) error {
if os.Getenv("SHOW_SQL") == "true" {
go log.Println(formatSql(sql, params))
}
return r.sqlx.Select(dest, sql, params...)
}
func (r SqlxQuery) Get(ctx context.Context, dest any, sql string, params ...any) error {
if os.Getenv("SHOW_SQL") == "true" {
go log.Println(formatSql(sql, params))
}
return r.sqlx.Get(dest, sql, params...)
}
func formatSql(sql string, params []any) string {
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
}