Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/api dbfind #749

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions packages/api/dbfind.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (C) 2017, 2018, 2019 EGAAS S.A.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

package api

import (
"encoding/json"
"net/http"
"strings"

"github.com/AplaProject/go-apla/packages/conf"
"github.com/AplaProject/go-apla/packages/converter"
"github.com/AplaProject/go-apla/packages/model"
"github.com/AplaProject/go-apla/packages/smart"
"github.com/AplaProject/go-apla/packages/types"
"github.com/AplaProject/go-apla/packages/utils/tx"
"github.com/gorilla/mux"
)

type dbFindResult struct {
List []interface{} `json:"list"`
}

type dbfindForm struct {
ID int64 `schema:"id"`
Order string `schema:"order"`
Columns string `schema:"columns"`
paginatorForm
}

func (f *dbfindForm) Validate(r *http.Request) error {
if err := f.paginatorForm.Validate(r); err != nil {
return err
}

if len(f.Columns) > 0 {
f.Columns = converter.EscapeName(f.Columns)
}

return nil
}

func getDbFindHandler(w http.ResponseWriter, r *http.Request) {
form := &dbfindForm{}
if err := parseForm(r, form); err != nil {
errorResponse(w, err, http.StatusBadRequest)
return
}

params := mux.Vars(r)
client := getClient(r)
tableName := strings.ToLower(params["table"])
sc := smart.SmartContract{
OBS: conf.Config.IsSupportingOBS(),
VM: smart.GetVM(),
TxSmart: tx.SmartContract{
Header: tx.Header{
EcosystemID: client.EcosystemID,
KeyID: client.KeyID,
NetworkID: conf.Config.NetworkID,
},
},
}

// Check table existence
prefix := client.Prefix()
table := &model.Table{}
table.SetTablePrefix(prefix)
if found, err := table.Get(nil, tableName); !found || nil != err {
errorResponse(w, errTableNotFound.Errorf(tableName))
return
}

// Unmarshall where clause if there is any
var formWhere map[string]interface{}
if whereValue := r.FormValue("where"); 0 < len(whereValue) {
if err := json.Unmarshal([]byte(r.FormValue("where")), &formWhere); err != nil {
errorResponse(w, err, http.StatusBadRequest)
return
}
}
whereClause := types.LoadMap(formWhere)

// Perform the actual request
_, ret, err := smart.DBSelect(&sc, tableName, form.Columns, form.ID, form.Order, form.Offset, form.Limit, whereClause)
if err != nil {
errorResponse(w, err)
return
}

result := new(dbFindResult)
result.List = ret
jsonResponse(w, result)
}
62 changes: 62 additions & 0 deletions packages/api/dbfind_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (C) 2017, 2018, 2019 EGAAS S.A.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

package api

import (
"fmt"
"net/url"
"testing"
)

func TestDbFind(t *testing.T) {
if err := keyLogin(1); err != nil {
t.Error(err)
return
}
var ret dbFindResult

// Query table that is known to be existing
if err := sendPost("dbfind/keys", &url.Values{}, &ret); nil != err {
t.Error(err)
return
}
if length := len(ret.List); 0 == length {
t.Error(fmt.Errorf(`The number of records %d = 0`, length))
return
}

// Query table that doesn't exist
if err := sendPost("dbfind/QA_not_existing_table", &url.Values{}, &ret); nil != err {
if err.Error() != `404 {"error":"E_TABLENOTFOUND","msg":"Table qa_not_existing_table has not been found"}` {
t.Error(err)
return
}
}

// Query table with specified columns
if err := sendPost("dbfind/keys", &url.Values{"Columns": {"id, account"}}, &ret); nil != err {
t.Error(err)
}

// Query table with specified columns that are known to be missing
if err := sendPost("dbfind/keys", &url.Values{"Columns": {"id,account,id_non_existing"}}, &ret); nil != err {
if err.Error() != `400 {"error":"E_SERVER","msg":"column id_non_existing doesn't exist"}` {
t.Error(err)
return
}
}
}
1 change: 1 addition & 0 deletions packages/api/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (m Mode) SetCommonRoutes(r Router) {
api.HandleFunc("/interface/block/{name}", authRequire(getBlockInterfaceRowHandler)).Methods("GET")
api.HandleFunc("/table/{name}", authRequire(getTableHandler)).Methods("GET")
api.HandleFunc("/tables", authRequire(getTablesHandler)).Methods("GET")
api.HandleFunc("/dbfind/{table}", authRequire(getDbFindHandler)).Methods("POST")
api.HandleFunc("/test/{name}", getTestHandler).Methods("GET", "POST")
api.HandleFunc("/version", getVersionHandler).Methods("GET")
api.HandleFunc("/config/{option}", getConfigOptionHandler).Methods("GET")
Expand Down
4 changes: 4 additions & 0 deletions packages/smart/smart.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,10 @@ func (sc *SmartContract) AccessColumns(table string, columns *[]string, update b
colname := converter.Sanitize(col, `->`)
if strings.Contains(colname, `->`) {
colname = colname[:strings.Index(colname, `->`)]
} else if _, ok := cols[colname]; "id" != colname && !ok {
err = fmt.Errorf(eColumnNotExist, colname)
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Errorf("getting column")
return err
}
colList[i] = colname
}
Expand Down