dialect/sql/sqljson: cast marshaled args as json (#3008)

This commit is contained in:
Ariel Mashraki
2022-10-11 14:15:01 +03:00
committed by GitHub
parent a26e21ff6a
commit cf137c665a
14 changed files with 310 additions and 34 deletions

View File

@@ -3331,22 +3331,19 @@ func (b *Builder) Arg(a any) *Builder {
b.Join(a)
return b
}
b.total++
b.args = append(b.args, a)
// Default placeholder param (MySQL and SQLite).
param := "?"
format := "?"
if b.postgres() {
// Postgres' arguments are referenced using the syntax $n.
// $1 refers to the 1st argument, $2 to the 2nd, and so on.
param = "$" + strconv.Itoa(b.total)
format = "$" + strconv.Itoa(b.total+1)
}
if f, ok := a.(ParamFormatter); ok {
param = f.FormatParam(param, &StmtInfo{
format = f.FormatParam(format, &StmtInfo{
Dialect: b.dialect,
})
}
b.WriteString(param)
return b
return b.Argf(format, a)
}
// Args appends a list of arguments to the builder.
@@ -3360,6 +3357,29 @@ func (b *Builder) Args(a ...any) *Builder {
return b
}
// Argf appends an input argument to the builder
// with the given format. For example:
//
// FormatArg("JSON(?)", b).
// FormatArg("ST_GeomFromText(?)", geom)
func (b *Builder) Argf(format string, a any) *Builder {
switch a := a.(type) {
case nil:
b.WriteString("NULL")
return b
case *raw:
b.WriteString(a.s)
return b
case Querier:
b.Join(a)
return b
}
b.total++
b.args = append(b.args, a)
b.WriteString(format)
return b
}
// Comma adds a comma to the query.
func (b *Builder) Comma() *Builder {
return b.WriteString(", ")