forked from prest/controllers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabases.go
54 lines (45 loc) · 1.37 KB
/
databases.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
package controllers
import (
"fmt"
"net/http"
"strings"
"github.com/prest/config"
)
// GetDatabases list all (or filter) databases
func GetDatabases(w http.ResponseWriter, r *http.Request) {
requestWhere, values, err := config.PrestConf.Adapter.WhereByRequest(r, 1)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
requestWhere = config.PrestConf.Adapter.DatabaseWhere(requestWhere)
query, hasCount := config.PrestConf.Adapter.DatabaseClause(r)
sqlDatabases := fmt.Sprint(query, requestWhere)
distinct, err := config.PrestConf.Adapter.DistinctClause(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if distinct != "" {
sqlDatabases = strings.Replace(sqlDatabases, "SELECT", distinct, 1)
}
order, err := config.PrestConf.Adapter.OrderByRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
order = config.PrestConf.Adapter.DatabaseOrderBy(order, hasCount)
sqlDatabases = fmt.Sprint(sqlDatabases, order)
page, err := config.PrestConf.Adapter.PaginateIfPossible(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sqlDatabases = fmt.Sprint(sqlDatabases, " ", page)
sc := config.PrestConf.Adapter.Query(sqlDatabases, values...)
if sc.Err() != nil {
http.Error(w, sc.Err().Error(), http.StatusBadRequest)
return
}
w.Write(sc.Bytes())
}