Add statements debug sql support.

This commit is contained in:
zer0sub 2019-05-12 18:15:23 +02:00
parent 439c9f1ef9
commit 240ddd65e6
27 changed files with 1013 additions and 426 deletions

View file

@ -3,12 +3,33 @@ package sqlbuilder
import (
"database/sql"
"github.com/sub0zero/go-sqlbuilder/types"
"strconv"
"strings"
)
type statement interface {
type Statement interface {
// String returns generated SQL as string.
Sql() (query string, args []interface{}, err error)
DebugSql() (query string, err error)
Query(db types.Db, destination interface{}) error
Execute(db types.Db) (sql.Result, error)
}
func DebugSql(statement Statement) (string, error) {
sql, args, err := statement.Sql()
if err != nil {
return "", err
}
debugSql := sql
for i, arg := range args {
argPlaceholder := "$" + strconv.Itoa(i+1)
debugSql = strings.Replace(debugSql, argPlaceholder, ArgToString(arg), 1)
}
return debugSql, nil
}