@@ -217,7 +217,7 @@ def wrap(arg):
217217
218218 args = tuple (wrap (a ) for a in args )
219219 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 ) ))
221221
222222
223223def explode_query (cr , query , alias = None , num_buckets = 8 , prefix = None ):
@@ -557,7 +557,7 @@ def create_column(cr, table, column, definition, **kwargs):
557557 fk = (
558558 sql .SQL ("REFERENCES {}(id) ON DELETE {}" )
559559 .format (sql .Identifier (fk_table ), sql .SQL (on_delete_action ))
560- .as_string (cr . _cnx )
560+ .as_string (cursor_get_connection ( cr ) )
561561 )
562562 elif on_delete_action is not no_def :
563563 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):
845845 """
846846 _validate_table (table )
847847
848- if cr . _cnx .server_version >= 90500 :
848+ if cursor_get_connection ( cr ) .server_version >= 90500 :
849849 position = "array_position(x.indkey, x.unnest_indkey)"
850850 else :
851851 # array_position does not exists prior postgresql 9.5
@@ -980,10 +980,10 @@ class ColumnList(UserList, sql.Composable):
980980 >>> list(columns)
981981 ['id', '"field_Yx"']
982982
983- >>> columns.using(alias="t").as_string(cr._cnx )
983+ >>> columns.using(alias="t").as_string(cursor_get_connection(cr) )
984984 '"t"."id", "t"."field_Yx"'
985985
986- >>> columns.using(leading_comma=True).as_string(cr._cnx )
986+ >>> columns.using(leading_comma=True).as_string(cursor_get_connection(cr) )
987987 ', "id", "field_Yx"'
988988
989989 >>> 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_):
10261026
10271027 :param list(str) list_: list of unquoted column names
10281028 """
1029- quoted = [quote_ident (c , cr . _cnx ) for c in list_ ]
1029+ quoted = [quote_ident (c , cursor_get_connection ( cr ) ) for c in list_ ]
10301030 return cls (list_ , quoted )
10311031
10321032 def using (self , leading_comma = KEEP_CURRENT , trailing_comma = KEEP_CURRENT , alias = KEEP_CURRENT ):
@@ -1482,7 +1482,8 @@ def get_m2m_tables(cr, table):
14821482
14831483class named_cursor (object ):
14841484 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 )
14861487 if itersize :
14871488 self ._ncr .itersize = itersize
14881489
@@ -1571,3 +1572,9 @@ def create_id_sequence(cr, table, set_as_default=True):
15711572 table = table_sql ,
15721573 )
15731574 )
1575+
1576+
1577+ def cursor_get_connection (cursor ):
1578+ if hasattr (cursor , '_cnx__' ):
1579+ return cursor ._cnx__
1580+ return cursor ._cnx
0 commit comments