]> git.phdru.name Git - sqlconvert.git/blob - scripts/mysql2sql
Condense a sequence of newlines after a /*! directive */;
[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         if grouper.statements:
52             for statement in grouper.get_statements():
53                 if got_directive and is_newline_statement(statement):
54                     # Condense a sequence of newlines after a /*! directive */;
55                     got_directive = False
56                     continue
57                 got_directive = is_directive_statement(statement)
58                 if got_directive:
59                     continue
60                 process_statement(statement)
61                 print_tokens(statement, outfile=outfile,
62                              encoding=output_encoding)
63     tokens = grouper.close()
64     if tokens:
65         for token in tokens:
66             print_tokens(token, outfile=outfile, encoding=output_encoding)
67
68     if use_pbar:
69         pbar.erase()
70     print("done.")
71
72 if __name__ == '__main__':
73     parser = argparse.ArgumentParser(description='Convert MySQL to SQL')
74     parser.add_argument('-e', '--encoding', default='utf-8',
75                         help='input/output encoding, default is utf-8')
76     parser.add_argument('-E', '--output-encoding',
77                         help='separate output encoding, default is the same '
78                         'as -e except for console; for console output '
79                         'charset from the current locale is used')
80     parser.add_argument('-o', '--outfile', help='output file name')
81     parser.add_argument('-P', '--no-pbar', action='store_true',
82                         help='inhibit progress bar')
83     parser.add_argument('infile', help='input file name')
84     parser.add_argument('output_file', nargs='?', help='output file name')
85     args = parser.parse_args()
86
87     if args.infile:
88         if args.infile == '-':
89             infile = sys.stdin
90         else:
91             infile = open(args.infile, 'rt', encoding=args.encoding)
92     else:
93         infile = sys.stdin
94
95     if infile.isatty():
96         print("Error: cannot read from console", file=sys.stderr)
97         parser.print_help()
98         sys.exit(1)
99
100     if args.outfile:
101         if args.output_file:
102             print("Error: too many output files", file=sys.stderr)
103             parser.print_help()
104             sys.exit(1)
105
106         outfile = args.outfile
107
108     elif args.output_file:
109         outfile = args.output_file
110
111     else:
112         outfile = '-'
113
114     if args.output_encoding:
115         output_encoding = args.output_encoding
116     elif outfile == '-':
117         output_encoding = default_encoding
118     else:
119         output_encoding = args.encoding
120
121     if outfile == '-':
122         outfile = sys.stdout
123     else:
124         try:
125             outfile = open(outfile, 'wt', encoding=output_encoding)
126         except:
127             if infile is not sys.stdin:
128                 infile.close()
129             raise
130
131     main(infile, args.encoding, outfile, output_encoding, not args.no_pbar)