strings.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package orm
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. /**
  7. * Just a collectiong on strings (output) methods
  8. * for many of the types in the package
  9. *
  10. * Moved here for better readability in source code.
  11. **/
  12. func (f fieldType) String() string {
  13. switch f {
  14. case Relation:
  15. return "Relation"
  16. }
  17. return "Column"
  18. }
  19. func (t tables) String() string {
  20. s := make([]string, 0)
  21. for _, tbl := range t {
  22. s = append(s, tbl.String())
  23. }
  24. return strings.Join(s, "\n\n")
  25. }
  26. func (t *table) String() string {
  27. t.Lock()
  28. defer t.Unlock()
  29. return fmt.Sprintf("%s (%s):%s%s",
  30. t.getStructName(), t.getName(false),
  31. func() string { // Print columns
  32. s := []string{}
  33. cols := strings.Split(t.cols.String(), "\n")
  34. if len(cols) > 0 {
  35. s = append(s, "\n - Fields")
  36. }
  37. max := 0
  38. for _, col := range cols {
  39. col := strings.Split(col, ":")
  40. if len(col[0]) > max {
  41. max = len(col[0])
  42. }
  43. if pk := t.getPrimaryKey(); pk != nil && "* "+pk.getName(true) == col[0] {
  44. col[1] = col[1] + ", primary_key"
  45. }
  46. s = append(s, strings.Join(col, ":"))
  47. }
  48. for i, col := range s {
  49. col := strings.Split(col, ":")
  50. if len(col) != 2 || len(col[0]) == max {
  51. continue
  52. }
  53. s[i] = col[0] + ":" + strings.Repeat(" ", max-len(col[0])) + col[1]
  54. }
  55. return strings.Join(s, "\n\t")
  56. }(),
  57. func() string {
  58. if len(t.rels.rmap) == 0 {
  59. return ""
  60. }
  61. s := []string{}
  62. rels := strings.Split(t.rels.String(), "\n")
  63. for _, rel := range rels {
  64. xrel := strings.Split(rel, ":")
  65. s = append(s, " - "+xrel[0])
  66. for _, rel := range strings.Split(xrel[1], ",") {
  67. s = append(s, "\t"+strings.Trim(rel, " "))
  68. }
  69. }
  70. return "\n" + strings.Join(s, "\n")
  71. }(),
  72. )
  73. }
  74. func (c column) String() string {
  75. return c.getFieldName() + ": " + c.getName(true) + ", " + c.getKind().String()
  76. }
  77. func (cs columns) String() string {
  78. s := []string{}
  79. for _, c := range cs {
  80. s = append(s, c.String())
  81. }
  82. return "* " + strings.Join(s, "\n* ")
  83. }
  84. func (t relType) String() string {
  85. switch t {
  86. case hasOne:
  87. return "HasOne"
  88. case hasMany:
  89. return "HasMany"
  90. case belongsTo:
  91. return "BelongsTo"
  92. }
  93. return "Not mapped"
  94. }
  95. func (r *relation) String() string {
  96. return r.getStructName() + " » " + r.getName(true) + " ON " + r.on.getName(true) + " WITH " + r.key.getName(true)
  97. }
  98. func (r relations) String() string {
  99. r.m.Lock()
  100. defer r.m.Unlock()
  101. s := []string{}
  102. for t, rels := range r.rmap {
  103. if len(rels) == 0 {
  104. continue
  105. }
  106. ss := []string{}
  107. for _, rel := range rels {
  108. ss = append(ss, rel.String())
  109. }
  110. s = append(s, t.String()+":"+strings.Join(ss, ", "))
  111. }
  112. return strings.Join(s, "\n")
  113. }