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