Finished insert template
* Removed functions in helpers not being used
This commit is contained in:
parent
a957bc3836
commit
f059bdebf4
10 changed files with 576 additions and 98 deletions
strmangle
|
@ -2,12 +2,15 @@ package strmangle
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/jinzhu/inflection"
|
||||
"github.com/pobri19/sqlboiler/dbdrivers"
|
||||
)
|
||||
|
||||
var rgxAutoIncColumn = regexp.MustCompile(`^nextval\(.*\)`)
|
||||
|
||||
// Plural converts singular words to plural words (eg: person to people)
|
||||
func Plural(name string) string {
|
||||
splits := strings.Split(name, "_")
|
||||
|
@ -247,6 +250,18 @@ func PrimaryKeyFuncSig(cols []dbdrivers.Column, pkeyCols []string) string {
|
|||
return strings.Join(output, ", ")
|
||||
}
|
||||
|
||||
// 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, ",")
|
||||
}
|
||||
|
||||
// WherePrimaryKey returns the where clause using start as the $ flag index
|
||||
// For example, if start was 2 output would be: "colthing=$2 AND colstuff=$3"
|
||||
func WherePrimaryKey(pkeyCols []string, start int) string {
|
||||
|
@ -280,6 +295,26 @@ func PrimaryKeyStrList(pkeyCols []string) string {
|
|||
return strings.Join(cols, ", ")
|
||||
}
|
||||
|
||||
// AutoIncPrimKey returns the auto-increment primary key column name or an empty string
|
||||
func AutoIncPrimaryKey(cols []dbdrivers.Column, pkey *dbdrivers.PrimaryKey) string {
|
||||
if pkey == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
for _, c := range cols {
|
||||
if rgxAutoIncColumn.MatchString(c.Default) &&
|
||||
c.IsNullable == false && c.Type == "int64" {
|
||||
for _, p := range pkey.Columns {
|
||||
if c.Name == p {
|
||||
return p
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// CommaList returns a comma seperated list: "col1, col2, col3"
|
||||
func CommaList(cols []string) string {
|
||||
return strings.Join(cols, ", ")
|
||||
|
@ -307,3 +342,32 @@ func ParamsPrimaryKey(prefix string, columns []string, shouldTitleCase bool) str
|
|||
func PrimaryKeyFlagIndex(regularCols []dbdrivers.Column, pkeyCols []string) int {
|
||||
return len(regularCols) - len(pkeyCols) + 1
|
||||
}
|
||||
|
||||
// SupportsResult returns whether the database driver supports the sql.Results
|
||||
// interface, i.e. LastReturnId and RowsAffected
|
||||
func SupportsResultObject(driverName string) bool {
|
||||
switch driverName {
|
||||
case "postgres":
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// FilterColumnsByDefault generates the list of columns that have default values
|
||||
func FilterColumnsByDefault(columns []dbdrivers.Column, defaults bool) string {
|
||||
var cols []string
|
||||
|
||||
for _, c := range columns {
|
||||
if (defaults && len(c.Default) != 0) || (!defaults && len(c.Default) == 0) {
|
||||
cols = append(cols, fmt.Sprintf(`"%s"`, c.Name))
|
||||
}
|
||||
}
|
||||
|
||||
return strings.Join(cols, `,`)
|
||||
}
|
||||
|
||||
// DEFAULT WHITELIST: The things that are not default values. The things we want to insert all the time.
|
||||
// WHITELIST: The things that we will NEVER return. The things that we will ALWAYS insert.
|
||||
// DEFAULTS: The things that we will return (if not in WHITELIST)
|
||||
// NON-ZEROS: The things that we will return (if not in WHITELIST)
|
||||
|
|
|
@ -11,6 +11,73 @@ var testColumns = []dbdrivers.Column{
|
|||
{Name: "enemy_column_thing", Type: "string", IsNullable: true},
|
||||
}
|
||||
|
||||
func TestAutoIncPrimaryKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var pkey *dbdrivers.PrimaryKey
|
||||
var cols []dbdrivers.Column
|
||||
|
||||
r := AutoIncPrimaryKey(cols, pkey)
|
||||
if r != "" {
|
||||
t.Errorf("Expected empty string, got %s", r)
|
||||
}
|
||||
|
||||
pkey = &dbdrivers.PrimaryKey{
|
||||
Columns: []string{
|
||||
"col1", "auto",
|
||||
},
|
||||
Name: "",
|
||||
}
|
||||
|
||||
cols = []dbdrivers.Column{
|
||||
{
|
||||
Name: "thing",
|
||||
IsNullable: true,
|
||||
Type: "int64",
|
||||
Default: "nextval('abc'::regclass)",
|
||||
},
|
||||
{
|
||||
Name: "stuff",
|
||||
IsNullable: false,
|
||||
Type: "string",
|
||||
Default: "nextval('abc'::regclass)",
|
||||
},
|
||||
{
|
||||
Name: "other",
|
||||
IsNullable: false,
|
||||
Type: "int64",
|
||||
Default: "nextval",
|
||||
},
|
||||
}
|
||||
|
||||
r = AutoIncPrimaryKey(cols, pkey)
|
||||
if r != "" {
|
||||
t.Errorf("Expected empty string, got %s", r)
|
||||
}
|
||||
|
||||
cols = append(cols, dbdrivers.Column{
|
||||
Name: "auto",
|
||||
IsNullable: false,
|
||||
Type: "int64",
|
||||
Default: "nextval('abc'::regclass)",
|
||||
})
|
||||
|
||||
r = AutoIncPrimaryKey(cols, pkey)
|
||||
if r != "auto" {
|
||||
t.Errorf("Expected empty string, got %s", r)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateParamFlags(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
x := GenerateParamFlags(5, 1)
|
||||
want := "$1,$2,$3,$4,$5"
|
||||
if want != x {
|
||||
t.Errorf("want %s, got %s", want, x)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSingular(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
@ -263,3 +330,41 @@ func TestWherePrimaryKey(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterColumnsByDefault(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cols := []dbdrivers.Column{
|
||||
{
|
||||
Name: "col1",
|
||||
Default: "",
|
||||
},
|
||||
{
|
||||
Name: "col2",
|
||||
Default: "things",
|
||||
},
|
||||
{
|
||||
Name: "col3",
|
||||
Default: "",
|
||||
},
|
||||
{
|
||||
Name: "col4",
|
||||
Default: "things2",
|
||||
},
|
||||
}
|
||||
|
||||
res := FilterColumnsByDefault(cols, false)
|
||||
if res != `"col1","col3"` {
|
||||
t.Errorf("Invalid result: %s", res)
|
||||
}
|
||||
|
||||
res = FilterColumnsByDefault(cols, true)
|
||||
if res != `"col2","col4"` {
|
||||
t.Errorf("Invalid result: %s", res)
|
||||
}
|
||||
|
||||
res = FilterColumnsByDefault([]dbdrivers.Column{}, false)
|
||||
if res != `` {
|
||||
t.Errorf("Invalid result: %s", res)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue