# HG changeset patch # User paulb@localhost.localdomain # Date 1186943471 -7200 # Node ID 14daa2c75f60947743bedd134435d6bfe50c2b4f # Parent b3785fe13c924a43e4d3fbadf189a7c1ee384c0d Added parameter marker replacement support with tests. Updated release information. diff -r b3785fe13c92 -r 14daa2c75f60 PKG-INFO --- a/PKG-INFO Sat Jun 02 20:36:06 2007 +0200 +++ b/PKG-INFO Sun Aug 12 20:31:11 2007 +0200 @@ -1,18 +1,18 @@ Metadata-Version: 1.1 Name: sqlliterals -Version: 0.1 +Version: 0.2 Author: Paul Boddie Author-email: paul at boddie org uk Maintainer: Paul Boddie Maintainer-email: paul at boddie org uk Home-page: http://www.python.org/pypi/sqlliterals -Download-url: http://www.boddie.org.uk/python/downloads/sqlliterals-0.1.tar.gz +Download-url: http://www.boddie.org.uk/python/downloads/sqlliterals-0.2.tar.gz Summary: SQL statement tokenisation for the detection of literal regions. License: LGPL Description: The sqlliterals distribution consists of a package containing two different implementations of SQL statement tokenisation for the detection of literal regions in such statements. -Keywords: SQL pyparsing parsing re +Keywords: SQL pyparsing parsing re paramstyle Requires: pyparsing Classifier: Development Status :: 3 - Alpha Classifier: License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL) diff -r b3785fe13c92 -r 14daa2c75f60 README.txt --- a/README.txt Sat Jun 02 20:36:06 2007 +0200 +++ b/README.txt Sun Aug 12 20:31:11 2007 +0200 @@ -3,7 +3,17 @@ The sqlliterals distribution consists of a package containing two different implementations of SQL statement tokenisation for the detection of literal -regions in such statements. +regions in such statements. It also provides a function which can replace +one kind of parameter marker with another in statements, subject to the +careful choice of the original parameter marker (since the replacement process +builds on the above tokenisation process and the identification of non-literal +regions). + +Examples +-------- + +See the test.py file for some tests and simple examples which use the parsing/ +tokenising and replacement functions. Contact, Copyright and Licence Information ------------------------------------------ @@ -25,6 +35,13 @@ pyparsing Tested with 1.2 +New in sqlliterals 0.2 (Changes since sqlliterals 0.1) +------------------------------------------------------ + + * Added a replace function to the different modules which permits the + replacement of carefully chosen parameter markers with markers having a + different representation. + Release Procedures ------------------ diff -r b3785fe13c92 -r 14daa2c75f60 sqlliterals/__init__.py --- a/sqlliterals/__init__.py Sat Jun 02 20:36:06 2007 +0200 +++ b/sqlliterals/__init__.py Sun Aug 12 20:31:11 2007 +0200 @@ -20,6 +20,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -__version__ = "0.1" +__version__ = "0.2" # vim: tabstop=4 expandtab shiftwidth=4 diff -r b3785fe13c92 -r 14daa2c75f60 sqlliterals/pyparser.py --- a/sqlliterals/pyparser.py Sat Jun 02 20:36:06 2007 +0200 +++ b/sqlliterals/pyparser.py Sun Aug 12 20:31:11 2007 +0200 @@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +import sqlliterals.common from pyparsing import * try: @@ -63,4 +64,16 @@ first = 0 return regions +# NOTE: Duplicated across implementations. + +def replace(fromstyle, tostyle, s): + + """ + Return a new string, where the paramstyle 'fromstyle' is + replaced with 'tostyle' in the appropriate regions of 's'. + """ + + regions = parseString(s) + return "".join(sqlliterals.common.replace(fromstyle, tostyle, regions)) + # vim: tabstop=4 expandtab shiftwidth=4 diff -r b3785fe13c92 -r 14daa2c75f60 sqlliterals/regexp.py --- a/sqlliterals/regexp.py Sat Jun 02 20:36:06 2007 +0200 +++ b/sqlliterals/regexp.py Sun Aug 12 20:31:11 2007 +0200 @@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +import sqlliterals.common import re region_expr = re.compile("(?P[^']+)|(?P'(?:[^']|(?:''))*')") @@ -47,4 +48,16 @@ return regions +# NOTE: Duplicated across implementations. + +def replace(fromstyle, tostyle, s): + + """ + Return a new string, where the paramstyle 'fromstyle' is + replaced with 'tostyle' in the appropriate regions of 's'. + """ + + regions = parseString(s) + return "".join(sqlliterals.common.replace(fromstyle, tostyle, regions)) + # vim: tabstop=4 expandtab shiftwidth=4 diff -r b3785fe13c92 -r 14daa2c75f60 test.py --- a/test.py Sat Jun 02 20:36:06 2007 +0200 +++ b/test.py Sun Aug 12 20:31:11 2007 +0200 @@ -33,4 +33,21 @@ show(sqlliterals.pyparser.parseString(s)) show(sqlliterals.regexp.parseString(s)) +l2 = [ + "a = ?", + "a = '?'", + "'a' = ?", + "'a' = '?'", + "a = ''?''", + "'''' = ?", + "'''' = ''?''", + "a = '''?'''", + "'''a''' = ?", + "'''a''' = '''?'''" + ] + +for s in l2: + print sqlliterals.pyparser.replace("?", "%s", s) + print sqlliterals.regexp.replace("?", "%s", s) + # vim: tabstop=4 expandtab shiftwidth=4