strings.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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(),
  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. s = append(s, strings.Join(col, ":"))
  44. }
  45. for i, col := range s {
  46. col := strings.Split(col, ":")
  47. if len(col) != 2 || len(col[0]) == max {
  48. continue
  49. }
  50. s[i] = col[0] + ":" + strings.Repeat(" ", max-len(col[0])) + col[1]
  51. }
  52. return strings.Join(s, "\n\t")
  53. }(),
  54. func() string {
  55. if len(t.rels.rmap) == 0 {
  56. return ""
  57. }
  58. s := []string{}
  59. rels := strings.Split(t.rels.String(), "\n")
  60. for _, rel := range rels {
  61. xrel := strings.Split(rel, ":")
  62. s = append(s, " - "+xrel[0])
  63. for _, rel := range strings.Split(xrel[1], ",") {
  64. s = append(s, "\t"+strings.Trim(rel, " "))
  65. }
  66. }
  67. return "\n" + strings.Join(s, "\n")
  68. }(),
  69. )
  70. }
  71. func (c column) String() string {
  72. return c.getFieldName() + ": " + c.GetName() + ", " + c.getKind().String()
  73. }
  74. func (cs columns) String() string {
  75. s := []string{}
  76. for _, c := range cs {
  77. s = append(s, c.String())
  78. }
  79. return "* " + strings.Join(s, "\n* ")
  80. }
  81. func (t relType) String() string {
  82. switch t {
  83. case hasOne:
  84. return "HasOne"
  85. case hasMany:
  86. return "HasMany"
  87. case belongsTo:
  88. return "BelongsTo"
  89. }
  90. return "Not mapped"
  91. }
  92. func (r *relation) String() string {
  93. return r.f.getFieldType() + " AS `" + r.f.getFieldName() + "` ON " + r.on.GetName(r) + " WITH " + r.key.GetName()
  94. }
  95. func (r relations) String() string {
  96. r.m.Lock()
  97. defer r.m.Unlock()
  98. s := []string{}
  99. for t, rels := range r.rmap {
  100. if len(rels) == 0 {
  101. continue
  102. }
  103. ss := []string{}
  104. for _, rel := range rels {
  105. ss = append(ss, rel.String())
  106. }
  107. s = append(s, t.String()+":"+strings.Join(ss, ", "))
  108. }
  109. return strings.Join(s, "\n")
  110. }