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
|
package models
import (
"database/sql"
"time"
)
type Person struct {
ID string
FirstName string
LastName string
OrganisationName string
OrganisationID int
RoleName string
Created time.Time
}
type PersonModel struct {
DB *sql.DB
}
func (m *PersonModel) ListAll() ([]Person, error) {
stmt := `SELECT p.id, p.first_name, p.last_name, p.role_name, org.name, org.id
FROM persons p
INNER JOIN organisations org ON p.organisation_id=org.id`
rows, err := m.DB.Query(stmt)
if err != nil {
return nil, err
}
defer rows.Close()
var ps []Person
for rows.Next() {
var p Person
err = rows.Scan(&p.ID, &p.FirstName, &p.LastName, &p.RoleName, &p.OrganisationName, &p.OrganisationID)
if err != nil {
return nil, err
}
ps = append(ps, p)
}
if err = rows.Err(); err != nil {
return nil, err
}
return ps, err
}
|