X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;ds=sidebyside;f=scripts%2Fmysql2sql;h=98c5f10e6eeee216120facaa3b53250ab47decdb;hb=ba77a72dda60df0d20f8493559382b8f756c61ba;hp=23197ffce6b2cbe7b312d7ab7b0f60d02ba7a7c2;hpb=06c8b16824855cf8fa4a92abc1eb0b8580079124;p=sqlconvert.git diff --git a/scripts/mysql2sql b/scripts/mysql2sql index 23197ff..98c5f10 100755 --- a/scripts/mysql2sql +++ b/scripts/mysql2sql @@ -3,21 +3,61 @@ from __future__ import print_function import argparse from io import open +import os import sys -from mysql2sql.print_tokens import print_tokens -from mysql2sql.process_tokens import requote_names, StatementGrouper +from sqlparse.compat import text_type +from sqlconvert.print_tokens import print_tokens +from sqlconvert.process_mysql import is_directive_statement, process_statement +from sqlconvert.process_tokens import is_newline_statement, StatementGrouper from m_lib.defenc import default_encoding +from m_lib.pbar.tty_pbar import ttyProgressBar -def main(infile, encoding, outfile, output_encoding): +def get_fsize(fp): + try: + fp.seek(0, os.SEEK_END) + except IOError: + return None # File size is unknown + size = fp.tell() + fp.seek(0, os.SEEK_SET) + return size + + +def main(infile, encoding, outfile, output_encoding, use_pbar): + if use_pbar: + size = get_fsize(infile) + if size is None: + use_pbar = False + + print("Converting: ", end='', file=sys.stderr) + sys.stderr.flush() + + if use_pbar: + pbar = ttyProgressBar(0, size-1) + cur_pos = 0 + grouper = StatementGrouper(encoding=encoding) + got_directive = False for line in infile: + if use_pbar: + if isinstance(line, text_type): + cur_pos += len(line.encode(encoding)) + else: + cur_pos += len(line) + pbar.display(cur_pos) grouper.process_line(line) if grouper.statements: for statement in grouper.get_statements(): - requote_names(statement) + if got_directive and is_newline_statement(statement): + # Condense a sequence of newlines after a /*! directive */; + got_directive = False + continue + got_directive = is_directive_statement(statement) + if got_directive: + continue + process_statement(statement) print_tokens(statement, outfile=outfile, encoding=output_encoding) tokens = grouper.close() @@ -25,6 +65,9 @@ def main(infile, encoding, outfile, output_encoding): for token in tokens: print_tokens(token, outfile=outfile, encoding=output_encoding) + if use_pbar: + pbar.erase() + print("done.") if __name__ == '__main__': parser = argparse.ArgumentParser(description='Convert MySQL to SQL') @@ -35,6 +78,8 @@ if __name__ == '__main__': 'as -e except for console; for console output ' 'charset from the current locale is used') parser.add_argument('-o', '--outfile', help='output file name') + parser.add_argument('-P', '--no-pbar', action='store_true', + help='inhibit progress bar') parser.add_argument('infile', help='input file name') parser.add_argument('output_file', nargs='?', help='output file name') args = parser.parse_args() @@ -83,4 +128,4 @@ if __name__ == '__main__': infile.close() raise - main(infile, args.encoding, outfile, output_encoding) + main(infile, args.encoding, outfile, output_encoding, not args.no_pbar)