parser.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. type (
  16. Table struct {
  17. Name stringx.String
  18. PrimaryKey Primary
  19. Fields []Field
  20. }
  21. Primary struct {
  22. Field
  23. AutoIncrement bool
  24. }
  25. Field struct {
  26. Name stringx.String
  27. DataBaseType string
  28. DataType string
  29. IsKey bool
  30. IsPrimaryKey bool
  31. Comment string
  32. }
  33. KeyType int
  34. )
  35. func Parse(ddl string) (*Table, error) {
  36. stmt, err := sqlparser.ParseStrictDDL(ddl)
  37. if err != nil {
  38. return nil, err
  39. }
  40. ddlStmt, ok := stmt.(*sqlparser.DDL)
  41. if !ok {
  42. return nil, unSupportDDL
  43. }
  44. action := ddlStmt.Action
  45. if action != sqlparser.CreateStr {
  46. return nil, fmt.Errorf("expected [CREATE] action,but found: %s", action)
  47. }
  48. tableName := ddlStmt.NewName.Name.String()
  49. tableSpec := ddlStmt.TableSpec
  50. if tableSpec == nil {
  51. return nil, tableBodyIsNotFound
  52. }
  53. columns := tableSpec.Columns
  54. indexes := tableSpec.Indexes
  55. keyMap := make(map[string]KeyType)
  56. for _, index := range indexes {
  57. info := index.Info
  58. if info == nil {
  59. continue
  60. }
  61. if info.Primary {
  62. if len(index.Columns) > 1 {
  63. return nil, errPrimaryKey
  64. }
  65. keyMap[index.Columns[0].Column.String()] = primary
  66. continue
  67. }
  68. // can optimize
  69. if len(index.Columns) > 1 {
  70. continue
  71. }
  72. column := index.Columns[0]
  73. columnName := column.Column.String()
  74. camelColumnName := stringx.From(columnName).Snake2Camel()
  75. // by default, createTime|updateTime findOne is not used.
  76. if camelColumnName == "CreateTime" || camelColumnName == "UpdateTime" {
  77. continue
  78. }
  79. if info.Unique {
  80. keyMap[columnName] = unique
  81. } else if info.Spatial {
  82. keyMap[columnName] = spatial
  83. } else {
  84. keyMap[columnName] = normal
  85. }
  86. }
  87. var (
  88. fields []Field
  89. primaryKey Primary
  90. )
  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. if field.IsPrimaryKey {
  113. primaryKey.Field = field
  114. if column.Type.Autoincrement {
  115. primaryKey.AutoIncrement = true
  116. }
  117. }
  118. }
  119. fields = append(fields, field)
  120. }
  121. return &Table{
  122. Name: stringx.From(tableName),
  123. PrimaryKey: primaryKey,
  124. Fields: fields,
  125. }, nil
  126. }