createtable.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. "strings"
  7. )
  8. // NewCreateTableBuilder creates a new CREATE TABLE builder.
  9. func NewCreateTableBuilder() *CreateTableBuilder {
  10. return DefaultFlavor.NewCreateTableBuilder()
  11. }
  12. func newCreateTableBuilder() *CreateTableBuilder {
  13. args := &Args{}
  14. return &CreateTableBuilder{
  15. verb: "CREATE TABLE",
  16. args: args,
  17. }
  18. }
  19. // CreateTableBuilder is a builder to build CREATE TABLE.
  20. type CreateTableBuilder struct {
  21. verb string
  22. ifNotExists bool
  23. table string
  24. defs [][]string
  25. options [][]string
  26. args *Args
  27. }
  28. // CreateTable sets the table name in CREATE TABLE.
  29. func (ctb *CreateTableBuilder) CreateTable(table string) *CreateTableBuilder {
  30. ctb.table = Escape(table)
  31. return ctb
  32. }
  33. // CreateTempTable sets the table name and changes the verb of ctb to CREATE TEMPORARY TABLE.
  34. func (ctb *CreateTableBuilder) CreateTempTable(table string) *CreateTableBuilder {
  35. ctb.verb = "CREATE TEMPORARY TABLE"
  36. ctb.table = Escape(table)
  37. return ctb
  38. }
  39. // IfNotExists adds IF NOT EXISTS before table name in CREATE TABLE.
  40. func (ctb *CreateTableBuilder) IfNotExists() *CreateTableBuilder {
  41. ctb.ifNotExists = true
  42. return ctb
  43. }
  44. // Define adds definition of a column or index in CREATE TABLE.
  45. func (ctb *CreateTableBuilder) Define(def ...string) *CreateTableBuilder {
  46. ctb.defs = append(ctb.defs, def)
  47. return ctb
  48. }
  49. // Option adds a table option in CREATE TABLE.
  50. func (ctb *CreateTableBuilder) Option(opt ...string) *CreateTableBuilder {
  51. ctb.options = append(ctb.options, opt)
  52. return ctb
  53. }
  54. // String returns the compiled INSERT string.
  55. func (ctb *CreateTableBuilder) String() string {
  56. s, _ := ctb.Build()
  57. return s
  58. }
  59. // Build returns compiled CREATE TABLE string and args.
  60. // They can be used in `DB#Query` of package `database/sql` directly.
  61. func (ctb *CreateTableBuilder) Build() (sql string, args []interface{}) {
  62. return ctb.BuildWithFlavor(ctb.args.Flavor)
  63. }
  64. // BuildWithFlavor returns compiled CREATE TABLE string and args with flavor and initial args.
  65. // They can be used in `DB#Query` of package `database/sql` directly.
  66. func (ctb *CreateTableBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{}) {
  67. buf := &bytes.Buffer{}
  68. buf.WriteString(ctb.verb)
  69. if ctb.ifNotExists {
  70. buf.WriteString(" IF NOT EXISTS")
  71. }
  72. buf.WriteRune(' ')
  73. buf.WriteString(ctb.table)
  74. buf.WriteString(" (")
  75. defs := make([]string, 0, len(ctb.defs))
  76. for _, def := range ctb.defs {
  77. defs = append(defs, strings.Join(def, " "))
  78. }
  79. buf.WriteString(strings.Join(defs, ", "))
  80. buf.WriteRune(')')
  81. if len(ctb.options) > 0 {
  82. buf.WriteRune(' ')
  83. opts := make([]string, 0, len(ctb.options))
  84. for _, opt := range ctb.options {
  85. opts = append(opts, strings.Join(opt, " "))
  86. }
  87. buf.WriteString(strings.Join(opts, ", "))
  88. }
  89. return ctb.args.CompileWithFlavor(buf.String(), flavor, initialArg...)
  90. }
  91. // SetFlavor sets the flavor of compiled sql.
  92. func (ctb *CreateTableBuilder) SetFlavor(flavor Flavor) (old Flavor) {
  93. old = ctb.args.Flavor
  94. ctb.args.Flavor = flavor
  95. return
  96. }