Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Dynamic SQL Query in scripted data set
rsenden
<div>Hi,</div>
<div> </div>
<div>I have some advanced requirements where I need to dynamically build SQL queries based on binding parameters. I do not have control over the BIRT (4.2.2) runtime environment, so this needs to be done using report scripting.</div>
<div> </div>
<div>I have successfully implemented a solution, based on the attached JavaScript code that is used from within scripted data sets. The scripted data sets are defined using the following pseudocode:</div>
<div>
<pre class="_prettyXprint">
open():
var dataSourceName = "MyDataSource";
var birtSqlQueryData = new BirtSqlQueryData();
var someDateParameter = this.getInputParameterValue("someDateParameter");
birtSqlQueryData.appendToQuery("select ... from ... where someColumn = ?");
birtSqlQueryData.addQueryParameter(6, new ScriptExpression("new java.util.Date("+someDateParameter +")" ));
this.birtSqlQuery = new BirtSqlQuery(dataSourceName, birtSqlQueryData);
fetch(): return this.birtSqlQuery.fetchNextRow(row);
close(): this.birtSqlQuery.close();</pre>
</div>
<div> </div>
<div>For example, I have two scripted data sets based on this approach; one named MyListData and another one MyChartData. Individually, these data sets and report elements using those data sets work as expected.</div>
<div> </div>
<div>Unfortunately, this approach doesn't work when using nested report elements. For example, when defining a List bound to MyListData, containing a Chart bound to MyChartData (with a binding parameter set to a current MyListData column value), the report fails with an exception saying 'QueryResults or its iterator has been closed'.</div>
<div> </div>
<div>Apparently closing resources for one of the data sets also closes resources for the other one. Anyone any idea why this happens?</div>
<div> </div>
<div>Thanks,</div>
<div> </div>
<div>Ruud</div>
Find more posts tagged with
Comments
rsenden
<p>Hi all,</p>
<p> </p>
<p>I found out that the problem was related to incorrect caching of the <span style="color:rgb(102,0,102);">BirtSqlQuery instance. When a scripted data set was invoked multiple times within a report, multiple invocations were accessing the same </span><span style="color:rgb(102,0,102);">BirtSqlQuery instance. I fixed this by caching the BirtSqlQuery instance using a unique id passed as a data set parameter.</span></p>
<p> </p>
<p><span style="color:rgb(102,0,102);">Attached is an updated Javascript file for dynamically executing SQL queries from within BIRT. This new version also has the added capability of retrieving data for an existing data set, instead of defining a new temporary data set based on a query. Please see the comments in the file on how to use this; note that function names have changed to the example in my original post above no longer works.</span></p>
<p> </p>
<p>For now there is one problem remaining when executing existing data sets that use scripting; see <a data-ipb='nomediaparse' href='
http://developer.actuate.com/community/forum/index.php?/topic/35151-how-to-set-script-context-for-data-engine-from-script/'>http://developer.actuate.com/community/forum/index.php?/topic/35151-how-to-set-script-context-for-data-engine-from-script/</a> for
more info.</p>