parser.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package parser
  2. import (
  3. "fmt"
  4. "github.com/tal-tech/go-zero/tools/goctl/model/sql/converter"
  5. "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
  6. "github.com/xwb1989/sqlparser"
  7. )
  8. const (
  9. none = iota
  10. primary
  11. unique
  12. normal
  13. spatial
  14. )
  15. const timeImport = "time.Time"
  16. type (
  17. Table struct {
  18. Name stringx.String
  19. PrimaryKey Primary
  20. Fields []Field
  21. }
  22. Primary struct {
  23. Field
  24. AutoIncrement bool
  25. }
  26. Field struct {
  27. Name stringx.String
  28. DataBaseType string
  29. DataType string
  30. IsKey bool
  31. IsPrimaryKey bool
  32. IsUniqueKey bool
  33. Comment string
  34. }
  35. KeyType int
  36. )
  37. func Parse(ddl string) (*Table, error) {
  38. stmt, err := sqlparser.ParseStrictDDL(ddl)
  39. if err != nil {
  40. return nil, err
  41. }
  42. ddlStmt, ok := stmt.(*sqlparser.DDL)
  43. if !ok {
  44. return nil, unSupportDDL
  45. }
  46. action := ddlStmt.Action
  47. if action != sqlparser.CreateStr {
  48. return nil, fmt.Errorf("expected [CREATE] action,but found: %s", action)
  49. }
  50. tableName := ddlStmt.NewName.Name.String()
  51. tableSpec := ddlStmt.TableSpec
  52. if tableSpec == nil {
  53. return nil, tableBodyIsNotFound
  54. }
  55. columns := tableSpec.Columns
  56. indexes := tableSpec.Indexes
  57. keyMap := make(map[string]KeyType)
  58. for _, index := range indexes {
  59. info := index.Info
  60. if info == nil {
  61. continue
  62. }
  63. if info.Primary {
  64. if len(index.Columns) > 1 {
  65. return nil, errPrimaryKey
  66. }
  67. keyMap[index.Columns[0].Column.String()] = primary
  68. continue
  69. }
  70. // can optimize
  71. if len(index.Columns) > 1 {
  72. continue
  73. }
  74. column := index.Columns[0]
  75. columnName := column.Column.String()
  76. camelColumnName := stringx.From(columnName).ToCamel()
  77. // by default, createTime|updateTime findOne is not used.
  78. if camelColumnName == "CreateTime" || camelColumnName == "UpdateTime" {
  79. continue
  80. }
  81. if info.Unique {
  82. keyMap[columnName] = unique
  83. } else if info.Spatial {
  84. keyMap[columnName] = spatial
  85. } else {
  86. keyMap[columnName] = normal
  87. }
  88. }
  89. var fields []Field
  90. var primaryKey Primary
  91. for _, column := range columns {
  92. if column == nil {
  93. continue
  94. }
  95. var comment string
  96. if column.Type.Comment != nil {
  97. comment = string(column.Type.Comment.Val)
  98. }
  99. dataType, err := converter.ConvertDataType(column.Type.Type)
  100. if err != nil {
  101. return nil, err
  102. }
  103. var field Field
  104. field.Name = stringx.From(column.Name.String())
  105. field.DataBaseType = column.Type.Type
  106. field.DataType = dataType
  107. field.Comment = comment
  108. key, ok := keyMap[column.Name.String()]
  109. if ok {
  110. field.IsKey = true
  111. field.IsPrimaryKey = key == primary
  112. field.IsUniqueKey = key == unique
  113. if field.IsPrimaryKey {
  114. primaryKey.Field = field
  115. if column.Type.Autoincrement {
  116. primaryKey.AutoIncrement = true
  117. }
  118. }
  119. }
  120. fields = append(fields, field)
  121. }
  122. return &Table{
  123. Name: stringx.From(tableName),
  124. PrimaryKey: primaryKey,
  125. Fields: fields,
  126. }, nil
  127. }
  128. func (t *Table) ContainsTime() bool {
  129. for _, item := range t.Fields {
  130. if item.DataType == timeImport {
  131. return true
  132. }
  133. }
  134. return false
  135. }