RE: [InterSystems-Zen] Re: Report Query Different Namespace

199 views
Skip to first unread message

Derek Day

unread,
Mar 9, 2010, 6:07:35 AM3/9/10
to intersys...@googlegroups.com
Hi, Ryan.

Vlado is correct, changing namespaces mid-stream will badly hurt your performance. While creating a report that might not be a bad thing.

In the current version of Caché the docs that Vlado cited should probably be changed -- code shouldn't run incorrectly anymore -- Caché Objects will now run the code in the original namespace properly.

You basically have a few options, you may end up using a cache - the ache can be in a global or in XML depending on how you need the report data:
Extended Global References
Report Daemons for each namespace to gather the report data and periodically put it in a cache.
A new namespace that contains the mappings that you need
Use a custom result set or query
Gather the data in your Execute Logic,
as your code switches namespaces, be sure to close any objects and put the data into a cache.
Then your Fetch/Next logic reads from the cache.

There are various other permutations, but hopefully this will get you started.

Best Regards,
Derek Day

Moderator note: while writing resultset or objects code that uses multiple namespaces is not a Zen specific question, it is a common reporting question. Since we do not have a separate Zen Reports community, it seems that people will probably look here for this discussion in the future.
________________________________________
From: intersys...@googlegroups.com [intersys...@googlegroups.com] On Behalf Of Vlado [vili...@earthlink.net]
Sent: Monday, March 08, 2010 22:12
To: InterSystems: Zen Community
Subject: [InterSystems-Zen] Re: Query Different Namespace

Hi Ryan,
ZN "NEWSMEDIA" is a command.You can use it
in terminal mode, but not here.
Try with
SET $ZNSPACE="NEWSMEDIA"
or read for $zutil(5).
Here is something from the DOC:

"Changing Namespaces within Application Code
Object and SQL code assumes that it is running in a single namespace;
hence, changing namespaces with open object instances or SQL cursors
can lead to code running incorrectly. Typically, there is no need to
explicitly change namespaces, as the various Object, SQL, and CSP
servers automatically ensure that application code is run in the
correct namespace.
Also, changing namespaces demands a relatively high amount of
computing power compared to other commands; if possible, application
code should avoid it."


