-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmysql.go
109 lines (94 loc) · 2.54 KB
/
mysql.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
/*
SaMLer - Smart Meter data colletor at the edge
Copyright (C) 2024 Florian Heubeck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"database/sql"
"fmt"
"log"
"time"
_ "github.com/go-sql-driver/mysql"
)
func InitializeMySQL(
mysqlDSN string,
tableName string,
) func(measurement Measurement) bool {
fmt.Printf("Init MySQL")
initialized := false
var database *sql.DB
runInit := func() bool {
db, err := sql.Open("mysql", mysqlDSN)
if err != nil {
log.Printf("Failed to connect to MySQL: %s\n", err)
return false
}
db.SetConnMaxLifetime(3 * time.Minute)
db.SetMaxOpenConns(3)
db.SetMaxIdleConns(1)
if _, err := db.Exec("select now()"); err != nil {
log.Printf("Could not connect to database: %s\n", err)
return false
}
database = db
return setupSchema(db, tableName)
}
sender := func(measurement Measurement) bool {
if !initialized {
initialized = runInit()
if !initialized {
return false
}
}
debug("Sending to MySQL", &measurement)
if _, err := database.Exec(fmt.Sprintf(`INSERT INTO %s
(time, ident, value, unit, prefix, suffix)
VALUES(?, ?, ?, ?, ?, ?)`, tableName),
measurement.Time,
measurement.Ident,
measurement.Value,
measurement.Unit,
measurement.Prefix,
measurement.Suffix,
); err != nil {
log.Printf("Failed sending to MySQL %s\n", err)
initialized = false
return false
}
return true
}
return sender
}
func setupSchema(db *sql.DB, tableName string) bool {
log.Println("Creating database schema")
schema := [...]string{
fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s (
id bigint NOT NULL AUTO_INCREMENT,
time timestamp NOT NULL,
ident varchar(10) NOT NULL,
value double NOT NULL,
unit varchar(5),
prefix varchar(5),
suffix varchar(5),
PRIMARY KEY (id),
INDEX idxTime (time DESC)
)`, tableName),
}
for _, s := range schema {
if _, err := db.Exec(s); err != nil {
log.Printf("Failed to create schema: %s\n", err)
return false
}
}
return true
}