Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into issue_104_improve_o…
Browse files Browse the repository at this point in the history
…data_query_error_handling
  • Loading branch information
bertt committed Jan 16, 2018
2 parents a7c1fc9 + 4c2d335 commit 4ff2a76
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -10,7 +10,7 @@

GOST (Go-SensorThings) is an IoT Platform written in Golang (Go). It implements the Sensing profile (part 1) of the [OGC SensorThings API] (http://ogc-iot.github.io/ogc-iot-api/api.html) standard including the MQTT extension.

<a href="http://www.opengeospatial.org/resource/products/details/?pid=1419"><img src ="https://raw.githubusercontent.com/gost/docs/master/images/Certified_OGC_Compliant_Logo_Web.png"/><br/></a>
<a href="http://www.opengeospatial.org/resource/products/details/?pid=1468"><img src ="https://raw.githubusercontent.com/gost/docs/master/images/Certified_OGC_Compliant_Logo_Web.png"/><br/></a>

Implementation of the Tasking profile (part 2) and Rules Engine profile (part 3) of the OGC SensorThings API is planned as a future work activity.

Expand Down
2 changes: 1 addition & 1 deletion database/postgis/location.go
Expand Up @@ -134,7 +134,7 @@ func processLocations(db *sql.DB, sql string, qo *odata.QueryOptions, qi *QueryP
}

var count int
if !disableNextLink && len(countSQL) > 0 {
if len(countSQL) > 0 {
count, err = ExecuteSelectCount(db, countSQL)
if err != nil {
return nil, 0, hasNext, fmt.Errorf("error executing count %v", err)
Expand Down
21 changes: 16 additions & 5 deletions database/postgis/querybuilder.go
Expand Up @@ -62,11 +62,17 @@ func (qb *QueryBuilder) getOffset(qo *odata.QueryOptions) string {

// getOrderBy returns the string that needs to be placed after ORDER BY, this is set using
// ODATA's $orderby if not given use the default ORDER BY "table".id DESC
func (qb *QueryBuilder) getOrderBy(et entities.EntityType, qo *odata.QueryOptions) string {
func (qb *QueryBuilder) getOrderBy(et entities.EntityType, qo *odata.QueryOptions, fromAs bool) string {
if qo != nil && qo.OrderBy != nil && len(qo.OrderBy.OrderByItems) > 0 {
obString := ""
for _, obi := range qo.OrderBy.OrderByItems {
propertyName := selectMappings[et][strings.ToLower(obi.Field.Value)]
propertyName := ""
if fromAs {
propertyName = asMappings[et][strings.ToLower(obi.Field.Value)]
} else {
propertyName = selectMappings[et][strings.ToLower(obi.Field.Value)]
}

if len(obString) == 0 {
obString = fmt.Sprintf("%s %s", propertyName, obi.Order)
} else {
Expand All @@ -77,6 +83,10 @@ func (qb *QueryBuilder) getOrderBy(et entities.EntityType, qo *odata.QueryOption
return obString
}

if fromAs {
return fmt.Sprintf("%s DESC", asMappings[et][idField])
}

return fmt.Sprintf("%s DESC", selectMappings[et][idField])
}

Expand Down Expand Up @@ -274,7 +284,7 @@ func (qb *QueryBuilder) createJoin(e1 entities.Entity, e2 entities.Entity, id in
qb.tables[et2],
join,
qb.getFilterQueryString(et2, nqo, filterPrefix),
qb.getOrderBy(et2, nqo),
qb.getOrderBy(et2, nqo, false),
qb.getLimit(nqo, 0),
qb.getOffset(nqo),
qb.addAsPrefix(qpi, tableMappings[et2]))
Expand Down Expand Up @@ -928,11 +938,12 @@ func (qb *QueryBuilder) CreateQuery(e1 entities.Entity, e2 entities.Entity, id i
if qo != nil && qo.Top != nil && int(*qo.Top) != -1 {
limit = fmt.Sprintf("LIMIT %v", qb.getLimit(qo, 1))
}
queryString = fmt.Sprintf("%s ORDER BY %s )", queryString, qb.getOrderBy(et1, qo))
queryString = fmt.Sprintf("%s AS %s %s %s OFFSET %s",
//queryString = fmt.Sprintf("%s ORDER BY %s )", queryString, qb.getOrderBy(et1, qo))
queryString = fmt.Sprintf("%s) AS %s %s ORDER BY %s %s OFFSET %s",
queryString,
qb.addAsPrefix(qpi, tableMappings[et1]),
qb.createJoin(e1, e2, id, false, false, qo, qpi, ""),
fmt.Sprintf("%s_%s", qpi.AsPrefix, qb.getOrderBy(et1, qo, true)),
limit,
qb.getOffset(qo),
)
Expand Down
6 changes: 3 additions & 3 deletions database/postgis/querybuilder_test.go
Expand Up @@ -106,7 +106,7 @@ func TestGetOrderByWithNilOptions(t *testing.T) {
qb := CreateQueryBuilder("v1.0", 1)
ds := &entities.Datastream{}
// act
res := qb.getOrderBy(ds.GetEntityType(), nil)
res := qb.getOrderBy(ds.GetEntityType(), nil, false)

// assert
assert.NotNil(t, res)
Expand Down Expand Up @@ -160,7 +160,7 @@ func TestGetOrderByWithQueryOptions(t *testing.T) {
ds := &entities.Datastream{}

// act
res := qb.getOrderBy(ds.GetEntityType(), qo)
res := qb.getOrderBy(ds.GetEntityType(), qo, false)

// assert
assert.NotNil(t, res)
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestCreateCountQueryWithoutId(t *testing.T) {
func TestCreateQuery(t *testing.T) {
// arrange
qb := CreateQueryBuilder("v1.0", 1)
expected := "SELECT A_datastream.datastream_id AS A_datastream_id, A_datastream.datastream_name AS A_datastream_name, A_datastream.datastream_description AS A_datastream_description, A_datastream.datastream_unitofmeasurement AS A_datastream_unitofmeasurement, A_datastream.datastream_observationtype AS A_datastream_observationtype, A_datastream.datastream_observedarea AS A_datastream_observedarea, A_datastream.datastream_phenomenontime AS A_datastream_phenomenontime, A_datastream.datastream_resulttime AS A_datastream_resulttime FROM (SELECT datastream.thing_id AS datastream_thing_id, datastream.observedproperty_id AS datastream_observedproperty_id, datastream.sensor_id AS datastream_sensor_id, datastream.id AS datastream_id, datastream.name AS datastream_name, datastream.description AS datastream_description, datastream.unitofmeasurement AS datastream_unitofmeasurement, datastream.observationtype AS datastream_observationtype, public.ST_AsGeoJSON(datastream.observedarea) AS datastream_observedarea, datastream.phenomenontime AS datastream_phenomenontime, datastream.resulttime AS datastream_resulttime FROM v1.0.datastream ORDER BY datastream.id DESC ) AS A_datastream INNER JOIN LATERAL (SELECT thing.id AS thing_id FROM v1.0.thing WHERE thing.id = A_datastream.datastream_thing_id AND thing.id = 0) AS thing on true OFFSET 0"
expected := "SELECT A_datastream.datastream_id AS A_datastream_id, A_datastream.datastream_name AS A_datastream_name, A_datastream.datastream_description AS A_datastream_description, A_datastream.datastream_unitofmeasurement AS A_datastream_unitofmeasurement, A_datastream.datastream_observationtype AS A_datastream_observationtype, A_datastream.datastream_observedarea AS A_datastream_observedarea, A_datastream.datastream_phenomenontime AS A_datastream_phenomenontime, A_datastream.datastream_resulttime AS A_datastream_resulttime FROM (SELECT datastream.thing_id AS datastream_thing_id, datastream.observedproperty_id AS datastream_observedproperty_id, datastream.sensor_id AS datastream_sensor_id, datastream.id AS datastream_id, datastream.name AS datastream_name, datastream.description AS datastream_description, datastream.unitofmeasurement AS datastream_unitofmeasurement, datastream.observationtype AS datastream_observationtype, public.ST_AsGeoJSON(datastream.observedarea) AS datastream_observedarea, datastream.phenomenontime AS datastream_phenomenontime, datastream.resulttime AS datastream_resulttime FROM v1.0.datastream) AS A_datastream INNER JOIN LATERAL (SELECT thing.id AS thing_id FROM v1.0.thing WHERE thing.id = A_datastream.datastream_thing_id AND thing.id = 0) AS thing on true ORDER BY A_datastream_id DESC OFFSET 0"

// act
query, _ := qb.CreateQuery(&entities.Datastream{}, &entities.Thing{}, 0, nil)
Expand Down

0 comments on commit 4ff2a76

Please sign in to comment.