|
| 1 | +import google.api_core.exceptions |
| 2 | +import google.cloud.bigquery.schema |
| 3 | +import google.cloud.bigquery.table |
| 4 | +import contextlib |
| 5 | +import sqlite3 |
| 6 | + |
| 7 | + |
| 8 | +class Connection: |
| 9 | + |
| 10 | + connection = None |
| 11 | + |
| 12 | + def __init__(self, client=None, bqstorage_client=None): |
| 13 | + # share a single connection: |
| 14 | + if self.connection is None: |
| 15 | + self.__class__.connection = sqlite3.connect(":memory:") |
| 16 | + self._client = FauxClient(client, self.connection) |
| 17 | + |
| 18 | + def cursor(self): |
| 19 | + return Cursor(self.connection) |
| 20 | + |
| 21 | + def commit(self): |
| 22 | + pass |
| 23 | + |
| 24 | + def rollback(self): |
| 25 | + pass |
| 26 | + |
| 27 | + def close(self): |
| 28 | + self.connection.close() |
| 29 | + |
| 30 | + |
| 31 | +class Cursor: |
| 32 | + |
| 33 | + arraysize = 1 |
| 34 | + |
| 35 | + def __init__(self, connection): |
| 36 | + self.connection = connection |
| 37 | + self.cursor = connection.cursor() |
| 38 | + |
| 39 | + def execute(self, operation, parameters=None): |
| 40 | + if parameters: |
| 41 | + parameters = { |
| 42 | + name: "null" if value is None else repr(value) |
| 43 | + for name, value in parameters.items() |
| 44 | + } |
| 45 | + operation %= parameters |
| 46 | + self.cursor.execute(operation, parameters) |
| 47 | + self.description = self.cursor.description |
| 48 | + self.rowcount = self.cursor.rowcount |
| 49 | + |
| 50 | + def executemany(self, operation, parameters_list): |
| 51 | + for parameters in parameters_list: |
| 52 | + self.execute(operation, parameters) |
| 53 | + |
| 54 | + def close(self): |
| 55 | + self.cursor.close() |
| 56 | + |
| 57 | + def fetchone(self): |
| 58 | + return self.cursor.fetchone() |
| 59 | + |
| 60 | + def fetchmany(self, size=None): |
| 61 | + self.cursor.fetchmany(size or self.arraysize) |
| 62 | + |
| 63 | + def fetchall(self): |
| 64 | + return self.cursor.fetchall() |
| 65 | + |
| 66 | + def setinputsizes(self, sizes): |
| 67 | + pass |
| 68 | + |
| 69 | + def setoutputsize(self, size, column=None): |
| 70 | + pass |
| 71 | + |
| 72 | + |
| 73 | +class FauxClient: |
| 74 | + def __init__(self, client, connection): |
| 75 | + self._client = client |
| 76 | + self.project = client.project |
| 77 | + self.connection = connection |
| 78 | + |
| 79 | + def get_table(self, table_ref): |
| 80 | + table_name = table_ref.table_id |
| 81 | + with contextlib.closing(self.connection.cursor()) as cursor: |
| 82 | + cursor.execute( |
| 83 | + f"select name from sqlite_master" |
| 84 | + f" where type='table' and name='{table_name}'" |
| 85 | + ) |
| 86 | + if list(cursor): |
| 87 | + cursor.execute("PRAGMA table_info('{table_name}')") |
| 88 | + schema = [ |
| 89 | + google.cloud.bigquery.schema.SchemaField( |
| 90 | + name=name, |
| 91 | + field_type=type_, |
| 92 | + mode="REQUIRED" if notnull else "NULLABLE", |
| 93 | + ) |
| 94 | + for cid, name, type_, notnull, dflt_value, pk in cursor |
| 95 | + ] |
| 96 | + return google.cloud.bigquery.table.Table(table_ref, schema) |
| 97 | + else: |
| 98 | + raise google.api_core.exceptions.NotFound(table_ref) |
0 commit comments