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