-
Notifications
You must be signed in to change notification settings - Fork 2
/
manager.go
111 lines (99 loc) · 2.26 KB
/
manager.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
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
package olapsql
import (
"fmt"
"github.com/awatercolorpen/olap-sql/api/types"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type Manager struct {
clients Clients
dictionary *Dictionary
}
func (m *Manager) GetClients() (Clients, error) {
if m.clients == nil {
return nil, fmt.Errorf("it is no initialization")
}
return m.clients, nil
}
func (m *Manager) GetDictionary() (*Dictionary, error) {
if m.dictionary == nil {
return nil, fmt.Errorf("it is no initialization")
}
return m.dictionary, nil
}
func (m *Manager) SetLogger(log logger.Interface) {
c, err := m.GetClients()
if err == nil {
c.SetLogger(log)
}
}
func (m *Manager) RunSync(query *types.Query) (*types.Result, error) {
db, err := m.BuildTransaction(query)
if err != nil {
return nil, err
}
rows, err := RunSync(db)
if err != nil {
return nil, err
}
return BuildResultSync(query, rows)
}
func (m *Manager) RunChan(query *types.Query) (*types.Result, error) {
db, err := m.BuildTransaction(query)
if err != nil {
return nil, err
}
rows, err := RunChan(db)
if err != nil {
return nil, err
}
return BuildResultChan(query, rows)
}
func (m *Manager) BuildTransaction(query *types.Query) (*gorm.DB, error) {
clients, clause, err := m.build(query)
if err != nil {
return nil, err
}
return clients.BuildDB(clause)
}
func (m *Manager) BuildSQL(query *types.Query) (string, error) {
clients, clause, err := m.build(query)
if err != nil {
return "", err
}
return clients.BuildSQL(clause)
}
func (m *Manager) build(query *types.Query) (Clients, types.Clause, error) {
query.TranslateTimeIntervalToFilter()
dict, err := m.GetDictionary()
if err != nil {
return nil, nil, err
}
clause, err := dict.Translate(query)
if err != nil {
return nil, nil, err
}
clients, err := m.GetClients()
if err != nil {
return nil, nil, err
}
return clients, clause, nil
}
func NewManager(configuration *Configuration) (*Manager, error) {
m := &Manager{}
if configuration.ClientsOption != nil {
clients, err := NewClients(configuration.ClientsOption)
if err != nil {
return nil, err
}
m.clients = clients
}
if configuration.DictionaryOption != nil {
dict, err := NewDictionary(configuration.DictionaryOption)
if err != nil {
return nil, err
}
m.dictionary = dict
}
return m, nil
}