def is_run_id_in_database(conn: ConnectionPlus, *run_ids) -> Dict[int, bool]: """ Look up run_ids and return a dictionary with the answers to the question "is this run_id in the database?" Args: conn: the connection to the database run_ids: the run_ids to look up Returns: a dict with the run_ids as keys and bools as values. True means that the run_id DOES exist in the database """ run_ids = np.unique(run_ids) placeholders = sql_placeholder_string(len(run_ids)) query = f""" SELECT run_id FROM runs WHERE run_id in {placeholders} """ cursor = conn.cursor() cursor.execute(query, run_ids) rows = cursor.fetchall() existing_ids = [row[0] for row in rows] return {run_id: (run_id in existing_ids) for run_id in run_ids}
def _populate_results_table(source_conn: ConnectionPlus, target_conn: ConnectionPlus, source_table_name: str, target_table_name: str) -> None: """ Copy over all the entries of the results table """ get_data_query = f""" SELECT * FROM "{source_table_name}" """ source_cursor = source_conn.cursor() target_cursor = target_conn.cursor() for row in source_cursor.execute(get_data_query): column_names = ','.join(row.keys()[1:]) # the first key is "id" values = tuple(val for val in row[1:]) value_placeholders = sql_placeholder_string(len(values)) insert_data_query = f""" INSERT INTO "{target_table_name}" ({column_names}) values {value_placeholders} """ target_cursor.execute(insert_data_query, values)
def get_exp_ids_from_run_ids(conn: ConnectionPlus, run_ids: Sequence[int]) -> List[int]: """ Get the corresponding exp_id for a sequence of run_ids Args: conn: connection to the database run_ids: a sequence of the run_ids to get the exp_id of Returns: A list of exp_ids matching the run_ids """ sql_placeholders = sql_placeholder_string(len(run_ids)) exp_id_query = f""" SELECT exp_id FROM runs WHERE run_id IN {sql_placeholders} """ cursor = conn.cursor() cursor.execute(exp_id_query, run_ids) rows = cursor.fetchall() return [exp_id for row in rows for exp_id in row]