github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/sqlstore/system_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 SqlSystemStore struct {
    14  	SqlStore
    15  }
    16  
    17  func NewSqlSystemStore(sqlStore SqlStore) store.SystemStore {
    18  	s := &SqlSystemStore{sqlStore}
    19  
    20  	for _, db := range sqlStore.GetAllConns() {
    21  		table := db.AddTableWithName(model.System{}, "Systems").SetKeys(false, "Name")
    22  		table.ColMap("Name").SetMaxSize(64)
    23  		table.ColMap("Value").SetMaxSize(1024)
    24  	}
    25  
    26  	return s
    27  }
    28  
    29  func (s SqlSystemStore) CreateIndexesIfNotExists() {
    30  }
    31  
    32  func (s SqlSystemStore) Save(system *model.System) store.StoreChannel {
    33  	return store.Do(func(result *store.StoreResult) {
    34  		if err := s.GetMaster().Insert(system); err != nil {
    35  			result.Err = model.NewAppError("SqlSystemStore.Save", "store.sql_system.save.app_error", nil, err.Error(), http.StatusInternalServerError)
    36  		}
    37  	})
    38  }
    39  
    40  func (s SqlSystemStore) SaveOrUpdate(system *model.System) store.StoreChannel {
    41  	return store.Do(func(result *store.StoreResult) {
    42  		if err := s.GetReplica().SelectOne(&model.System{}, "SELECT * FROM Systems WHERE Name = :Name", map[string]interface{}{"Name": system.Name}); err == nil {
    43  			if _, err := s.GetMaster().Update(system); err != nil {
    44  				result.Err = model.NewAppError("SqlSystemStore.SaveOrUpdate", "store.sql_system.update.app_error", nil, err.Error(), http.StatusInternalServerError)
    45  			}
    46  		} else {
    47  			if err := s.GetMaster().Insert(system); err != nil {
    48  				result.Err = model.NewAppError("SqlSystemStore.SaveOrUpdate", "store.sql_system.save.app_error", nil, err.Error(), http.StatusInternalServerError)
    49  			}
    50  		}
    51  	})
    52  }
    53  
    54  func (s SqlSystemStore) Update(system *model.System) store.StoreChannel {
    55  	return store.Do(func(result *store.StoreResult) {
    56  		if _, err := s.GetMaster().Update(system); err != nil {
    57  			result.Err = model.NewAppError("SqlSystemStore.Update", "store.sql_system.update.app_error", nil, err.Error(), http.StatusInternalServerError)
    58  		}
    59  	})
    60  }
    61  
    62  func (s SqlSystemStore) Get() store.StoreChannel {
    63  	return store.Do(func(result *store.StoreResult) {
    64  		var systems []model.System
    65  		props := make(model.StringMap)
    66  		if _, err := s.GetReplica().Select(&systems, "SELECT * FROM Systems"); err != nil {
    67  			result.Err = model.NewAppError("SqlSystemStore.Get", "store.sql_system.get.app_error", nil, err.Error(), http.StatusInternalServerError)
    68  		} else {
    69  			for _, prop := range systems {
    70  				props[prop.Name] = prop.Value
    71  			}
    72  
    73  			result.Data = props
    74  		}
    75  	})
    76  }
    77  
    78  func (s SqlSystemStore) GetByName(name string) store.StoreChannel {
    79  	return store.Do(func(result *store.StoreResult) {
    80  		var system model.System
    81  		if err := s.GetReplica().SelectOne(&system, "SELECT * FROM Systems WHERE Name = :Name", map[string]interface{}{"Name": name}); err != nil {
    82  			result.Err = model.NewAppError("SqlSystemStore.GetByName", "store.sql_system.get_by_name.app_error", nil, err.Error(), http.StatusInternalServerError)
    83  		}
    84  
    85  		result.Data = &system
    86  	})
    87  }
    88  
    89  func (s SqlSystemStore) PermanentDeleteByName(name string) store.StoreChannel {
    90  	return store.Do(func(result *store.StoreResult) {
    91  		var system model.System
    92  		if _, err := s.GetMaster().Exec("DELETE FROM Systems WHERE Name = :Name", map[string]interface{}{"Name": name}); err != nil {
    93  			result.Err = model.NewAppError("SqlSystemStore.PermanentDeleteByName", "store.sql_system.permanent_delete_by_name.app_error", nil, err.Error(), http.StatusInternalServerError)
    94  		}
    95  
    96  		result.Data = &system
    97  	})
    98  }