On Mar 8, 1:57 pm, Ryan McCormick <ryan.mccormi...@gmail.com> wrote:
> I have a couple places where I need to return a ResultSet with data
> from a different namespace. I have searched the community, but only
> found mapping or a security API as an answer and they won't work for
> this application. I need to populate a TablePane with data from a
> different namespace so the end-user can multi-select values and then
> use those values they select in a ZEN report with data that also
> crosses namespaces. I don't think that the results are making it
> across the namespace. I get a ERROR #6023: Query not Prepared in the
> table pane and just no results on the report. What is missing in the
> CreateRS method?
>
> On a zen page I have this .....
>
> <tablePane
> id="CategoryTable"
> OnCreateResultSet="CreateRS"
> multiSelect="true"
> caption="Select Story Categories"
> useKeys="true"
> valueColumn="categoryId"
> whereClause="categoryId != 0"
> orderByClause="categoryName"
> width="200px"
> align="center">
> <column colName="categoryId" hidden="true" />
> <column header="Category" colName="categoryName" />
> <tablePane>
>
> ClassMethod CreateRS(ByRef pSC As %Status, ByRef pParameters) As
> %ResultSet
> {
> ZN "NEWSMEDIA"
> set rs = ##class(%Library.ResultSet).%New()
> set sql = "SELECT * FROM dbo.Category"
> set pSC = rs.Prepare(sql)
> set pSC = rs.Execute()
> ZN "AD"
> quit rs
>
> }
>
> On the ZEN report I have this...
>
> /// This XML defines the logical contents of this report.
> XData ReportDefinition [ XMLNamespace = "http://www.intersystems.com/
> zen/report/definition" ]
> {
> <report xmlns="http://www.intersystems.com/zen/report/definition"
> name="Report"
> OnCreateResultSet="CreateRS"
> orderby="categoryName">
> <group breakOnField="categoryId" name="Category">
> <attribute name="ID" field="categoryId" />
> <attribute name="Name" field="categoryName" />
> </group>
> </report>
>
> }
>
> /// This XML defines the display for this report.
> /// This is used to generate the XSLT stylesheets for both HTML and
> XSL-FO.
> XData ReportDisplay [ XMLNamespace = "http://www.intersystems.com/zen/
> report/display" ]
> {
> <report xmlns="http://www.intersystems.com/zen/report/display"
> name="Report">
> <!-- add display definition of the report here. -->
> </report>
>
> }
>
> ClassMethod CreateRS(ByRef pSC As %Status, ByRef pParameters) As
> %ResultSet
> {
> ZN "NEWSMEDIA"
> set rs = ##class(%Library.ResultSet).%New()
> set sql = "SELECT * FROM dbo.Category"
> set pSC = rs.Prepare(sql)
> set pSC = rs.Execute()
> ZN "AD"
> quit rs
>
> }
>
> Any ideas?

--
You received this message because you are subscribed to the Google Groups "InterSystems: Zen Community" group.
To post to this group, send email to InterSys...@googlegroups.com
To unsubscribe from this group, send email to InterSystems-Z...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/InterSystems-ZEN?hl=en
Zen Community Terms and Conditions: http://groups.google.com/group/InterSystems-ZEN/web/community-terms-and-conditions

Ryan McCormick

unread,
Mar 10, 2010, 4:06:42 PM3/10/10
to InterSystems: Zen Community
Thank you both for responding. I think that I am on the right path
now. The "trick", I think, is to use a resultset that you can save
like a ScrollableResultSet and to create a property in your report to
store the ID of that saved resultset. Thanks to my company's other
developers for the original code to copy from. I don't really know if
my example shows good coding practice or if it is as clean as it
should be, but it will work. Please let me know if there is something
I am missing or should do differently because I am still learning.
Here's some code that I used. Hopefully it will help someone if they
are having trouble finding out how to switch namespaces to get data
from anther namespace on their ZEN Report.

/// test.Report
Class test.Report Extends %ZEN.Report.reportPage
{

/// Class name of application this report belongs to.
Parameter APPLICATION = "";

/// This is the default display mode for this report.
Parameter DEFAULTMODE = "xml";

/// This is the optional XML namespace used for the report.
Parameter REPORTXMLNAMESPACE = "";

/// Property to hold the ID of the saved result set from the other
namespace.
Property rsId As %Integer [Internal];

/// This XML defines the logical contents of this report.

XData ReportDefinition [XMLNamespace="http://www.intersystems.com/zen/


report/definition"]
{
<report xmlns="http://www.intersystems.com/zen/report/definition"

name="AcrossNamespaces" OnCreateResultSet="CreateRS">
<!-- add definition of the report here. -->
<group breakOnField="ID" name="Person">
<attribute name="ID" field="ID" />
<attribute name="Name" field="Name" />
<attribute name ="SSN" field="SSN" />


</group>
</report>
}
/// This XML defines the display for this report.
/// This is used to generate the XSLT stylesheets for both HTML and
XSL-FO.

XData ReportDisplay [XMLNamespace="http://www.intersystems.com/zen/


report/display"]
{
<report xmlns="http://www.intersystems.com/zen/report/display"

name="AcrossNamespaces">


<!-- add display definition of the report here. -->
</report>
}

Method CreateRS(


ByRef pSC As %Status,
ByRef pParameters) As %ResultSet
{

set sc = $$$OK
set startNamespace = $ZNSPACE
set NameSpace = "SAMPLES"
ZN:1 NameSpace
set sql = "SELECT ID, Name, SSN "
_"FROM sample.Person "
_"ORDER BY Name"
set rs = ##class(%Library.ScrollableResultSet).%New()
set sc = rs.Prepare(sql)
set sc = rs.Execute()
set sc = rs.Next()
set sc = rs.Previous()
set sc = rs.%Save()
set ..rsId = rs.%Id()
kill rs
ZN:1 startNamespace
set rs = ##class(%Library.ScrollableResultSet).%OpenId(..rsId,,.sc)
Quit rs
}
}

On Mar 9, 6:07 am, Derek Day <Derek....@intersystems.com> wrote:
> Hi, Ryan.
>
> Vlado is correct, changing namespaces mid-stream will badly hurt your performance. While creating a report that might not be a bad thing.
>
> In the current version of Caché the docs that Vlado cited should probably be changed -- code shouldn't run incorrectly anymore -- Caché Objects will now run the code in the original namespace properly.
>
> You basically have a few options, you may end up using a cache - the ache can be in a global or in XML depending on how you need the report data:
> Extended Global References
> Report Daemons for each namespace to gather the report data and periodically put it in a cache.
> A new namespace that contains the mappings that you need
> Use a custom result set or query
>    Gather the data in your Execute Logic,
>     as your code switches namespaces, be sure to close any objects and put the data into a cache.
>    Then your Fetch/Next logic reads from the cache.
>
> There are various other permutations, but hopefully this will get you started.
>
> Best Regards,
> Derek Day
>
> Moderator note: while writing resultset or objects code that uses multiple namespaces is not a Zen specific question, it is a common reporting question. Since we do not have a separate Zen Reports community, it seems that people will probably look here for this discussion in the future.
> ________________________________________

> From: intersys...@googlegroups.com [intersys...@googlegroups.com] On Behalf Of Vlado [viliyc...@earthlink.net]

> For more options, visit this group athttp://groups.google.com/group/InterSystems-ZEN?hl=en
> Zen Community Terms and Conditions:http://groups.google.com/group/InterSystems-ZEN/web/community-terms-a...- Hide quoted text -
>
> - Show quoted text -

Eric

unread,
Mar 11, 2010, 7:05:38 AM3/11/10
to InterSystems: Zen Community

Ryan,

You don't need to do all this. Use the NsResultset instead since it
extends %ScrollableResultSet. It allows you to query across namespaces
without switching and with far less code. fetching is done
transparently across namespaces by using extended global references as
suggested by Derek except you don't have to do it yourself.

ps to others: The NsResultset is one of our our custom resultset
object. It's not part of the standard cache release.

Eric

> > Zen Community Terms and Conditions:http://groups.google.com/group/InterSystems-ZEN/web/community-terms-a...Hide quoted text -

Reply all
Reply to author
Forward
0 new messages