[grassyknoll commit] r969 - branches/unhork/grassyknoll/backend/sql

0 views
Skip to first unread message

codesite...@google.com

unread,
Dec 29, 2008, 1:41:40 PM12/29/08
to grassykno...@googlegroups.com
Author: jemfinch
Date: Mon Dec 29 10:37:34 2008
New Revision: 969

Modified:
branches/unhork/grassyknoll/backend/sql/MysqlCollection.py
branches/unhork/grassyknoll/backend/sql/PostgresqlCollection.py
branches/unhork/grassyknoll/backend/sql/SqlCollection.py

Log:
Issue #161: Factored out common Postgres/Mysql .execute code into
SqlCollection, added a replaceNamedParameters classmethod which can be
tested separately (if needed).


Modified: branches/unhork/grassyknoll/backend/sql/MysqlCollection.py
==============================================================================
--- branches/unhork/grassyknoll/backend/sql/MysqlCollection.py (original)
+++ branches/unhork/grassyknoll/backend/sql/MysqlCollection.py Mon Dec 29
10:37:34 2008
@@ -20,19 +20,16 @@
self.connection = self.connect(location)

_namesRe = re.compile(r':\w+')
- def execute(self, *args, **kwargs):
- if len(args) > 1:
- (sql, parameters) = args
- if isinstance(parameters, dict):
- # Using named parameters, which MySQLdb doesn't support.
- new_parameters = []
- # SqlCollection uses the :<name> format for named
parameters.
- for name in self._namesRe.findall(sql):
- new_parameters.append(parameters[name[1:]])
- sql = sql.replace(name, self.placeholder, 1)
- args = (sql, new_parameters)
- return super(MysqlCollection, self).execute(*args, **kwargs)
-
+ @classmethod
+ def replaceNamedParameters(cls, sql, parameters):
+ # Using named parameters, which MySQLdb doesn't support.
+ new_parameters = []
+ # SqlCollection uses the :<name> format for named parameters.
+ for name in cls._namesRe.findall(sql):
+ new_parameters.append(parameters[name[1:]])
+ sql = cls._namesRe.sub(cls.placeholder, sql)
+ return (sql, new_parameters)
+
@classmethod
def connect(cls, location):
(host, port, user, passwd, db) =
(getattr(location, 'host', 'localhost') or 'localhost',

Modified: branches/unhork/grassyknoll/backend/sql/PostgresqlCollection.py
==============================================================================
--- branches/unhork/grassyknoll/backend/sql/PostgresqlCollection.py
(original)
+++ branches/unhork/grassyknoll/backend/sql/PostgresqlCollection.py Mon Dec
29 10:37:34 2008
@@ -20,16 +20,12 @@
self.connection = self.connect(location)

_namesRe = re.compile(r':(\w+)')
- def execute(self, *args, **kwargs):
- if len(args) > 1:
- (sql, parameters) = args
- if isinstance(parameters, dict):
- # Using named parameters, which psycopg2 supports
differently
- # SqlCollection uses the :<name> format for named
parameters.
- sql = self._namesRe.sub(r'%(\1)s', sql)
- args = (sql, parameters)
- return super(PostgresqlCollection, self).execute(*args, **kwargs)
-
+ @classmethod
+ def replaceNamedParameters(cls, sql, parameters):
+ # Using named parameters, which psycopg2 supports differently
+ # SqlCollection uses the :<name> format for named parameters.
+ return (cls._namesRe.sub(r'%(\1)s', sql), parameters)
+
@classmethod
def connect(cls, location):
dsn_parts = []

Modified: branches/unhork/grassyknoll/backend/sql/SqlCollection.py
==============================================================================
--- branches/unhork/grassyknoll/backend/sql/SqlCollection.py (original)
+++ branches/unhork/grassyknoll/backend/sql/SqlCollection.py Mon Dec 29
10:37:34 2008
@@ -83,7 +83,22 @@
def cursor(self):
return self.connection.cursor()

+ @classmethod
+ def replaceNamedParameters(cls, sql, parameters):
+ """Replaces named parameters in the 'named' DBAPI style with the
appropriate parameter style used by a particular subclass.
+
+ @arg sql: A SQL query with named paramters,
e.g., 'WHERE "name"=:foo'
+ @type sql: str
+
+ @arg parameters: A dictionary of name => value mappings
+ @type parameters: dict
+ """
+ return (sql, parameters) # By default, change nothing.
+
def execute(self, *args, **kwargs):
+ if len(args) > 1 and isinstance(args[1], dict):
+ # Using named parameters.
+ args = self.replaceNamedParameters(*args)
cursor = self.cursor()
cursor.execute(*args, **kwargs)
return cursor

Reply all
Reply to author
Forward
0 new messages