Adding functionality to execute a query via mustache template.
This commit is contained in:
parent
49b7051cea
commit
7ea78bf64c
|
|
@ -1,6 +1,7 @@
|
|||
module gitlab.com/mobtools/internal-tools-server
|
||||
|
||||
require (
|
||||
github.com/cbroglie/mustache v1.0.1
|
||||
github.com/gorilla/mux v1.6.2
|
||||
github.com/jinzhu/gorm v1.9.2
|
||||
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
cloud.google.com/go v0.30.0 h1:xKvyLgk56d0nksWq49J0UyGEeUIicTl4+UBiX1NPX9g=
|
||||
cloud.google.com/go v0.30.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
|
||||
github.com/cbroglie/mustache v1.0.1 h1:ivMg8MguXq/rrz2eu3tw6g3b16+PQhoTn6EZAhst2mw=
|
||||
github.com/cbroglie/mustache v1.0.1/go.mod h1:R/RUa+SobQ14qkP4jtx5Vke5sDytONDQXNLPY/PO69g=
|
||||
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
|
||||
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
|
||||
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@ package services
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/cbroglie/mustache"
|
||||
"github.com/jinzhu/gorm"
|
||||
"gitlab.com/mobtools/internal-tools-server/models"
|
||||
"gitlab.com/mobtools/internal-tools-server/storage"
|
||||
|
|
@ -10,25 +12,42 @@ import (
|
|||
|
||||
// ExecuteQuery runs a custom SQL query on the client database
|
||||
func ExecuteQuery(queryBody models.ExecQuery) ([]map[string]interface{}, error) {
|
||||
if queryBody.QueryType == "sql" {
|
||||
// Get the actual query from the DB
|
||||
datastore := storage.StorageEngine.GetDatastore()
|
||||
queryDAO := &models.Query{}
|
||||
datastore := storage.StorageEngine.GetDatastore()
|
||||
queryDAO := &models.Query{}
|
||||
|
||||
if err := datastore.Where("name = ?", queryBody.Name).First(queryDAO).Error; gorm.IsRecordNotFoundError(err) {
|
||||
return nil, fmt.Errorf("Invalid queryName: %s provided", queryBody.Name)
|
||||
if err := datastore.Where("name = ?", queryBody.Name).First(queryDAO).Error; gorm.IsRecordNotFoundError(err) {
|
||||
return nil, fmt.Errorf("Invalid queryName: %s provided", queryBody.Name)
|
||||
}
|
||||
|
||||
//TODO: Move to a factory method for better readability
|
||||
if queryDAO.QueryType == "sql" {
|
||||
// Get the actual query from the DB
|
||||
queryStr := queryDAO.Executable
|
||||
|
||||
// Extract the key-value pairs from the request
|
||||
templateKeyValue := make(map[string]string)
|
||||
if len(queryBody.Params.QueryParams) > 0 {
|
||||
for _, elem := range queryBody.Params.QueryParams {
|
||||
templateKeyValue[elem.Key] = elem.Value
|
||||
}
|
||||
}
|
||||
|
||||
queryStr := queryDAO.Executable
|
||||
mapArray, err := storage.StorageEngine.ExecuteQuery(queryStr)
|
||||
// Substitute it in the template string
|
||||
log.Printf("Going to parse string: %s", queryStr)
|
||||
renderedStr, err := mustache.Render(queryStr, templateKeyValue)
|
||||
if err != nil {
|
||||
log.Printf("Error while parsing the mustache template. %s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mapArray, err := storage.StorageEngine.ExecuteQuery(renderedStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mapArray, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("QueryType: %s not supported", queryBody.QueryType)
|
||||
return nil, fmt.Errorf("QueryType: %s not supported", queryDAO.QueryType)
|
||||
}
|
||||
|
||||
// CreateQuery creates a new query that can be executed by name at runtime
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user