Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the Unicode issue for all connection code paths #52

Merged
merged 1 commit into from
Oct 12, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions pandasql/sqldf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
from warnings import catch_warnings, filterwarnings
from sqlalchemy.exc import DatabaseError, ResourceClosedError
from sqlalchemy.pool import NullPool

from sqlalchemy.event import listen

__all__ = ['PandaSQL', 'PandaSQLException', 'sqldf']


class PandaSQLException(Exception):
pass

Expand All @@ -24,6 +23,10 @@ def __init__(self, db_uri='sqlite:///:memory:', persist=False):
:param persist: keep tables in database between different calls on the same object of this class.
"""
self.engine = create_engine(db_uri, poolclass=NullPool)

if self.engine.name == 'sqlite':
listen(self.engine, 'connect', self._set_text_factory)

if self.engine.name not in ('sqlite', 'postgresql'):
raise PandaSQLException('Currently only sqlite and postgresql are supported.')

Expand Down Expand Up @@ -77,7 +80,6 @@ def conn(self):
else:
# create the connection
conn = self.engine.connect()
conn.text_factory = str
self._init_connection(conn)
try:
yield conn
Expand All @@ -89,6 +91,9 @@ def _init_connection(self, conn):
if self.engine.name == 'postgresql':
conn.execute('set search_path to pg_temp')

def _set_text_factory(self, dbapi_con, connection_record):
dbapi_con.text_factory = str


def get_outer_frame_variables():
""" Get a dict of local and global variables of the first outer frame from another file. """
Expand Down