github.com/gocaveman/caveman@v0.0.0-20191211162744-0ddf99dbdf6e/menus/menusdbr/db-menu-store.go (about) 1 package menusdbr 2 3 import ( 4 "github.com/gocaveman/caveman/autowire" 5 "github.com/gocaveman/caveman/menus" 6 "github.com/gocaveman/caveman/migrate" 7 "github.com/gocaveman/caveman/migrate/migrateregistry" 8 "github.com/gocaveman/caveman/webutil" 9 "github.com/gocraft/dbr" 10 ) 11 12 var Migrations migrate.MigrationList 13 14 func init() { 15 16 // register in migrateregistry and with autowire for all 3 databases 17 reg := func(m *migrate.SQLTmplMigration) { 18 var rm migrate.Migration 19 rm = m.NewWithDriverName("sqlite3") 20 Migrations = append(Migrations, rm) 21 autowire.Populate(migrateregistry.MustRegister(rm)) 22 rm = m.NewWithDriverName("mysql") 23 Migrations = append(Migrations, rm) 24 autowire.Populate(migrateregistry.MustRegister(rm)) 25 rm = m.NewWithDriverName("postgres") 26 Migrations = append(Migrations, rm) 27 autowire.Populate(migrateregistry.MustRegister(rm)) 28 } 29 30 reg(&migrate.SQLTmplMigration{ 31 CategoryValue: "menusdbr", 32 VersionValue: "0001_menus_create", // must be unique and indicates sequence 33 UpSQL: []string{` 34 CREATE TABLE {{.TablePrefix}}menu ( 35 menu_id VARCHAR(255), 36 parent_menu_id VARCHAR(255), 37 sequence DOUBLE, 38 title VARCHAR(255), 39 meta TEXT, 40 enabled INTEGER, 41 registry_edited INTEGER, 42 PRIMARY KEY (menu_id) 43 ) 44 `}, 45 DownSQL: []string{`DROP TABLE {{.TablePrefix}}menu`}, 46 }) 47 48 reg(&migrate.SQLTmplMigration{ 49 CategoryValue: "menusdbr", 50 VersionValue: "0002_menus_indexes", // must be unique and indicates sequence 51 UpSQL: []string{` 52 CREATE INDEX {{.TablePrefix}}menu_parent ON {{.TablePrefix}}menu (parent_menu_id) 53 `}, 54 DownSQL: []string{` 55 DROP INDEX {{.TablePrefix}}menu_parent ON {{.TablePrefix}}menu 56 `}, 57 }) 58 59 } 60 61 // FIXME: timestamp columns (get the types and setup right both in 62 // the Go struct and the DDL) 63 64 type DBMenuItem struct { 65 menus.MenuItem 66 RegistryEdited bool `db:"registry_edited"` 67 } 68 69 type DBMenuStore struct { 70 Connection *dbr.Connection // `autowire:""` 71 TablePrefix string `autowire:"db_table_prefix,optional"` 72 } 73 74 func (s *DBMenuStore) ReadMenuItem(id string) (*menus.MenuItem, error) { 75 sess := s.Connection.NewSession(nil) 76 var dbmi DBMenuItem 77 err := sess.Select("*").From(s.TablePrefix+"menu").Where("menu_id=?", id).LoadOne(&dbmi) 78 if err != nil { 79 if err == dbr.ErrNotFound { 80 return nil, menus.ErrNotFound 81 } 82 return nil, err 83 } 84 return &dbmi.MenuItem, nil 85 } 86 87 func (s *DBMenuStore) CreateMenuItem(mi *menus.MenuItem) error { 88 sess := s.Connection.NewSession(nil) 89 var dbmi DBMenuItem 90 dbmi.MenuItem = *mi 91 // TODO: registry integration 92 _, err := sess.InsertInto(s.TablePrefix+"menu"). 93 Columns("menu_id", "parent_menu_id", "sequence", "title", "meta", "enabled", "registry_edited"). 94 Values(dbmi.MenuID, dbmi.ParentMenuID, dbmi.Sequence, dbmi.Title, webutil.SimpleStringDataMap(dbmi.Meta), dbmi.Enabled, dbmi.RegistryEdited). 95 Exec() 96 if err != nil { 97 return err 98 } 99 return nil 100 } 101 102 func (s *DBMenuStore) UpdateMenuItem(mi *menus.MenuItem) error { 103 sess := s.Connection.NewSession(nil) 104 var dbmi DBMenuItem 105 dbmi.MenuItem = *mi 106 // TODO: registry integration 107 res, err := sess.Update(s.TablePrefix+"menu"). 108 Set("menu_id", dbmi.MenuID). 109 Set("parent_menu_id", dbmi.ParentMenuID). 110 Set("sequence", dbmi.Sequence). 111 Set("title", dbmi.Title). 112 Set("meta", webutil.SimpleStringDataMap(dbmi.Meta)). 113 Set("enabled", dbmi.Enabled). 114 Set("registry_edited", dbmi.RegistryEdited). 115 Exec() 116 if err != nil { 117 return err 118 } 119 n, err := res.RowsAffected() 120 if err != nil { 121 return err 122 } 123 if n != 1 { 124 return menus.ErrNotFound 125 } 126 return nil 127 } 128 129 func (s *DBMenuStore) DeleteMenuItem(id string) error { 130 sess := s.Connection.NewSession(nil) 131 132 res, err := sess.DeleteFrom(s.TablePrefix+"menu").Where("menu_id=?", id).Exec() 133 if err != nil { 134 return err 135 } 136 n, err := res.RowsAffected() 137 if err != nil { 138 return err 139 } 140 if n != 1 { 141 return menus.ErrNotFound 142 } 143 return nil 144 145 } 146 147 func (s *DBMenuStore) FindChildren(id string) ([]string, error) { 148 sess := s.Connection.NewSession(nil) 149 var ret []string 150 _, err := sess.Select("menu_id").From(s.TablePrefix+"menu").Where("parent_menu_id=?", id).Load(&ret) 151 return ret, err 152 }