-
Notifications
You must be signed in to change notification settings - Fork 2
/
select.go
52 lines (47 loc) · 1.11 KB
/
select.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
package main
import (
"database/sql"
"github.com/jinzhu/gorm"
)
type AccountRecords struct {
Account
Record
Recorded bool
temperature sql.NullFloat64
tempType sql.NullInt32
createdAt sql.NullTime
reason sql.NullString
}
func selectRecords(tx *gorm.DB) (result []AccountRecords) {
var err error
var length int
rows, err := tx.Select(`
accounts.id, classes.name, accounts.number, accounts.name,
role, record, account,
temperature, type, records.created_at, records.reason
`).Count(&length).Rows()
if err != nil {
panic(err)
}
result = make([]AccountRecords, length)
for i := 0; rows.Next(); i++ {
var r AccountRecords
err = rows.Scan(
&r.Account.ID, &r.Class.Name, &r.Number, &r.Name,
&r.Role, &r.Authority.Record, &r.Authority.Account,
&r.temperature, &r.tempType, &r.createdAt, &r.reason,
)
if err != nil {
panic(err)
}
if r.temperature.Valid {
r.Recorded = true
r.Record.Temperature = r.temperature.Float64
r.Record.Type = TempType(r.tempType.Int32)
r.Record.CreatedAt = r.createdAt.Time
r.Record.Reason = r.reason.String
}
result[i] = r
}
return
}