From 7ea78bf64c131dfdeff89a1e441a120340b8a8b4 Mon Sep 17 00:00:00 2001 From: Arpit Mohan Date: Sat, 16 Mar 2019 18:28:44 +0530 Subject: [PATCH] Adding functionality to execute a query via mustache template. --- app/server/go.mod | 1 + app/server/go.sum | 2 ++ app/server/services/query.go | 37 +++++++++++++++++++++++++++--------- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/app/server/go.mod b/app/server/go.mod index 8c2a66b6f8..580d8ef2f4 100644 --- a/app/server/go.mod +++ b/app/server/go.mod @@ -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 diff --git a/app/server/go.sum b/app/server/go.sum index ca80784b75..e153a134bf 100644 --- a/app/server/go.sum +++ b/app/server/go.sum @@ -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= diff --git a/app/server/services/query.go b/app/server/services/query.go index 4042388dd2..a1a478adca 100644 --- a/app/server/services/query.go +++ b/app/server/services/query.go @@ -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