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