|
16 | 16 | SearchNamespaceAccounts = "accounts" |
17 | 17 | // SearchNamespaceTransactions holds search namespace of transactions |
18 | 18 | SearchNamespaceTransactions = "transactions" |
| 19 | + // SortDescByTime option sorts search items in descending order of time |
| 20 | + SortDescByTime = "desc" |
| 21 | + // SortAscByTime option sorts search items in ascending order of time |
| 22 | + SortAscByTime = "asc" |
19 | 23 | ) |
20 | 24 |
|
21 | 25 | // SearchEngine is the interface for all search operations |
@@ -118,9 +122,10 @@ type QueryContainer struct { |
118 | 122 |
|
119 | 123 | // SearchRawQuery represents the format of search query |
120 | 124 | type SearchRawQuery struct { |
121 | | - Offset int `json:"from,omitempty"` |
122 | | - Limit int `json:"size,omitempty"` |
123 | | - Query struct { |
| 125 | + Offset int `json:"from,omitempty"` |
| 126 | + Limit int `json:"size,omitempty"` |
| 127 | + SortTime string `json:"sort_time,omitempty"` |
| 128 | + Query struct { |
124 | 129 | MustClause QueryContainer `json:"must"` |
125 | 130 | ShouldClause QueryContainer `json:"should"` |
126 | 131 | } `json:"query"` |
@@ -188,9 +193,9 @@ func (rawQuery *SearchRawQuery) ToSQLQuery(namespace string) *SearchSQLQuery { |
188 | 193 | var args []interface{} |
189 | 194 |
|
190 | 195 | switch namespace { |
191 | | - case "accounts": |
| 196 | + case SearchNamespaceAccounts: |
192 | 197 | q = "SELECT id, balance, data FROM current_balances" |
193 | | - case "transactions": |
| 198 | + case SearchNamespaceTransactions: |
194 | 199 | q = `SELECT id, timestamp, data, |
195 | 200 | array_to_json(ARRAY( |
196 | 201 | SELECT lines.account_id FROM lines |
@@ -244,27 +249,31 @@ func (rawQuery *SearchRawQuery) ToSQLQuery(namespace string) *SearchSQLQuery { |
244 | 249 | return &SearchSQLQuery{sql: q, args: args} |
245 | 250 | } |
246 | 251 |
|
247 | | - q = q + " WHERE " |
| 252 | + q += " WHERE " |
248 | 253 | if len(mustWhere) != 0 { |
249 | | - q = q + "(" + strings.Join(mustWhere, " AND ") + ")" |
| 254 | + q += "(" + strings.Join(mustWhere, " AND ") + ")" |
250 | 255 | if len(shouldWhere) != 0 { |
251 | | - q = q + " AND " |
| 256 | + q += " AND " |
252 | 257 | } |
253 | 258 | } |
254 | 259 |
|
255 | 260 | if len(shouldWhere) != 0 { |
256 | | - q = q + "(" + strings.Join(shouldWhere, " OR ") + ")" |
| 261 | + q += "(" + strings.Join(shouldWhere, " OR ") + ")" |
257 | 262 | } |
258 | 263 |
|
259 | | - if namespace == "transactions" { |
260 | | - q = q + " ORDER BY timestamp" |
| 264 | + if namespace == SearchNamespaceTransactions { |
| 265 | + if rawQuery.SortTime == SortDescByTime { |
| 266 | + q += " ORDER BY timestamp DESC" |
| 267 | + } else { |
| 268 | + q += " ORDER BY timestamp" |
| 269 | + } |
261 | 270 | } |
262 | 271 |
|
263 | 272 | if offset > 0 { |
264 | | - q = q + " OFFSET " + strconv.Itoa(offset) + " " |
| 273 | + q += " OFFSET " + strconv.Itoa(offset) + " " |
265 | 274 | } |
266 | 275 | if limit > 0 { |
267 | | - q = q + " LIMIT " + strconv.Itoa(limit) |
| 276 | + q += " LIMIT " + strconv.Itoa(limit) |
268 | 277 | } |
269 | 278 |
|
270 | 279 | q = enumerateSQLPlacholder(q) |
|
0 commit comments