summaryrefslogtreecommitdiffstats
path: root/store/sql_license_store.go
blob: 6f40cb6855f0595af1e00c1fb35fc1de47a5fd14 (plain)
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
79
80
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package store

import (
	"github.com/mattermost/platform/model"
)

type SqlLicenseStore struct {
	SqlStore
}

func NewSqlLicenseStore(sqlStore SqlStore) 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() {
}

func (ls SqlLicenseStore) Save(license *model.LicenseRecord) StoreChannel {

	storeChannel := make(StoreChannel, 1)

	go func() {
		result := StoreResult{}

		license.PreSave()
		if result.Err = license.IsValid(); result.Err != nil {
			storeChannel <- result
			close(storeChannel)
			return
		}

		// Only insert if not exists
		if err := ls.GetReplica().SelectOne(&model.LicenseRecord{}, "SELECT * FROM Licenses WHERE Id = :Id", map[string]interface{}{"Id": license.Id}); err != nil {
			if err := ls.GetMaster().Insert(license); err != nil {
				result.Err = model.NewLocAppError("SqlLicenseStore.Save", "store.sql_license.save.app_error", nil, "license_id="+license.Id+", "+err.Error())
			} else {
				result.Data = license
			}
		}

		storeChannel <- result
		close(storeChannel)
	}()

	return storeChannel
}

func (ls SqlLicenseStore) Get(id string) StoreChannel {

	storeChannel := make(StoreChannel, 1)

	go func() {
		result := StoreResult{}

		if obj, err := ls.GetReplica().Get(model.LicenseRecord{}, id); err != nil {
			result.Err = model.NewLocAppError("SqlLicenseStore.Get", "store.sql_license.get.app_error", nil, "license_id="+id+", "+err.Error())
		} else if obj == nil {
			result.Err = model.NewLocAppError("SqlLicenseStore.Get", "store.sql_license.get.missing.app_error", nil, "license_id="+id)
		} else {
			result.Data = obj.(*model.LicenseRecord)
		}

		storeChannel <- result
		close(storeChannel)

	}()

	return storeChannel
}