-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource.go
83 lines (67 loc) · 2.17 KB
/
datasource.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
package web
import (
"database/sql"
"github.com/golang/protobuf/proto"
log "github.com/tommzn/go-log"
utils "github.com/tommzn/go-utils"
"google.golang.org/protobuf/encoding/protojson"
)
func NewDataSourceRepository(db *sql.DB, logger log.Logger) *DataSourceRepository {
return &DataSourceRepository{logger: logger, db: db}
}
func (repo *DataSourceRepository) Get(id string) (*DataSource, error) {
datasource := DataSource{DataSourceConfig: make(map[string]string)}
dataSourceRow := repo.db.QueryRow("SELECT * FROM datasources WHERE id = ?", id)
err := dataSourceRow.Scan(&datasource.Id, &datasource.Type, &datasource.Event, &datasource.Name)
if err != nil {
return nil, err
}
dsConfigRows, err := repo.db.Query("SELECT key, value FROM datasource_config WHERE datasource_id = ?", id)
if err != nil {
return nil, err
}
for dsConfigRows.Next() {
var key, value string
err := dsConfigRows.Scan(&key, &value)
if err != nil {
return nil, err
}
datasource.DataSourceConfig[key] = value
}
return &datasource, nil
}
func (repo *DataSourceRepository) Set(datasource DataSource) (*DataSource, error) {
if len(datasource.Id) == 0 {
datasource.Id = utils.NewId()
}
_, err := repo.db.Exec("INSERT INTO datasources(id, type, event_type, name) VALUES(?, ?, ?, ?)", datasource.Id, datasource.Type, datasource.Event, datasource.Name)
return &datasource, err
}
func (repo *DataSourceRepository) Delete(id string) error {
_, err := repo.db.Exec("DELETE FROM datasources WHERE id = ?", id)
return err
}
func (repo *DataSourceRepository) List() ([]DataSource, error) {
datasources := []DataSource{}
dataSourceRows, err := repo.db.Query("SELECT id FROM datasources")
if err != nil {
return datasources, err
}
for dataSourceRows.Next() {
var id string
err := dataSourceRows.Scan(&id)
if err != nil {
return datasources, err
}
datasource, err := repo.Get(id)
if err != nil {
return datasources, err
}
datasources = append(datasources, *datasource)
}
return datasources, nil
}
// AsJson converts given protobuf message to its JSON representation.
func asJson(message proto.Message) ([]byte, error) {
return protojson.Marshal(proto.MessageV2(message))
}