diff --git a/README.md b/README.md
index b91fa91..54a8a68 100644
--- a/README.md
+++ b/README.md
@@ -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.
-![](https://raw.githubusercontent.com/gost/docs/master/images/Certified_OGC_Compliant_Logo_Web.png)
+![](https://raw.githubusercontent.com/gost/docs/master/images/Certified_OGC_Compliant_Logo_Web.png)
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.
diff --git a/database/postgis/location.go b/database/postgis/location.go
index 8c8d961..3f1f8bf 100755
--- a/database/postgis/location.go
+++ b/database/postgis/location.go
@@ -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)
diff --git a/database/postgis/querybuilder.go b/database/postgis/querybuilder.go
index 52467f6..4ce570d 100755
--- a/database/postgis/querybuilder.go
+++ b/database/postgis/querybuilder.go
@@ -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 {
@@ -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])
}
@@ -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]))
@@ -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),
)
diff --git a/database/postgis/querybuilder_test.go b/database/postgis/querybuilder_test.go
index 7554b0a..2fb5d68 100755
--- a/database/postgis/querybuilder_test.go
+++ b/database/postgis/querybuilder_test.go
@@ -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)
@@ -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)
@@ -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)