Dear Iron Python people:<br><br>Earlier today I sent a question out the the Python DB-SIG.<br><br>I  got the following response, and I think Andy&#39;s last question (<span style="background-color: rgb(255, 255, 51);">highlighted</span> below) is a good one.<br>

<br>What <b>would</b> happen?  <br><br>Opinions, please.<br>--<br>Vernon<br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Andy Dustman</b> <span dir="ltr">&lt;<a href="mailto:farcepest@gmail.com">farcepest@gmail.com</a>&gt;</span><br>

Date: Mon, Aug 2, 2010 at 7:55 AM<br>Subject: Re: [DB-SIG] How can I reliably detect whether an SQL statement is a Query?<br>To: Vernon Cole &lt;<a href="mailto:vernondcole@gmail.com">vernondcole@gmail.com</a>&gt;<br>Cc: &quot;DB-SIG @ Python.org&quot; &lt;<a href="mailto:db-sig@python.org">db-sig@python.org</a>&gt;<br>

<br><br><div class="im">On Mon, Aug 2, 2010 at 5:57 AM, Vernon Cole &lt;<a href="mailto:vernondcole@gmail.com">vernondcole@gmail.com</a>&gt; wrote:<br>
&gt; Dear Gurus:<br>
&gt;<br>
&gt; Please give your advice up front to help me avoid making a design error. I<br>
&gt; am asking for help because:<br>
&gt; 1) I am not confident in my ability to understand Regular Expression<br>
&gt; strings.<br>
&gt; 2) I do not know much about any dialect of SQL other than Microsoft T-SQL<br>
&gt; (and sometime precious little of that.)<br>
&gt;<br>
&gt; I am ready for the next step in development of adodbapi, which is to use<br>
&gt; real <a href="http://ADO.NET" target="_blank">ADO.NET</a> (rather than COM ADO-db) when running on Iron Python.<br>
&gt;<br>
&gt; My research indicates that, when using <a href="http://ADO.NET" target="_blank">ADO.NET</a>, one must choose to call<br>
&gt; either  an ExecuteReader() method, or an  ExecuteNonQuery() method.<br>
&gt;<br>
&gt; I am attempting to use a lightweight db-api implementation from FePy for my<br>
&gt; pattern. It includes the following snippets of code:<br>
&gt;<br>
&gt; &lt;code snippet 1&gt;<br>
&gt; import re<br>
&gt; P_IS_QUERY = re.compile(&#39;^[ \r\n]*SELECT &#39;,re.IGNORECASE)<br>
<br>
</div>A slightly better expression would be ^\w*SELECT<br>
<br>
\w matches whitespace.<br>
<div class="im"><br>
&gt; &lt;code snippet 2&gt;<br>
&gt; class Cursor(object):<br>
&gt;<br>
&gt; &lt;code snippet 2A&gt;<br>
&gt;     def _is_query(self, operation):<br>
&gt;         &#39;&#39;&#39;Identify whether an operation is a query or not&#39;&#39;&#39;<br>
&gt;         if P_IS_QUERY.match(operation):<br>
&gt;             return True<br>
&gt;         else:<br>
&gt;             return False<br>
&gt;<br>
&gt; &lt;code snippet 2B&gt;<br>
&gt;         if self._is_query(operation):<br>
&gt;             self.reader = command.ExecuteReader()<br>
&gt;             self._set_description()<br>
&gt;         else:<br>
&gt;             command.ExecuteNonQuery()<br>
&gt;             self.description = None<br>
&gt; &lt;/code&gt;<br>
&gt;<br>
&gt; It seems to me that this code could be confused by the substring &#39;SELECT&#39;<br>
&gt; being included as part of a longer string, or in a string literal. Am<br>
&gt; reading it wrong?<br>
<br>
</div>The way your expression is written, it only matches SELECT at the<br>
beginning of the line (after any whitespace).<br>
<div class="im"><br>
&gt; It also seems to me that I should be able to detect a query by the fact that<br>
&gt; the first token in the command will be either &#39;SELECT&#39; or &#39;WITH, but would<br>
&gt; that still be true for other dialects of SQL?&#39;<br>
&gt;<br>
&gt; I am thinking of using something like:<br>
&gt; &lt;code&gt;<br>
&gt; def is_query(operation):<br>
&gt;     return operation.split(&#39; &#39;)[0].upcase in [&#39;SELECT&#39;,&#39;WITH&#39;]<br>
&gt; &lt;/code&gt;<br>
&gt;<br>
&gt; Good idea, or Bad idea?<br>
&gt;<br>
&gt; Any comments appreciated.<br>
<br>
</div><span style="background-color: rgb(255, 255, 0);">What are the consequences of using ExecuteReader() when there is</span><br style="background-color: rgb(255, 255, 0);"><span style="background-color: rgb(255, 255, 0);">
nothing to read? If none, i.e. you get an empty set of results, then I</span><br style="background-color: rgb(255, 255, 0);"><span style="background-color: rgb(255, 255, 0);">
would say to use that all the time, and don&#39;t bother to examine your</span><br style="background-color: rgb(255, 255, 0);"><span style="background-color: rgb(255, 255, 0);">
SQL at all.</span><br>
<font color="#888888">--<br>
Question the answers<br>
</font></div><br>