From 91ce3cf19e8e9aec7dbacd0f7b8338979a78c823 Mon Sep 17 00:00:00 2001 From: Colm Date: Tue, 6 Apr 2021 15:20:40 +0100 Subject: [PATCH 1/2] Doco for parameters --- README.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/README.md b/README.md index 38dcc5f..7984314 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,59 @@ key = storage.store('.xlsx') { Adhoq::Reporter.generate(execution) } storage.get(key) #=> report data ``` +###Query Parameters +A query may contain parameters that can be substitued in at run time. Parameters are identified with a $ prefix for +string parameters and $ prefix with curly brackets for number parameters, for example : + +```sql +SELECT * FROM users where created_at > '$time' AND activated = ${activated} +``` + +Parameters are shown as input fields in the engine UI in the Create Report section below the query. Note that currently +if the same parameter is used more than once in the query there is an input field for each occurance in the query, rather than +one input being substituted multiple times. + +Parameters can also be manipluated programatically. For example the following code shows how you might use parameters +in an API type situation. The index method returns all queries as JSON with the parameters extracted and added as a separate +property. Note that the query.parameters method actually extracts the parameters from query on the fly rather than +being a simple Getter for a parameters property, hence the need to add it into the as_json variable. + +The associated show method substitutes in any query parameters passed in and runs the report. The line + + sub = params.permit(*query.parameters).to_hash + +uses the query's paramenters as a white list against any passed in params, which are then stored as a hash to be used +in the subsequent substitute_query call. + +```ruby +class AdminApi::ReportsController < AdminApi::BaseController + def index + respond_to do |format| + @output = [] + ah_queries = Adhoq::Query.all + ah_queries.each do |ah_query| + j_query = ah_query.as_json + j_query['params'] = ah_query.parameters + @output.push(j_query) + end + format.html { render json: @output } + format.json { render json: @output } + end + end + + def show + query = Adhoq::Query.find(params[:id]) + sub = params.permit(*query.parameters).to_hash + substituted_query = query.substitute_query(sub) + execution = Adhoq::AdhocExecution.new( + 'csv', + substituted_query + ) + report = (Adhoq::Reporter.generate(execution)) + send_file report + end +end +``` ## Contributing 1. Fork it ( https://github.com/esminc/adhoq/fork ) From fcb7d424267d650b02fe7b70c1a097dea26261e9 Mon Sep 17 00:00:00 2001 From: Colm Date: Tue, 6 Apr 2021 15:24:08 +0100 Subject: [PATCH 2/2] Doco for parameters --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7984314..11b9921 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ one input being substituted multiple times. Parameters can also be manipluated programatically. For example the following code shows how you might use parameters in an API type situation. The index method returns all queries as JSON with the parameters extracted and added as a separate -property. Note that the query.parameters method actually extracts the parameters from query on the fly rather than +property. Note that the query.parameters method actually extracts the parameters from the query on the fly rather than being a simple Getter for a parameters property, hence the need to add it into the as_json variable. The associated show method substitutes in any query parameters passed in and runs the report. The line