parser.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 (
  16. timeImport = "time.Time"
  17. )
  18. type (
  19. Table struct {
  20. Name stringx.String
  21. PrimaryKey Primary
  22. Fields []Field
  23. }
  24. Primary struct {
  25. Field
  26. AutoIncrement bool
  27. }
  28. Field struct {
  29. Name stringx.String
  30. DataBaseType string
  31. DataType string
  32. IsKey bool
  33. IsPrimaryKey bool
  34. Comment string
  35. }
  36. KeyType int
  37. )
  38. func Parse(ddl string) (*Table, error) {
  39. stmt, err := sqlparser.ParseStrictDDL(ddl)
  40. if err != nil {
  41. return nil, err
  42. }
  43. ddlStmt, ok := stmt.(*sqlparser.DDL)
  44. if !ok {
  45. return nil, unSupportDDL
  46. }
  47. action := ddlStmt.Action
  48. if action != sqlparser.CreateStr {
  49. return nil, fmt.Errorf("expected [CREATE] action,but found: %s", action)
  50. }
  51. tableName := ddlStmt.NewName.Name.String()
  52. tableSpec := ddlStmt.TableSpec
  53. if tableSpec == nil {
  54. return nil, tableBodyIsNotFound
  55. }
  56. columns := tableSpec.Columns
  57. indexes := tableSpec.Indexes
  58. keyMap := make(map[string]KeyType)
  59. for _, index := range indexes {
  60. info := index.Info
  61. if info == nil {
  62. continue
  63. }
  64. if info.Primary {
  65. if len(index.Columns) > 1 {
  66. return nil, errPrimaryKey
  67. }
  68. keyMap[index.Columns[0].Column.String()] = primary
  69. continue
  70. }
  71. // can optimize
  72. if len(index.Columns) > 1 {
  73. continue
  74. }
  75. column := index.Columns[0]
  76. columnName := column.Column.String()
  77. camelColumnName := stringx.From(columnName).ToCamel()
  78. // by default, createTime|updateTime findOne is not used.
  79. if camelColumnName == "CreateTime" || camelColumnName == "UpdateTime" {
  80. continue
  81. }
  82. if info.Unique {
  83. keyMap[columnName] = unique
  84. } else if info.Spatial {
  85. keyMap[columnName] = spatial
  86. } else {
  87. keyMap[columnName] = normal
  88. }
  89. }
  90. var fields []Field
  91. var primaryKey Primary
  92. for _, column := range columns {
  93. if column == nil {
  94. continue
  95. }
  96. var comment string
  97. if column.Type.Comment != nil {
  98. comment = string(column.Type.Comment.Val)
  99. }
  100. dataType, err := converter.ConvertDataType(column.Type.Type)
  101. if err != nil {
  102. return nil, err
  103. }
  104. var field Field
  105. field.Name = stringx.From(column.Name.String())
  106. field.DataBaseType = column.Type.Type
  107. field.DataType = dataType
  108. field.Comment = comment
  109. key, ok := keyMap[column.Name.String()]
  110. if ok {
  111. field.IsKey = true
  112. field.IsPrimaryKey = key == primary
  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. }