Last active
September 27, 2019 18:26
-
-
Save chernomyrdin/e4e0a66d4c456ca206d76baedd538e3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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