insert.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2018 Huan Du. All rights reserved.
  2. // Licensed under the MIT license that can be found in the LICENSE file.
  3. package sqlbuilder
  4. import (
  5. "bytes"
  6. "fmt"
  7. "strings"
  8. )
  9. // NewInsertBuilder creates a new INSERT builder.
  10. func NewInsertBuilder() *InsertBuilder {
  11. return DefaultFlavor.NewInsertBuilder()
  12. }
  13. func newInsertBuilder() *InsertBuilder {
  14. args := &Args{}
  15. return &InsertBuilder{
  16. verb: "INSERT",
  17. args: args,
  18. }
  19. }
  20. // InsertBuilder is a builder to build INSERT.
  21. type InsertBuilder struct {
  22. verb string
  23. table string
  24. cols []string
  25. values [][]string
  26. args *Args
  27. }
  28. // InsertInto sets table name in INSERT.
  29. func (ib *InsertBuilder) InsertInto(table string) *InsertBuilder {
  30. ib.table = Escape(table)
  31. return ib
  32. }
  33. // InsertIgnoreInto sets table name in INSERT IGNORE.
  34. func (ib *InsertBuilder) InsertIgnoreInto(table string) *InsertBuilder {
  35. ib.verb = "INSERT IGNORE"
  36. ib.table = Escape(table)
  37. return ib
  38. }
  39. // ReplaceInto sets table name and changes the verb of ib to REPLACE.
  40. // REPLACE INTO is a MySQL extension to the SQL standard.
  41. func (ib *InsertBuilder) ReplaceInto(table string) *InsertBuilder {
  42. ib.verb = "REPLACE"
  43. ib.table = Escape(table)
  44. return ib
  45. }
  46. // Cols sets columns in INSERT.
  47. func (ib *InsertBuilder) Cols(col ...string) *InsertBuilder {
  48. ib.cols = EscapeAll(col...)
  49. return ib
  50. }
  51. // Values adds a list of values for a row in INSERT.
  52. func (ib *InsertBuilder) Values(value ...interface{}) *InsertBuilder {
  53. placeholders := make([]string, 0, len(value))
  54. for _, v := range value {
  55. placeholders = append(placeholders, ib.args.Add(v))
  56. }
  57. ib.values = append(ib.values, placeholders)
  58. return ib
  59. }
  60. // String returns the compiled INSERT string.
  61. func (ib *InsertBuilder) String() string {
  62. s, _ := ib.Build()
  63. return s
  64. }
  65. // Build returns compiled INSERT string and args.
  66. // They can be used in `DB#Query` of package `database/sql` directly.
  67. func (ib *InsertBuilder) Build() (sql string, args []interface{}) {
  68. return ib.BuildWithFlavor(ib.args.Flavor)
  69. }
  70. // BuildWithFlavor returns compiled INSERT string and args with flavor and initial args.
  71. // They can be used in `DB#Query` of package `database/sql` directly.
  72. func (ib *InsertBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{}) {
  73. buf := &bytes.Buffer{}
  74. buf.WriteString(ib.verb)
  75. buf.WriteString(" INTO ")
  76. buf.WriteString(ib.table)
  77. if len(ib.cols) > 0 {
  78. buf.WriteString(" (")
  79. buf.WriteString(strings.Join(ib.cols, ", "))
  80. buf.WriteString(")")
  81. }
  82. buf.WriteString(" VALUES ")
  83. values := make([]string, 0, len(ib.values))
  84. for _, v := range ib.values {
  85. values = append(values, fmt.Sprintf("(%v)", strings.Join(v, ", ")))
  86. }
  87. buf.WriteString(strings.Join(values, ", "))
  88. return ib.args.CompileWithFlavor(buf.String(), flavor, initialArg...)
  89. }
  90. // SetFlavor sets the flavor of compiled sql.
  91. func (ib *InsertBuilder) SetFlavor(flavor Flavor) (old Flavor) {
  92. old = ib.args.Flavor
  93. ib.args.Flavor = flavor
  94. return
  95. }