Skip to content

Commit

Permalink
support all fields in batch create do not base on create input
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLindhout committed Dec 23, 2021
1 parent 70d4bbd commit 8dd7a67
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion convert_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ func enhanceModelsWithFields(enums []*Enum, schema *ast.Schema, cfg *config.Conf
IsPrimaryNumberID: isPrimaryNumberID,
IsPrimaryStringID: isPrimaryStringID,
IsRelation: boilerField.IsRelation,
IsRelationAndNotForeignKey: boilerField.IsRelation && !strings.HasSuffix(strings.ToLower(name), "id"),
IsRelationAndNotForeignKey: boilerField.IsRelation && !boilerField.IsForeignKey,
IsObject: isObject,
IsOr: strings.EqualFold(name, "or"),
IsAnd: strings.EqualFold(name, "and"),
Expand Down
9 changes: 8 additions & 1 deletion sqlboiler_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type BoilerField struct {
IsArray bool
IsEnum bool
IsRelation bool
InTable bool
InTableNotID bool
Enum BoilerEnum
RelationshipName string
Relationship *BoilerModel
Expand Down Expand Up @@ -109,6 +111,8 @@ func GetBoilerModels(dir string) ([]*BoilerModel, []*BoilerEnum) { //nolint:goco
IsRelation: true,
IsRequired: false,
IsArray: isArray,
InTable: false,
InTableNotID: false,
}
addFieldToMap(relationsPerModelName, modelName, relationField)
}
Expand All @@ -121,7 +125,8 @@ func GetBoilerModels(dir string) ([]*BoilerModel, []*BoilerEnum) { //nolint:goco
if boilerFieldName == "L" || boilerFieldName == "R" {
continue
}
isRelation := strings.HasSuffix(boilerFieldName, "ID") && boilerFieldName != "ID"
isID := boilerFieldName == "ID"
isRelation := strings.HasSuffix(boilerFieldName, "ID") && !isID

addFieldToMap(fieldsPerModelName, modelName, &BoilerField{
Name: boilerFieldName,
Expand All @@ -131,6 +136,8 @@ func GetBoilerModels(dir string) ([]*BoilerModel, []*BoilerEnum) { //nolint:goco
IsRequired: isRequired(boiler.Type),
RelationshipName: strings.TrimSuffix(boilerFieldName, "ID"),
IsForeignKey: isRelation,
InTable: true,
InTableNotID: !isID,
})
}
sort.Strings(modelNames)
Expand Down
17 changes: 10 additions & 7 deletions template_files/generated_convert_batch.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,28 @@ const batchInsertStatement = "INSERT INTO %s (%s) VALUES %s"
{{ if .IsCreateInput }}

var {{ lcFirst .BoilerModel.PluralName }}BatchCreateColumns = []string{
{{ range $field := .Fields -}}
{{ $.Backend.PackageName }}.{{ $model.BoilerModel.Name }}Columns.{{- $field.BoilerField.Name }},
{{ end }}
{{- range $field := .BoilerModel.Fields -}}
{{- if $field.InTableNotID -}}
{{ $.Backend.PackageName }}.{{ $model.BoilerModel.Name }}Columns.{{- $field.Name }},
{{- end -}}
{{- end -}}
}

var {{ lcFirst .BoilerModel.PluralName }}BatchCreateColumnsMarks = boilergql.GetQuestionMarksForColumns({{ lcFirst .BoilerModel.PluralName }}BatchCreateColumns)

func {{ lcFirst .BoilerModel.Name }}ToBatchCreateValues(e *{{ $.Backend.PackageName }}.{{ .BoilerModel.Name }}) []interface{} {
return []interface{}{
{{ range $field := .Fields -}}
e.{{- $field.BoilerField.Name }},
{{ end }}
{{- range $field := .BoilerModel.Fields -}}
{{- if $field.InTableNotID -}}
e.{{- $field.Name }},
{{- end -}}
{{- end -}}
}
}

func {{ lcFirst .BoilerModel.PluralName }}ToBatchCreate(a []*{{ $.Backend.PackageName }}.{{ .BoilerModel.Name }}) ([]string, []interface{}) {
queryMarks := make([]string, len(a))
// nolint:prealloc
// yes?
var values []interface{}
for i, boilerRow := range a {
queryMarks[i] = {{ lcFirst .BoilerModel.PluralName }}BatchCreateColumnsMarks
Expand Down

0 comments on commit 8dd7a67

Please sign in to comment.