-
Notifications
You must be signed in to change notification settings - Fork 7k
/
license_store.go
78 lines (67 loc) · 2.8 KB
/
license_store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package sqlstore
import (
"net/http"
sq "github.com/Masterminds/squirrel"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/store"
)
// SqlLicenseStore encapsulates the database writes and reads for
// model.LicenseRecord objects.
type SqlLicenseStore struct {
SqlStore
}
func newSqlLicenseStore(sqlStore SqlStore) store.LicenseStore {
ls := &SqlLicenseStore{sqlStore}
for _, db := range sqlStore.GetAllConns() {
table := db.AddTableWithName(model.LicenseRecord{}, "Licenses").SetKeys(false, "Id")
table.ColMap("Id").SetMaxSize(26)
table.ColMap("Bytes").SetMaxSize(10000)
}
return ls
}
func (ls SqlLicenseStore) createIndexesIfNotExists() {
}
// Save validates and stores the license instance in the database. The Id
// and Bytes fields are mandatory. The Bytes field is limited to a maximum
// of 10000 bytes. If the license ID matches an existing license in the
// database it returns the license stored in the database. If not, it saves the
// new database and returns the created license with the CreateAt field
// updated.
func (ls SqlLicenseStore) Save(license *model.LicenseRecord) (*model.LicenseRecord, *model.AppError) {
license.PreSave()
if err := license.IsValid(); err != nil {
return nil, err
}
query := ls.getQueryBuilder().
Select("*").
From("Licenses").
Where(sq.Eq{"Id": license.Id})
queryString, args, err := query.ToSql()
if err != nil {
return nil, model.NewAppError("SqlLicenseStore.Save", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
}
var storedLicense model.LicenseRecord
if err := ls.GetReplica().SelectOne(&storedLicense, queryString, args...); err != nil {
// Only insert if not exists
if err := ls.GetMaster().Insert(license); err != nil {
return nil, model.NewAppError("SqlLicenseStore.Save", "store.sql_license.save.app_error", nil, "license_id="+license.Id+", "+err.Error(), http.StatusInternalServerError)
}
return license, nil
}
return &storedLicense, nil
}
// Get obtains the license with the provided id parameter from the database.
// If the license doesn't exist it returns a model.AppError with
// http.StatusNotFound in the StatusCode field.
func (ls SqlLicenseStore) Get(id string) (*model.LicenseRecord, *model.AppError) {
obj, err := ls.GetReplica().Get(model.LicenseRecord{}, id)
if err != nil {
return nil, model.NewAppError("SqlLicenseStore.Get", "store.sql_license.get.app_error", nil, "license_id="+id+", "+err.Error(), http.StatusInternalServerError)
}
if obj == nil {
return nil, model.NewAppError("SqlLicenseStore.Get", "store.sql_license.get.missing.app_error", nil, "license_id="+id, http.StatusNotFound)
}
return obj.(*model.LicenseRecord), nil
}