Skip to content
This repository was archived by the owner on May 16, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
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
37 changes: 28 additions & 9 deletions pybigquery/sqlalchemy_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

from google.cloud.bigquery import dbapi
from google.cloud import bigquery
from google.api.core.exceptions import NotFound
from google.api_core.exceptions import NotFound
from sqlalchemy.exc import NoSuchTableError
from sqlalchemy import types, util
from sqlalchemy.sql.compiler import SQLCompiler, IdentifierPreparer
from sqlalchemy.engine.default import DefaultDialect, DefaultExecutionContext
from sqlalchemy.engine.base import Engine


class UniversalSet(object):
Expand Down Expand Up @@ -118,14 +119,22 @@ def _split_table_name(self, full_table_name):

return (project, dataset, table_name)

def _get_table(self, connection, table_name):
project, dataset, table_name = self._split_table_name(table_name)
table = connection.connection._client.dataset(dataset, project=project).table(table_name)
def _get_table(self, connection, table_name,schema=None):
if (isinstance(connection,Engine)):
connection = connection.connect()
project, dataset, tablename = self._split_table_name(table_name)
if dataset == None and schema != None:
dataset = schema
tablename = table_name
print(project)
print(schema)
print(tablename)
table = connection.connection._client.dataset(dataset, project=project).table(tablename)
try:
table.reload()
t = connection.connection._client.get_table(table)
except NotFound as e:
raise NoSuchTableError(table_name)
return table
return t

def has_table(self, connection, table_name, schema=None):
try:
Expand All @@ -135,7 +144,7 @@ def has_table(self, connection, table_name, schema=None):
return False

def get_columns(self, connection, table_name, schema=None, **kw):
table = self._get_table(connection, table_name)
table = self._get_table(connection, table_name,schema)
columns = table.schema
result = []
for col in columns:
Expand Down Expand Up @@ -164,14 +173,23 @@ def get_pk_constraint(self, connection, table_name, schema=None, **kw):
def get_indexes(self, connection, table_name, schema=None, **kw):
# BigQuery has no support for indexes.
return []
def get_schema_names(self, connection, **kw):
if (isinstance(connection,Engine)):
connection = connection.connect()
datasets = connection.connection._client.list_datasets()
return [d.dataset_id for d in datasets]

def get_table_names(self, connection, schema=None, **kw):
if (isinstance(connection,Engine)):
connection = connection.connect()
datasets = connection.connection._client.list_datasets()
result = []
for d in datasets:
tables = d.list_tables()
if schema != None and d.dataset_id != schema:
continue
tables = connection.connection._client.list_dataset_tables(d)
for t in tables:
result.append(d.name + '.' + t.name)
result.append(d.dataset_id + '.' + t.table_id)
return result

def do_rollback(self, dbapi_connection):
Expand All @@ -186,3 +204,4 @@ def _check_unicode_description(self, connection):
# requests gives back Unicode strings
return True


2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
install_requires=[
'sqlalchemy>=1.1.9',
'google-cloud-bigquery>=0.27.0',
'google-cloud-bigquery>=0.28.0',
'future',
],
tests_require=[
Expand Down