]> git.phdru.name Git - sqlconvert.git/blob - scripts/mysql2sql
Do not test statements in StatementGrouper
[sqlconvert.git] / scripts / mysql2sql
1 #! /usr/bin/env python
2 from __future__ import print_function
3
4 import argparse
5 from io import open
6 import os
7 import sys
8
9 from sqlparse.compat import text_type
10 from sqlconvert.print_tokens import print_tokens
11 from sqlconvert.process_mysql import is_directive_statement, process_statement
12 from sqlconvert.process_tokens import is_newline_statement, StatementGrouper
13
14 from m_lib.defenc import default_encoding
15 from m_lib.pbar.tty_pbar import ttyProgressBar
16
17
18 def get_fsize(fp):
19     try:
20         fp.seek(0, os.SEEK_END)
21     except IOError:
22         return None  # File size is unknown
23     size = fp.tell()
24     fp.seek(0, os.SEEK_SET)
25     return size
26
27
28 def main(infile, encoding, outfile, output_encoding, use_pbar):
29     if use_pbar:
30         size = get_fsize(infile)
31         if size is None:
32             use_pbar = False
33
34     print("Converting: ", end='', file=sys.stderr)
35     sys.stderr.flush()
36
37     if use_pbar:
38         pbar = ttyProgressBar(0, size-1)
39         cur_pos = 0
40
41     grouper = StatementGrouper(encoding=encoding)
42     got_directive = False
43     for line in infile:
44         if use_pbar:
45             if isinstance(line, text_type):
46                 cur_pos += len(line.encode(encoding))
47             else:
48                 cur_pos += len(line)
49             pbar.display(cur_pos)
50         grouper.process_line(line)
51         for statement in grouper.get_statements():
52             if got_directive and is_newline_statement(statement):
53                 # Condense a sequence of newlines after a /*! directive */;
54                 got_directive = False
55                 continue
56             got_directive = is_directive_statement(statement)
57             if got_directive:
58                 continue
59             process_statement(statement)
60             print_tokens(statement, outfile=outfile,
61                          encoding=output_encoding)
62     tokens = grouper.close()
63     if tokens:
64         for token in tokens:
65             print_tokens(token, outfile=outfile, encoding=output_encoding)
66
67     if use_pbar:
68         pbar.erase()
69     print("done.")
70
71 if __name__ == '__main__':
72     parser = argparse.ArgumentParser(description='Convert MySQL to SQL')
73     parser.add_argument('-e', '--encoding', default='utf-8',
74                         help='input/output encoding, default is utf-8')
75     parser.add_argument('-E', '--output-encoding',
76                         help='separate output encoding, default is the same '
77                         'as -e except for console; for console output '
78                         'charset from the current locale is used')
79     parser.add_argument('-o', '--outfile', help='output file name')
80     parser.add_argument('-P', '--no-pbar', action='store_true',
81                         help='inhibit progress bar')
82     parser.add_argument('infile', help='input file name')
83     parser.add_argument('output_file', nargs='?', help='output file name')
84     args = parser.parse_args()
85
86     if args.infile:
87         if args.infile == '-':
88             infile = sys.stdin
89         else:
90             infile = open(args.infile, 'rt', encoding=args.encoding)
91     else:
92         infile = sys.stdin
93
94     if infile.isatty():
95         print("Error: cannot read from console", file=sys.stderr)
96         parser.print_help()
97         sys.exit(1)
98
99     if args.outfile:
100         if args.output_file:
101             print("Error: too many output files", file=sys.stderr)
102             parser.print_help()
103             sys.exit(1)
104
105         outfile = args.outfile
106
107     elif args.output_file:
108         outfile = args.output_file
109
110     else:
111         outfile = '-'
112
113     if args.output_encoding:
114         output_encoding = args.output_encoding
115     elif outfile == '-':
116         output_encoding = default_encoding
117     else:
118         output_encoding = args.encoding
119
120     if outfile == '-':
121         outfile = sys.stdout
122     else:
123         try:
124             outfile = open(outfile, 'wt', encoding=output_encoding)
125         except:
126             if infile is not sys.stdin:
127                 infile.close()
128             raise
129
130     main(infile, args.encoding, outfile, output_encoding, not args.no_pbar)