-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
146 lines (118 loc) · 3.2 KB
/
db.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
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
package nmc_typhoon_db_client
import (
"fmt"
"log"
"strconv"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type QueryConditions struct {
StartTime string
EndTime string
ForecastHour string
}
const queryTimeFormat = "2006-01-02 15:04:05"
const optionTimeFormat = "2006010215"
func GetRecords(
conditions QueryConditions,
config DatabaseConfig,
) ([]Record, error) {
datetimeQuery, err := generateDateTimeQuery(conditions)
if err != nil {
return nil, fmt.Errorf("get datetime query has error: %v", err)
}
forecastHourQuery, err := generateForecastHourQuery(conditions)
if err != nil {
return nil, fmt.Errorf("parse forecast hour query has error: %v", err)
}
var db *sqlx.DB
conn := fmt.Sprintf(
"%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true&loc=Local",
config.Auth.User,
config.Auth.Password,
config.Host,
config.DatabaseName)
db, err = sqlx.Open("mysql", conn)
if err != nil {
log.Fatal("open db connection has error:", err)
return nil, err
}
defer db.Close()
queryColumnsString := strings.Join(QueryColumns, ",")
tableName := config.TableName
querySQL := fmt.Sprintf("SELECT %s FROM %s WHERE %s AND %s",
queryColumnsString,
tableName,
datetimeQuery,
forecastHourQuery,
)
// log.Println(querySQL)
rows, err := db.Queryx(querySQL)
if err != nil {
log.Fatal("query db has error:", err)
return nil, err
}
defer rows.Close()
records := []Record{}
for rows.Next() {
var record Record
err = rows.StructScan(&record)
if err != nil {
log.Fatal("scan row has error:", err)
return nil, err
}
// log.Println(record.xuhao, record.center, record.datetime)
records = append(records, record)
}
err = rows.Err()
if err != nil {
log.Fatal("query rows has error:", err)
return nil, err
}
return records, nil
}
func generateDateTimeQuery(
conditions QueryConditions,
) (string, error) {
startTime, err := time.Parse(optionTimeFormat, conditions.StartTime)
if err != nil {
return "", fmt.Errorf("parse start time has error: %v", err)
}
if len(conditions.EndTime) == 0 {
query := fmt.Sprintf("datetime='%s'", startTime.Format(queryTimeFormat))
return query, nil
}
endTime, err := time.Parse(optionTimeFormat, conditions.EndTime)
if err != nil {
return "", fmt.Errorf("parse end time has error: %v", err)
}
query := fmt.Sprintf(
"datetime BETWEEN '%s' and '%s'",
startTime.Format(queryTimeFormat),
endTime.Format(queryTimeFormat),
)
return query, nil
}
func generateForecastHourQuery(
conditions QueryConditions,
) (string, error) {
i := strings.Index(conditions.ForecastHour, "-")
if i == -1 {
forecastHour, err := strconv.Atoi(conditions.ForecastHour)
if err != nil {
return "", fmt.Errorf("parse forecsat hour has error: %v", err)
}
return fmt.Sprintf("fcsthour=%d", forecastHour), nil
}
startForecastHour, err := strconv.Atoi(conditions.ForecastHour[:i])
if err != nil {
return "", fmt.Errorf("parse forecsat hour has error: %v", err)
}
endForecastHour, err := strconv.Atoi(conditions.ForecastHour[i+1:])
if err != nil {
return "", fmt.Errorf("parse forecsat hour has error: %v", err)
}
return fmt.Sprintf("fcsthour BETWEEN %d AND %d", startForecastHour, endForecastHour), nil
}