manager.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package back
  2. import (
  3. "errors"
  4. )
  5. type manager struct {
  6. ID int32 `json:"ID"`
  7. Login string `json:"Login"`
  8. Password string `json:"Password"`
  9. FirstName string `json:"FirstName"`
  10. LastName string `json:"LastName"`
  11. Patronymic string `json:"Patronymic"`
  12. Role string `json:"Rolee"`
  13. Rows []manager
  14. }
  15. func prepareQueriesManager() []string {
  16. var e error
  17. ebox := make([]string, 0)
  18. queries["Select#Manager#Test"], e = db.Prepare(`SELECT count(*) FROM "Manager"`)
  19. if e != nil {
  20. ebox = append(ebox, "Select#Manager#Test - "+e.Error())
  21. }
  22. queries["Select#Manager#"], e = db.Prepare(`SELECT "ID", "FirstName", "LastName", "Patronymic", "Role" FROM "Manager" WHERE "Login"=$1 AND "Password"=$2`)
  23. if e != nil {
  24. ebox = append(ebox, "Select#Manager# - "+e.Error())
  25. }
  26. queries["Select#Manager#ByID"], e = db.Prepare(`SELECT "Login", "Password", "FirstName", "LastName", "Patronymic", "Role" FROM "Manager" WHERE "ID"=$1`)
  27. if e != nil {
  28. ebox = append(ebox, "Select#Manager#ByID - "+e.Error())
  29. }
  30. queries["Insert#Manager#"], e = db.Prepare(`INSERT INTO "Manager"(
  31. "Login", "Password", "FirstName", "LastName", "Patronymic", "Role")
  32. VALUES ($1, $2, $3, $4, $5, $6)`)
  33. if e != nil {
  34. ebox = append(ebox, "Insert#Manager# - "+e.Error())
  35. }
  36. return ebox
  37. }
  38. func (m *manager) SelectTest() error {
  39. stmt, ok := queries["Select#Manager#Test"]
  40. if !ok {
  41. return errors.New("Запрос Select#Manager#Test не найден")
  42. }
  43. count := int32(0)
  44. r := stmt.QueryRow()
  45. e := r.Scan(&count)
  46. if e != nil {
  47. return e
  48. }
  49. if count == 0 {
  50. stmt, ok = queries["Insert#Manager#"]
  51. if !ok {
  52. return errors.New("Запрос Insert#Manager# не найден")
  53. }
  54. stmt.Exec("8009008070", "!@#$%^&*", "Administrator", "", "", "__admin")
  55. }
  56. return errors.New("YES")
  57. }
  58. func (m *manager) Select() error {
  59. stmt, ok := queries["Select#Manager#"]
  60. if !ok {
  61. return errors.New("Запрос Select#Manager# не найден")
  62. }
  63. r := stmt.QueryRow(m.Login, m.Password)
  64. e := r.Scan(&m.ID, &m.FirstName, &m.LastName, &m.Patronymic, &m.Role)
  65. if e != nil {
  66. return e
  67. }
  68. return nil
  69. }
  70. func (m *manager) SelectByID() error {
  71. stmt, ok := queries["Select#Manager#ByID"]
  72. if !ok {
  73. return errors.New("Запрос Select#Manager#ByID не найден")
  74. }
  75. r := stmt.QueryRow(m.ID)
  76. e := r.Scan(&m.Login, &m.Password, &m.FirstName, &m.LastName, &m.Patronymic, &m.Role)
  77. if e != nil {
  78. return e
  79. }
  80. return nil
  81. }