]> git.phdru.name Git - sqlconvert.git/blob - scripts/mysql-to-sql.py
Use encoding (default is utf-8) and unicode
[sqlconvert.git] / scripts / mysql-to-sql.py
1 #! /usr/bin/env python
2 from __future__ import print_function
3
4 import argparse
5 from io import open
6 import sys
7
8 from mysql2sql.print_tokens import print_tokens
9 from mysql2sql.process_tokens import requote_names, StatementGrouper
10
11 from m_lib.defenc import default_encoding
12
13
14 def main(infile, encoding, outfile, output_encoding):
15     grouper = StatementGrouper(encoding=encoding)
16     for line in infile:
17         grouper.process_line(line)
18         if grouper.statements:
19             for statement in grouper.get_statements():
20                 requote_names(statement)
21                 print_tokens(statement, outfile=outfile,
22                              encoding=output_encoding)
23     tokens = grouper.close()
24     if tokens:
25         for token in tokens:
26             print_tokens(token, outfile=outfile, encoding=output_encoding)
27
28
29 if __name__ == '__main__':
30     parser = argparse.ArgumentParser(description='Convert MySQL to SQL')
31     parser.add_argument('-e', '--encoding', default='utf-8',
32                         help='input/output encoding, default is utf-8')
33     parser.add_argument('-E', '--output-encoding',
34                         help='separate output encoding, default is the same '
35                         'as -e except for console; for console output '
36                         'charset from the current locale is used')
37     parser.add_argument('-o', '--outfile', help='output file name')
38     parser.add_argument('infile', help='input file name')
39     parser.add_argument('output_file', nargs='?', help='output file name')
40     args = parser.parse_args()
41
42     if args.infile:
43         if args.infile == '-':
44             infile = sys.stdin
45         else:
46             infile = open(args.infile, 'rt', encoding=args.encoding)
47     else:
48         infile = sys.stdin
49
50     if infile.isatty():
51         print("Error: cannot read from console", file=sys.stderr)
52         parser.print_help()
53         sys.exit(1)
54
55     if args.outfile:
56         if args.output_file:
57             print("Error: too many output files", file=sys.stderr)
58             parser.print_help()
59             sys.exit(1)
60
61         outfile = args.outfile
62
63     elif args.output_file:
64         outfile = args.output_file
65
66     else:
67         outfile = '-'
68
69     if args.output_encoding:
70         output_encoding = args.output_encoding
71     elif outfile == '-':
72         output_encoding = default_encoding
73     else:
74         output_encoding = args.encoding
75
76     if outfile == '-':
77         outfile = sys.stdout
78     else:
79         try:
80             outfile = open(outfile, 'wt', encoding=output_encoding)
81         except:
82             if infile is not sys.stdin:
83                 infile.close()
84             raise
85
86     main(infile, args.encoding, outfile, output_encoding)