<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Passing params to db-api queries</title>
	<link>http://ramblings.timgolden.me.uk/2009/08/28/passing-params-to-db-api-queries/</link>
	<description>The ramblings of Tim Golden</description>
	<pubDate>Thu, 09 Feb 2012 10:14:22 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2</generator>

	<item>
		<title>By: tim</title>
		<link>http://ramblings.timgolden.me.uk/2009/08/28/passing-params-to-db-api-queries/#comment-1008</link>
		<author>tim</author>
		<pubDate>Fri, 28 Aug 2009 18:04:39 +0000</pubDate>
		<guid>http://ramblings.timgolden.me.uk/2009/08/28/passing-params-to-db-api-queries/#comment-1008</guid>
		<description>@zzzeeek: Neat trick; never thought of that one. 

FWIW I'm not that averse to 3rd party toolkits, and if I ever have the need to use an ORM then sqlalchemy's pretty much top of the list. It's just that I've spent so long writing and maintaining quite complex SQL structures, triggers, procedures, etc. that anything on top seems cumbersome. (As long as I don't have to pass too many parameters in :) )</description>
		<content:encoded><![CDATA[<p>@zzzeeek: Neat trick; never thought of that one. </p>
<p>FWIW I&#8217;m not that averse to 3rd party toolkits, and if I ever have the need to use an ORM then sqlalchemy&#8217;s pretty much top of the list. It&#8217;s just that I&#8217;ve spent so long writing and maintaining quite complex SQL structures, triggers, procedures, etc. that anything on top seems cumbersome. (As long as I don&#8217;t have to pass too many parameters in :) )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: http://zzzeek.blogspot.com/</title>
		<link>http://ramblings.timgolden.me.uk/2009/08/28/passing-params-to-db-api-queries/#comment-1007</link>
		<author>http://zzzeek.blogspot.com/</author>
		<pubDate>Fri, 28 Aug 2009 16:49:17 +0000</pubDate>
		<guid>http://ramblings.timgolden.me.uk/2009/08/28/passing-params-to-db-api-queries/#comment-1007</guid>
		<description>If you're averse to 3rd party toolkits, the ? as bind parameter issue is easily solved using a small regexp:


import re

binds = re.compile(r':(\w+)')
def stmt_and_args(stmt, params):
    positional = []
    def repl(m):
        bindname = m.group(1)
        positional.append(params[bindname])
        return "?"
    return binds.sub(repl, stmt), positional
    
stmt = "select * from foo where id=:bar and bat=:foo"

print stmt_and_args(stmt, {'bar':5, 'foo':12})</description>
		<content:encoded><![CDATA[<p>If you&#8217;re averse to 3rd party toolkits, the ? as bind parameter issue is easily solved using a small regexp:</p>
<p>import re</p>
<p>binds = re.compile(r&#8217;:(\w+)&#8217;)<br />
def stmt_and_args(stmt, params):<br />
    positional = []<br />
    def repl(m):<br />
        bindname = m.group(1)<br />
        positional.append(params[bindname])<br />
        return &#8220;?&#8221;<br />
    return binds.sub(repl, stmt), positional</p>
<p>stmt = &#8220;select * from foo where id=:bar and bat=:foo&#8221;</p>
<p>print stmt_and_args(stmt, {&#8217;bar&#8217;:5, &#8216;foo&#8217;:12})</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tim</title>
		<link>http://ramblings.timgolden.me.uk/2009/08/28/passing-params-to-db-api-queries/#comment-1006</link>
		<author>tim</author>
		<pubDate>Fri, 28 Aug 2009 14:55:36 +0000</pubDate>
		<guid>http://ramblings.timgolden.me.uk/2009/08/28/passing-params-to-db-api-queries/#comment-1006</guid>
		<description>@Brandon: thanks for the tip. I have played around with SQLAlchemy at different levels, and I probably should go back again. It's just that I've got so much code invested in my very lightweight sql-row wrappers that any extra overhead is overhead.</description>
		<content:encoded><![CDATA[<p>@Brandon: thanks for the tip. I have played around with SQLAlchemy at different levels, and I probably should go back again. It&#8217;s just that I&#8217;ve got so much code invested in my very lightweight sql-row wrappers that any extra overhead is overhead.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brandon Craig Rhodes</title>
		<link>http://ramblings.timgolden.me.uk/2009/08/28/passing-params-to-db-api-queries/#comment-1005</link>
		<author>Brandon Craig Rhodes</author>
		<pubDate>Fri, 28 Aug 2009 14:37:59 +0000</pubDate>
		<guid>http://ramblings.timgolden.me.uk/2009/08/28/passing-params-to-db-api-queries/#comment-1005</guid>
		<description>You should try SQLAlchemy.  Not their ORM, and not their Pythonic method-based method for building queries; but their old-fashioned interpolated-string way of doing SQL calls. To make things easy, they use ":names" to do interpolation, regardless of what the database back-end wants; if it wants something different, they translate the ":names" for you, so that you just have to worry about clean, name-based interpolation regardless of how obnoxious the real database back-end is. Check out their documentation for the feature:

http://www.sqlalchemy.org/docs/05/session.html#using-sql-expressions-with-sessions

It looks like exactly what you want!</description>
		<content:encoded><![CDATA[<p>You should try SQLAlchemy.  Not their ORM, and not their Pythonic method-based method for building queries; but their old-fashioned interpolated-string way of doing SQL calls. To make things easy, they use &#8220;:names&#8221; to do interpolation, regardless of what the database back-end wants; if it wants something different, they translate the &#8220;:names&#8221; for you, so that you just have to worry about clean, name-based interpolation regardless of how obnoxious the real database back-end is. Check out their documentation for the feature:</p>
<p><a href="http://www.sqlalchemy.org/docs/05/session.html#using-sql-expressions-with-sessions" rel="nofollow">http://www.sqlalchemy.org/docs/05/session.html#using-sql-expressions-with-sessions</a></p>
<p>It looks like exactly what you want!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

