github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/store/sqlstore/command_store.go (about) 1 // Copyright (c) 2016-present Xenia, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package sqlstore 5 6 import ( 7 "net/http" 8 9 "github.com/xzl8028/xenia-server/model" 10 "github.com/xzl8028/xenia-server/store" 11 ) 12 13 type SqlCommandStore struct { 14 SqlStore 15 } 16 17 func NewSqlCommandStore(sqlStore SqlStore) store.CommandStore { 18 s := &SqlCommandStore{sqlStore} 19 20 for _, db := range sqlStore.GetAllConns() { 21 tableo := db.AddTableWithName(model.Command{}, "Commands").SetKeys(false, "Id") 22 tableo.ColMap("Id").SetMaxSize(26) 23 tableo.ColMap("Token").SetMaxSize(26) 24 tableo.ColMap("CreatorId").SetMaxSize(26) 25 tableo.ColMap("TeamId").SetMaxSize(26) 26 tableo.ColMap("Trigger").SetMaxSize(128) 27 tableo.ColMap("URL").SetMaxSize(1024) 28 tableo.ColMap("Method").SetMaxSize(1) 29 tableo.ColMap("Username").SetMaxSize(64) 30 tableo.ColMap("IconURL").SetMaxSize(1024) 31 tableo.ColMap("AutoCompleteDesc").SetMaxSize(1024) 32 tableo.ColMap("AutoCompleteHint").SetMaxSize(1024) 33 tableo.ColMap("DisplayName").SetMaxSize(64) 34 tableo.ColMap("Description").SetMaxSize(128) 35 } 36 37 return s 38 } 39 40 func (s SqlCommandStore) CreateIndexesIfNotExists() { 41 s.CreateIndexIfNotExists("idx_command_team_id", "Commands", "TeamId") 42 s.CreateIndexIfNotExists("idx_command_update_at", "Commands", "UpdateAt") 43 s.CreateIndexIfNotExists("idx_command_create_at", "Commands", "CreateAt") 44 s.CreateIndexIfNotExists("idx_command_delete_at", "Commands", "DeleteAt") 45 } 46 47 func (s SqlCommandStore) Save(command *model.Command) (*model.Command, *model.AppError) { 48 if len(command.Id) > 0 { 49 return nil, model.NewAppError("SqlCommandStore.Save", "store.sql_command.save.saving_overwrite.app_error", nil, "id="+command.Id, http.StatusBadRequest) 50 } 51 52 command.PreSave() 53 if err := command.IsValid(); err != nil { 54 return nil, err 55 } 56 57 if err := s.GetMaster().Insert(command); err != nil { 58 return nil, model.NewAppError("SqlCommandStore.Save", "store.sql_command.save.saving.app_error", nil, "id="+command.Id+", "+err.Error(), http.StatusInternalServerError) 59 } 60 61 return command, nil 62 } 63 64 func (s SqlCommandStore) Get(id string) (*model.Command, *model.AppError) { 65 var command model.Command 66 67 if err := s.GetReplica().SelectOne(&command, "SELECT * FROM Commands WHERE Id = :Id AND DeleteAt = 0", map[string]interface{}{"Id": id}); err != nil { 68 return nil, model.NewAppError("SqlCommandStore.Get", "store.sql_command.save.get.app_error", nil, "id="+id+", err="+err.Error(), http.StatusInternalServerError) 69 } 70 71 return &command, nil 72 } 73 74 func (s SqlCommandStore) GetByTeam(teamId string) ([]*model.Command, *model.AppError) { 75 var commands []*model.Command 76 77 if _, err := s.GetReplica().Select(&commands, "SELECT * FROM Commands WHERE TeamId = :TeamId AND DeleteAt = 0", map[string]interface{}{"TeamId": teamId}); err != nil { 78 return nil, model.NewAppError("SqlCommandStore.GetByTeam", "store.sql_command.save.get_team.app_error", nil, "teamId="+teamId+", err="+err.Error(), http.StatusInternalServerError) 79 } 80 81 return commands, nil 82 } 83 84 func (s SqlCommandStore) GetByTrigger(teamId string, trigger string) (*model.Command, *model.AppError) { 85 var command model.Command 86 87 var query string 88 if s.DriverName() == "mysql" { 89 query = "SELECT * FROM Commands WHERE TeamId = :TeamId AND `Trigger` = :Trigger AND DeleteAt = 0" 90 } else { 91 query = "SELECT * FROM Commands WHERE TeamId = :TeamId AND \"trigger\" = :Trigger AND DeleteAt = 0" 92 } 93 94 if err := s.GetReplica().SelectOne(&command, query, map[string]interface{}{"TeamId": teamId, "Trigger": trigger}); err != nil { 95 return nil, model.NewAppError("SqlCommandStore.GetByTrigger", "store.sql_command.get_by_trigger.app_error", nil, "teamId="+teamId+", trigger="+trigger+", err="+err.Error(), http.StatusInternalServerError) 96 } 97 98 return &command, nil 99 } 100 101 func (s SqlCommandStore) Delete(commandId string, time int64) *model.AppError { 102 _, err := s.GetMaster().Exec("Update Commands SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt WHERE Id = :Id", map[string]interface{}{"DeleteAt": time, "UpdateAt": time, "Id": commandId}) 103 if err != nil { 104 return model.NewAppError("SqlCommandStore.Delete", "store.sql_command.save.delete.app_error", nil, "id="+commandId+", err="+err.Error(), http.StatusInternalServerError) 105 } 106 107 return nil 108 } 109 110 func (s SqlCommandStore) PermanentDeleteByTeam(teamId string) *model.AppError { 111 _, err := s.GetMaster().Exec("DELETE FROM Commands WHERE TeamId = :TeamId", map[string]interface{}{"TeamId": teamId}) 112 if err != nil { 113 return model.NewAppError("SqlCommandStore.DeleteByTeam", "store.sql_command.save.delete_perm.app_error", nil, "id="+teamId+", err="+err.Error(), http.StatusInternalServerError) 114 } 115 return nil 116 } 117 118 func (s SqlCommandStore) PermanentDeleteByUser(userId string) *model.AppError { 119 _, err := s.GetMaster().Exec("DELETE FROM Commands WHERE CreatorId = :UserId", map[string]interface{}{"UserId": userId}) 120 if err != nil { 121 return model.NewAppError("SqlCommandStore.DeleteByUser", "store.sql_command.save.delete_perm.app_error", nil, "id="+userId+", err="+err.Error(), http.StatusInternalServerError) 122 } 123 124 return nil 125 } 126 127 func (s SqlCommandStore) Update(cmd *model.Command) (*model.Command, *model.AppError) { 128 cmd.UpdateAt = model.GetMillis() 129 130 if err := cmd.IsValid(); err != nil { 131 return nil, err 132 } 133 134 if _, err := s.GetMaster().Update(cmd); err != nil { 135 return nil, model.NewAppError("SqlCommandStore.Update", "store.sql_command.save.update.app_error", nil, "id="+cmd.Id+", "+err.Error(), http.StatusInternalServerError) 136 } 137 138 return cmd, nil 139 } 140 141 func (s SqlCommandStore) AnalyticsCommandCount(teamId string) (int64, *model.AppError) { 142 query := 143 `SELECT 144 COUNT(*) 145 FROM 146 Commands 147 WHERE 148 DeleteAt = 0` 149 150 if len(teamId) > 0 { 151 query += " AND TeamId = :TeamId" 152 } 153 154 c, err := s.GetReplica().SelectInt(query, map[string]interface{}{"TeamId": teamId}) 155 if err != nil { 156 return 0, model.NewAppError("SqlCommandStore.AnalyticsCommandCount", "store.sql_command.analytics_command_count.app_error", nil, err.Error(), http.StatusInternalServerError) 157 } 158 return c, nil 159 }