Vous êtes sur la page 1sur 3

Denoting Query Contexts on a User Report Page 1 of 3

integra solutions inc Last Update June 17, 1999 www.islink.com

Denoting Query Contexts on a User Report


The Problem Defined
In some universes, a query context is required to specify an exact path for the SQL generator. The user is presented with a context pop up when he or she attempts to run the query. For example, in the Island Resorts universe, selecting certain objects pops up the dialog box shown to the right. The obvious question becomes, How can I show my user which context they selected on the report itself? The context selection is only used on the query panel. Therefore there are no current functions that will give you that information. Instead, we have to look at the SQL that was generated in the data provider. In the Island Resorts Marketing universe, a query with Customer and Resort as the only objects will require a context selection. If the Reservations context is selected, the following SQL is generated:
SELECT Resort.resort, Customer.last_name FROM Resort, Customer, Service_Line, Service, Reservation_Line, Reservations WHERE ( Resort.resort_id=Service_Line.resort_id ) AND ( Service.sl_id=Service_Line.sl_id ) AND ( Customer.cust_id=Reservations.cust_id ) AND ( Reservation_Line.res_id=Reservations.res_id ) AND ( Reservation_Line.service_id=Service.service_id

If the Sales context is selected, then this SQL is generated instead:


SELECT Resort.resort, Customer.last_name FROM Resort, Customer, Service_Line, Service, Invoice_Line, Sales WHERE ( Customer.cust_id=Sales.cust_id ) AND ( Invoice_Line.inv_id=Sales.inv_id ) AND ( Invoice_Line.service_id=Service.service_id AND ( Resort.resort_id=Service_Line.resort_id ) AND ( Service.sl_id=Service_Line.sl_id )

The information in this document is provided as-is. The information is believed to be correct, but future releases of the BusinessObjects software may render suggestions or strategies provided here useless, inappropriate, or incompatible. All information is copyright integra solutions, inc, and may not be redistributed in any form or via any media electronic or otherwise without obtaining prior written consent from the author(s).

Denoting Query Contexts on a User Report Page 2 of 3

integra solutions inc Last Update June 17, 1999 www.islink.com

The select clause is the same in each case, but the FROM and WHERE are different. Using the DataProviderSQL() function, we can look at the SQL text and determine which context is being used.

Function Fun!
The following functions will be used in this solution: DataProvider() Argument: variable name Return: data provider name Argument: data provider name Return: SQL from data provider Arguments: String to search, text item to find Return: integer showing position of found item, returns zero (0) if not found Argument: numeric value Return: Argument = 0, return 0 Argument > 0, return 1 Argument < 0, return 1 Argument is null, return null

DataProviderSQL()

Pos()

Sign()

To finish everything off, the power of BusinessObjects If logic will be used as well.

Finding the SQL


The first step is to obtain the data provider SQL. To do this, we need the name of the data provider. You can hard-code this value, but I like to use an additional function: DataProvider(). This function takes a single variable as its argument, and returns the name of the data provider that queries that value. So, given that I selected Resort and Customer, all I have to do is:
=DataProvider(<Customer>)

to get this information. Once we have the name of the data provider, the next step is to retrieve the SQL for that query. This is where the DataProviderSQL() function comes in. With the name of the data provider, the SQL from the query can be found by using:
=DataProviderSQL( DataProvider(<Customer>) )

The result of this formula would match the SQL that was shown earlier in this document. Thats a little much for a user to work through, so Ill go a few more steps to isolate the actual context name. Reviewing the Where clause for each context shows the following tables: Sales Context
Resort, Customer, Service_Line, Service, Invoice_Line, Sales

Reservations Context
Resort, Customer, Service_Line, Service, Reservation_Line, Reservations

Note that the first four tables are the same in either case. Only the last two are different! So, if I find the table Sales, I am in the Sales context. The presence of the Reservations table implies that the Reservations context was selected. (Please note that it is not required that the table names match the context names in any universe.)
The information in this document is provided as-is. The information is believed to be correct, but future releases of the BusinessObjects software may render suggestions or strategies provided here useless, inappropriate, or incompatible. All information is copyright integra solutions, inc, and may not be redistributed in any form or via any media electronic or otherwise without obtaining prior written consent from the author(s).

Denoting Query Contexts on a User Report Page 3 of 3

integra solutions inc Last Update June 17, 1999 www.islink.com

Processing the SQL


Once we have the SQL, and once we have identified what is unique about each context, we can use the Pos() function to look in the SQL and determine what is going on. For example:
=Pos(DataProviderSQL(DataProvider(<Customer>)) ,"Reservation")

will return the point in the SQL where the word Reservation was found. If the word is not found (meaning that the Sales context was selected) then this formula will return the value of zero (0). Somehow this has to be useful! By using the If feature of BusinessObjects, we can use the value returned by Pos() and make a decision on which context name to display. If the word Reservation does not appear in the data provider SQL, then we assume that the Sales context has been selected. If the word Reservation does appear, then we assume the Reservation context was the one chosen. I got a little fancy at this point. I could have used:
=If Pos(DataProviderSQL(DataProvider(<Customer>)) ,"Reservation") >0 Then "Reservation" Else "Sales"

but instead used:


=If Sign(Pos(DataProviderSQL(DataProvider(<Customer>)) ,"Reservation"))<>0 Then "Reservation" Else "Sales"

Really, the functionality of both of these formulas is the same. The Sign() function is used to eliminate all but three possible values, -1, 0, or 1. Since I know the Pos() function can never return a negative number, then really what I have is a binary flag: zero or one. I tend to like this sort of result because I can use it in an Alerter, a Filter, or any other feature of BusinessObjects. The final result is displayed in a BusinessObjects report that can be downloaded along with this document.

Possible Problem
This is not a perfect solution. What if the word Reservation appears somewhere else in the SQL? We would want to make sure that the word is found only in the From clause of the SQL, and nowhere else. Without going into a full explanation of the details, here is a set of variables that would solve this problem by first isolating the From clause of the SQL before looking for a specific table name. Variable
Data Provider SQL From Clause Start From Clause Stop From Clause SQL

Formula
=DataProviderSQL(DataProvider(<Customer>)) =Pos(<Data Provider SQL> ,"FROM")+4 =Pos(<Data Provider SQL> ,"WHERE")-1 =SubStr(<Data Provider SQL> ,<From Clause Start> ,<From Clause Stop>-<From Clause Start>) = If Sign( Pos(<From Clause SQL> ,"Reservation") ) <> 0 Then "Reservation" Else "Sales"

Context Name

The variable expression is built in steps to allow for easier debugging. Also, note in the formula for From Clause SQL the variable <From Clause Start> is used twice. So, with careful construction of our variables, we can display the name of a context on a user report.
The information in this document is provided as-is. The information is believed to be correct, but future releases of the BusinessObjects software may render suggestions or strategies provided here useless, inappropriate, or incompatible. All information is copyright integra solutions, inc, and may not be redistributed in any form or via any media electronic or otherwise without obtaining prior written consent from the author(s).

Vous aimerez peut-être aussi