def insert_patch_into_multibandtable(cursor, schemaName, tables, object_id): """ Insert a patch into a multiband table. @param cursor DB connection's cursor object @param schemaName Name of the schema in which to locate the master table @param tables List of (table: DBTable, filter: str). Tables in this list must have identical object_id (Or, identical (tract, patch)) with different colors. @param object_id numpy.array of object ID. This is used as the primary key. """ columns = [object_id] fieldNames = ["object_id"] format = "%ld" for name, fmt, cols in tables[0][0].get_bandindependent_backend_field_data( ): columns.extend(cols) fieldNames.append(name) format += "\t" + fmt for table, filter in tables: for name, fmt, cols in table.get_backend_field_data(filter): columns.extend(cols) fieldNames.append(name) format += "\t" + fmt format += "\n" format = format.encode("utf-8") if lib.config.MULTICORE: fin = pipe_printf.open(format, *columns) cursor.copy_from(fin, '"{}"."{}"'.format(schemaName, table.name), sep='\t', columns=fieldNames) else: tsv = b''.join(format % tpl for tpl in zip(*columns)) fin = io.BytesIO(tsv) cursor.copy_from(fin, '"{}"."{}"'.format(schemaName, table.name), sep='\t', size=-1, columns=fieldNames)
def insert_bit(use_cursor, schema_name, dbimage, **determiners): """ Insert data corresponding to one input file into Postgres @param use_cursor db cursor or None (for dryrun) @param schema_name @dbimage DbImage instance @determiners Uniquely determines this part of the data """ columns = [] field_names = [] format = "" # For convenience stuff schema_name into determiners determiners['schema_name'] = schema_name dryrun = (use_cursor is None) if not dryrun: # create table to keep track of ingest if it doesn't already exist use_cursor.execute(""" CREATE TABLE IF NOT EXISTS "{schema_name}"."_temp:forced_bit" ( visit Bigint, raft int, sensor int, unique (visit, raft, sensor) ) """.format(**locals())) # check if our entry is already there select_q = """ SELECT visit FROM "{schema_name}"."_temp:forced_bit" WHERE visit={visit} and raft={raft} and sensor={sensor} """.format(**determiners) use_cursor.execute(select_q) if use_cursor.fetchone() is not None: # bit is already there return first = True for name, fmt, cols in dbimage.get_backend_field_data(""): columns.extend(cols) field_names.append(name) if first: format = fmt first = False else: format += "\t" + fmt if dryrun: # print a piece of the data and exit print("Bit raft={raft}, sensor={sensor}, visit={visit}".format( **determiners)) #print("field names: ") all_fields = ' '.join(field_names) print('All fields: ', all_fields) print('Format is: \n', format) tsv = ''.join(format % tpl for tpl in zip(*columns)) print('Printing tsv[:600]') print(tsv[:600]) return format += "\n" format = format.encode("utf-8") if lib.config.MULTICORE: fin = pipe_printf.open(format, *columns) if use_cursor is not None: use_cursor.copy_from(fin, '"{}"."{}"'.format(schema_name, dbimage.name), sep='\t', columns=field_names) else: tsv = b''.join(format % tpl for tpl in zip(*columns)) fin = io.BytesIO(tsv) if use_cursor is not None: use_cursor.copy_from(fin, '"{}"."{}"'.format(schema_name, dbimage.name), sep='\t', size=-1, columns=field_names) # Update bookkeeping table insert_q = """ INSERT INTO "{schema_name}"."_temp:forced_bit" (visit, raft, sensor) values({visit}, {raft}, {sensor}) """.format(**determiners) use_cursor.execute(insert_q)