github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/store/sqlstore/license_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 SqlLicenseStore struct {
    14  	SqlStore
    15  }
    16  
    17  func NewSqlLicenseStore(sqlStore SqlStore) store.LicenseStore {
    18  	ls := &SqlLicenseStore{sqlStore}
    19  
    20  	for _, db := range sqlStore.GetAllConns() {
    21  		table := db.AddTableWithName(model.LicenseRecord{}, "Licenses").SetKeys(false, "Id")
    22  		table.ColMap("Id").SetMaxSize(26)
    23  		table.ColMap("Bytes").SetMaxSize(10000)
    24  	}
    25  
    26  	return ls
    27  }
    28  
    29  func (ls SqlLicenseStore) CreateIndexesIfNotExists() {
    30  }
    31  
    32  func (ls SqlLicenseStore) Save(license *model.LicenseRecord) store.StoreChannel {
    33  	return store.Do(func(result *store.StoreResult) {
    34  		license.PreSave()
    35  		if result.Err = license.IsValid(); result.Err != nil {
    36  			return
    37  		}
    38  
    39  		// Only insert if not exists
    40  		if err := ls.GetReplica().SelectOne(&model.LicenseRecord{}, "SELECT * FROM Licenses WHERE Id = :Id", map[string]interface{}{"Id": license.Id}); err != nil {
    41  			if err := ls.GetMaster().Insert(license); err != nil {
    42  				result.Err = model.NewAppError("SqlLicenseStore.Save", "store.sql_license.save.app_error", nil, "license_id="+license.Id+", "+err.Error(), http.StatusInternalServerError)
    43  			} else {
    44  				result.Data = license
    45  			}
    46  		}
    47  	})
    48  }
    49  
    50  func (ls SqlLicenseStore) Get(id string) store.StoreChannel {
    51  	return store.Do(func(result *store.StoreResult) {
    52  		if obj, err := ls.GetReplica().Get(model.LicenseRecord{}, id); err != nil {
    53  			result.Err = model.NewAppError("SqlLicenseStore.Get", "store.sql_license.get.app_error", nil, "license_id="+id+", "+err.Error(), http.StatusInternalServerError)
    54  		} else if obj == nil {
    55  			result.Err = model.NewAppError("SqlLicenseStore.Get", "store.sql_license.get.missing.app_error", nil, "license_id="+id, http.StatusNotFound)
    56  		} else {
    57  			result.Data = obj.(*model.LicenseRecord)
    58  		}
    59  	})
    60  }