简体   繁体   中英

Unable to Pass Comma seperated values to Birt Report

I am trying to pass a string value which is comma separated to birt report as parameter but failing

Java code

    String userlist="\"a\",\"b\",\"c\"";
    task.setParameterValue("userlist", userlist);

BeforeOpen has

params["userlist"].value.join("','");

SQL Query is

select * from users where name in (?)

I have already linked data set parameter to report parameter param_1 It's always giving me empty report even though DB table has 3 users. Any advise?

Know your tools: In this context, You have to understand SQL and the concept of bind variables. Javascript and BIRT. Unfortunately every single piece of code you posted is wrong (or at least incomplete).

But you are on the right track: You can modify the SQL text in the beforeOpen event of the database. I'll sketch the idea here:

In your SQL, replace the? with a placeholder like 'IN-LIST' (such that it is valid SQL).

You should still use the bind variable in the SQL (to avoid pitfalls caused by BIRT's caching mechanism), but in an effective no-op way, eg "where? is not null".

In the beforeOpen event of the data set, you can modify the SQL text:

  1. Get the original SQL text (var query = this.queryText; IIRC).
  2. Split your report parameter into the individual search terms. How exactly to do this depends on your input format. In your example, you are using " around your search terms, which looks overly complicated, unless individual search terms may contain commas. You should now have a list of your search strings, eg ["a", "b", "c"] .
  3. Convert each term into a valid SQL string literal. Beware of SQL injection attacks, so carefully escape characters like single-quotes, You should now have a list of valid SQL string literals. eg ["'a"', "'b'", "'c'"] .
  4. Join your list of SQL string literals from 3) into a single string with ", ". You should now have a string like 'a', 'b', 'c' .
  5. In your query, replace your placeholder string with the string from 4).
  6. Write the modified SQL text back to the DS object: this.queryText = query;

Probably there is also an example somewhere in the mists of the internet.

If you had invested five minutes more, you should have found an existing answer here on stack overflow: How to create a BIRT dataset that accepts multiple (CSV) values that it can be used inside "IN" clause in select statement , the only difference being that you are searching for a list of strings, while that question was about a list of numbers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM