123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- package back
- import (
- "database/sql"
- "errors"
- )
- func prepareQueriesMenu() []string {
- var e error
- var ebox []string
- queries["Insert#Menu#"], e = db.Prepare(`INSERT INTO "Menu"
- ("Parent", "Caption", "Link", "Position", "Active", "Category")
- VALUES ($1, $2, $3, $4, TRUE, $5)`)
- if e != nil {
- ebox = append(ebox, "Insert#Menu# - "+e.Error())
- }
- queries["Delete#Menu#"], e = db.Prepare(`DELETE FROM "Menu" WHERE "ID"=$1`)
- if e != nil {
- ebox = append(ebox, "Delete#Menu# - "+e.Error())
- }
- queries["Update#Menu#"], e = db.Prepare(`UPDATE "Menu"
- SET "Caption"=$2, "Link"=$3 WHERE "ID"=$1`)
- if e != nil {
- ebox = append(ebox, "Update#Menu# - "+e.Error())
- }
- queries["Update#Menu#Active"], e = db.Prepare(`UPDATE "Menu"
- SET "Active"=$2 WHERE "ID"=$1`)
- if e != nil {
- ebox = append(ebox, "Update#Menu#Active - "+e.Error())
- }
- queries["Select#Menu#"], e = db.Prepare(`SELECT
- "ID", "Parent", "Caption", "Link", "Position", "Active" FROM "Menu" WHERE "Active"=TRUE AND "Category"=$1
- ORDER BY "Parent" DESC, "Position", "Caption"`)
- if e != nil {
- ebox = append(ebox, "Select#Menu# - "+e.Error())
- }
- queries["Select#Menu#All"], e = db.Prepare(`SELECT
- "ID", "Parent", "Caption", "Link", "Position", "Active" FROM "Menu" WHERE "Category"=$1
- ORDER BY "Parent" DESC, "Position", "Caption"`)
- if e != nil {
- ebox = append(ebox, "Select#Menu#All - "+e.Error())
- }
- return ebox
- }
- type menu struct {
- ID int32 `json:"ID"`
- Parent sql.NullInt32 `json:"Parent"`
- Caption string `json:"Caption"`
- Link sql.NullString `json:"Link"`
- Position int32 `json:"Position"`
- Active bool `json:"Active"`
- Categoty string `json:"Category"`
- Rows []menu
- }
- func (m *menu) Insert() error {
- stmt, ok := queries["Insert#Menu#"]
- if !ok {
- return errors.New("Запрос Insert#Menu# не найден")
- }
- _, e := stmt.Exec(m.Parent, m.Caption, m.Link, m.Position, m.Categoty)
- if e != nil {
- return e
- }
- return nil
- }
- func (m *menu) Delete() error {
- stmt, ok := queries["Delete#Menu#"]
- if !ok {
- return errors.New("Запрос Delete#Menu# не найден")
- }
- _, e := stmt.Exec(m.ID)
- if e != nil {
- return e
- }
- return nil
- }
- func (m *menu) Update() error {
- stmt, ok := queries["Update#Menu#"]
- if !ok {
- return errors.New("Запрос Update#Menu# не найден")
- }
- _, e := stmt.Exec(m.ID, m.Caption, m.Link)
- if e != nil {
- return e
- }
- return nil
- }
- func (m *menu) UpdateActive() error {
- stmt, ok := queries["Update#Menu#Active"]
- if !ok {
- return errors.New("Запрос Update#Menu#Active не найден")
- }
- _, e := stmt.Exec(m.ID, m.Active)
- if e != nil {
- return e
- }
- return nil
- }
- func (m *menu) Select() error {
- stmt, ok := queries["Select#Menu#"]
- if !ok {
- return errors.New("Запрос Select#Menu# не найден")
- }
- rows, e := stmt.Query(m.Categoty)
- if e != nil {
- return e
- }
- defer rows.Close()
- for rows.Next() {
- e = rows.Scan(
- &m.ID,
- &m.Parent,
- &m.Caption,
- &m.Link,
- &m.Position,
- &m.Active,
- )
- if e != nil {
- return e
- }
- m.Rows = append(m.Rows, menu{
- ID: m.ID,
- Parent: m.Parent,
- Caption: m.Caption,
- Link: m.Link,
- Position: m.Position,
- Active: m.Active,
- })
- }
- return nil
- }
- func (m *menu) SelectAll() error {
- stmt, ok := queries["Select#Menu#All"]
- if !ok {
- return errors.New("Запрос Select#Menu#All не найден")
- }
- rows, e := stmt.Query(m.Categoty)
- if e != nil {
- return e
- }
- defer rows.Close()
- for rows.Next() {
- e = rows.Scan(
- &m.ID,
- &m.Parent,
- &m.Caption,
- &m.Link,
- &m.Position,
- &m.Active,
- )
- if e != nil {
- return e
- }
- m.Rows = append(m.Rows, menu{
- ID: m.ID,
- Parent: m.Parent,
- Caption: m.Caption,
- Link: m.Link,
- Position: m.Position,
- Active: m.Active,
- })
- }
- return nil
- }
|