-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite3Helper.go
More file actions
executable file
·157 lines (143 loc) · 3.46 KB
/
sqlite3Helper.go
File metadata and controls
executable file
·157 lines (143 loc) · 3.46 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"encoding/json"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"log"
"path/filepath"
"time"
)
type InfluxSqlite3 struct {
db *gorm.DB
Root string
}
type InfluxQueryHistory struct {
Time *time.Time `gorm:"Column:time;default:(datetime(CURRENT_TIMESTAMP,'localtime'))"`
ConnectionName *string `gorm:"column:connection_name;index:db"`
DbName *string `gorm:"column:db_name;index:db"`
Query string `gorm:"column:query;index:query"`
Param *string
Mark *string
}
func NewSqlite3() InfluxSqlite3 {
db := InfluxSqlite3{}
db.GetSettingFromInit()
return db
}
func (lite *InfluxSqlite3) Close() error {
return lite.db.Close()
}
func (lite *InfluxSqlite3) GetSettingFromInit() bool {
settings := NewSettings()
initDBName := "ifdb.init"
dbFile := filepath.Join(settings.SettingPath, initDBName)
var db *gorm.DB
if db = openDB(dbFile); db == nil {
return false
}
lite.Root = settings.SettingPath
db.LogMode(true)
lite.db = db
newSetting := Settings{}
hasSettings := db.HasTable(&newSetting)
if !hasSettings {
db.CreateTable(&newSetting)
}
db.First(&newSetting)
if newSetting.SettingPath == "" { // init the first record
newSetting = NewSettings()
db.Create(&newSetting)
} else {
dbFile = filepath.Join(newSetting.SettingPath, initDBName)
if FileExist(dbFile) {
if db = openDB(dbFile); db != nil {
_ = lite.db.Close()
lite.db = db
}
log.Println("db path is " + dbFile)
} else {
log.Println(dbFile + " is invalid, use default root path")
}
}
lite.Root = newSetting.SettingPath
return true
}
func openDB(dbFile string) *gorm.DB {
db, err := gorm.Open("sqlite3", dbFile)
if err != nil {
log.Println(err.Error())
return nil
}
db.LogMode(true)
return db
}
func (lite *InfluxSqlite3) SaveConnectionInfo(info ConnectionInfo) int64 {
if !lite.db.HasTable(&info) {
lite.db.CreateTable(&info)
}
if info.RowId > 0 {
lite.db.Model(&info).Omit("row_id").Updates(info)
} else {
lite.db.Create(&info)
}
return info.RowId
}
func (lite *InfluxSqlite3) LoadConnections() (result string) {
var info ConnectionInfo
if !lite.db.HasTable(&info) {
lite.db.CreateTable(&info)
}
var infos []ConnectionInfo
lite.db.Find(&infos)
if len(infos) > 0 {
if connections, err := json.Marshal(infos); err != nil {
log.Println(err.Error())
return
} else {
result = string(connections)
}
}
return
}
func (lite *InfluxSqlite3) RemoveConnection(info ConnectionInfo) {
if !lite.db.HasTable(&info) {
lite.db.CreateTable(&info)
return
}
if info.RowId < 1 {
log.Println("RowId is empty, do not delete it")
return
}
lite.db.Delete(&info)
}
func (lite *InfluxSqlite3) ShowHistory(condition *map[string]interface{}) (historys []InfluxQueryHistory) {
history := InfluxQueryHistory{}
if !lite.db.HasTable(&history) {
lite.db.CreateTable(&history)
return historys
}
sql_db := lite.db
limit := 2000
if condition != nil {
if v, ok := (*condition)["ConnectionName"]; ok {
cv := v.(string)
history.ConnectionName = &cv
}
if v, ok := (*condition)["DbName"]; ok {
cv := v.(string)
history.DbName = &cv
}
if v, ok := (*condition)["Query"]; ok {
history.Query = v.(string)
}
if v, ok := (*condition)["offset"]; ok {
sql_db = sql_db.Offset(v.(int))
}
if v, ok := (*condition)["limit"]; ok {
limit = v.(int)
}
}
sql_db = sql_db.Limit(limit)
sql_db.Order("time desc").Where(&history).Find(&historys)
return historys
}