[sqlgen] Allow to unprotect some parameters during the creation of an SQL INSERT query

authorVincent Michel <vincent.michel@logilab.fr>
changeset34f9dfea50fc
branchstable
phasedraft
hiddenyes
parent revision#abf6be2bf5fa default is stable
child revision#11b6cb57cf65 Add a SQL_CONVERTERS dict to allow possible conversions to SQL for specific types
files modified by this revision
sqlgen.py
# HG changeset patch
# User Vincent Michel <vincent.michel@logilab.fr>
# Date 1359123501 -3600
# Fri Jan 25 15:18:21 2013 +0100
# Branch stable
# Node ID 34f9dfea50fc81fdf35dd111a2b17e2642bc9e28
# Parent abf6be2bf5fa309b3c25a3a655b3e7136a809e26
[sqlgen] Allow to unprotect some parameters during the creation of an SQL INSERT query

diff --git a/sqlgen.py b/sqlgen.py
@@ -54,23 +54,31 @@
1          >>> s.set(['nom','prenom'])
2          'nom = %(nom)s, prenom = %(prenom)s'
3          """
4          return ", ".join(["%s = %%(%s)s" % (x, x) for x in keys])
5 
6 -    def insert(self, table, params):
7 +    def insert(self, table, params, pre_params=None):
8          """
9          :param table: name of the table
10          :param params:  dictionary that will be used as in cursor.execute(sql,params)
11 
12          >>> s = SQLGenerator()
13          >>> s.insert('test',{'nom':'dupont'})
14          'INSERT INTO test ( nom ) VALUES ( %(nom)s )'
15          >>> s.insert('test',{'nom':'dupont','prenom':'jean'})
16          'INSERT INTO test ( nom, prenom ) VALUES ( %(nom)s, %(prenom)s )'
17          """
18 +        pre_params = pre_params or {}
19 +        # Pre keys - Should not be protected in doexec
20 +        pre_keys = ', '.join(pre_params)
21 +        pre_values = ', '.join([pre_params[k] for k in pre_params])
22 +        # Classical case
23          keys = ', '.join(params.keys())
24          values = ', '.join(["%%(%s)s" % x for x in params])
25 +        # Join keys and pre_keys
26 +        keys = keys + ((', ' + pre_keys) if pre_keys else '')
27 +        values = values + ((', ' + pre_values) if pre_values else '')
28          sql = 'INSERT INTO %s ( %s ) VALUES ( %s )' % (table, keys, values)
29          return sql
30 
31      def select(self, table, params=None, selection=None):
32          """