github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/extend/guilds/lib/guild_store.go (about) 1 package guilds 2 3 import ( 4 "database/sql" 5 6 qgen "github.com/Azareal/Gosora/query_gen" 7 ) 8 9 var Gstore GuildStore 10 11 type GuildStore interface { 12 Get(id int) (g *Guild, err error) 13 Create(name, desc string, active bool, privacy, uid, fid int) (int, error) 14 } 15 16 type SQLGuildStore struct { 17 get *sql.Stmt 18 create *sql.Stmt 19 } 20 21 func NewSQLGuildStore() (*SQLGuildStore, error) { 22 acc := qgen.NewAcc() 23 return &SQLGuildStore{ 24 get: acc.Select("guilds").Columns("name, desc, active, privacy, joinable, owner, memberCount, mainForum, backdrop, createdAt, lastUpdateTime").Where("guildID=?").Prepare(), 25 create: acc.Insert("guilds").Columns("name, desc, active, privacy, joinable, owner, memberCount, mainForum, backdrop, createdAt, lastUpdateTime").Fields("?,?,?,?,1,?,1,?,'',UTC_TIMESTAMP(),UTC_TIMESTAMP()").Prepare(), 26 }, acc.FirstError() 27 } 28 29 func (s *SQLGuildStore) Close() { 30 _ = s.get.Close() 31 _ = s.create.Close() 32 } 33 34 func (s *SQLGuildStore) Get(id int) (g *Guild, err error) { 35 g = &Guild{ID: id} 36 err = s.get.QueryRow(id).Scan(&g.Name, &g.Desc, &g.Active, &g.Privacy, &g.Joinable, &g.Owner, &g.MemberCount, &g.MainForumID, &g.Backdrop, &g.CreatedAt, &g.LastUpdateTime) 37 return g, err 38 } 39 40 func (s *SQLGuildStore) Create(name, desc string, active bool, privacy, uid, fid int) (int, error) { 41 res, err := s.create.Exec(name, desc, active, privacy, uid, fid) 42 if err != nil { 43 return 0, err 44 } 45 lastID, err := res.LastInsertId() 46 return int(lastID), err 47 }