Skip to content
This repository was archived by the owner on Dec 2, 2019. It is now read-only.

Commit

Permalink
Less hacky sqlalchemy registration
Browse files Browse the repository at this point in the history
Loosen versions in setup.py
  • Loading branch information
jingw committed Feb 3, 2014
1 parent 0326f49 commit a4c3ca9
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 36 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ DB-API
SQLAlchemy
----------
First install this package to register it with SQLAlchemy (see ``setup.py``).

.. code-block:: python
# This import has the side effect of registering with sqlalchemy
from pyhive import sqlalchemy_presto
from sqlalchemy import *
from sqlalchemy.engine import create_engine
from sqlalchemy.schema import *
Expand Down
13 changes: 1 addition & 12 deletions pyhive/sqlalchemy_hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
from sqlalchemy.sql import compiler
import decimal
import re
import sqlalchemy
import sys
import warnings

try:
Expand Down Expand Up @@ -49,13 +47,6 @@ def process_result_value(self, value, dialect):
return decimal.Decimal(value)


def register_in_sqlalchemy():
# Register us under SQLAlchemy
sys.modules['sqlalchemy.databases.hive'] = sys.modules[__name__]
sqlalchemy.databases.hive = sys.modules[__name__]
register_in_sqlalchemy()


class HiveIdentifierPreparer(compiler.IdentifierPreparer):
# https://github.com/apache/hive/blob/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/HiveLexer.g
reserved_words = frozenset([
Expand Down Expand Up @@ -361,7 +352,7 @@ def reflecttable(self, connection, table, include_columns=None):
try:
# This needs the table name to be unescaped (no backticks).
rows = connection.execute('DESCRIBE {}'.format(table)).fetchall()
except sqlalchemy.exc.OperationalError as e:
except exc.OperationalError as e:
# Does the table exist?
regex_fmt = r'TExecuteStatementResp.*SemanticException.*Table not found {}'
regex = regex_fmt.format(re.escape(table.name))
Expand Down Expand Up @@ -401,5 +392,3 @@ def reflecttable(self, connection, table, include_columns=None):
def do_rollback(self, dbapi_connection):
# No transactions for Hive
pass

dialect = HiveDialect
11 changes: 0 additions & 11 deletions pyhive/sqlalchemy_presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,9 @@
from sqlalchemy.engine import default
from sqlalchemy.sql import compiler
import re
import sqlalchemy
import sys
import warnings


def register_in_sqlalchemy():
# Register us under SQLAlchemy
sys.modules['sqlalchemy.databases.presto'] = sys.modules[__name__]
sqlalchemy.databases.presto = sys.modules[__name__]
register_in_sqlalchemy()


class PrestoIdentifierPreparer(compiler.IdentifierPreparer):
# https://github.com/facebook/presto/blob/master/presto-parser/src/main/antlr3/com/facebook/presto/sql/parser/Statement.g
reserved_words = frozenset([
Expand Down Expand Up @@ -181,5 +172,3 @@ def reflecttable(self, connection, table, include_columns=None):
def do_rollback(self, dbapi_connection):
# No transactions for Presto
pass

dialect = PrestoDialect
2 changes: 0 additions & 2 deletions pyhive/tests/test_sqlalchemy_hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
from sqlalchemy.types import String
import datetime
import decimal
# This import has the side effect of registering with sqlalchemy
import pyhive.sqlalchemy_hive


class TestSqlAlchemyHive(SqlAlchemyTestCase):
Expand Down
2 changes: 0 additions & 2 deletions pyhive/tests/test_sqlalchemy_presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from sqlalchemy.schema import Table
from sqlalchemy.types import String
import contextlib
# This import has the side effect of registering with sqlalchemy
import pyhive.sqlalchemy_presto


class TestSqlAlchemyPresto(SqlAlchemyTestCase):
Expand Down
26 changes: 19 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,32 @@
"Topic :: Database",
],
extras_require={
"Presto": ['requests'],
"Hive": ['sasl>=0.1.3', 'thrift>=0.9.1'],
"SQLAlchemy": ['sqlalchemy==0.5.8'],
"Presto": ['requests>=1.0.0'],
"Hive": ['sasl>=0.1.3', 'thrift>=0.8.0'],
"SQLAlchemy": ['sqlalchemy>=0.5.0'],
},
test_suite='nose.collector',
tests_require=[
'mock',
'mock>=1.0.0',
'nose',
'requests',
'requests>=1.0.0',
'sasl>=0.1.3',
'sqlalchemy==0.5.8',
'thrift>=0.9.1',
'sqlalchemy>=0.5.0',
'thrift>=0.8.0',
],
package_data={
'': ['*.rst'],
},
entry_points={
# New versions
'sqlalchemy.dialects': [
'hive = pyhive.sqlalchemy_hive:HiveDialect',
'presto = pyhive.sqlalchemy_presto:PrestoDialect',
],
# Version 0.5
'sqlalchemy.databases': [
'hive = pyhive.sqlalchemy_hive:HiveDialect',
'presto = pyhive.sqlalchemy_presto:PrestoDialect',
],
}
)

0 comments on commit a4c3ca9

Please sign in to comment.