MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / renderInsert

Function renderInsert

pkg/formatter/render.go:381–437  ·  view source on GitHub ↗
(i *ast.InsertStatement, opts ast.FormatOptions)

Source from the content-addressed store, hash-verified

379}
380
381func renderInsert(i *ast.InsertStatement, opts ast.FormatOptions) string {
382 if i == nil {
383 return ""
384 }
385 f := newNodeFormatter(opts)
386 sb := f.sb
387
388 if i.With != nil {
389 sb.WriteString(renderWith(i.With, f))
390 sb.WriteString(f.clauseSep())
391 }
392
393 sb.WriteString(f.kw("INSERT INTO"))
394 sb.WriteString(" ")
395 sb.WriteString(i.TableName)
396
397 if len(i.Columns) > 0 {
398 sb.WriteString(" (")
399 sb.WriteString(exprListSQL(i.Columns))
400 sb.WriteString(")")
401 }
402
403 if i.Query != nil {
404 sb.WriteString(f.clauseSep())
405 sb.WriteString(FormatStatement(i.Query, opts))
406 } else if len(i.Values) > 0 {
407 sb.WriteString(f.clauseSep())
408 sb.WriteString(f.kw("VALUES"))
409 sb.WriteString(" ")
410 rows := make([]string, len(i.Values))
411 for idx, row := range i.Values {
412 vals := make([]string, len(row))
413 for j, v := range row {
414 vals[j] = exprSQL(v)
415 }
416 rows[idx] = "(" + strings.Join(vals, ", ") + ")"
417 }
418 sb.WriteString(strings.Join(rows, ", "))
419 }
420
421 if i.OnConflict != nil {
422 sb.WriteString(onConflictSQL(i.OnConflict))
423 }
424
425 if len(i.Returning) > 0 {
426 sb.WriteString(f.clauseSep())
427 sb.WriteString(f.kw("RETURNING"))
428 sb.WriteString(" ")
429 sb.WriteString(exprListSQL(i.Returning))
430 }
431
432 if opts.AddSemicolon {
433 sb.WriteString(";")
434 }
435
436 return f.result()
437}
438

Callers 1

FormatStatementFunction · 0.85

Calls 9

newNodeFormatterFunction · 0.85
renderWithFunction · 0.85
FormatStatementFunction · 0.85
clauseSepMethod · 0.80
kwMethod · 0.80
resultMethod · 0.80
exprListSQLFunction · 0.70
exprSQLFunction · 0.70
onConflictSQLFunction · 0.70

Tested by

no test coverage detected