2016-08-14 01:03:34 +02:00
|
|
|
// Package strmangle is a collection of string manipulation functions.
|
|
|
|
// Primarily used by boil and templates for code generation.
|
|
|
|
// Because it is focused on pipelining inside templates
|
2016-06-20 03:45:33 +02:00
|
|
|
// you will see some odd parameter ordering.
|
2016-04-25 03:43:09 +02:00
|
|
|
package strmangle
|
2016-02-29 10:39:49 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-07-02 00:52:40 +02:00
|
|
|
"math"
|
2016-08-02 04:55:40 +02:00
|
|
|
"regexp"
|
2016-09-16 08:28:59 +02:00
|
|
|
"sort"
|
2016-02-29 10:39:49 +01:00
|
|
|
"strings"
|
2016-09-02 09:09:42 +02:00
|
|
|
"sync"
|
2016-02-29 10:39:49 +01:00
|
|
|
)
|
|
|
|
|
2016-07-02 00:52:40 +02:00
|
|
|
var (
|
2016-08-22 05:50:54 +02:00
|
|
|
idAlphabet = []byte("abcdefghijklmnopqrstuvwxyz")
|
|
|
|
smartQuoteRgx = regexp.MustCompile(`^(?i)"?[a-z_][_a-z0-9]*"?(\."?[_a-z][_a-z0-9]*"?)*(\.\*)?$`)
|
2016-07-02 00:52:40 +02:00
|
|
|
)
|
|
|
|
|
2016-08-22 05:50:54 +02:00
|
|
|
var uppercaseWords = map[string]struct{}{
|
2016-09-05 13:47:17 +02:00
|
|
|
"guid": {},
|
|
|
|
"id": {},
|
|
|
|
"uid": {},
|
|
|
|
"uuid": {},
|
2016-09-08 17:22:22 +02:00
|
|
|
"json": {},
|
2016-08-22 05:50:54 +02:00
|
|
|
}
|
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
func init() {
|
2016-08-21 05:40:20 +02:00
|
|
|
// Our Boil inflection Ruleset does not include uncountable inflections.
|
2016-08-17 07:19:23 +02:00
|
|
|
// This way, people using words like Sheep will not have
|
|
|
|
// collisions with their model name (Sheep) and their
|
|
|
|
// function name (Sheep()). Instead, it will
|
|
|
|
// use the regular inflection rules: Sheep, Sheeps().
|
2016-08-21 05:40:20 +02:00
|
|
|
boilRuleset = newBoilRuleset()
|
2016-08-17 07:19:23 +02:00
|
|
|
}
|
|
|
|
|
2016-09-08 23:23:10 +02:00
|
|
|
// SchemaTable returns a table name with a schema prefixed if
|
|
|
|
// using a database that supports real schemas, for example,
|
|
|
|
// for Postgres: "schema_name"."table_name", versus
|
|
|
|
// simply "table_name" for MySQL (because it does not support real schemas)
|
2016-09-12 07:30:25 +02:00
|
|
|
func SchemaTable(lq, rq string, driver string, schema string, table string) string {
|
2016-09-09 17:06:07 +02:00
|
|
|
if driver == "postgres" && schema != "public" {
|
2016-09-12 07:30:25 +02:00
|
|
|
return fmt.Sprintf(`%s%s%s.%s%s%s`, lq, schema, rq, lq, table, rq)
|
2016-09-08 23:23:10 +02:00
|
|
|
}
|
|
|
|
|
2016-09-12 07:30:25 +02:00
|
|
|
return fmt.Sprintf(`%s%s%s`, lq, table, rq)
|
2016-09-08 23:23:10 +02:00
|
|
|
}
|
|
|
|
|
2016-08-02 05:25:02 +02:00
|
|
|
// IdentQuote attempts to quote simple identifiers in SQL tatements
|
2016-09-09 19:35:32 +02:00
|
|
|
func IdentQuote(lq byte, rq byte, s string) string {
|
2016-09-09 21:15:50 +02:00
|
|
|
if strings.ToLower(s) == "null" || s == "?" {
|
2016-08-02 04:55:40 +02:00
|
|
|
return s
|
|
|
|
}
|
2016-08-01 09:41:52 +02:00
|
|
|
|
2016-08-02 04:55:40 +02:00
|
|
|
if m := smartQuoteRgx.MatchString(s); m != true {
|
|
|
|
return s
|
|
|
|
}
|
2016-08-01 09:41:52 +02:00
|
|
|
|
2016-08-13 10:07:45 +02:00
|
|
|
buf := GetBuffer()
|
|
|
|
defer PutBuffer(buf)
|
|
|
|
|
2016-08-02 04:55:40 +02:00
|
|
|
splits := strings.Split(s, ".")
|
|
|
|
for i, split := range splits {
|
2016-08-13 10:07:45 +02:00
|
|
|
if i != 0 {
|
|
|
|
buf.WriteByte('.')
|
|
|
|
}
|
|
|
|
|
2016-09-09 21:15:50 +02:00
|
|
|
if split[0] == lq || split[len(split)-1] == rq || split == "*" {
|
2016-08-13 10:07:45 +02:00
|
|
|
buf.WriteString(split)
|
2016-08-02 04:55:40 +02:00
|
|
|
continue
|
|
|
|
}
|
2016-08-01 09:41:52 +02:00
|
|
|
|
2016-09-09 21:15:50 +02:00
|
|
|
buf.WriteByte(lq)
|
2016-08-13 10:07:45 +02:00
|
|
|
buf.WriteString(split)
|
2016-09-09 21:15:50 +02:00
|
|
|
buf.WriteByte(rq)
|
2016-08-01 09:41:52 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 10:07:45 +02:00
|
|
|
return buf.String()
|
2016-08-01 09:41:52 +02:00
|
|
|
}
|
|
|
|
|
2016-08-04 06:46:58 +02:00
|
|
|
// IdentQuoteSlice applies IdentQuote to a slice.
|
2016-09-09 19:35:32 +02:00
|
|
|
func IdentQuoteSlice(lq byte, rq byte, s []string) []string {
|
2016-08-04 06:46:58 +02:00
|
|
|
if len(s) == 0 {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
strs := make([]string, len(s))
|
|
|
|
for i, str := range s {
|
2016-09-09 21:15:50 +02:00
|
|
|
strs[i] = IdentQuote(lq, rq, str)
|
2016-08-04 06:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return strs
|
|
|
|
}
|
|
|
|
|
2016-07-17 05:33:16 +02:00
|
|
|
// Identifier is a base conversion from Base 10 integers to Base 26
|
2016-07-02 00:52:40 +02:00
|
|
|
// integers that are represented by an alphabet from a-z
|
|
|
|
// See tests for example outputs.
|
|
|
|
func Identifier(in int) string {
|
|
|
|
ln := len(idAlphabet)
|
|
|
|
var n int
|
|
|
|
if in == 0 {
|
|
|
|
n = 1
|
|
|
|
} else {
|
|
|
|
n = 1 + int(math.Log(float64(in))/math.Log(float64(ln)))
|
|
|
|
}
|
|
|
|
|
2016-08-13 15:30:53 +02:00
|
|
|
cols := GetBuffer()
|
|
|
|
defer PutBuffer(cols)
|
2016-07-02 00:52:40 +02:00
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
divisor := int(math.Pow(float64(ln), float64(n-i-1)))
|
|
|
|
rem := in / divisor
|
2016-08-13 15:30:53 +02:00
|
|
|
cols.WriteByte(idAlphabet[rem])
|
2016-07-02 00:52:40 +02:00
|
|
|
|
|
|
|
in -= rem * divisor
|
|
|
|
}
|
|
|
|
|
2016-08-13 15:30:53 +02:00
|
|
|
return cols.String()
|
2016-07-02 00:52:40 +02:00
|
|
|
}
|
|
|
|
|
2016-09-12 07:30:25 +02:00
|
|
|
// QuoteCharacter returns a string that allows the quote character
|
|
|
|
// to be embedded into a Go string that uses double quotes:
|
|
|
|
func QuoteCharacter(q byte) string {
|
|
|
|
if q == '"' {
|
|
|
|
return `\"`
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(q)
|
|
|
|
}
|
|
|
|
|
2016-04-25 03:43:09 +02:00
|
|
|
// Plural converts singular words to plural words (eg: person to people)
|
|
|
|
func Plural(name string) string {
|
2016-08-13 15:39:13 +02:00
|
|
|
buf := GetBuffer()
|
|
|
|
defer PutBuffer(buf)
|
|
|
|
|
2016-03-18 12:26:48 +01:00
|
|
|
splits := strings.Split(name, "_")
|
2016-08-13 15:39:13 +02:00
|
|
|
|
|
|
|
for i := 0; i < len(splits); i++ {
|
|
|
|
if i != 0 {
|
|
|
|
buf.WriteByte('_')
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == len(splits)-1 {
|
2016-08-21 05:40:20 +02:00
|
|
|
buf.WriteString(boilRuleset.Pluralize(splits[len(splits)-1]))
|
2016-08-13 15:39:13 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString(splits[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
2016-03-18 12:26:48 +01:00
|
|
|
}
|
|
|
|
|
2016-04-25 03:43:09 +02:00
|
|
|
// Singular converts plural words to singular words (eg: people to person)
|
|
|
|
func Singular(name string) string {
|
2016-08-13 15:39:13 +02:00
|
|
|
buf := GetBuffer()
|
|
|
|
defer PutBuffer(buf)
|
|
|
|
|
2016-03-18 12:26:48 +01:00
|
|
|
splits := strings.Split(name, "_")
|
2016-08-13 15:39:13 +02:00
|
|
|
|
|
|
|
for i := 0; i < len(splits); i++ {
|
|
|
|
if i != 0 {
|
|
|
|
buf.WriteByte('_')
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == len(splits)-1 {
|
2016-08-21 05:40:20 +02:00
|
|
|
buf.WriteString(boilRuleset.Singularize(splits[len(splits)-1]))
|
2016-08-13 15:39:13 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString(splits[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
2016-03-18 12:26:48 +01:00
|
|
|
}
|
|
|
|
|
2016-09-02 09:09:42 +02:00
|
|
|
// titleCaseCache holds the mapping of title cases.
|
|
|
|
// Example: map["MyWord"] == "my_word"
|
|
|
|
var (
|
|
|
|
mut sync.RWMutex
|
|
|
|
titleCaseCache = map[string]string{}
|
|
|
|
)
|
|
|
|
|
2016-04-25 03:43:09 +02:00
|
|
|
// TitleCase changes a snake-case variable name
|
2016-03-02 04:11:47 +01:00
|
|
|
// into a go styled object variable name of "ColumnName".
|
2016-03-01 17:34:57 +01:00
|
|
|
// titleCase also fully uppercases "ID" components of names, for example
|
2016-02-29 10:39:49 +01:00
|
|
|
// "column_name_id" to "ColumnNameID".
|
2016-08-22 05:50:54 +02:00
|
|
|
//
|
|
|
|
// Note: This method is ugly because it has been highly optimized,
|
|
|
|
// we found that it was a fairly large bottleneck when we were using regexp.
|
|
|
|
func TitleCase(n string) string {
|
2016-09-02 09:09:42 +02:00
|
|
|
// Attempt to fetch from cache
|
|
|
|
mut.RLock()
|
|
|
|
val, ok := titleCaseCache[n]
|
|
|
|
mut.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2016-08-22 05:50:54 +02:00
|
|
|
ln := len(n)
|
|
|
|
name := []byte(n)
|
2016-08-13 15:48:31 +02:00
|
|
|
buf := GetBuffer()
|
|
|
|
|
2016-08-22 05:50:54 +02:00
|
|
|
start := 0
|
|
|
|
end := 0
|
|
|
|
for start < ln {
|
|
|
|
// Find the start and end of the underscores to account
|
|
|
|
// for the possibility of being multiple underscores in a row.
|
2016-08-22 07:10:15 +02:00
|
|
|
if end < ln {
|
|
|
|
if name[start] == '_' {
|
|
|
|
start++
|
|
|
|
end++
|
|
|
|
continue
|
|
|
|
// Once we have found the end of the underscores, we can
|
|
|
|
// find the end of the first full word.
|
|
|
|
} else if name[end] != '_' {
|
|
|
|
end++
|
|
|
|
continue
|
|
|
|
}
|
2016-02-29 10:39:49 +01:00
|
|
|
}
|
2016-03-01 17:34:57 +01:00
|
|
|
|
2016-08-22 05:50:54 +02:00
|
|
|
word := name[start:end]
|
2016-08-22 07:49:57 +02:00
|
|
|
wordLen := len(word)
|
2016-08-22 05:50:54 +02:00
|
|
|
var vowels bool
|
|
|
|
|
2016-08-22 07:49:57 +02:00
|
|
|
numStart := wordLen
|
2016-08-22 05:50:54 +02:00
|
|
|
for i, c := range word {
|
2016-08-22 07:49:57 +02:00
|
|
|
vowels = vowels || (c == 97 || c == 101 || c == 105 || c == 111 || c == 117 || c == 121)
|
2016-08-22 05:50:54 +02:00
|
|
|
|
2016-08-22 07:49:57 +02:00
|
|
|
if c > 47 && c < 58 && numStart == wordLen {
|
2016-08-22 05:50:54 +02:00
|
|
|
numStart = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 07:49:57 +02:00
|
|
|
_, match := uppercaseWords[string(word[:numStart])]
|
2016-08-22 05:50:54 +02:00
|
|
|
|
|
|
|
if match || !vowels {
|
|
|
|
// Uppercase all a-z characters
|
|
|
|
for _, c := range word {
|
|
|
|
if c > 96 && c < 123 {
|
|
|
|
buf.WriteByte(c - 32)
|
|
|
|
} else {
|
|
|
|
buf.WriteByte(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-08-22 08:00:22 +02:00
|
|
|
if c := word[0]; c > 96 && c < 123 {
|
|
|
|
buf.WriteByte(word[0] - 32)
|
|
|
|
buf.Write(word[1:])
|
|
|
|
} else {
|
|
|
|
buf.Write(word)
|
|
|
|
}
|
2016-08-22 05:50:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
start = end + 1
|
|
|
|
end = start
|
2016-02-29 10:39:49 +01:00
|
|
|
}
|
|
|
|
|
2016-08-22 05:50:54 +02:00
|
|
|
ret := buf.String()
|
|
|
|
PutBuffer(buf)
|
2016-09-02 09:09:42 +02:00
|
|
|
|
|
|
|
// Cache the title case result
|
|
|
|
mut.Lock()
|
|
|
|
titleCaseCache[n] = ret
|
|
|
|
mut.Unlock()
|
|
|
|
|
2016-08-22 05:50:54 +02:00
|
|
|
return ret
|
2016-02-29 10:39:49 +01:00
|
|
|
}
|
|
|
|
|
2016-04-25 03:43:09 +02:00
|
|
|
// CamelCase takes a variable name in the format of "var_name" and converts
|
2016-02-29 10:39:49 +01:00
|
|
|
// it into a go styled variable name of "varName".
|
2016-03-01 17:34:57 +01:00
|
|
|
// camelCase also fully uppercases "ID" components of names, for example
|
2016-02-29 10:39:49 +01:00
|
|
|
// "var_name_id" to "varNameID".
|
2016-04-25 03:43:09 +02:00
|
|
|
func CamelCase(name string) string {
|
2016-08-13 15:48:31 +02:00
|
|
|
buf := GetBuffer()
|
|
|
|
defer PutBuffer(buf)
|
|
|
|
|
2016-08-22 05:50:54 +02:00
|
|
|
index := -1
|
|
|
|
for i := 0; i < len(name); i++ {
|
|
|
|
if name[i] != '_' {
|
|
|
|
index = i
|
|
|
|
break
|
2016-02-29 10:39:49 +01:00
|
|
|
}
|
2016-08-22 05:50:54 +02:00
|
|
|
}
|
2016-02-29 10:39:49 +01:00
|
|
|
|
2016-08-22 05:50:54 +02:00
|
|
|
if index != -1 {
|
|
|
|
name = name[index:]
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
index = -1
|
|
|
|
for i := 0; i < len(name); i++ {
|
|
|
|
if name[i] == '_' {
|
|
|
|
index = i
|
|
|
|
break
|
2016-07-17 05:33:16 +02:00
|
|
|
}
|
2016-08-22 05:50:54 +02:00
|
|
|
}
|
2016-07-17 05:33:16 +02:00
|
|
|
|
2016-08-22 05:50:54 +02:00
|
|
|
if index == -1 {
|
|
|
|
buf.WriteString(name)
|
|
|
|
} else {
|
|
|
|
buf.WriteString(name[:index])
|
|
|
|
buf.WriteString(TitleCase(name[index+1:]))
|
2016-02-29 10:39:49 +01:00
|
|
|
}
|
|
|
|
|
2016-08-13 15:48:31 +02:00
|
|
|
return buf.String()
|
2016-02-29 10:39:49 +01:00
|
|
|
}
|
|
|
|
|
2016-09-01 06:11:42 +02:00
|
|
|
// TitleCaseIdentifier splits on dots and then titlecases each fragment.
|
|
|
|
// map titleCase (split c ".")
|
2016-09-02 09:09:42 +02:00
|
|
|
func TitleCaseIdentifier(id string) string {
|
2016-09-01 06:11:42 +02:00
|
|
|
nextDot := strings.IndexByte(id, '.')
|
|
|
|
if nextDot < 0 {
|
2016-09-02 09:09:42 +02:00
|
|
|
return TitleCase(id)
|
2016-09-01 06:11:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := GetBuffer()
|
2016-09-04 01:29:30 +02:00
|
|
|
defer PutBuffer(buf)
|
2016-09-01 06:11:42 +02:00
|
|
|
lastDot := 0
|
|
|
|
ln := len(id)
|
|
|
|
addDots := false
|
|
|
|
|
|
|
|
for i := 0; nextDot >= 0; i++ {
|
|
|
|
fragment := id[lastDot:nextDot]
|
|
|
|
|
2016-09-02 09:09:42 +02:00
|
|
|
titled := TitleCase(fragment)
|
2016-09-01 06:11:42 +02:00
|
|
|
|
|
|
|
if addDots {
|
|
|
|
buf.WriteByte('.')
|
|
|
|
}
|
|
|
|
buf.WriteString(titled)
|
|
|
|
addDots = true
|
|
|
|
|
|
|
|
if nextDot == ln {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
lastDot = nextDot + 1
|
|
|
|
if nextDot = strings.IndexByte(id[lastDot:], '.'); nextDot >= 0 {
|
|
|
|
nextDot += lastDot
|
|
|
|
} else {
|
|
|
|
nextDot = ln
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2016-07-13 18:51:40 +02:00
|
|
|
// MakeStringMap converts a map[string]string into the format:
|
|
|
|
// "key": "value", "key": "value"
|
|
|
|
func MakeStringMap(types map[string]string) string {
|
2016-08-13 15:54:57 +02:00
|
|
|
buf := GetBuffer()
|
|
|
|
defer PutBuffer(buf)
|
|
|
|
|
2016-09-16 08:28:59 +02:00
|
|
|
keys := make([]string, 0, len(types))
|
|
|
|
for k := range types {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
2016-08-13 15:54:57 +02:00
|
|
|
c := 0
|
2016-09-16 08:28:59 +02:00
|
|
|
for _, k := range keys {
|
|
|
|
v := types[k]
|
2016-08-13 15:54:57 +02:00
|
|
|
buf.WriteString(fmt.Sprintf(`"%s": "%s"`, k, v))
|
|
|
|
if c < len(types)-1 {
|
|
|
|
buf.WriteString(", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
c++
|
2016-07-13 18:51:40 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 15:54:57 +02:00
|
|
|
return buf.String()
|
2016-07-13 18:51:40 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 03:45:33 +02:00
|
|
|
// StringMap maps a function over a slice of strings.
|
|
|
|
func StringMap(modifier func(string) string, strs []string) []string {
|
|
|
|
ret := make([]string, len(strs))
|
2016-04-25 03:43:09 +02:00
|
|
|
|
2016-06-20 03:45:33 +02:00
|
|
|
for i, str := range strs {
|
|
|
|
ret[i] = modifier(str)
|
2016-05-06 11:31:51 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 03:45:33 +02:00
|
|
|
return ret
|
2016-04-25 03:43:09 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 03:45:33 +02:00
|
|
|
// PrefixStringSlice with the given str.
|
|
|
|
func PrefixStringSlice(str string, strs []string) []string {
|
|
|
|
ret := make([]string, len(strs))
|
2016-03-18 12:26:48 +01:00
|
|
|
|
2016-06-20 03:45:33 +02:00
|
|
|
for i, s := range strs {
|
|
|
|
ret[i] = fmt.Sprintf("%s%s", str, s)
|
2016-03-18 12:26:48 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 03:45:33 +02:00
|
|
|
return ret
|
2016-04-04 12:28:58 +02:00
|
|
|
}
|
|
|
|
|
2016-08-09 05:19:26 +02:00
|
|
|
// Placeholders generates the SQL statement placeholders for in queries.
|
2016-08-21 04:34:18 +02:00
|
|
|
// For example, ($1,$2,$3),($4,$5,$6) etc.
|
2016-08-09 05:19:26 +02:00
|
|
|
// It will start counting placeholders at "start".
|
2016-09-09 21:15:50 +02:00
|
|
|
// If indexPlaceholders is false, it will convert to ? instead of $1 etc.
|
|
|
|
func Placeholders(indexPlaceholders bool, count int, start int, group int) string {
|
2016-08-13 15:55:52 +02:00
|
|
|
buf := GetBuffer()
|
|
|
|
defer PutBuffer(buf)
|
2016-08-08 15:30:29 +02:00
|
|
|
|
2016-08-11 10:23:47 +02:00
|
|
|
if start == 0 || group == 0 {
|
|
|
|
panic("Invalid start or group numbers supplied.")
|
|
|
|
}
|
|
|
|
|
2016-08-09 05:19:26 +02:00
|
|
|
if group > 1 {
|
2016-08-08 15:30:29 +02:00
|
|
|
buf.WriteByte('(')
|
|
|
|
}
|
2016-08-09 05:19:26 +02:00
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
if i != 0 {
|
|
|
|
if group > 1 && i%group == 0 {
|
2016-08-13 09:57:13 +02:00
|
|
|
buf.WriteString("),(")
|
2016-08-08 15:30:29 +02:00
|
|
|
} else {
|
2016-08-13 09:57:13 +02:00
|
|
|
buf.WriteByte(',')
|
2016-08-08 15:30:29 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-09 21:15:50 +02:00
|
|
|
if indexPlaceholders {
|
|
|
|
buf.WriteString(fmt.Sprintf("$%d", start+i))
|
|
|
|
} else {
|
|
|
|
buf.WriteByte('?')
|
|
|
|
}
|
2016-08-08 15:30:29 +02:00
|
|
|
}
|
2016-08-09 05:19:26 +02:00
|
|
|
if group > 1 {
|
2016-08-08 15:30:29 +02:00
|
|
|
buf.WriteByte(')')
|
2016-05-02 08:34:25 +02:00
|
|
|
}
|
|
|
|
|
2016-08-08 15:30:29 +02:00
|
|
|
return buf.String()
|
2016-05-02 08:34:25 +02:00
|
|
|
}
|
|
|
|
|
2016-08-09 07:57:54 +02:00
|
|
|
// SetParamNames takes a slice of columns and returns a comma separated
|
|
|
|
// list of parameter names for a template statement SET clause.
|
|
|
|
// eg: "col1"=$1, "col2"=$2, "col3"=$3
|
2016-09-13 08:28:23 +02:00
|
|
|
func SetParamNames(lq, rq string, start int, columns []string) string {
|
2016-08-13 15:59:02 +02:00
|
|
|
buf := GetBuffer()
|
|
|
|
defer PutBuffer(buf)
|
|
|
|
|
|
|
|
for i, c := range columns {
|
2016-09-13 08:28:23 +02:00
|
|
|
if start != 0 {
|
|
|
|
buf.WriteString(fmt.Sprintf(`%s%s%s=$%d`, lq, c, rq, i+start))
|
|
|
|
} else {
|
|
|
|
buf.WriteString(fmt.Sprintf(`%s%s%s=?`, lq, c, rq))
|
|
|
|
}
|
|
|
|
|
2016-08-13 15:59:02 +02:00
|
|
|
if i < len(columns)-1 {
|
2016-09-13 08:28:23 +02:00
|
|
|
buf.WriteByte(',')
|
2016-08-13 15:59:02 +02:00
|
|
|
}
|
2016-08-08 19:33:24 +02:00
|
|
|
}
|
2016-08-13 15:59:02 +02:00
|
|
|
|
|
|
|
return buf.String()
|
2016-08-08 19:33:24 +02:00
|
|
|
}
|
|
|
|
|
2016-08-09 07:57:54 +02:00
|
|
|
// WhereClause returns the where clause using start as the $ flag index
|
|
|
|
// For example, if start was 2 output would be: "colthing=$2 AND colstuff=$3"
|
2016-09-12 07:30:25 +02:00
|
|
|
func WhereClause(lq, rq string, start int, cols []string) string {
|
2016-08-13 16:01:06 +02:00
|
|
|
buf := GetBuffer()
|
|
|
|
defer PutBuffer(buf)
|
|
|
|
|
2016-08-09 07:57:54 +02:00
|
|
|
for i, c := range cols {
|
2016-09-13 08:28:23 +02:00
|
|
|
if start != 0 {
|
|
|
|
buf.WriteString(fmt.Sprintf(`%s%s%s=$%d`, lq, c, rq, start+i))
|
|
|
|
} else {
|
|
|
|
buf.WriteString(fmt.Sprintf(`%s%s%s=?`, lq, c, rq))
|
|
|
|
}
|
|
|
|
|
2016-08-13 16:01:06 +02:00
|
|
|
if i < len(cols)-1 {
|
|
|
|
buf.WriteString(" AND ")
|
|
|
|
}
|
2016-08-08 18:52:34 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 16:01:06 +02:00
|
|
|
return buf.String()
|
2016-08-08 18:52:34 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 08:54:23 +02:00
|
|
|
// JoinSlices merges two string slices of equal length
|
|
|
|
func JoinSlices(sep string, a, b []string) []string {
|
|
|
|
lna, lnb := len(a), len(b)
|
|
|
|
if lna != lnb {
|
|
|
|
panic("joinSlices: can only merge slices of same length")
|
|
|
|
} else if lna == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := make([]string, len(a))
|
|
|
|
for i, elem := range a {
|
|
|
|
ret[i] = fmt.Sprintf("%s%s%s", elem, sep, b[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
2016-08-21 07:44:49 +02:00
|
|
|
|
|
|
|
// StringSliceMatch returns true if the length of both
|
|
|
|
// slices is the same, and the elements of both slices are the same.
|
|
|
|
// The elements can be in any order.
|
|
|
|
func StringSliceMatch(a []string, b []string) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, aval := range a {
|
|
|
|
found := false
|
|
|
|
for _, bval := range b {
|
|
|
|
if bval == aval {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2016-08-28 16:12:37 +02:00
|
|
|
|
|
|
|
// ContainsAny returns true if any of the passed in strings are
|
|
|
|
// found in the passed in string slice
|
|
|
|
func ContainsAny(a []string, finds ...string) bool {
|
|
|
|
for _, s := range a {
|
|
|
|
for _, find := range finds {
|
|
|
|
if s == find {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2016-09-04 15:44:54 +02:00
|
|
|
|
|
|
|
// GenerateTags converts a slice of tag strings into tags that
|
|
|
|
// can be passed onto the end of a struct, for example:
|
|
|
|
// tags: ["xml", "db"] convert to: xml:"column_name" db:"column_name"
|
|
|
|
func GenerateTags(tags []string, columnName string) string {
|
|
|
|
buf := GetBuffer()
|
|
|
|
defer PutBuffer(buf)
|
|
|
|
|
|
|
|
for _, tag := range tags {
|
|
|
|
buf.WriteString(tag)
|
|
|
|
buf.WriteString(`:"`)
|
|
|
|
buf.WriteString(columnName)
|
|
|
|
buf.WriteString(`" `)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateIgnoreTags converts a slice of tag strings into
|
|
|
|
// ignore tags that can be passed onto the end of a struct, for example:
|
|
|
|
// tags: ["xml", "db"] convert to: xml:"-" db:"-"
|
|
|
|
func GenerateIgnoreTags(tags []string) string {
|
|
|
|
buf := GetBuffer()
|
|
|
|
defer PutBuffer(buf)
|
|
|
|
|
|
|
|
for _, tag := range tags {
|
|
|
|
buf.WriteString(tag)
|
|
|
|
buf.WriteString(`:"-" `)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|