-
Notifications
You must be signed in to change notification settings - Fork 19
/
thing.go
executable file
·226 lines (186 loc) · 6.92 KB
/
thing.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package postgis
import (
"encoding/json"
"fmt"
entities "github.com/gost/core"
"database/sql"
"errors"
gostErrors "github.com/gost/server/errors"
"github.com/gost/server/sensorthings/odata"
)
func thingParamFactory(values map[string]interface{}) (entities.Entity, error) {
t := &entities.Thing{}
for as, value := range values {
if value == nil {
continue
}
if as == asMappings[entities.EntityTypeThing][thingID] {
t.ID = value
} else if as == asMappings[entities.EntityTypeThing][thingName] {
t.Name = value.(string)
} else if as == asMappings[entities.EntityTypeThing][thingDescription] {
t.Description = value.(string)
} else if as == asMappings[entities.EntityTypeThing][thingProperties] {
p := value.(string)
propertiesMap, err := JSONToMap(&p)
if err != nil {
return nil, err
}
t.Properties = propertiesMap
}
}
return t, nil
}
// GetThing returns a thing entity based on id and query
func (gdb *GostDatabase) GetThing(id interface{}, qo *odata.QueryOptions) (*entities.Thing, error) {
intID, ok := ToIntID(id)
if !ok {
return nil, gostErrors.NewRequestNotFound(errors.New("Thing does not exist"))
}
query, qi := gdb.QueryBuilder.CreateQuery(&entities.Thing{}, nil, intID, qo)
return processThing(gdb.Db, query, qi)
}
//GetThingByDatastream retrieves the thing linked to a datastream
func (gdb *GostDatabase) GetThingByDatastream(id interface{}, qo *odata.QueryOptions) (*entities.Thing, error) {
intID, ok := ToIntID(id)
if !ok {
return nil, gostErrors.NewRequestNotFound(errors.New("Datastream does not exist"))
}
query, qi := gdb.QueryBuilder.CreateQuery(&entities.Thing{}, &entities.Datastream{}, intID, qo)
return processThing(gdb.Db, query, qi)
}
//GetThingsByLocation retrieves the thing linked to a location
func (gdb *GostDatabase) GetThingsByLocation(id interface{}, qo *odata.QueryOptions) ([]*entities.Thing, int, bool, error) {
intID, ok := ToIntID(id)
if !ok {
return nil, 0, false, gostErrors.NewRequestNotFound(errors.New("Location does not exist"))
}
query, qi := gdb.QueryBuilder.CreateQuery(&entities.Thing{}, &entities.Location{}, intID, qo)
countSQL := gdb.QueryBuilder.CreateCountQuery(&entities.Thing{}, &entities.Location{}, intID, qo)
return processThings(gdb.Db, query, qo, qi, countSQL)
}
//GetThingByHistoricalLocation retrieves the thing linked to a HistoricalLocation
func (gdb *GostDatabase) GetThingByHistoricalLocation(id interface{}, qo *odata.QueryOptions) (*entities.Thing, error) {
intID, ok := ToIntID(id)
if !ok {
return nil, gostErrors.NewRequestNotFound(errors.New("HistoricalLocation does not exist"))
}
query, qi := gdb.QueryBuilder.CreateQuery(&entities.Thing{}, &entities.HistoricalLocation{}, intID, qo)
return processThing(gdb.Db, query, qi)
}
// GetThings returns an array of things
func (gdb *GostDatabase) GetThings(qo *odata.QueryOptions) ([]*entities.Thing, int, bool, error) {
query, qi := gdb.QueryBuilder.CreateQuery(&entities.Thing{}, nil, nil, qo)
countSQL := gdb.QueryBuilder.CreateCountQuery(&entities.Thing{}, nil, nil, qo)
return processThings(gdb.Db, query, qo, qi, countSQL)
}
func processThing(db *sql.DB, sql string, qi *QueryParseInfo) (*entities.Thing, error) {
things, _, _, err := processThings(db, sql, nil, qi, "")
if err != nil {
return nil, err
}
if len(things) == 0 {
return nil, gostErrors.NewRequestNotFound(errors.New("Thing not found"))
}
return things[0], nil
}
func processThings(db *sql.DB, sql string, qo *odata.QueryOptions, qi *QueryParseInfo, countSQL string) ([]*entities.Thing, int, bool, error) {
data, hasNext, err := ExecuteSelect(db, qi, sql, qo)
if err != nil {
return nil, 0, hasNext, fmt.Errorf("Error executing query %v", err)
}
things := make([]*entities.Thing, 0)
for _, d := range data {
entity := d.(*entities.Thing)
things = append(things, entity)
}
var count int
if len(countSQL) > 0 {
count, err = ExecuteSelectCount(db, countSQL)
if err != nil {
return nil, 0, hasNext, fmt.Errorf("Error executing count %v", err)
}
}
return things, count, hasNext, nil
}
// PostThing receives a posted thing entity and adds it to the database
// returns the created Thing including the generated id
func (gdb *GostDatabase) PostThing(thing *entities.Thing) (*entities.Thing, error) {
jsonProperties, _ := json.Marshal(thing.Properties)
var thingID int
query := fmt.Sprintf("INSERT INTO %s.thing (name, description, properties) VALUES ($1, $2, $3) RETURNING id", gdb.Schema)
err := gdb.Db.QueryRow(query, thing.Name, thing.Description, jsonProperties).Scan(&thingID)
if err != nil {
return nil, err
}
thing.ID = thingID
return thing, nil
}
// PutThing receives a Thing entity and changes it in the database
// returns the adapted Thing
func (gdb *GostDatabase) PutThing(id interface{}, thing *entities.Thing) (*entities.Thing, error) {
return gdb.PatchThing(id, thing)
}
// PatchThing receives a to be patched Thing entity and changes it in the database
// returns the patched Thing
func (gdb *GostDatabase) PatchThing(id interface{}, thing *entities.Thing) (*entities.Thing, error) {
var err error
var ok bool
var intID int
updates := make(map[string]interface{})
thing.ID = id
if intID, ok = ToIntID(id); !ok || !gdb.ThingExists(intID) {
return nil, gostErrors.NewRequestNotFound(errors.New("Thing does not exist"))
}
if len(thing.Name) > 0 {
updates[thingName] = thing.Name
}
if len(thing.Description) > 0 {
updates[thingDescription] = thing.Description
}
if len(thing.Properties) > 0 {
jsonProperties, _ := json.Marshal(thing.Properties)
updates[thingProperties] = string(jsonProperties[:])
}
if thing.Locations != nil {
if len(thing.Locations) > 0 {
for _, l := range thing.Locations {
location, _ := gdb.GetLocation(l.ID, nil)
// todo: check if location exist
if location != nil {
query := fmt.Sprintf("update %s.thing_to_location set location_id = $1 where thing_id= $2", gdb.Schema)
res, err := gdb.Db.Exec(query, location.ID, intID)
if err != nil {
return nil, err
}
if c, _ := res.RowsAffected(); c == 0 {
sqlInsert := fmt.Sprintf("insert into %s.thing_to_location (location_id,thing_id) values ($1, $2)", gdb.Schema)
_, err := gdb.Db.Exec(sqlInsert, location.ID, intID)
if err != nil {
return nil, err
}
}
hl := &entities.HistoricalLocation{
Thing: thing,
Locations: []*entities.Location{location},
}
hl.ContainsMandatoryParams()
gdb.PostHistoricalLocation(hl)
}
}
}
}
if err = gdb.updateEntityColumns("thing", updates, intID); err != nil {
return nil, err
}
nt, _ := gdb.GetThing(intID, nil)
return nt, nil
}
// ThingExists checks if a thing is present in the database based on a given id
func (gdb *GostDatabase) ThingExists(id interface{}) bool {
return EntityExists(gdb, id, "thing")
}
// DeleteThing tries to delete a Thing by the given id
func (gdb *GostDatabase) DeleteThing(id interface{}) error {
return DeleteEntity(gdb, id, "thing")
}