You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
2.9 KiB

2 years ago
  1. // Copyright 2016 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package builder
  5. import (
  6. "bytes"
  7. "fmt"
  8. "sort"
  9. )
  10. // Insert creates an insert Builder
  11. func Insert(eq ...interface{}) *Builder {
  12. builder := &Builder{cond: NewCond()}
  13. return builder.Insert(eq...)
  14. }
  15. func (b *Builder) insertSelectWriteTo(w Writer) error {
  16. if _, err := fmt.Fprintf(w, "INSERT INTO %s ", b.into); err != nil {
  17. return err
  18. }
  19. if len(b.insertCols) > 0 {
  20. fmt.Fprintf(w, "(")
  21. for _, col := range b.insertCols {
  22. fmt.Fprintf(w, col)
  23. }
  24. fmt.Fprintf(w, ") ")
  25. }
  26. return b.selectWriteTo(w)
  27. }
  28. func (b *Builder) insertWriteTo(w Writer) error {
  29. if len(b.into) <= 0 {
  30. return ErrNoTableName
  31. }
  32. if len(b.insertCols) <= 0 && b.from == "" {
  33. return ErrNoColumnToInsert
  34. }
  35. if b.into != "" && b.from != "" {
  36. return b.insertSelectWriteTo(w)
  37. }
  38. if _, err := fmt.Fprintf(w, "INSERT INTO %s (", b.into); err != nil {
  39. return err
  40. }
  41. var args = make([]interface{}, 0)
  42. var bs []byte
  43. var valBuffer = bytes.NewBuffer(bs)
  44. for i, col := range b.insertCols {
  45. value := b.insertVals[i]
  46. fmt.Fprint(w, col)
  47. if e, ok := value.(expr); ok {
  48. fmt.Fprintf(valBuffer, "(%s)", e.sql)
  49. args = append(args, e.args...)
  50. } else if value == nil {
  51. fmt.Fprintf(valBuffer, `null`)
  52. } else {
  53. fmt.Fprint(valBuffer, "?")
  54. args = append(args, value)
  55. }
  56. if i != len(b.insertCols)-1 {
  57. if _, err := fmt.Fprint(w, ","); err != nil {
  58. return err
  59. }
  60. if _, err := fmt.Fprint(valBuffer, ","); err != nil {
  61. return err
  62. }
  63. }
  64. }
  65. if _, err := fmt.Fprint(w, ") Values ("); err != nil {
  66. return err
  67. }
  68. if _, err := w.Write(valBuffer.Bytes()); err != nil {
  69. return err
  70. }
  71. if _, err := fmt.Fprint(w, ")"); err != nil {
  72. return err
  73. }
  74. w.Append(args...)
  75. return nil
  76. }
  77. type insertColsSorter struct {
  78. cols []string
  79. vals []interface{}
  80. }
  81. func (s insertColsSorter) Len() int {
  82. return len(s.cols)
  83. }
  84. func (s insertColsSorter) Swap(i, j int) {
  85. s.cols[i], s.cols[j] = s.cols[j], s.cols[i]
  86. s.vals[i], s.vals[j] = s.vals[j], s.vals[i]
  87. }
  88. func (s insertColsSorter) Less(i, j int) bool {
  89. return s.cols[i] < s.cols[j]
  90. }
  91. // Insert sets insert SQL
  92. func (b *Builder) Insert(eq ...interface{}) *Builder {
  93. if len(eq) > 0 {
  94. var paramType = -1
  95. for _, e := range eq {
  96. switch t := e.(type) {
  97. case Eq:
  98. if paramType == -1 {
  99. paramType = 0
  100. }
  101. if paramType != 0 {
  102. break
  103. }
  104. for k, v := range t {
  105. b.insertCols = append(b.insertCols, k)
  106. b.insertVals = append(b.insertVals, v)
  107. }
  108. case string:
  109. if paramType == -1 {
  110. paramType = 1
  111. }
  112. if paramType != 1 {
  113. break
  114. }
  115. b.insertCols = append(b.insertCols, t)
  116. }
  117. }
  118. }
  119. if len(b.insertCols) == len(b.insertVals) {
  120. sort.Sort(insertColsSorter{
  121. cols: b.insertCols,
  122. vals: b.insertVals,
  123. })
  124. }
  125. b.optype = insertType
  126. return b
  127. }