@@ -217,7 +217,7 @@ def wrap(arg):
217
217
218
218
args = tuple (wrap (a ) for a in args )
219
219
kwargs = {k : wrap (v ) for k , v in kwargs .items ()}
220
- return SQLStr (sql .SQL (query ).format (* args , ** kwargs ).as_string (cr . _cnx ))
220
+ return SQLStr (sql .SQL (query ).format (* args , ** kwargs ).as_string (cursor_get_connection ( cr ) ))
221
221
222
222
223
223
def explode_query (cr , query , alias = None , num_buckets = 8 , prefix = None ):
@@ -557,7 +557,7 @@ def create_column(cr, table, column, definition, **kwargs):
557
557
fk = (
558
558
sql .SQL ("REFERENCES {}(id) ON DELETE {}" )
559
559
.format (sql .Identifier (fk_table ), sql .SQL (on_delete_action ))
560
- .as_string (cr . _cnx )
560
+ .as_string (cursor_get_connection ( cr ) )
561
561
)
562
562
elif on_delete_action is not no_def :
563
563
raise ValueError ("`on_delete_action` argument can only be used if `fk_table` argument is set." )
@@ -845,7 +845,7 @@ def get_index_on(cr, table, *columns):
845
845
"""
846
846
_validate_table (table )
847
847
848
- if cr . _cnx .server_version >= 90500 :
848
+ if cursor_get_connection ( cr ) .server_version >= 90500 :
849
849
position = "array_position(x.indkey, x.unnest_indkey)"
850
850
else :
851
851
# array_position does not exists prior postgresql 9.5
@@ -980,10 +980,10 @@ class ColumnList(UserList, sql.Composable):
980
980
>>> list(columns)
981
981
['id', '"field_Yx"']
982
982
983
- >>> columns.using(alias="t").as_string(cr._cnx )
983
+ >>> columns.using(alias="t").as_string(cursor_get_connection(cr) )
984
984
'"t"."id", "t"."field_Yx"'
985
985
986
- >>> columns.using(leading_comma=True).as_string(cr._cnx )
986
+ >>> columns.using(leading_comma=True).as_string(cursor_get_connection(cr) )
987
987
', "id", "field_Yx"'
988
988
989
989
>>> util.format_query(cr, "SELECT {} t.name FROM table t", columns.using(alias="t", trailing_comma=True))
@@ -1026,7 +1026,7 @@ def from_unquoted(cls, cr, list_):
1026
1026
1027
1027
:param list(str) list_: list of unquoted column names
1028
1028
"""
1029
- quoted = [quote_ident (c , cr . _cnx ) for c in list_ ]
1029
+ quoted = [quote_ident (c , cursor_get_connection ( cr ) ) for c in list_ ]
1030
1030
return cls (list_ , quoted )
1031
1031
1032
1032
def using (self , leading_comma = KEEP_CURRENT , trailing_comma = KEEP_CURRENT , alias = KEEP_CURRENT ):
@@ -1482,7 +1482,8 @@ def get_m2m_tables(cr, table):
1482
1482
1483
1483
class named_cursor (object ):
1484
1484
def __init__ (self , cr , itersize = None ):
1485
- self ._ncr = cr ._cnx .cursor ("upg_nc_" + uuid .uuid4 ().hex , withhold = True )
1485
+ pgconn = cursor_get_connection (cr )
1486
+ self ._ncr = pgconn .cursor ("upg_nc_" + uuid .uuid4 ().hex , withhold = True )
1486
1487
if itersize :
1487
1488
self ._ncr .itersize = itersize
1488
1489
@@ -1571,3 +1572,9 @@ def create_id_sequence(cr, table, set_as_default=True):
1571
1572
table = table_sql ,
1572
1573
)
1573
1574
)
1575
+
1576
+
1577
+ def cursor_get_connection (cursor ):
1578
+ if hasattr (cursor , '_cnx__' ):
1579
+ return cursor ._cnx__
1580
+ return cursor ._cnx
0 commit comments