|
| 1 | +import contextlib |
| 2 | +from dataclasses import dataclass, field |
| 3 | +from typing import TYPE_CHECKING, Any, Optional |
| 4 | + |
| 5 | +from sqlspec.adapters.bigquery.config._common import BigQueryConnectionConfigCommon |
| 6 | +from sqlspec.adapters.bigquery.driver import BigQueryConnection, BigQueryDriver |
| 7 | +from sqlspec.base import NoPoolSyncConfig |
| 8 | +from sqlspec.typing import dataclass_to_dict |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + from collections.abc import Iterator |
| 12 | + |
| 13 | +__all__ = ("BigQueryConfig", "BigQueryConnectionConfig") |
| 14 | + |
| 15 | + |
| 16 | +class BigQueryConnectionConfig(BigQueryConnectionConfigCommon): |
| 17 | + """BigQuery Connection Configuration.""" |
| 18 | + |
| 19 | + |
| 20 | +@dataclass |
| 21 | +class BigQueryConfig(NoPoolSyncConfig["BigQueryConnection", "BigQueryDriver"]): |
| 22 | + """BigQuery Synchronous Driver Configuration.""" |
| 23 | + |
| 24 | + connection_config: "BigQueryConnectionConfig" = field(default_factory=BigQueryConnectionConfig) |
| 25 | + """BigQuery Connection Configuration.""" |
| 26 | + driver_type: "type[BigQueryDriver]" = field(init=False, repr=False, default=BigQueryDriver) |
| 27 | + """BigQuery Driver Type.""" |
| 28 | + connection_type: "type[BigQueryConnection]" = field(init=False, repr=False, default=BigQueryConnection) |
| 29 | + """BigQuery Connection Type.""" |
| 30 | + pool_instance: "None" = field(init=False, repr=False, default=None, hash=False) |
| 31 | + """This is set to have a init=False since BigQuery does not support pooling.""" |
| 32 | + connection_instance: "Optional[BigQueryConnection]" = field(init=False, repr=False, default=None, hash=False) |
| 33 | + """BigQuery Connection Instance.""" |
| 34 | + |
| 35 | + @property |
| 36 | + def connection_config_dict(self) -> "dict[str, Any]": |
| 37 | + """Return the connection configuration as a dict. |
| 38 | +
|
| 39 | + Returns: |
| 40 | + A string keyed dict of config kwargs for the BigQueryConnection constructor. |
| 41 | + """ |
| 42 | + return dataclass_to_dict( |
| 43 | + self.connection_config, |
| 44 | + exclude_empty=True, |
| 45 | + exclude_none=True, |
| 46 | + exclude={"dataset_id", "credentials_path"}, |
| 47 | + ) |
| 48 | + |
| 49 | + def create_connection(self) -> "BigQueryConnection": |
| 50 | + """Create a BigQuery Client instance. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + A BigQuery Client instance. |
| 54 | + """ |
| 55 | + if self.connection_instance is not None: |
| 56 | + return self.connection_instance |
| 57 | + |
| 58 | + self.connection_instance = self.connection_type(**self.connection_config_dict) |
| 59 | + return self.connection_instance |
| 60 | + |
| 61 | + @contextlib.contextmanager |
| 62 | + def provide_connection(self, *args: Any, **kwargs: Any) -> "Iterator[BigQueryConnection]": |
| 63 | + """Provide a BigQuery client within a context manager. |
| 64 | +
|
| 65 | + Args: |
| 66 | + *args: Additional arguments to pass to the connection. |
| 67 | + **kwargs: Additional keyword arguments to pass to the connection. |
| 68 | +
|
| 69 | + Yields: |
| 70 | + An iterator of BigQuery Client instances. |
| 71 | + """ |
| 72 | + conn = self.create_connection() |
| 73 | + yield conn |
| 74 | + |
| 75 | + @contextlib.contextmanager |
| 76 | + def provide_session(self, *args: Any, **kwargs: Any) -> "Iterator[BigQueryDriver]": |
| 77 | + """Provide a BigQuery driver session within a context manager. |
| 78 | +
|
| 79 | + Args: |
| 80 | + *args: Additional arguments to pass to the driver. |
| 81 | + **kwargs: Additional keyword arguments to pass to the driver. |
| 82 | +
|
| 83 | + Yields: |
| 84 | + An iterator of BigQueryDriver instances. |
| 85 | + """ |
| 86 | + conn = self.create_connection() |
| 87 | + yield self.driver_type(connection=conn) |
0 commit comments