voidwalker/model/mysql_upsert.go

59 lines
1.4 KiB
Go
Raw Normal View History

// Code generated by SQLBoiler 3.7.1 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package model
import (
"fmt"
"strings"
"github.com/volatiletech/sqlboiler/drivers"
"github.com/volatiletech/sqlboiler/strmangle"
)
// buildUpsertQueryMySQL builds a SQL statement string using the upsertData provided.
func buildUpsertQueryMySQL(dia drivers.Dialect, tableName string, update, whitelist []string) string {
whitelist = strmangle.IdentQuoteSlice(dia.LQ, dia.RQ, whitelist)
tableName = strmangle.IdentQuote(dia.LQ, dia.RQ, tableName)
buf := strmangle.GetBuffer()
defer strmangle.PutBuffer(buf)
var columns string
if len(whitelist) != 0 {
columns = strings.Join(whitelist, ",")
}
if len(update) == 0 {
fmt.Fprintf(
buf,
"INSERT IGNORE INTO %s (%s) VALUES (%s)",
tableName,
columns,
strmangle.Placeholders(dia.UseIndexPlaceholders, len(whitelist), 1, 1),
)
return buf.String()
}
fmt.Fprintf(
buf,
"INSERT INTO %s (%s) VALUES (%s) ON DUPLICATE KEY UPDATE ",
tableName,
columns,
strmangle.Placeholders(dia.UseIndexPlaceholders, len(whitelist), 1, 1),
)
for i, v := range update {
if i != 0 {
buf.WriteByte(',')
}
quoted := strmangle.IdentQuote(dia.LQ, dia.RQ, v)
buf.WriteString(quoted)
buf.WriteString(" = VALUES(")
buf.WriteString(quoted)
buf.WriteByte(')')
}
return buf.String()
}