def __init__(self, sqlite_db, column_dispatcher, identifier=None): """Interpret `column_dispatcher`""" self.sqlite_db = sqlite_db self.identifier = identifier self.sqltransactions = SQLTransactions(sqlite_db, identifier) self._column_dispatcher = column_dispatcher self.name = None self._columns, _index_names = [], set() for n, p in column_dispatcher.items(): validate_no_backtick(n, "column") validate_no_backtick(p, "table_part") if isinstance(n, SQLiteIndexName): _index_names.add(n) else: self._columns.append([n]) if self.name is None: self.name = p if len(_index_names) == 0: msg = "StreamedDataTableWizard(): no index" raise GeneFabDatabaseException(msg, table=self.name) elif len(_index_names) > 1: msg = "StreamedDataTableWizard(): indexes of parts do not match" _kw = dict(table=self.name, index_names=_index_names) raise GeneFabDatabaseException(msg, **_kw) self._index_name = _index_names.pop()
def __init__(self, *, sqlite_db, source_select, targets, query_filter, na_rep=None): """Infer index names and columns, retain connection and query information""" from genefab3.db.sql.streamed_tables import SQLiteIndexName _split3 = lambda c: (c[0].split("/", 2) + ["*", "*"])[:3] self.sqlite_db = sqlite_db self.source_select = source_select self.sqltransactions = SQLTransactions(sqlite_db, source_select.name) self.targets = targets self.query_filter = query_filter self.na_rep = na_rep self.query = f""" SELECT {targets} FROM `{source_select.name}` {query_filter} """ desc = "tables/StreamedDataTable" with self.sqltransactions.concurrent(desc) as (connection, execute): try: cursor = connection.cursor() cursor.execute(self.query) self._index_name = SQLiteIndexName(cursor.description[0][0]) self._columns = [_split3(c) for c in cursor.description[1:]] _count_query = f"SELECT count(*) FROM ({self.query})" _nrows = (execute(_count_query).fetchone() or [0])[0] self.shape = (_nrows, len(self._columns)) except OperationalError as e: reraise_operational_error(self, e) self.accessions = {c[0] for c in self._columns} self.n_index_levels = 1 self.datatypes, self.gct_validity_set = set(), set()
def __init__(self, sqlite_dbs): self.sqlite_db = sqlite_dbs.response_cache["db"] self.maxdbsize = sqlite_dbs.response_cache["maxsize"] or float("inf") if self.sqlite_db is None: msg = "LRU SQL cache DISABLED by client parameter" _logw(f"ResponseCache():\n {msg}") else: self.sqltransactions = SQLTransactions(self.sqlite_db) desc = "response_cache/ensure_schema" with self.sqltransactions.concurrent(desc) as (_, execute): for table, schema in RESPONSE_CACHE_SCHEMAS: execute(f"CREATE TABLE IF NOT EXISTS `{table}` {schema}")
def __init__(self, *, sqlite_db, identifier=None, table_schemas=None): """Initialize SQLiteObject, ensure tables in `sqlite_db`""" self.sqlite_db, self.table_schemas = sqlite_db, table_schemas self.identifier = identifier self.changed = None self.sqltransactions = SQLTransactions(sqlite_db, identifier) desc = "tables/ensure_schema" with self.sqltransactions.concurrent(desc) as (_, execute): for table, schema in (table_schemas or {}).items(): execute( "CREATE TABLE IF NOT EXISTS `{}` ({})".format( validate_no_backtick(table, "table"), ", ".join("`" + validate_no_backtick(f, "field") + "` " + k for f, k in schema.items()), ), )
def __init__(self, *, sqlite_db, query, targets, kind="TABLE", _depends_on=None, msg=None): self.sqlite_db = sqlite_db self._depends_on = _depends_on # keeps sources from being deleted early self.query, self.targets, self.kind = query, targets, kind self.name = "TEMP:" + random_unique_string(seed=query) self.sqltransactions = SQLTransactions(self.sqlite_db, self.name) with self.sqltransactions.exclusive("TempSelect") as (_, execute): if msg: GeneFabLogger.info(msg) try: execute(f"CREATE {self.kind} `{self.name}` as {query}") except OperationalError as e: reraise_operational_error(self, e) else: query_repr = repr(query.lstrip()[:200] + "...") msg = f"Created temporary SQLite {self.kind}" GeneFabLogger.info(f"{msg} {self.name} from\n {query_repr}")