]> git.phdru.name Git - sqlconvert.git/blobdiff - scripts/mysql-to-sql.py
Use encoding (default is utf-8) and unicode
[sqlconvert.git] / scripts / mysql-to-sql.py
index c40563fed0e7d849554eac67358026789bbc090f..23197ffce6b2cbe7b312d7ab7b0f60d02ba7a7c2 100755 (executable)
@@ -2,28 +2,38 @@
 from __future__ import print_function
 
 import argparse
+from io import open
 import sys
 
 from mysql2sql.print_tokens import print_tokens
 from mysql2sql.process_tokens import requote_names, StatementGrouper
 
+from m_lib.defenc import default_encoding
 
-def main(infile, outfile):
-    grouper = StatementGrouper()
+
+def main(infile, encoding, outfile, output_encoding):
+    grouper = StatementGrouper(encoding=encoding)
     for line in infile:
         grouper.process_line(line)
         if grouper.statements:
             for statement in grouper.get_statements():
                 requote_names(statement)
-                print_tokens(statement, outfile=outfile)
+                print_tokens(statement, outfile=outfile,
+                             encoding=output_encoding)
     tokens = grouper.close()
     if tokens:
         for token in tokens:
-            print_tokens(token, outfile=outfile)
+            print_tokens(token, outfile=outfile, encoding=output_encoding)
 
 
 if __name__ == '__main__':
     parser = argparse.ArgumentParser(description='Convert MySQL to SQL')
+    parser.add_argument('-e', '--encoding', default='utf-8',
+                        help='input/output encoding, default is utf-8')
+    parser.add_argument('-E', '--output-encoding',
+                        help='separate output encoding, default is the same '
+                        '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('infile', help='input file name')
     parser.add_argument('output_file', nargs='?', help='output file name')
@@ -33,7 +43,7 @@ if __name__ == '__main__':
         if args.infile == '-':
             infile = sys.stdin
         else:
-            infile = open(args.infile, 'rt')
+            infile = open(args.infile, 'rt', encoding=args.encoding)
     else:
         infile = sys.stdin
 
@@ -56,14 +66,21 @@ if __name__ == '__main__':
     else:
         outfile = '-'
 
+    if args.output_encoding:
+        output_encoding = args.output_encoding
+    elif outfile == '-':
+        output_encoding = default_encoding
+    else:
+        output_encoding = args.encoding
+
     if outfile == '-':
         outfile = sys.stdout
     else:
         try:
-            outfile = open(outfile, 'wt')
+            outfile = open(outfile, 'wt', encoding=output_encoding)
         except:
             if infile is not sys.stdin:
                 infile.close()
             raise
 
-    main(infile, outfile)
+    main(infile, args.encoding, outfile, output_encoding)