Skip to content

Instantly share code, notes, and snippets.

@chernomyrdin
Last active September 27, 2019 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chernomyrdin/e4e0a66d4c456ca206d76baedd538e3e to your computer and use it in GitHub Desktop.
Save chernomyrdin/e4e0a66d4c456ca206d76baedd538e3e to your computer and use it in GitHub Desktop.
package qb
import (
"io"
"strconv"
"strings"
)
const PlaceHolder = "?"
type builder interface {
build(b io.StringWriter, arg []interface{}) []interface{}
}
type expr struct {
expression string
arguments []interface{}
}
func (a *expr) build(b io.StringWriter, arg []interface{}) []interface{} {
var (
column = a.expression
max = strings.Count(column, PlaceHolder)
)
for i, a := range a.arguments {
if i >= max {
break
}
switch v := a.(type) {
case builder:
n := new(strings.Builder)
arg = append(arg, v.args(n, nil)...)
column = strings.Replace(column, PlaceHolder, n.String(), 1)
default:
arg = append(arg, a)
column = strings.Replace(column, PlaceHolder, "$"+strconv.Itoa(len(arg)), 1)
}
}
return arg
}
func Expr(expression string, arg ...interface{}) *expr {
return &expr{
expression: expression,
arguments: arg,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment