-
Notifications
You must be signed in to change notification settings - Fork 1
/
fmresultset.go
107 lines (90 loc) · 2.32 KB
/
fmresultset.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
package gofmcon
import (
"fmt"
)
type FMResultset struct {
Resultset *Resultset `xml:"resultset"`
DataSource *DataSource `xml:"datasource"`
Version string `xml:"version,attr"`
FMError FMError `xml:"error"`
}
func (rs *FMResultset) HasError() bool {
return rs.FMError.Code != 0
}
type DataSource struct {
Database string `xml:"database"`
DateFormat string `xml:"date_format"`
Layout string `xml:"layout"`
Table string `xml:"table"`
TimeFormat string `xml:"time-format"`
TimestampFormat string `xml:"timestamp-format"`
TotalCount int `xml:"total-count"`
}
type FMError struct {
Code int `xml:"code,attr"`
}
func (e *FMError) String() string {
return fmt.Sprintf("filemaker_error: %s", FileMakerErrorCodes[e.Code])
}
func (e *FMError) Error() string {
return fmt.Sprintf("filemaker_error: %s", FileMakerErrorCodes[e.Code])
}
type Resultset struct {
Count int `xml:"count,attr" json:"count"`
Fetched int `xml:"fetch-size,attr" json:"fetched"`
Records []*Record `xml:"record"`
}
type Record struct {
ID int `xml:"record-id,attr"`
Fields []*Field `xml:"field"`
fieldsMap map[string][]string
RelatedSet []*RelatedSet `xml:"relatedset"`
}
type RelatedSet struct {
Count int `xml:"count,attr"`
Table string `xml:"table,attr"`
Records []*Record `xml:"record"`
}
func (rs *Resultset) prepareRecords() {
for _, r := range rs.Records {
r.makeFieldsMap()
}
}
func (r *Record) makeFieldsMap() {
if r.fieldsMap == nil {
r.fieldsMap = map[string][]string{}
}
for _, f := range r.Fields {
r.fieldsMap[f.FieldName] = f.FieldData
}
for _, rs := range r.RelatedSet {
for _, rr := range rs.Records {
rr.makeFieldsMap()
}
}
}
type Field struct {
FieldName string `xml:"name,attr" json:"fieldName"`
FieldData []string `xml:"data" json:"fieldData"`
}
func (r *Record) RelatedSetFromTable(t string) *RelatedSet {
rSet := &RelatedSet{}
for _, elem := range r.RelatedSet {
if elem.Table == t {
rSet = elem
}
}
return rSet
}
// Field returns fields first data if exists
func (r *Record) Field(name string) string {
f := r.fieldsMap[name]
if len(f) < 1{
return ""
}
return f[0]
}
// ContainerField returns all data of container field
func (r *Record) ContainerField(name string) []string {
return r.fieldsMap[name]
}