You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Kevin Sonney <ke...@webslingerz.com> on 2000/09/25 16:33:16 UTC

esql with Vars, or esql diff of the moment...

After Donald pointed out the thread less than safety issue of last week's
<xsp:variable> patch for global vars, I had to rethink how I was goin to
pass dunamic java variables to esql queries. 

The answer turned out to be quite simple. Why not pass the esql:query
string to the method, instead of "hard-coding" it into the
_esql_session? 

So I did just that. The diff, as it were is below. Now, this is the
warning - the testing on this patch is *VERY* rudimentary - I havemn't
tried it with esql queries within esql results. Yet. 

Anyone have any feedback on this one? Donald?

-- 
+-------------------------------------------+
| Kevin Sonney        kevin@webslingerZ.com |
| Systems Programmer    www.webslingerZ.com |
+-------------------------------------------+


Re: esql with Vars, or esql diff of the moment...

Posted by Kevin Sonney <ke...@webslingerz.com>.
On Mon, 25 Sep 2000, Kevin Sonney wrote:
> Anyone have any feedback on this one? Donald?

Of course, I forgot to attache the Diff, didn't I? Here it is :

Index: src/org/apache/cocoon/processor/xsp/library/sql/esql.xsl
===================================================================
RCS file: /home/cvspublic/xml-cocoon/src/org/apache/cocoon/processor/xsp/library/sql/esql.xsl,v
retrieving revision 1.15
diff -u -r1.15 esql.xsl
--- src/org/apache/cocoon/processor/xsp/library/sql/esql.xsl	2000/09/15 05:07:35	1.15
+++ src/org/apache/cocoon/processor/xsp/library/sql/esql.xsl	2000/09/25 14:40:53
@@ -110,7 +110,7 @@
                   DBConnection db_connection=null;
                   Connection connection=null;
                   boolean close_connection = true;
-		  String query;
+                  String query;
                   Statement statement;
                   ResultSet resultset;
                   ResultSetMetaData resultset_metadata;
@@ -143,7 +143,13 @@
 
  <xspdoc:desc>indicates that a sql connection is going to be defined and one or more queries may be executed</xspdoc:desc>
 <xsl:template match="esql:execute-query">
- <xsp:logic>_esql_execute_query_<xsl:value-of select="generate-id(.)"/>(request,response,document,xspParentNode,xspCurrentNode,xspNodeStack,session,_esql_sessions,_esql_session);</xsp:logic>
+ <xsl:variable name="query">
+     <xsl:call-template name="get-nested-string">
+       <xsl:with-param name="content" select="esql:query"/>
+     </xsl:call-template>
+ </xsl:variable>
+ <xsp:logic>_esql_execute_query_<xsl:value-of select="generate-id(.)"/>(request,response,document,xspParentNode,xspCurrentNode,xspNodeStack,session,_esql_sessions,_esql_session,<xsl:copy-of select="$query" />);
+</xsp:logic>
 </xsl:template>
 
 <xsl:template match="esql:execute-query" mode="generate-method">
@@ -182,11 +188,6 @@
 			<xsl:with-param name="content" select="esql:skip-rows"/>
 		</xsl:call-template>
 	</xsl:variable>
