2016-06-20 03:45:33 +02:00
|
|
|
// Package strmangle is used exclusively by the templates in sqlboiler.
|
2016-06-23 08:09:56 +02:00
|
|
|
// There are many helper functions to deal with bdb.* values as well
|
2016-06-20 03:45:33 +02:00
|
|
|
// as string manipulation. Because it is focused on pipelining inside templates
|
|
|
|
// 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"
|
|
|
|
"strings"
|
|
|
|
|
2016-03-18 12:26:48 +01:00
|
|
|
"github.com/jinzhu/inflection"
|
2016-02-29 10:39:49 +01:00
|
|
|
)
|
|
|
|
|
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-03-18 12:26:48 +01:00
|
|
|
splits := strings.Split(name, "_")
|
|
|
|
splits[len(splits)-1] = inflection.Plural(splits[len(splits)-1])
|
|
|
|
return strings.Join(splits, "_")
|
|
|
|
}
|
|
|
|
|
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-03-18 12:26:48 +01:00
|
|
|
splits := strings.Split(name, "_")
|
|
|
|
splits[len(splits)-1] = inflection.Singular(splits[len(splits)-1])
|
|
|
|
return strings.Join(splits, "_")
|
|
|
|
}
|
|
|
|
|
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-04-25 03:43:09 +02:00
|
|
|
func TitleCase(name string) string {
|
2016-03-01 17:34:57 +01:00
|
|
|
splits := strings.Split(name, "_")
|
2016-02-29 10:39:49 +01:00
|
|
|
|
2016-03-01 17:34:57 +01:00
|
|
|
for i, split := range splits {
|
|
|
|
if split == "id" {
|
|
|
|
splits[i] = "ID"
|
2016-02-29 10:39:49 +01:00
|
|
|
continue
|
|
|
|
}
|
2016-03-01 17:34:57 +01:00
|
|
|
|
|
|
|
splits[i] = strings.Title(split)
|
2016-02-29 10:39:49 +01:00
|
|
|
}
|
|
|
|
|
2016-03-01 17:34:57 +01:00
|
|
|
return strings.Join(splits, "")
|
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-03-01 17:34:57 +01:00
|
|
|
splits := strings.Split(name, "_")
|
2016-02-29 10:39:49 +01:00
|
|
|
|
2016-03-01 17:34:57 +01:00
|
|
|
for i, split := range splits {
|
|
|
|
if split == "id" && i > 0 {
|
2016-03-02 04:11:47 +01:00
|
|
|
splits[i] = "ID"
|
2016-02-29 10:39:49 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-03-01 17:34:57 +01:00
|
|
|
splits[i] = strings.Title(split)
|
2016-02-29 10:39:49 +01:00
|
|
|
}
|
|
|
|
|
2016-03-01 17:34:57 +01:00
|
|
|
return strings.Join(splits, "")
|
2016-02-29 10:39:49 +01: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
|
|
|
}
|
|
|
|
|
|
|
|
// MakeDBName takes a table name in the format of "table_name" and a
|
2016-02-29 10:39:49 +01:00
|
|
|
// column name in the format of "column_name" and returns a name used in the
|
|
|
|
// `db:""` component of an object in the format of "table_name_column_name"
|
2016-04-25 03:43:09 +02:00
|
|
|
func MakeDBName(tableName, colName string) string {
|
2016-06-20 03:45:33 +02:00
|
|
|
return fmt.Sprintf("%s_%s", tableName, colName)
|
2016-03-19 07:22:10 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 03:45:33 +02:00
|
|
|
// HasElement checks to see if the string is found in the string slice
|
|
|
|
func HasElement(str string, slice []string) bool {
|
|
|
|
for _, s := range slice {
|
|
|
|
if str == s {
|
2016-04-04 12:28:58 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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-05-02 08:34:25 +02:00
|
|
|
// GenerateParamFlags generates the SQL statement parameter flags
|
|
|
|
// For example, $1,$2,$3 etc. It will start counting at startAt.
|
|
|
|
func GenerateParamFlags(colCount int, startAt int) string {
|
|
|
|
cols := make([]string, 0, colCount)
|
|
|
|
|
|
|
|
for i := startAt; i < colCount+startAt; i++ {
|
|
|
|
cols = append(cols, fmt.Sprintf("$%d", i))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(cols, ",")
|
|
|
|
}
|
|
|
|
|
2016-06-23 08:09:56 +02:00
|
|
|
// WhereClause returns the where clause using start as the $ flag index
|
2016-04-04 12:28:58 +02:00
|
|
|
// For example, if start was 2 output would be: "colthing=$2 AND colstuff=$3"
|
2016-06-26 05:17:39 +02:00
|
|
|
func WhereClause(cols []string, start int) string {
|
2016-06-14 16:01:28 +02:00
|
|
|
if start == 0 {
|
2016-06-23 08:09:56 +02:00
|
|
|
panic("0 is not a valid start number for whereClause")
|
2016-06-14 16:01:28 +02:00
|
|
|
}
|
|
|
|
|
2016-06-26 05:17:39 +02:00
|
|
|
ret := make([]string, len(cols))
|
|
|
|
for i, c := range cols {
|
|
|
|
ret[i] = fmt.Sprintf(`"%s"=$%d`, c, start+i)
|
2016-03-18 16:27:55 +01:00
|
|
|
}
|
|
|
|
|
2016-06-26 05:17:39 +02:00
|
|
|
return strings.Join(ret, " AND ")
|
2016-03-18 16:27:55 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 03:45:33 +02:00
|
|
|
// DriverUsesLastInsertID returns whether the database driver supports the
|
|
|
|
// sql.Result interface.
|
|
|
|
func DriverUsesLastInsertID(driverName string) bool {
|
2016-05-02 08:34:25 +02:00
|
|
|
switch driverName {
|
|
|
|
case "postgres":
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 01:00:37 +02:00
|
|
|
// Substring returns a substring of str starting at index start and going
|
|
|
|
// to end-1.
|
|
|
|
func Substring(start, end int, str string) string {
|
|
|
|
return str[start:end]
|
|
|
|
}
|