2019-03-02 12:34:08 +01:00
|
|
|
package sqlbuilder
|
|
|
|
|
|
|
|
|
|
import (
|
2019-03-05 18:55:47 +01:00
|
|
|
"database/sql"
|
2019-04-20 19:49:29 +02:00
|
|
|
"github.com/sub0zero/go-sqlbuilder/types"
|
2019-05-12 18:15:23 +02:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2019-03-02 12:34:08 +01:00
|
|
|
)
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
type Statement interface {
|
2019-03-02 12:34:08 +01:00
|
|
|
// String returns generated SQL as string.
|
2019-04-29 14:39:48 +02:00
|
|
|
Sql() (query string, args []interface{}, err error)
|
2019-03-02 12:34:08 +01:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
DebugSql() (query string, err error)
|
|
|
|
|
|
2019-04-20 19:49:29 +02:00
|
|
|
Query(db types.Db, destination interface{}) error
|
|
|
|
|
Execute(db types.Db) (sql.Result, error)
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
2019-05-12 18:15:23 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|