Sometimes, you may want to modify the query generated for a report based on the input parameters selected by end user. Different filter values may trigger different queries in such cases. To achieve this easily, dynamic query view feature has been added at metadata level in Helical Insight.
What is dynamic query?
Dynamic query essentially implies that the query is generated at runtime depending on the user inputs.This can be achieved by adding a dynamic logic/groovy script using which end user can use the filters, formdata expressions and groovy user session variables on runtime.
Adhoc report can be created using metadata with dynamic view where in report inputs will be taken from either metadata or from report or from both.
A report with dynamic view gives the better query performance in report along with runtime sql query execution.
Examples of Dynamic View:
Simple Dynamic View:
def q = "select * from \"travel_details\"" if(check("${filter}.label" , "booking")){ def f = findFilterByLabel("booking") return q+" where \"booking_platform\" = $f.value " } else{ return q }
Advanced Example with multiple condition and control statement
def q = "select * from \"travel_details\"" def reports_filter = ['booking','mode'] int wc = 0; for(int i=0;i<filters.size();i++){ def item=filters[i] if(reports_filter.contains(item.label)){ if(wc == 0){ q = q + " where ${item.column} ${item.condition} ${item.value}" wc++ } else{ q = q + " and ${item.column} ${item.condition} ${item.value}" } } } return q
Note: It is mandatory for the groovy code in the dynamic query to have a return type.
Available Configurations:
Below are the available filter and global expressions using which you can create the dynamic view:
-
- FormData:
It returns the information about the report, metadata, columns, filters etc. used in the adhoc report.
Expression |
formData |
Drescrption | formData returns adhoc report formdata with all details of report ,metadata,tables.colums,filters info etc. used in adhoc report. For Ex. return formData In adhoc report ,based on the created report it will return the respective formdata. It returns formdata data in json format. Note : formdata specific information can be retrieved by (.) dot notation. Example : return formData.location |
This way end user can utilize required formdata information.
More details present in the blog FormData
-
- Filters:
Dynamic queries allow the report level filters to be used in the view to generate the query based on those filters.
Filters can be accessed by using the ${filter} notation.
Filter has many properties like filter label,mode,condition,value etc based on which user can create dynamic view based upon the requirement and the logic.
Below mentioned image shows the output of the different expressions:
Expression |
Filters |
Drescrption | filter returns the filter component including all filters used in adhoc report along with its filter information. With (.) dot notation end user can fetch required information from filters. For Ex. return filters Output: [ { fullyQualifiedColumn="FilterDynamicView"."booking_platform", column="booking_platform", condition==, isCustomCondition=false, dataType=java.lang.String, custom=null, value='Agent', values=[Agent], isCustomValue=false, id=0, label=booking_platform, mode=auto } , { fullyQualifiedColumn="FilterDynamicView"."destination", column="destination", condition==, isCustomCondition=false, dataType=java.lang.String, custom=null, value='Agra', values=[Agra], isCustomValue=false, id=1, label=destination, mode=auto } ] It returns filters component with all filter information. Note : filters specific information can be retrieved by (.) dot notation. Example :return filters.fullyQualifiedColumn This way end user can utilize required filters information. |
For details on the various filter expressions available refer to this blog Filter Expressions
-
- Logical
User can use logical expression in dynamic view which are used to check the conditions like check for filters, has filters etc.
Below are some logical expressions for dynamic view:
-
-
- check(“${filter}.label” ,”value”)
-
This expression is used to compare the filter label to a user defined string.
Expression |
check(“${filter}.label” ,”value”) |
Drescrption | This function will return true when there is any filter withprovided filter label as value.The check function works with all other filter properties such as column, value, values, condition etc.We can use check function with if condition to check whether any filter exists or not. For Ex. if( check("${filter}.label" ,"booking_platform")) { return " ${filter}.label" } Output : booking_platform,destination If filter label exists then will get output.In case filter label does not match then end user will get error or user can handle condition using else statement. |
-
-
- hasFilters()
-
This expression is used to check whether the report has any filters defined.
Expression |
hasFilters() |
Drescrption | This function will return true if there is any filters available otherwise false. Can be used to whether any filters exists/not during runtime. We can use hasFilters function with if condition to check whether any filter exists or not. For Ex. if(hasFilters()) { return " ${filter}.values" } Output : ‘Agent’,’Agra’ If filter label exists then will get output.In case of no filters end user will get error or user can handle condition using else statement. |
-
- GroovyUserSession
End user can use groovy user session variables for writing dynamic view.Using it end user can get all user,role,profile,organization information based on which we can write the logic inside dynamic view.
Below is the sample example of dynamic view using GroovyUserSession
import com.helicalinsight.efw.utility.GroovyUsersSession; def userRole = GroovyUsersSession.getValue('${user}.name'); if(userRole.contains('test')){ return "select * from \"meeting_details\"" }
-
- sqlContext
By using sqlContext functions using (.) dot notation end user can get required information. The sqlContext is a groovy object that is available by default inside the script. You can use it as sqlContext. The sqlContext has some useful method that can help you to dive deep into the formdata and the metadata from sqlContext methods.
Below is the list of functions that can be used during writing script:
SR. No: | Function Name: | Return Type |
1 | sqlContext.getDatabaseName() | String |
2 | sqlContext.getDerivedTableColumn() | List<String> |
3 | sqlContext.getDerivedTableNames() | List |
4 | sqlContext.getDriverClassName() | String |
5 | sqlContext.getFormData() | JSONObject |
6 | sqlContext.getHibernateDialect() | Dialect |
7 | sqlContext.getMetadata() | Metadata |
8 | sqlContext.getOpenQuote() | String |
9 | sqlContext.getQueryLimit() | String |
10 | sqlContext.getQueryOffset() | String |
11 | sqlContext.getReferenceFile() | String |
12 | sqlContext.getReferenceFile() | List |
13 | sqlContext.getStandardSqlFunction() | SqlFuntion |
14 | sqlContext.getTableName(String) | String |
15 | sqlContext.getTables() | List |
16 | sqlContext.isApplyAggregation() | boolean |
17 | sqlContext.isApplyGroupBy() | boolean |
18 | sqlContext.isApplyHaving() | boolean |
19 | sqlContext.isApplyOrderBy() | boolean |
20 | sqlContext.isApplyWhere() | boolean |
21 | sqlContext.isDistinctResults() | boolean |
22 | sqlContext.isLimitRequested() | boolean |
23 | sqlContext.quotes(String) | String |
-
- addQuotes(string
This function can be used to add single quotes to the provided string.
Expression |
addQuotes(string) |
Drescrption | For Ex. return addQuotes(filter) Output : ' [fullyQualifiedColumn:"FilterDynamicView"."booking_platform", "FilterDynamicView"."destination", condition:=, =, isCustomCondition:false, false, dataType:java.lang.String, java.lang.String, custom:null, null, values:'Agent', 'Agra', isCustomValue:false, false, id:0, 1, label:booking_platform, destination, mode:auto, auto, column:"booking_platform", "destination"]' It will return filter array as output because we have passed filter as string. |
-
- getFilterArrayByLabels([“labelArray”])
This utility gives the filter components based on the provided filter label array as parameter.
Expression |
getFilterArrayByLabels([“labelArray”]) |
Drescrption | For Ex. filtersbyLabels=getFilterArrayByLabels([“booking_platform”,”destination”]) return “$filtersbyLabels” Output : [[fullyQualifiedColumn:"FilterDynamicView"."booking_platform", column:"booking_platform", condition:=, isCustomCondition:false, dataType:java.lang.String, custom:null, value:'Agent', values:[Agent], isCustomValue:false, id:0, label:booking_platform, mode:auto], [fullyQualifiedColumn:"FilterDynamicView"."destination",column:"destination",condition:=,isCustomCondition:false, dataType:java.lang.String,custom:null,value:'Agra',values:[Agra], isCustomValue:false, id:1, label:destination, mode:auto]] It will return filter array as output because we have passed filter as string. |
-
- findFilterByLabel(“label”)
This utility gives the filter component based on the provided filter label as parameter.
Expression |
findFilterByLabel(“label”) |
Drescrption | For Ex. bookingFilter= findFilterByLabel(“booking_platform”) bookingCondition=bookingFilter .condition return “$bookingCondition” Here bookingFilter is a groovy object that will hold the filter component if a filter with label booking_platform is found. bookingFilter and its properties can be used with dot(.) notation. bookingFilter .condition will return the condition of that filter or we can use any other filter attribute Output : = It returns filter condition as output when filter label matches. |
-
- findFilter([key:value,key:value])
Expression |
findFilter([key:value,key:value]) |
Drescrption | Using this groovy map utillity end user will get the filter component based on the provided json key and value Syntax : findFilter(criteriaUsingGroovyMap) Note : The usage of groovy map as criteria. For Ex. getFilter= findFilter([condition:”=”,id:1]) Here getFilter is a groovy object that will hold the filter component if a filter with condition = and id as 1 is found Output: [fullyQualifiedColumn:"FilterDynamicView"."destination", column:"destination", condition:=, isCustomCondition:false, dataType:java.lang.String, custom:null, value:'Agra', values:[Agra], isCustomValue:false, id:1, label:destination, mode:auto] It returns filter component as output when filter with provided conditions matches. |
-
- findFilter(‘{“key”:”value”}’)
Expression |
findFilter(‘{“key”:”value”}’) |
Drescrption | Using this json string utillity end user will get the filter component based on the provided json key and value. Syntax : findFilter(jsonFindParameters) Note : The usage of json criteria as a string parameter Here json key can be filter attributes like label,value,condition,mode etc. Here bookingFilter is a groovy object that will hold the filter component if a filter with label booking_platform is found. It returns filter condition as output when filter label matches. |
-
- filters.size()
This function returns the number of filter count present in report. To check the filter size we can use this function. It return 0 when no filters are available.
Expression |
filters.size() |
Drescrption | For Ex.
if( filters.size()>0) { return "${filter}.column" } Output : “booking_platform”,”destination” It returns filter column as output when filter size is greater than 0. |
For further assistance, kindly contact us on support@helicalinsight.com or post your queries at Helical Forum