diff options
author | Markus Grüneis <gimpf@gimpf.org> | 2014-10-25 18:13:01 +0200 |
---|---|---|
committer | Markus Grüneis <gimpf@gimpf.org> | 2014-10-25 18:13:10 +0200 |
commit | 419e825590d1188da42e324eb0839e9f85194e69 (patch) | |
tree | b5709a7b948b375d02f266f26fe85249e21aa3b7 /src | |
parent | updated changelog for release (diff) |
hub: Add support for /updates query with sortOrder, fix invalid queries
Diffstat (limited to 'src')
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5srvWeb.go | 23 | ||||
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5store.go | 52 | ||||
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5typesApi.go | 1 |
3 files changed, 54 insertions, 22 deletions
diff --git a/src/hub/src/spreadspace.org/sfive/s5srvWeb.go b/src/hub/src/spreadspace.org/sfive/s5srvWeb.go index 67430c6..af9e986 100644 --- a/src/hub/src/spreadspace.org/sfive/s5srvWeb.go +++ b/src/hub/src/spreadspace.org/sfive/s5srvWeb.go @@ -104,6 +104,29 @@ func getFilter(r *http.Request) (filter StatsFilter) { filter.quality = &quality } + afterUpdateId := r.FormValue("afterUpdateId") + if afterUpdateId != "" { + id, err := strconv.ParseInt(afterUpdateId, 10, 32) + if err == nil { + idInt := int(id) + filter.afterUpdateId = &idInt + } + } + + limit := r.FormValue("limit") + if limit != "" { + limitInt, err := strconv.ParseInt(limit, 10, 32) + if err == nil { + limitIntInt := int(limitInt) + filter.limit = &limitIntInt + } + } + + sortOrder := r.FormValue("sortOrder") + if sortOrder != "" { + filter.sortOrder = &sortOrder + } + return } diff --git a/src/hub/src/spreadspace.org/sfive/s5store.go b/src/hub/src/spreadspace.org/sfive/s5store.go index 569abc4..17ddbd1 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store.go +++ b/src/hub/src/spreadspace.org/sfive/s5store.go @@ -159,25 +159,33 @@ func getFilteredDataUpdateSelect(filter *StatsFilter) (string, map[string]interf parameters := make(map[string]interface{}) needsAnd := false - query += " WHERE" - - if filter.start != nil { - query += insertAnd(&needsAnd) - query += " StartTime >= :filterstart" - parameters["filterstart"] = filter.start.Unix() - needsAnd = true - } - if filter.end != nil { - query += insertAnd(&needsAnd) - query += " StartTime < :filterend" - parameters["filterend"] = filter.end.Unix() - needsAnd = true - } - if filter.afterUpdateId != nil { - query += insertAnd(&needsAnd) - query += " " + dataUpdatesTn + ".Id > :afterUpdateId" - parameters["afterUpdateId"] = *filter.afterUpdateId - needsAnd = true + if filter.start != nil || filter.end != nil || filter.afterUpdateId != nil { + query += " WHERE" + + if filter.start != nil { + query += insertAnd(&needsAnd) + query += " StartTime >= :filterstart" + parameters["filterstart"] = filter.start.Unix() + needsAnd = true + } + if filter.end != nil { + query += insertAnd(&needsAnd) + query += " StartTime < :filterend" + parameters["filterend"] = filter.end.Unix() + needsAnd = true + } + if filter.afterUpdateId != nil { + query += insertAnd(&needsAnd) + query += " " + dataUpdatesTn + ".Id > :afterUpdateId" + parameters["afterUpdateId"] = *filter.afterUpdateId + needsAnd = true + } + } + + if filter.sortOrder != nil { + if *filter.sortOrder == "desc" { + query += " ORDER BY " + dataUpdatesTn + ".Id DESC" + } } if filter.limit != nil { query += " LIMIT :limit" @@ -431,10 +439,10 @@ func (s sqliteStore) GetUpdatesAfter(id int) (res []StatisticsData, err error) { func (s sqliteStore) GetUpdates(filter *StatsFilter) (res []StatisticsData, err error) { sourceSql, parameters := getFilteredDataUpdateSelect(filter) + sql := "SELECT " + updateColumnSelect + " FROM " + sourceSql + s5tl.Printf("store: sql: %s", sql) updates, err := s.db.Select( - dataUpdateQueryResult{}, - "SELECT "+updateColumnSelect+" FROM "+sourceSql, - parameters) + dataUpdateQueryResult{}, sql, parameters) if err == nil { res = make([]StatisticsData, len(updates)) for i := range updates { diff --git a/src/hub/src/spreadspace.org/sfive/s5typesApi.go b/src/hub/src/spreadspace.org/sfive/s5typesApi.go index c02e5b2..e3a397e 100644 --- a/src/hub/src/spreadspace.org/sfive/s5typesApi.go +++ b/src/hub/src/spreadspace.org/sfive/s5typesApi.go @@ -65,6 +65,7 @@ type StatsFilter struct { tagsAny []string afterUpdateId *int limit *int + sortOrder *string } func (self *StatisticsData) CopyFromSourceId(id *SourceId) { |