menu.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package back
  2. import (
  3. "database/sql"
  4. "errors"
  5. )
  6. func prepareQueriesMenu() []string {
  7. var e error
  8. var ebox []string
  9. queries["Insert#Menu#"], e = db.Prepare(`INSERT INTO "Menu"
  10. ("Parent", "Caption", "Link", "Position", "Active", "Category")
  11. VALUES ($1, $2, $3, $4, TRUE, $5)`)
  12. if e != nil {
  13. ebox = append(ebox, "Insert#Menu# - "+e.Error())
  14. }
  15. queries["Delete#Menu#"], e = db.Prepare(`DELETE FROM "Menu" WHERE "ID"=$1`)
  16. if e != nil {
  17. ebox = append(ebox, "Delete#Menu# - "+e.Error())
  18. }
  19. queries["Update#Menu#"], e = db.Prepare(`UPDATE "Menu"
  20. SET "Caption"=$2, "Link"=$3 WHERE "ID"=$1`)
  21. if e != nil {
  22. ebox = append(ebox, "Update#Menu# - "+e.Error())
  23. }
  24. queries["Update#Menu#Active"], e = db.Prepare(`UPDATE "Menu"
  25. SET "Active"=$2 WHERE "ID"=$1`)
  26. if e != nil {
  27. ebox = append(ebox, "Update#Menu#Active - "+e.Error())
  28. }
  29. queries["Select#Menu#"], e = db.Prepare(`SELECT
  30. "ID", "Parent", "Caption", "Link", "Position", "Active" FROM "Menu" WHERE "Active"=TRUE AND "Category"=$1
  31. ORDER BY "Parent" DESC, "Position", "Caption"`)
  32. if e != nil {
  33. ebox = append(ebox, "Select#Menu# - "+e.Error())
  34. }
  35. queries["Select#Menu#All"], e = db.Prepare(`SELECT
  36. "ID", "Parent", "Caption", "Link", "Position", "Active" FROM "Menu" WHERE "Category"=$1
  37. ORDER BY "Parent" DESC, "Position", "Caption"`)
  38. if e != nil {
  39. ebox = append(ebox, "Select#Menu#All - "+e.Error())
  40. }
  41. return ebox
  42. }
  43. type menu struct {
  44. ID int32 `json:"ID"`
  45. Parent sql.NullInt32 `json:"Parent"`
  46. Caption string `json:"Caption"`
  47. Link sql.NullString `json:"Link"`
  48. Position int32 `json:"Position"`
  49. Active bool `json:"Active"`
  50. Categoty string `json:"Category"`
  51. Rows []menu
  52. }
  53. func (m *menu) Insert() error {
  54. stmt, ok := queries["Insert#Menu#"]
  55. if !ok {
  56. return errors.New("Запрос Insert#Menu# не найден")
  57. }
  58. _, e := stmt.Exec(m.Parent, m.Caption, m.Link, m.Position, m.Categoty)
  59. if e != nil {
  60. return e
  61. }
  62. return nil
  63. }
  64. func (m *menu) Delete() error {
  65. stmt, ok := queries["Delete#Menu#"]
  66. if !ok {
  67. return errors.New("Запрос Delete#Menu# не найден")
  68. }
  69. _, e := stmt.Exec(m.ID)
  70. if e != nil {
  71. return e
  72. }
  73. return nil
  74. }
  75. func (m *menu) Update() error {
  76. stmt, ok := queries["Update#Menu#"]
  77. if !ok {
  78. return errors.New("Запрос Update#Menu# не найден")
  79. }
  80. _, e := stmt.Exec(m.ID, m.Caption, m.Link)
  81. if e != nil {
  82. return e
  83. }
  84. return nil
  85. }
  86. func (m *menu) UpdateActive() error {
  87. stmt, ok := queries["Update#Menu#Active"]
  88. if !ok {
  89. return errors.New("Запрос Update#Menu#Active не найден")
  90. }
  91. _, e := stmt.Exec(m.ID, m.Active)
  92. if e != nil {
  93. return e
  94. }
  95. return nil
  96. }
  97. func (m *menu) Select() error {
  98. stmt, ok := queries["Select#Menu#"]
  99. if !ok {
  100. return errors.New("Запрос Select#Menu# не найден")
  101. }
  102. rows, e := stmt.Query(m.Categoty)
  103. if e != nil {
  104. return e
  105. }
  106. defer rows.Close()
  107. for rows.Next() {
  108. e = rows.Scan(
  109. &m.ID,
  110. &m.Parent,
  111. &m.Caption,
  112. &m.Link,
  113. &m.Position,
  114. &m.Active,
  115. )
  116. if e != nil {
  117. return e
  118. }
  119. m.Rows = append(m.Rows, menu{
  120. ID: m.ID,
  121. Parent: m.Parent,
  122. Caption: m.Caption,
  123. Link: m.Link,
  124. Position: m.Position,
  125. Active: m.Active,
  126. })
  127. }
  128. return nil
  129. }
  130. func (m *menu) SelectAll() error {
  131. stmt, ok := queries["Select#Menu#All"]
  132. if !ok {
  133. return errors.New("Запрос Select#Menu#All не найден")
  134. }
  135. rows, e := stmt.Query(m.Categoty)
  136. if e != nil {
  137. return e
  138. }
  139. defer rows.Close()
  140. for rows.Next() {
  141. e = rows.Scan(
  142. &m.ID,
  143. &m.Parent,
  144. &m.Caption,
  145. &m.Link,
  146. &m.Position,
  147. &m.Active,
  148. )
  149. if e != nil {
  150. return e
  151. }
  152. m.Rows = append(m.Rows, menu{
  153. ID: m.ID,
  154. Parent: m.Parent,
  155. Caption: m.Caption,
  156. Link: m.Link,
  157. Position: m.Position,
  158. Active: m.Active,
  159. })
  160. }
  161. return nil
  162. }