def upload_batch(jdbc: Jdbc, batch_nr, id_start, word_list, use_paramers): cursor = None pk = id_start n_ascii = 0 n_other = 0 for w in word_list: try: w.encode().decode('ascii') n_ascii += 1 except UnicodeDecodeError: n_other += 1 if use_paramers: for w in word_list: pk += 1 # if batch_nr == 37: print('%8d %s' % (pk,w)) cursor = jdbc.execute( "INSERT INTO {0} (ID,STR_VALUE) VALUES(?,?)".format( TABLE_NAME), [pk, w], cursor=cursor) else: for w in word_list: pk += 1 # if batch_nr == 37: print('%8d %s' % (pk,w)) cursor = jdbc.execute( "INSERT INTO %s (ID,STR_VALUE) VALUES(%d,'%s')" % (TABLE_NAME, pk, w.replace("'", "''")), cursor=cursor) jdbc.commit(cursor) print('%4d. done ascii = %4d, other = %4d.' % (batch_nr, n_ascii, n_other)) return n_ascii, n_other
def commit(jdbc: lwetl.Jdbc, cursor, mode, row_count, tot_count): if mode == lwetl.UPLOAD_MODE_COMMIT: jdbc.commit(cursor) n_rollback = 0 else: jdbc.rollback(cursor) n_rollback = row_count print('%s for %3d rows (%6d total)' % (mode.upper(), row_count, tot_count)) return n_rollback
def create_target_table(jdbc: Jdbc): """ Creates the target table if it dows not exist @param jdbc: target database connection @return: True if created, False otherwise """ try: jdbc.execute(SQL_EXISTS) created = False except SQLExcecuteException: if jdbc.type not in CREATE_TABLE: raise LookupError('Database type %s not in known list: %s' % (jdbc.type, ', '.join(CREATE_TABLE.keys()))) print('CREATING TABLE:' + TARGET_TABLE) jdbc.execute(CREATE_TABLE[jdbc.type].format(TARGET_TABLE)) jdbc.commit() created = True return created
args = parser.parse_args() login = args.login jdbc = Jdbc(login) try: id_start = jdbc.get_int("SELECT MAX(ID) FROM {0}".format(TABLE_NAME)) print('Start inserting with: %d' % id_start) except SQLExcecuteException: if jdbc.type not in CREATE_TABLE: raise LookupError('Database type %s not in known list: %s' % (jdbc.type, ', '.join(CREATE_TABLE.keys()))) print('CREATING TABLE:' + TABLE_NAME) jdbc.execute(CREATE_TABLE[jdbc.type].format(TABLE_NAME)) jdbc.commit() id_start = 0 batch_size = args.batch_size n_switch = args.n_switch use_parameters = args.parameters nr_of_threads = args.threads if nr_of_threads > 0: pool = multiprocessing.Pool(args.threads) mngr = multiprocessing.Manager() lock = mngr.Lock() results = dict() batch_nr = 0 word_list = []
sequences[t] = Sequence(name, value) break # Now do the job for t, c in jdbc.query(SQL_LIST_TABLES_COLUMNS): if t not in sequences: continue sql = 'SELECT MAX({}) FROM {}'.format(c, t) try: max_value = jdbc.get_int(sql) if max_value > sequences[t].value: sname = sequences[t].name print('Updating {:_<30s} from {:8d} to {:8d} '.format(sname, sequences[t].value, max_value + 1)) nextv = 'SELECT {}.NEXTVAL FROM DUAL;'.format(sname) cursor = None for sql in [ 'DROP SEQUENCE {};'.format(sname), 'CREATE SEQUENCE {} START WITH 1 MINVALUE 1 INCREMENT BY {};'.format(sname, max_value), nextv, nextv, 'ALTER SEQUENCE {} INCREMENT BY 1;'.format(sname), nextv]: cursor = jdbc.execute(sql, cursor=cursor) if args.activate: jdbc.commit(cursor) else: jdbc.rollback(cursor) except SQLExcecuteException as se: print('ERROR with {}: {}'.format(sql, str(se))) print('IGNORED: {}'.sname)