github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/store/sqlstore/command_store.go (about)

     1  // Copyright (c) 2015-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/vnforks/kid/v5/model"
    10  	"github.com/vnforks/kid/v5/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("BranchId").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_branch_id", "Commands", "BranchId")
    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) GetByBranch(branchId string) ([]*model.Command, *model.AppError) {
    75  	var commands []*model.Command
    76  
    77  	if _, err := s.GetReplica().Select(&commands, "SELECT * FROM Commands WHERE BranchId = :BranchId AND DeleteAt = 0", map[string]interface{}{"BranchId": branchId}); err != nil {
    78  		return nil, model.NewAppError("SqlCommandStore.GetByBranch", "store.sql_command.save.get_branch.app_error", nil, "branchId="+branchId+", err="+err.Error(), http.StatusInternalServerError)
    79  	}
    80  
    81  	return commands, nil
    82  }
    83  
    84  func (s SqlCommandStore) GetByTrigger(branchId 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 BranchId = :BranchId AND `Trigger` = :Trigger AND DeleteAt = 0"
    90  	} else {
    91  		query = "SELECT * FROM Commands WHERE BranchId = :BranchId AND \"trigger\" = :Trigger AND DeleteAt = 0"
    92  	}
    93  
    94  	if err := s.GetReplica().SelectOne(&command, query, map[string]interface{}{"BranchId": branchId, "Trigger": trigger}); err != nil {
    95  		return nil, model.NewAppError("SqlCommandStore.GetByTrigger", "store.sql_command.get_by_trigger.app_error", nil, "branchId="+branchId+", 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) PermanentDeleteByBranch(branchId string) *model.AppError {
   111  	_, err := s.GetMaster().Exec("DELETE FROM Commands WHERE BranchId = :BranchId", map[string]interface{}{"BranchId": branchId})
   112  	if err != nil {
   113  		return model.NewAppError("SqlCommandStore.DeleteByBranch", "store.sql_command.save.delete_perm.app_error", nil, "id="+branchId+", 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(branchId string) (int64, *model.AppError) {
   142  	query :=
   143  		`SELECT
   144  			COUNT(*)
   145  		FROM
   146  			Commands
   147  		WHERE
   148  			DeleteAt = 0`
   149  
   150  	if len(branchId) > 0 {
   151  		query += " AND BranchId = :BranchId"
   152  	}
   153  
   154  	c, err := s.GetReplica().SelectInt(query, map[string]interface{}{"BranchId": branchId})
   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  }