forked from chararch/gobatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trade_reader.go
62 lines (56 loc) · 1.33 KB
/
trade_reader.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
package example2
import (
"database/sql"
"fmt"
"github.com/supreness/batch"
)
type tradeReader struct {
db *sql.DB
}
func (h *tradeReader) Open(execution *batch.StepExecution) batch.BatchError {
return nil
}
func (h *tradeReader) Close(execution *batch.StepExecution) batch.BatchError {
return nil
}
func (h *tradeReader) ReadKeys() ([]interface{}, error) {
rows, err := h.db.Query("select id from t_trade")
if err != nil {
return nil, err
}
defer rows.Close()
var result []interface{}
var id int64
for rows.Next() {
err = rows.Scan(&id)
if err != nil {
return nil, err
}
result = append(result, id)
}
return result, nil
}
func (h *tradeReader) ReadItem(key interface{}) (interface{}, error) {
id := int64(0)
switch r := key.(type) {
case int64:
id = r
case float64:
id = int64(r)
default:
return nil, fmt.Errorf("key type error, type:%T, value:%v", key, key)
}
rows, err := h.db.Query("select trade_no, account_no, type, amount, terms, interest_rate, trade_time, status from t_trade where id = ?", id)
if err != nil {
return nil, err
}
defer rows.Close()
trade := &Trade{}
if rows.Next() {
err = rows.Scan(&trade.TradeNo, &trade.AccountNo, &trade.Type, &trade.Amount, &trade.Terms, &trade.InterestRate, &trade.TradeTime, &trade.Status)
if err != nil {
return nil, err
}
}
return trade, nil
}