Skip to content

Commit 3f7f80f

Browse files
committed
[ADD] ColumnList.from_unquoted
Alternative constructor that doesn't need the explicitly quoted list. closes #237 Related: odoo/upgrade#7421 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 5c9b250 commit 3f7f80f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/util/pg.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import psycopg2
3333
from psycopg2 import errorcodes, sql
34+
from psycopg2.extensions import quote_ident
3435

3536
try:
3637
from odoo.modules import module as odoo_module
@@ -984,6 +985,14 @@ class ColumnList(UserList, sql.Composable):
984985
>>> util.format_query(cr, "SELECT {} t.name FROM table t", columns.using(alias="t", trailing_comma=True))
985986
'SELECT "t"."id", "t"."field_Yx", t.name FROM table t'
986987
988+
>>> columns = ColumnList.from_unquoted(cr, ["foo", "BAR"])
989+
990+
>>> list(columns)
991+
['"foo"', '"BAR"']
992+
993+
>>> list(columns.iter_unquoted())
994+
['foo', 'BAR']
995+
987996
.. note::
988997
This class is better used via :func:`~odoo.upgrade.util.pg.get_columns`
989998
"""
@@ -996,6 +1005,16 @@ def __init__(self, list_=(), quoted=()):
9961005
self._trailing_comma = False
9971006
self._alias = None
9981007

1008+
@classmethod
1009+
def from_unquoted(cls, cr, list_):
1010+
"""
1011+
Build a ColumnList from a list of column names that may need quoting.
1012+
1013+
:param list(str) list_: list of unquoted column names
1014+
"""
1015+
quoted = [quote_ident(c, cr._cnx) for c in list_]
1016+
return cls(list_, quoted)
1017+
9991018
def using(self, leading_comma=False, trailing_comma=False, alias=None):
10001019
"""
10011020
Set up parameters to render this list as a string.

0 commit comments

Comments
 (0)