github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/templates/main/15_insert.go.tpl (about) 1 {{- if or (not .Table.IsView) (.Table.ViewCapabilities.CanInsert) -}} 2 {{- $alias := .Aliases.Table .Table.Name}} 3 {{- $schemaTable := .Table.Name | .SchemaTable}} 4 {{if .AddGlobal -}} 5 // InsertG a single record. See Insert for whitelist behavior description. 6 func (o *{{$alias.UpSingular}}) InsertG({{if not .NoContext}}ctx context.Context, {{end -}} columns boil.Columns) error { 7 return o.Insert({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, columns) 8 } 9 10 {{end -}} 11 12 {{if .AddPanic -}} 13 // InsertP a single record using an executor, and panics on error. See Insert 14 // for whitelist behavior description. 15 func (o *{{$alias.UpSingular}}) InsertP({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, columns boil.Columns) { 16 if err := o.Insert({{if not .NoContext}}ctx, {{end -}} exec, columns); err != nil { 17 panic(boil.WrapErr(err)) 18 } 19 } 20 21 {{end -}} 22 23 {{if and .AddGlobal .AddPanic -}} 24 // InsertGP a single record, and panics on error. See Insert for whitelist 25 // behavior description. 26 func (o *{{$alias.UpSingular}}) InsertGP({{if not .NoContext}}ctx context.Context, {{end -}} columns boil.Columns) { 27 if err := o.Insert({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, columns); err != nil { 28 panic(boil.WrapErr(err)) 29 } 30 } 31 32 {{end -}} 33 34 // Insert a single record using an executor. 35 // See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts. 36 func (o *{{$alias.UpSingular}}) Insert({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, columns boil.Columns) error { 37 if o == nil { 38 return errors.New("{{.PkgName}}: no {{.Table.Name}} provided for insertion") 39 } 40 41 var err error 42 {{- template "timestamp_insert_helper" . }} 43 44 {{if not .NoHooks -}} 45 if err := o.doBeforeInsertHooks({{if not .NoContext}}ctx, {{end -}} exec); err != nil { 46 return err 47 } 48 {{- end}} 49 50 nzDefaults := queries.NonZeroDefaultSet({{$alias.DownSingular}}ColumnsWithDefault, o) 51 52 key := makeCacheKey(columns, nzDefaults) 53 {{$alias.DownSingular}}InsertCacheMut.RLock() 54 cache, cached := {{$alias.DownSingular}}InsertCache[key] 55 {{$alias.DownSingular}}InsertCacheMut.RUnlock() 56 57 if !cached { 58 wl, returnColumns := columns.InsertColumnSet( 59 {{$alias.DownSingular}}AllColumns, 60 {{$alias.DownSingular}}ColumnsWithDefault, 61 {{$alias.DownSingular}}ColumnsWithoutDefault, 62 nzDefaults, 63 ) 64 {{- if filterColumnsByAuto true .Table.Columns }} 65 wl = strmangle.SetComplement(wl, {{$alias.DownSingular}}GeneratedColumns) 66 {{- end}} 67 68 cache.valueMapping, err = queries.BindMapping({{$alias.DownSingular}}Type, {{$alias.DownSingular}}Mapping, wl) 69 if err != nil { 70 return err 71 } 72 cache.retMapping, err = queries.BindMapping({{$alias.DownSingular}}Type, {{$alias.DownSingular}}Mapping, returnColumns) 73 if err != nil { 74 return err 75 } 76 if len(wl) != 0 { 77 cache.query = fmt.Sprintf("INSERT INTO {{$schemaTable}} ({{.LQ}}%s{{.RQ}}) %%sVALUES (%s)%%s", strings.Join(wl, "{{.RQ}},{{.LQ}}"), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1)) 78 } else { 79 {{if .Dialect.UseDefaultKeyword -}} 80 cache.query = "INSERT INTO {{$schemaTable}} %sDEFAULT VALUES%s" 81 {{else -}} 82 cache.query = "INSERT INTO {{$schemaTable}} () VALUES ()%s%s" 83 {{end -}} 84 } 85 86 var queryOutput, queryReturning string 87 88 if len(cache.retMapping) != 0 { 89 {{if .Dialect.UseLastInsertID -}} 90 cache.retQuery = fmt.Sprintf("SELECT {{.LQ}}%s{{.RQ}} FROM {{$schemaTable}} WHERE %s", strings.Join(returnColumns, "{{.RQ}},{{.LQ}}"), strmangle.WhereClause("{{.LQ}}", "{{.RQ}}", {{if .Dialect.UseIndexPlaceholders}}1{{else}}0{{end}}, {{$alias.DownSingular}}PrimaryKeyColumns)) 91 {{else -}} 92 {{if .Dialect.UseOutputClause -}} 93 queryOutput = fmt.Sprintf("OUTPUT INSERTED.{{.LQ}}%s{{.RQ}} ", strings.Join(returnColumns, "{{.RQ}},INSERTED.{{.LQ}}")) 94 {{else -}} 95 queryReturning = fmt.Sprintf(" RETURNING {{.LQ}}%s{{.RQ}}", strings.Join(returnColumns, "{{.RQ}},{{.LQ}}")) 96 {{end -}} 97 {{end -}} 98 } 99 100 cache.query = fmt.Sprintf(cache.query, queryOutput, queryReturning) 101 } 102 103 value := reflect.Indirect(reflect.ValueOf(o)) 104 vals := queries.ValuesFromMapping(value, cache.valueMapping) 105 106 {{if .NoContext -}} 107 if boil.DebugMode { 108 fmt.Fprintln(boil.DebugWriter, cache.query) 109 fmt.Fprintln(boil.DebugWriter, vals) 110 } 111 {{else -}} 112 if boil.IsDebug(ctx) { 113 writer := boil.DebugWriterFrom(ctx) 114 fmt.Fprintln(writer, cache.query) 115 fmt.Fprintln(writer, vals) 116 } 117 {{end -}} 118 119 {{if .Dialect.UseLastInsertID -}} 120 {{- $canLastInsertID := .Table.CanLastInsertID -}} 121 {{if $canLastInsertID -}} 122 {{if .NoContext -}} 123 result, err := exec.Exec(cache.query, vals...) 124 {{else -}} 125 result, err := exec.ExecContext(ctx, cache.query, vals...) 126 {{end -}} 127 {{else -}} 128 {{if .NoContext -}} 129 _, err = exec.Exec(cache.query, vals...) 130 {{else -}} 131 _, err = exec.ExecContext(ctx, cache.query, vals...) 132 {{end -}} 133 {{- end}} 134 if err != nil { 135 return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}") 136 } 137 138 {{if $canLastInsertID -}} 139 var lastID int64 140 {{- end}} 141 var identifierCols []interface{} 142 143 if len(cache.retMapping) == 0 { 144 goto CacheNoHooks 145 } 146 147 {{if $canLastInsertID -}} 148 lastID, err = result.LastInsertId() 149 if err != nil { 150 return ErrSyncFail 151 } 152 153 {{$colName := index .Table.PKey.Columns 0 -}} 154 {{- $col := .Table.GetColumn $colName -}} 155 {{- $colTitled := $colName | titleCase}} 156 o.{{$colTitled}} = {{$col.Type}}(lastID) 157 if lastID != 0 && len(cache.retMapping) == 1 && cache.retMapping[0] == {{$alias.DownSingular}}Mapping["{{$colName}}"] { 158 goto CacheNoHooks 159 } 160 {{- end}} 161 162 identifierCols = []interface{}{ 163 {{range .Table.PKey.Columns -}} 164 o.{{$alias.Column .}}, 165 {{end -}} 166 } 167 168 {{if .NoContext -}} 169 if boil.DebugMode { 170 fmt.Fprintln(boil.DebugWriter, cache.retQuery) 171 fmt.Fprintln(boil.DebugWriter, identifierCols...) 172 } 173 {{else -}} 174 if boil.IsDebug(ctx) { 175 writer := boil.DebugWriterFrom(ctx) 176 fmt.Fprintln(writer, cache.retQuery) 177 fmt.Fprintln(writer, identifierCols...) 178 } 179 {{end -}} 180 181 {{if .NoContext -}} 182 err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...) 183 {{else -}} 184 err = exec.QueryRowContext(ctx, cache.retQuery, identifierCols...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...) 185 {{end -}} 186 if err != nil { 187 return errors.Wrap(err, "{{.PkgName}}: unable to populate default values for {{.Table.Name}}") 188 } 189 {{else}} 190 if len(cache.retMapping) != 0 { 191 {{if .NoContext -}} 192 err = exec.QueryRow(cache.query, vals...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...) 193 {{else -}} 194 err = exec.QueryRowContext(ctx, cache.query, vals...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...) 195 {{end -}} 196 } else { 197 {{if .NoContext -}} 198 _, err = exec.Exec(cache.query, vals...) 199 {{else -}} 200 _, err = exec.ExecContext(ctx, cache.query, vals...) 201 {{end -}} 202 } 203 204 if err != nil { 205 return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}") 206 } 207 {{end}} 208 209 {{if .Dialect.UseLastInsertID -}} 210 CacheNoHooks: 211 {{- end}} 212 if !cached { 213 {{$alias.DownSingular}}InsertCacheMut.Lock() 214 {{$alias.DownSingular}}InsertCache[key] = cache 215 {{$alias.DownSingular}}InsertCacheMut.Unlock() 216 } 217 218 {{if not .NoHooks -}} 219 return o.doAfterInsertHooks({{if not .NoContext}}ctx, {{end -}} exec) 220 {{- else -}} 221 return nil 222 {{- end}} 223 } 224 225 {{- end -}}