You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to read a POSTGRESQL dump file with INSERTs statements and convert it to COPY statements
I would like to know if this package could help me with this?
#!/usr/bin/env python3importsysimportsqlparsefromsqlparse.sqlimportIdentifier, Function, Valuesfromsqlparse.tokensimportDML, Keyworddefmain():
iflen(sys.argv) !=3:
print('Uso: python pg_dump_converter.py entrada.sql saida.sql')
returninput_file_path=sys.argv[1]
output_file_path=sys.argv[2]
try:
withopen(input_file_path, 'r', encoding='utf-8') asinput_file, \
open(output_file_path, 'w', encoding='utf-8') asoutput_file:
process_file(input_file, output_file)
exceptFileNotFoundError:
print('O arquivo de entrada não existe.')
returndefprocess_file(input_file, output_file):
statements=sqlparse.parsestream(input_file)
current_table=Nonecolumns=Nonevalues_list= []
forstatementinstatements:
print(f'statement: {statement}')
ifstatement.get_type() =='INSERT':
table_name, cols, values=process_insert_statement(statement)
ifcurrent_table!=table_nameorcolumns!=cols:
# Escreve os dados acumulados anterioresifcurrent_tableandvalues_list:
write_copy_statement(output_file, current_table, columns, values_list)
values_list= []
current_table=table_namecolumns=colsvalues_list.extend(values)
else:
# Escreve os dados acumulados antes de processar outras instruçõesifcurrent_tableandvalues_list:
write_copy_statement(output_file, current_table, columns, values_list)
values_list= []
current_table=Nonecolumns=Noneoutput_file.write(str(statement).strip() +'\n')
# Escreve quaisquer dados restantesifcurrent_tableandvalues_list:
write_copy_statement(output_file, current_table, columns, values_list)
defprocess_insert_statement(statement):
tokens=statement.tokenstable_name=''columns= []
values= []
idx=0whileidx<len(tokens):
token=tokens[idx]
iftoken.ttypeisDMLandtoken.value.upper() =='INSERT':
idx+=1# Avança para o próximo tokencontinueelifisinstance(token, Identifier):
table_name=token.get_name()
eliftoken.ttypeisKeywordandtoken.value.upper() =='VALUES':
# Coleta os valoresifidx+1<len(tokens) andisinstance(tokens[idx+1], Values):
values.extend(parse_values(tokens[idx+1]))
idx+=1# Pula o token 'Values'elifisinstance(token, sqlparse.sql.Parenthesis):
# Pode ser a lista de colunas ou valoresifnotcolumns:
# Assume que é a lista de colunascolumns= [str(id).strip('"') foridintoken.get_identifiers()]
else:
# Valores adicionaisvalues.extend(parse_values(token))
idx+=1returntable_name, columns, valuesdefparse_values(token):
values= []
ifisinstance(token, Values):
forparenthesisintoken.get_sublists():
values.append(parse_value_list(parenthesis))
elifisinstance(token, sqlparse.sql.Parenthesis):
values.append(parse_value_list(token))
returnvaluesdefparse_value_list(parenthesis):
value_list= []
fortokeninparenthesis.tokens:
ifisinstance(token, sqlparse.sql.IdentifierList):
foridintoken.get_identifiers():
value_list.append(process_value(id))
elifnottoken.is_whitespaceandtoken.ttype!=sqlparse.tokens.Punctuation:
value_list.append(process_value(token))
returnvalue_listdefprocess_value(token):
value=token.valuevalue=value.strip()
ifvalue.upper() =='NULL':
return'\\N'elifvalue.startswith("'") andvalue.endswith("'"):
# Remove as aspas e trata caracteres especiaisvalue=value[1:-1].replace("''", "'")
value=value.replace("\\", "\\\\").replace("\n", "\\n").replace("\r", "\\r")
returnvalueelse:
returnvaluedefwrite_copy_statement(output_file, table_name, columns, values_list):
output_file.write(f'COPY {table_name} ({", ".join(columns)}) FROM stdin;\n')
forvaluesinvalues_list:
values_line='\t'.join(values)
output_file.write(values_line+'\n')
output_file.write('\\.\n')
if__name__=='__main__':
try:
importsqlparseexceptImportError:
print('A biblioteca sqlparse é necessária. Instale-a usando "pip install sqlparse".')
else:
main()
I'm trying to read a POSTGRESQL dump file with INSERTs statements and convert it to COPY statements
I would like to know if this package could help me with this?
like
input
output
The text was updated successfully, but these errors were encountered: