github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/store/sqlstore/command_store.go (about)

     1  // Copyright (c) 2016-present Mattermost, 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/mattermost/mattermost-server/model"
    10  	"github.com/mattermost/mattermost-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) store.StoreChannel {
    48  	return store.Do(func(result *store.StoreResult) {
    49  		if len(command.Id) > 0 {
    50  			result.Err = model.NewAppError("SqlCommandStore.Save", "store.sql_command.save.saving_overwrite.app_error", nil, "id="+command.Id, http.StatusBadRequest)
    51  			return
    52  		}
    53  
    54  		command.PreSave()
    55  		if result.Err = command.IsValid(); result.Err != nil {
    56  			return
    57  		}
    58  
    59  		if err := s.GetMaster().Insert(command); err != nil {
    60  			result.Err = model.NewAppError("SqlCommandStore.Save", "store.sql_command.save.saving.app_error", nil, "id="+command.Id+", "+err.Error(), http.StatusInternalServerError)
    61  		} else {
    62  			result.Data = command
    63  		}
    64  	})
    65  }
    66  
    67  func (s SqlCommandStore) Get(id string) store.StoreChannel {
    68  	return store.Do(func(result *store.StoreResult) {
    69  		var command model.Command
    70  
    71  		if err := s.GetReplica().SelectOne(&command, "SELECT * FROM Commands WHERE Id = :Id AND DeleteAt = 0", map[string]interface{}{"Id": id}); err != nil {
    72  			result.Err = model.NewAppError("SqlCommandStore.Get", "store.sql_command.save.get.app_error", nil, "id="+id+", err="+err.Error(), http.StatusInternalServerError)
    73  		}
    74  
    75  		result.Data = &command
    76  	})
    77  }
    78  
    79  func (s SqlCommandStore) GetByTeam(teamId string) store.StoreChannel {
    80  	return store.Do(func(result *store.StoreResult) {
    81  		var commands []*model.Command
    82  
    83  		if _, err := s.GetReplica().Select(&commands, "SELECT * FROM Commands WHERE TeamId = :TeamId AND DeleteAt = 0", map[string]interface{}{"TeamId": teamId}); err != nil {
    84  			result.Err = model.NewAppError("SqlCommandStore.GetByTeam", "store.sql_command.save.get_team.app_error", nil, "teamId="+teamId+", err="+err.Error(), http.StatusInternalServerError)
    85  		}
    86  
    87  		result.Data = commands
    88  	})
    89  }
    90  
    91  func (s SqlCommandStore) GetByTrigger(teamId string, trigger string) store.StoreChannel {
    92  	return store.Do(func(result *store.StoreResult) {
    93  		var command model.Command
    94  
    95  		var query string
    96  		if s.DriverName() == "mysql" {
    97  			query = "SELECT * FROM Commands WHERE TeamId = :TeamId AND `Trigger` = :Trigger AND DeleteAt = 0"
    98  		} else {
    99  			query = "SELECT * FROM Commands WHERE TeamId = :TeamId AND \"trigger\" = :Trigger AND DeleteAt = 0"
   100  		}
   101  
   102  		if err := s.GetReplica().SelectOne(&command, query, map[string]interface{}{"TeamId": teamId, "Trigger": trigger}); err != nil {
   103  			result.Err = model.NewAppError("SqlCommandStore.GetByTrigger", "store.sql_command.get_by_trigger.app_error", nil, "teamId="+teamId+", trigger="+trigger+", err="+err.Error(), http.StatusInternalServerError)
   104  		}
   105  
   106  		result.Data = &command
   107  	})
   108  }
   109  
   110  func (s SqlCommandStore) Delete(commandId string, time int64) store.StoreChannel {
   111  	return store.Do(func(result *store.StoreResult) {
   112  		_, err := s.GetMaster().Exec("Update Commands SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt WHERE Id = :Id", map[string]interface{}{"DeleteAt": time, "UpdateAt": time, "Id": commandId})
   113  		if err != nil {
   114  			result.Err = model.NewAppError("SqlCommandStore.Delete", "store.sql_command.save.delete.app_error", nil, "id="+commandId+", err="+err.Error(), http.StatusInternalServerError)
   115  		}
   116  	})
   117  }
   118  
   119  func (s SqlCommandStore) PermanentDeleteByTeam(teamId string) store.StoreChannel {
   120  	return store.Do(func(result *store.StoreResult) {
   121  		_, err := s.GetMaster().Exec("DELETE FROM Commands WHERE TeamId = :TeamId", map[string]interface{}{"TeamId": teamId})
   122  		if err != nil {
   123  			result.Err = model.NewAppError("SqlCommandStore.DeleteByTeam", "store.sql_command.save.delete_perm.app_error", nil, "id="+teamId+", err="+err.Error(), http.StatusInternalServerError)
   124  		}
   125  	})
   126  }
   127  
   128  func (s SqlCommandStore) PermanentDeleteByUser(userId string) store.StoreChannel {
   129  	return store.Do(func(result *store.StoreResult) {
   130  		_, err := s.GetMaster().Exec("DELETE FROM Commands WHERE CreatorId = :UserId", map[string]interface{}{"UserId": userId})
   131  		if err != nil {
   132  			result.Err = model.NewAppError("SqlCommandStore.DeleteByUser", "store.sql_command.save.delete_perm.app_error", nil, "id="+userId+", err="+err.Error(), http.StatusInternalServerError)
   133  		}
   134  	})
   135  }
   136  
   137  func (s SqlCommandStore) Update(cmd *model.Command) store.StoreChannel {
   138  	return store.Do(func(result *store.StoreResult) {
   139  		cmd.UpdateAt = model.GetMillis()
   140  
   141  		if result.Err = cmd.IsValid(); result.Err != nil {
   142  			return
   143  		}
   144  
   145  		if _, err := s.GetMaster().Update(cmd); err != nil {
   146  			result.Err = model.NewAppError("SqlCommandStore.Update", "store.sql_command.save.update.app_error", nil, "id="+cmd.Id+", "+err.Error(), http.StatusInternalServerError)
   147  		} else {
   148  			result.Data = cmd
   149  		}
   150  	})
   151  }
   152  
   153  func (s SqlCommandStore) AnalyticsCommandCount(teamId string) store.StoreChannel {
   154  	return store.Do(func(result *store.StoreResult) {
   155  		query :=
   156  			`SELECT
   157  			    COUNT(*)
   158  			FROM
   159  			    Commands
   160  			WHERE
   161  			    DeleteAt = 0`
   162  
   163  		if len(teamId) > 0 {
   164  			query += " AND TeamId = :TeamId"
   165  		}
   166  
   167  		if c, err := s.GetReplica().SelectInt(query, map[string]interface{}{"TeamId": teamId}); err != nil {
   168  			result.Err = model.NewAppError("SqlCommandStore.AnalyticsCommandCount", "store.sql_command.analytics_command_count.app_error", nil, err.Error(), http.StatusInternalServerError)
   169  		} else {
   170  			result.Data = c
   171  		}
   172  	})
   173  }