def tag_attributes(conn, tag_links): """ Update attribute.attribute_tag_link table :param conn: Minerva database connection :param tag_links: list of tuples like (attribute_id, tag_name) """ tmp_table_name = store_in_temp_table(conn, tag_links) query = ( "INSERT INTO attribute_directory.attribute_tag_link " "(attribute_id, tag_id) " "(SELECT tmp.attribute_id, tag.id " "FROM {} tmp " "JOIN directory.tag tag ON lower(tag.name) = lower(tmp.tag) " "LEFT JOIN attribute_directory.attribute_tag_link ttl ON " "ttl.attribute_id = tmp.attribute_id AND ttl.tag_id = tag.id " "WHERE ttl.attribute_id IS NULL)").format(tmp_table_name) with closing(conn.cursor()) as cursor: cursor.execute(query) drop_table(conn, tmp_table_name) conn.commit()
def tag_trends(conn, tag_links): """ Update trend.trend_tag_link table :param conn: Minerva database connection :param tag_links: list of tuples like (trend_id, tag_name) """ tmp_table_name = store_in_temp_table(conn, tag_links) query = ( "INSERT INTO {0}.trend_tag_link (trend_id, tag_id) " "(" " SELECT tmp.trend_id, tag.id " " FROM {1} tmp " " JOIN directory.tag tag ON lower(tag.name) = lower(tmp.tag) " " LEFT JOIN {0}.trend_tag_link ttl ON " " ttl.trend_id = tmp.trend_id AND ttl.tag_id = tag.id " " WHERE ttl.trend_id IS NULL" ")").format(schema.name, tmp_table_name) with closing(conn.cursor()) as cursor: cursor.execute(query) drop_table(conn, tmp_table_name) conn.commit()