-	<xsl:variable name="query">
-		<xsl:call-template name="get-nested-string">
-			<xsl:with-param name="content" select="esql:query"/>
-		</xsl:call-template>
-	</xsl:variable>
 	<xsp:logic>
 	 void _esql_execute_query_<xsl:value-of select="generate-id(.)"/>(
 	 HttpServletRequest request,
@@ -197,7 +198,8 @@
 	 Stack xspNodeStack,
 	 HttpSession session,
 	 Stack _esql_sessions,
-	 EsqlSession _esql_session) throws Exception {
+	 EsqlSession _esql_session,
+	 String queryString) throws Exception {
 		if (_esql_session != null) {
 		 _esql_sessions.push(_esql_session);
 		}
@@ -241,7 +243,8 @@
 		 </xsl:otherwise>
 	        </xsl:choose>
 	       _esql_session.statement = _esql_session.connection.createStatement();
-	       _esql_session.query = String.valueOf(<xsl:copy-of select="$query"/>);
+<!--	       _esql_session.query = String.valueOf(<xsl:copy-of select="$query"/>); -->
+	       _esql_session.query = queryString;
 	       _esql_session.resultset = _esql_session.statement.executeQuery(_esql_session.query);
 	       _esql_session.resultset_metadata = _esql_session.resultset.getMetaData();
 	       _esql_session.count = 0;

-- 
+-------------------------------------------+
| Kevin Sonney        kevin@webslingerZ.com |
| Systems Programmer    www.webslingerZ.com |
+-------------------------------------------+


Re: esql with Vars, or esql diff of the moment...

Posted by Kevin Sonney <ke...@webslingerz.com>.
On Mon, 25 Sep 2000, Donald Ball wrote:
> I fail to see what this patch buys you, except making the code more
> complex. Can you explain?

Sure, why not?

Without the patch, the only way to put Java variables was to either
declare a "global" with <xsp:logic> (or my xsp:variable patch), or not use
variables at all - limiting the quesries somewhat. For example, the
following :

<!-- Stupid simple example -->
<xsp:logic>
String test_query = "Select * from bob_table where bob_id = 1";
</xsp:logic>
<esql:execute-query>
<!-- esql setup params go here -->
<esql:query><xsp:expr>test_query</xsp:expr></esql:query>
<!-- esql result processing goes here -->
</esql:execute-query>

would result in a "variable test_query not found" error message durring
compelation. Well, yeah, as test_query was outside the esql session
function. This is something that people had been lamenting on this list
since you released esql. The solutions were to (a) not use java vars &
expresions in esql:queries (which ahs met with some reluctance), (b) use
globals, or (c) patch esql. 

I opted for (b) last week, but the thread safety issue could be a big
problem for the one application i'm working on. So, fresh this morning
from a weekend away from the code, I opted for (c).

The solution I've given with this patch *ISN"T* a major code change. It
only does the following :

1) Moves processing of the esql:query element prior to (and outside) the
genration of the esql method,
2) Adds *1* variable to the method call, and 
3) Sets the value of _esql_session.query to the passed variable

Now this doesn't seem all that complex to me. But it does add a bit of
missing functionality (if the list archives of the past week or two are to
bebelived) to the esql logic sheet that wasn't there before. 

After the patch, I can pre-process my query string all I want - do things
like :

<xsp:logic>
String var1 = <request:get-parameter name="var1"/>;
String var2 = <request:get-parameter name="var2"/>;
String eqsl_query_test = "select test_data from test_table";

// conditional processing to add var1 & var2 to the query based on
// goes availability here, like testing for null, etc, etc

</xsp:logic>

<esql:execute-query>
<!-- esql setup params go here -->
<esql:query><xsp:expr>esql_query_test</xsp:expr></esql:query>
<!-- esql result s go here -->
</esql:execute-query>

This, in my mind, is a lot easier to deal with (and a lot less hassle to
implement - since I did it for you *grin*) than the "estra elements to
pass vars to esql since we can't use xsp:expr" ideas I saw last week. 

Does this make it any clearer on why I did it, and what you get? Feel free
to tell me if not. Heck, feel free to tell me it's a Really Bad Idea (tm),
and I'll work on a better one. But I figure this one :

(a) is easy to implement,
(b) requires no additional documentation,
(c) requires no extra elements or attributes in the logic sheet

Thoughts?

-- 
+-------------------------------------------+
| Kevin Sonney        kevin@webslingerZ.com |
| Systems Programmer    www.webslingerZ.com |
+-------------------------------------------+


Re: esql with Vars, or esql diff of the moment...

Posted by Donald Ball <ba...@webslingerZ.com>.
On Mon, 25 Sep 2000, Kevin Sonney wrote:

> After Donald pointed out the thread less than safety issue of last week's
> <xsp:variable> patch for global vars, I had to rethink how I was goin to
> pass dunamic java variables to esql queries. 
> 
> The answer turned out to be quite simple. Why not pass the esql:query
> string to the method, instead of "hard-coding" it into the
> _esql_session? 
> 
> So I did just that. The diff, as it were is below. Now, this is the
> warning - the testing on this patch is *VERY* rudimentary - I havemn't
> tried it with esql queries within esql results. Yet. 
> 
> Anyone have any feedback on this one? Donald?

I fail to see what this patch buys you, except making the code more
complex. Can you explain?

- donald