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 SQLAlchemy v2 issues #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
33 changes: 21 additions & 12 deletions src/rockset_sqlalchemy/sqlalchemy/dialect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from sqlalchemy import exc, types, util
from rockset.exceptions import NotFoundException

from sqlalchemy import exc
from sqlalchemy.engine import default, reflection
from sqlalchemy.sql import compiler

Expand Down Expand Up @@ -63,25 +65,25 @@ def create_connect_args(self, url):

@reflection.cache
def get_schema_names(self, connection, **kw):
return [w["name"] for w in connection.connect().connection._client.Workspaces.list()["data"]]
return [w["name"] for w in connection._dbapi_connection.connection._client.Workspaces.list()["data"]]

@reflection.cache
def get_table_names(self, connection, schema=None, **kw):
tables = (connection.connect().connection._client.Collections.list()
tables = (connection._dbapi_connection.connection._client.Collections.list()
if schema is None else
connection.connect().connection._client.Collections.workspace_collections(workspace=schema))['data']
connection._dbapi_connection.connection._client.Collections.workspace_collections(workspace=schema))['data']

return [w["name"] for w in tables]

def _get_table_columns(self, connection, table_name, schema):
schema = self.identifier_preparer.quote_identifier(schema)
schema = self.identifier_preparer.quote_identifier(schema or "commons")
table_name = self.identifier_preparer.quote_identifier(table_name)

# Get a single row and determine the schema from that.
# This assumes the whole collection has a fixed schema of course.
q = f"SELECT * FROM {schema}.{table_name} LIMIT 1"
try:
cursor = connection.connect().connection.cursor()
cursor = connection._dbapi_connection.connection.cursor()
cursor.execute(q)
fields = cursor.description
if not fields:
Expand All @@ -105,6 +107,13 @@ def _get_table_columns(self, connection, table_name, schema):
}
)
except Exception as e:
try:
connection._dbapi_connection.connection._client.Collections.get(
collection=table_name,
workspace=schema
)
except NotFoundException:
raise exc.NoSuchTableError(e)
# TODO: more graceful handling of exceptions.
raise e
return columns
Expand All @@ -116,24 +125,24 @@ def get_columns(self, connection, table_name, schema=None, **kw):
return self._get_table_columns(connection, table_name, schema)

@reflection.cache
def get_view_names(self, connection, schema=None, **kw):
def get_view_names(self, connection, schema="commons", **kw):
# TODO: implement this.
return []

@reflection.cache
def get_foreign_keys(self, connection, table_name, schema=None, **kw):
def get_foreign_keys(self, connection, table_name, schema="commons", **kw):
# Rockset does not have foreign keys.
return []

@reflection.cache
def get_pk_constraint(self, connection, table_name, schema=None, **kw):
def get_pk_constraint(self, connection, table_name, schema="commons", **kw):
return {"constrained_columns": ["_id"], "name": "_id_pk"}

@reflection.cache
def get_indexes(self, connection, table_name, schema=None, **kw):
def get_indexes(self, connection, table_name, schema="commons", **kw):
return []
def has_table(self, connection, table_name, schema=None):

def has_table(self, connection, table_name, schema="commons"):
try:
self._get_table_columns(connection, table_name, schema)
return True
Expand Down