6
6
from warnings import catch_warnings , filterwarnings
7
7
from sqlalchemy .exc import DatabaseError , ResourceClosedError
8
8
from sqlalchemy .pool import NullPool
9
-
9
+ from sqlalchemy . event import listen
10
10
11
11
__all__ = ['PandaSQL' , 'PandaSQLException' , 'sqldf' ]
12
12
13
-
14
13
class PandaSQLException (Exception ):
15
14
pass
16
15
@@ -24,6 +23,10 @@ def __init__(self, db_uri='sqlite:///:memory:', persist=False):
24
23
:param persist: keep tables in database between different calls on the same object of this class.
25
24
"""
26
25
self .engine = create_engine (db_uri , poolclass = NullPool )
26
+
27
+ if self .engine .name == 'sqlite' :
28
+ listen (self .engine , 'connect' , self ._set_text_factory )
29
+
27
30
if self .engine .name not in ('sqlite' , 'postgresql' ):
28
31
raise PandaSQLException ('Currently only sqlite and postgresql are supported.' )
29
32
@@ -77,7 +80,6 @@ def conn(self):
77
80
else :
78
81
# create the connection
79
82
conn = self .engine .connect ()
80
- conn .text_factory = str
81
83
self ._init_connection (conn )
82
84
try :
83
85
yield conn
@@ -89,6 +91,9 @@ def _init_connection(self, conn):
89
91
if self .engine .name == 'postgresql' :
90
92
conn .execute ('set search_path to pg_temp' )
91
93
94
+ def _set_text_factory (self , dbapi_con , connection_record ):
95
+ dbapi_con .text_factory = str
96
+
92
97
93
98
def get_outer_frame_variables ():
94
99
""" Get a dict of local and global variables of the first outer frame from another file. """
0 commit comments