chaosblade

Форк
0
/
source.go 
106 строк · 2.2 Кб
1
/*
2
 * Copyright 1999-2020 Alibaba Group Holding Ltd.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package data
18

19
import (
20
	"context"
21
	"database/sql"
22
	"fmt"
23
	"github.com/chaosblade-io/chaosblade-spec-go/log"
24
	"path"
25
	"sync"
26
	"unicode"
27

28
	"github.com/chaosblade-io/chaosblade-spec-go/util"
29
	_ "github.com/mattn/go-sqlite3"
30
)
31

32
const dataFile = "chaosblade.dat"
33

34
type SourceI interface {
35
	ExperimentSource
36
	PreparationSource
37
}
38

39
type Source struct {
40
	DB *sql.DB
41
}
42

43
var source SourceI
44
var once = sync.Once{}
45

46
func GetSource() SourceI {
47
	once.Do(func() {
48
		src := &Source{
49
			DB: getConnection(),
50
		}
51
		src.init()
52
		source = src
53
	})
54
	return source
55
}
56

57
const tableExistsDQL = `SELECT count(*) AS c
58
	FROM sqlite_master
59
	WHERE type = "table"
60
	AND name = ?
61
`
62

63
func (s *Source) init() {
64
	s.CheckAndInitExperimentTable()
65
	s.CheckAndInitPreTable()
66
}
67

68
func getConnection() *sql.DB {
69
	database, err := sql.Open("sqlite3", path.Join(util.GetProgramPath(), dataFile))
70
	if err != nil {
71
		log.Fatalf(context.Background(), "open data file err, %s", err.Error())
72
		//log.Error(err, "open data file err")
73
		//os.Exit(1)
74
	}
75
	return database
76
}
77

78
func (s *Source) Close() {
79
	if s.DB != nil {
80
		s.DB.Close()
81
	}
82
}
83

84
// GetUserVersion returns the user_version value
85
func (s *Source) GetUserVersion() (int, error) {
86
	userVerRows, err := s.DB.Query("PRAGMA user_version")
87
	if err != nil {
88
		return 0, err
89
	}
90
	defer userVerRows.Close()
91
	var userVersion int
92
	for userVerRows.Next() {
93
		userVerRows.Scan(&userVersion)
94
	}
95
	return userVersion, nil
96
}
97

98
// UpdateUserVersion to the latest
99
func (s *Source) UpdateUserVersion(version int) error {
100
	_, err := s.DB.Exec(fmt.Sprintf("PRAGMA user_version=%d", version))
101
	return err
102
}
103

104
func UpperFirst(str string) string {
105
	return string(unicode.ToUpper(rune(str[0]))) + str[1:]
106
}
107

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.