parser.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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).ToCamel()
  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 fields []Field
  88. var primaryKey Primary
  89. for _, column := range columns {
  90. if column == nil {
  91. continue
  92. }
  93. var comment string
  94. if column.Type.Comment != nil {
  95. comment = string(column.Type.Comment.Val)
  96. }
  97. dataType, err := converter.ConvertDataType(column.Type.Type)
  98. if err != nil {
  99. return nil, err
  100. }
  101. var field Field
  102. field.Name = stringx.From(column.Name.String())
  103. field.DataBaseType = column.Type.Type
  104. field.DataType = dataType
  105. field.Comment = comment
  106. key, ok := keyMap[column.Name.String()]
  107. if ok {
  108. field.IsKey = true
  109. field.IsPrimaryKey = key == primary
  110. if field.IsPrimaryKey {
  111. primaryKey.Field = field
  112. if column.Type.Autoincrement {
  113. primaryKey.AutoIncrement = true
  114. }
  115. }
  116. }
  117. fields = append(fields, field)
  118. }
  119. return &Table{
  120. Name: stringx.From(tableName),
  121. PrimaryKey: primaryKey,
  122. Fields: fields,
  123. }, nil
  124. }