github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/store/sqlstore/license_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 // SqlLicenseStore encapsulates the database writes and reads for 14 // model.LicenseRecord objects. 15 type SqlLicenseStore struct { 16 SqlStore 17 } 18 19 func newSqlLicenseStore(sqlStore SqlStore) store.LicenseStore { 20 ls := &SqlLicenseStore{sqlStore} 21 22 for _, db := range sqlStore.GetAllConns() { 23 table := db.AddTableWithName(model.LicenseRecord{}, "Licenses").SetKeys(false, "Id") 24 table.ColMap("Id").SetMaxSize(26) 25 table.ColMap("Bytes").SetMaxSize(10000) 26 } 27 28 return ls 29 } 30 31 func (ls SqlLicenseStore) createIndexesIfNotExists() { 32 } 33 34 // Save validates and stores the license instance in the database. The Id 35 // and Bytes fields are mandatory. The Bytes field is limited to a maximum 36 // of 10000 bytes. If the license ID matches an existing license in the 37 // database it returns the license stored in the database. If not, it saves the 38 // new database and returns the created license with the CreateAt field 39 // updated. 40 func (ls SqlLicenseStore) Save(license *model.LicenseRecord) (*model.LicenseRecord, *model.AppError) { 41 license.PreSave() 42 if err := license.IsValid(); err != nil { 43 return nil, err 44 } 45 46 var storedLicense model.LicenseRecord 47 if err := ls.GetReplica().SelectOne(&storedLicense, "SELECT * FROM Licenses WHERE Id = :Id", map[string]interface{}{"Id": license.Id}); err != nil { 48 // Only insert if not exists 49 if err := ls.GetMaster().Insert(license); err != nil { 50 return nil, model.NewAppError("SqlLicenseStore.Save", "store.sql_license.save.app_error", nil, "license_id="+license.Id+", "+err.Error(), http.StatusInternalServerError) 51 } 52 return license, nil 53 } 54 return &storedLicense, nil 55 } 56 57 // Get obtains the license with the provided id parameter from the database. 58 // If the license doesn't exist it returns a model.AppError with 59 // http.StatusNotFound in the StatusCode field. 60 func (ls SqlLicenseStore) Get(id string) (*model.LicenseRecord, *model.AppError) { 61 obj, err := ls.GetReplica().Get(model.LicenseRecord{}, id) 62 if err != nil { 63 return nil, model.NewAppError("SqlLicenseStore.Get", "store.sql_license.get.app_error", nil, "license_id="+id+", "+err.Error(), http.StatusInternalServerError) 64 } 65 if obj == nil { 66 return nil, model.NewAppError("SqlLicenseStore.Get", "store.sql_license.get.missing.app_error", nil, "license_id="+id, http.StatusNotFound) 67 } 68 return obj.(*model.LicenseRecord), nil 69 }