GenerateParamFlags -> Placeholders
- Rewrite Placeholders to not use as much wizardry and magnetism
This commit is contained in:
parent
a98e474c9c
commit
8e3c1d41da
6 changed files with 20 additions and 43 deletions
|
@ -117,19 +117,6 @@ Outer:
|
|||
return c
|
||||
}
|
||||
|
||||
// GenerateParamFlags generates the SQL statement parameter flags
|
||||
// For example, $1,$2,$3 etc. It will start counting at startAt.
|
||||
//
|
||||
// If GroupAt is greater than 1, instead of returning $1,$2,$3
|
||||
// it will return wrapped groups of param flags, for example:
|
||||
//
|
||||
// GroupAt(1): $1,$2,$3,$4,$5,$6
|
||||
// GroupAt(2): ($1,$2),($3,$4),($5,$6)
|
||||
// GroupAt(3): ($1,$2,$3),($4,$5,$6),($7,$8,$9)
|
||||
func GenerateParamFlags(colCount int, startAt int, groupAt int) string {
|
||||
return strmangle.GenerateParamFlags(colCount, startAt, groupAt)
|
||||
}
|
||||
|
||||
// WherePrimaryKeyIn generates a "in" string for where queries
|
||||
// For example: ("col1","col2") IN (($1,$2), ($3,$4))
|
||||
func WherePrimaryKeyIn(numRows int, keyNames ...string) string {
|
||||
|
|
|
@ -207,36 +207,26 @@ func PrefixStringSlice(str string, strs []string) []string {
|
|||
return ret
|
||||
}
|
||||
|
||||
// GenerateParamFlags generates the SQL statement parameter flags
|
||||
// For example, $1,$2,$3 etc. It will start counting at startAt.
|
||||
//
|
||||
// If GroupAt is greater than 1, instead of returning $1,$2,$3
|
||||
// it will return wrapped groups of param flags, for example:
|
||||
//
|
||||
// GroupAt(1): $1,$2,$3,$4,$5,$6
|
||||
// GroupAt(2): ($1,$2),($3,$4),($5,$6)
|
||||
// GroupAt(3): ($1,$2,$3),($4,$5,$6),($7,$8,$9)
|
||||
func GenerateParamFlags(colCount int, startAt int, groupAt int) string {
|
||||
// Placeholders generates the SQL statement placeholders for in queries.
|
||||
// For example, ($1,$2,$3),($4,$5,$6) etc.
|
||||
// It will start counting placeholders at "start".
|
||||
func Placeholders(count int, start int, group int) string {
|
||||
var buf bytes.Buffer
|
||||
|
||||
if groupAt > 1 {
|
||||
if group > 1 {
|
||||
buf.WriteByte('(')
|
||||
}
|
||||
|
||||
groupCounter := 0
|
||||
for i := startAt; i < colCount+startAt; i++ {
|
||||
groupCounter++
|
||||
buf.WriteString(fmt.Sprintf("$%d", i))
|
||||
if i+1 != colCount+startAt {
|
||||
if groupAt > 1 && groupCounter == groupAt {
|
||||
buf.WriteString("),(")
|
||||
groupCounter = 0
|
||||
for i := 0; i < count; i++ {
|
||||
if i != 0 {
|
||||
if group > 1 && i%group == 0 {
|
||||
buf.WriteString(`),(`)
|
||||
} else {
|
||||
buf.WriteByte(',')
|
||||
}
|
||||
}
|
||||
buf.WriteString(fmt.Sprintf("$%d", start+i))
|
||||
}
|
||||
if groupAt > 1 {
|
||||
if group > 1 {
|
||||
buf.WriteByte(')')
|
||||
}
|
||||
|
||||
|
|
|
@ -80,34 +80,34 @@ func TestDriverUsesLastInsertID(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGenerateParamFlags(t *testing.T) {
|
||||
func TestPlaceholders(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
x := GenerateParamFlags(1, 2, 1)
|
||||
x := Placeholders(1, 2, 1)
|
||||
want := "$2"
|
||||
if want != x {
|
||||
t.Errorf("want %s, got %s", want, x)
|
||||
}
|
||||
|
||||
x = GenerateParamFlags(5, 1, 1)
|
||||
x = Placeholders(5, 1, 1)
|
||||
want = "$1,$2,$3,$4,$5"
|
||||
if want != x {
|
||||
t.Errorf("want %s, got %s", want, x)
|
||||
}
|
||||
|
||||
x = GenerateParamFlags(6, 1, 2)
|
||||
x = Placeholders(6, 1, 2)
|
||||
want = "($1,$2),($3,$4),($5,$6)"
|
||||
if want != x {
|
||||
t.Errorf("want %s, got %s", want, x)
|
||||
}
|
||||
|
||||
x = GenerateParamFlags(9, 1, 3)
|
||||
x = Placeholders(9, 1, 3)
|
||||
want = "($1,$2,$3),($4,$5,$6),($7,$8,$9)"
|
||||
if want != x {
|
||||
t.Errorf("want %s, got %s", want, x)
|
||||
}
|
||||
|
||||
x = GenerateParamFlags(7, 1, 3)
|
||||
x = Placeholders(7, 1, 3)
|
||||
want = "($1,$2,$3),($4,$5,$6),($7)"
|
||||
if want != x {
|
||||
t.Errorf("want %s, got %s", want, x)
|
||||
|
|
|
@ -38,7 +38,7 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
|
|||
return err
|
||||
}
|
||||
|
||||
ins := fmt.Sprintf(`INSERT INTO {{.Table.Name}} ("%s") VALUES (%s)`, strings.Join(wl, `","`), boil.GenerateParamFlags(len(wl), 1, 1))
|
||||
ins := fmt.Sprintf(`INSERT INTO {{.Table.Name}} ("%s") VALUES (%s)`, strings.Join(wl, `","`), strmangle.Placeholders(len(wl), 1, 1))
|
||||
|
||||
{{if driverUsesLastInsertID .DriverName}}
|
||||
if len(returnColumns) != 0 {
|
||||
|
|
|
@ -104,7 +104,7 @@ func (o *{{$tableNameSingular}}) generateUpsertQuery(update bool, columns upsert
|
|||
query = fmt.Sprintf(
|
||||
`INSERT INTO {{.Table.Name}} (%s) VALUES (%s) ON CONFLICT`,
|
||||
strings.Join(columns.whitelist, `, `),
|
||||
boil.GenerateParamFlags(len(columns.whitelist), 1, 1),
|
||||
strmangle.Placeholders(len(columns.whitelist), 1, 1),
|
||||
)
|
||||
|
||||
if !update {
|
||||
|
|
|
@ -73,7 +73,7 @@ func (o *{{$tableNameSingular}}Slice) ReloadAll(exec boil.Executor) error {
|
|||
sql := fmt.Sprintf(
|
||||
`select {{.Table.Name}}.* from {{.Table.Name}} where (%s) in (%s)`,
|
||||
strings.Join({{$varNameSingular}}PrimaryKeyColumns, ","),
|
||||
strmangle.GenerateParamFlags(len(*o) * len({{$varNameSingular}}PrimaryKeyColumns), 1, len({{$varNameSingular}}PrimaryKeyColumns)),
|
||||
strmangle.Placeholders(len(*o) * len({{$varNameSingular}}PrimaryKeyColumns), 1, len({{$varNameSingular}}PrimaryKeyColumns)),
|
||||
)
|
||||
|
||||
q := boil.SQL(sql, args...)
|
||||
|
|
Loading…
Reference in a new issue