column.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package migrator
  2. // Notice
  3. // code based on parts from from https://github.com/go-xorm/core/blob/3e0fa232ab5c90996406c0cd7ae86ad0e5ecf85f/column.go
  4. type Column struct {
  5. Name string
  6. Type string
  7. Length int
  8. Length2 int
  9. Nullable bool
  10. IsPrimaryKey bool
  11. IsAutoIncrement bool
  12. Default string
  13. }
  14. func (col *Column) String(d Dialect) string {
  15. sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
  16. sql += d.SqlType(col) + " "
  17. if col.IsPrimaryKey {
  18. sql += "PRIMARY KEY "
  19. if col.IsAutoIncrement {
  20. sql += d.AutoIncrStr() + " "
  21. }
  22. }
  23. if d.ShowCreateNull() {
  24. if col.Nullable {
  25. sql += "NULL "
  26. } else {
  27. sql += "NOT NULL "
  28. }
  29. }
  30. if col.Default != "" {
  31. sql += "DEFAULT " + col.Default + " "
  32. }
  33. return sql
  34. }
  35. func (col *Column) StringNoPk(d Dialect) string {
  36. sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
  37. sql += d.SqlType(col) + " "
  38. if d.ShowCreateNull() {
  39. if col.Nullable {
  40. sql += "NULL "
  41. } else {
  42. sql += "NOT NULL "
  43. }
  44. }
  45. if col.Default != "" {
  46. sql += "DEFAULT " + d.Default(col) + " "
  47. }
  48. return sql
  49. }