Home
Analytics
Checking if a column exists or not
TorstenS
Hi!
I have a data source which returns a result set which may or may not include a given column "foo".
Now I would like to create a calculated column with an expression like:
if column foo exists
foo * 2
else
123
Obviously, as soon as I try to code something like
row["foo"]
and there is no column like that, I will get an exception.
Even if(row["foo"] == null) does not do the trick.
Any ideas?
Regards,
Torsten
Find more posts tagged with
Comments
mwilliams
Hi Torsten,<br />
<br />
I did the following in BIRT 2.5.1.<br />
<br />
In the afterOpen method of your dataSet you can put the following:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
columns = this.getColumnMetaData().getColumnCount();
i=1;
check=0;
while(i <= columns){
if (this.getColumnMetaData().getColumnName(i) == "foo"){
check = 1;
}
i++;
}
</pre>
<br />
Then, in your computed column, to go with your example, you'd put:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
if (check == 1){
row["foo"] * 2;
}
else{
123
}
</pre>
<br />
Not sure if this will work in earlier versions if you're using something earlier than 2.5. Let me know if you have questions or issues.
TorstenS
> In the afterOpen method of your dataSet
Where would I have to put this code?
Does your example only apply to a scripted data source? I do have a normal JDBC data source I am trying to use. I cannot find where I need to put this code or how I would add any event handlers at all.
TorstenS
Ok, I guess I should put it in like this:
<data-sets>
<oda-data-set extensionID="org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" name="Data Set" id="7">
...
<method name="afterOpen"><![CDATA[
columns = this.getColumnMetaData().getColumnCount();
i=1;
check=0;
while(i <= columns){
if (this.getColumnMetaData().getColumnName(i) == "foo"){
check = 1;
}
i++;
}
]]></method>
Unfortunately, I get this:
Caused by: org.eclipse.birt.core.exception.CoreException: There are errors evaluating script "__bm_afterOpen()":TypeError: Cannot find function getColumnMetaData. (<inline>#3). at org.eclipse.birt.core.script.ScriptContext.eval(ScriptContext.java:300) at org.eclipse.birt.core.script.ScriptContext.eval(ScriptContext.java:261) at org.eclipse.birt.data.engine.script.ScriptEvalUtil.evaluateJSAsExpr(ScriptEvalUtil.java:713) ... 73 more
Am I suffering from any misconception here? Actually, in the afterOpen() method, isn't this referring to the DataSet?
Regards,
Torsten
mwilliams
Torsten,
I used a JDBC dataset as well, the classic models one. The afterOpen is a dataSet script method. Are you using the designer or are you building the reports in Java? Also, what version of BIRT are you using? I think there was an issue with this method of getting the metadata in 2.3.x. Let me know.
TorstenS
Hi!<br />
<br />
> I think there was an issue with this method of getting the metadata in 2.3.x.<br />
<br />
Yes, that was exactly the problem. There is a bug [1] which has never been fixed in 2.3.x; even not in the very latest 2.3.2 release. I tried this with BIRT 2.5 and your code just worked fine.<br />
<br />
Possibly it's time to upgrade.<br />
<br />
Thanks for your help.<br />
<br />
[1] <a class='bbc_url' href='
https://bugs.eclipse.org/bugs/show_bug.cgi?id=259514'>https://bugs.eclipse.org/bugs/show_bug.cgi?id=259514</a>
;
mwilliams
No problem. Glad to help! Let us know whenever you have questions.
micajblock
<p>P.S. My preference would be to use a regular SQL statement and then Pivot the date in the report using a cross-tab. No coding needed.</p>