]> git.phdru.name Git - sqlconvert.git/blob - scripts/mysql2sql
Fix mysql2sql: args.pg
[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, quoting_style):
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             for statement in process_statement(statement, quoting_style):
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('-m', '--mysql', action='store_true',
80                         help='MySQL/MariaDB quoting style')
81     parser.add_argument('-p', '--pg', '--postgres', action='store_true',
82                         help='PostgreSQL quoting style')
83     parser.add_argument('-s', '--sqlite', action='store_true',
84                         help='Generic SQL/SQLite quoting style; '
85                         'this is the default')
86     parser.add_argument('-o', '--outfile', help='output file name')
87     parser.add_argument('-P', '--no-pbar', action='store_true',
88                         help='inhibit progress bar')
89     parser.add_argument('infile', help='input file name')
90     parser.add_argument('output_file', nargs='?', help='output file name')
91     args = parser.parse_args()
92
93     if int(args.mysql) + int(args.pg) + int(args.sqlite) > 1:
94         print("Error: options -m/-p/-s are mutually incompatible, "
95               "use only one of them",
96               file=sys.stderr)
97         parser.print_help()
98         sys.exit(1)
99
100     if args.infile:
101         if args.infile == '-':
102             infile = sys.stdin
103         else:
104             infile = open(args.infile, 'rt', encoding=args.encoding)
105     else:
106         infile = sys.stdin
107
108     if infile.isatty():
109         print("Error: cannot read from console", file=sys.stderr)
110         parser.print_help()
111         sys.exit(1)
112
113     if args.outfile:
114         if args.output_file:
115             print("Error: too many output files", file=sys.stderr)
116             parser.print_help()
117             sys.exit(1)
118
119         outfile = args.outfile
120
121     elif args.output_file:
122         outfile = args.output_file
123
124     else:
125         outfile = '-'
126
127     if args.output_encoding:
128         output_encoding = args.output_encoding
129     elif outfile == '-':
130         output_encoding = default_encoding
131     else:
132         output_encoding = args.encoding
133
134     if outfile == '-':
135         outfile = sys.stdout
136     else:
137         try:
138             outfile = open(outfile, 'wt', encoding=output_encoding)
139         except:
140             if infile is not sys.stdin:
141                 infile.close()
142             raise
143
144     if args.mysql:
145         quoting_style = 'mysql'
146     elif args.pg:
147         quoting_style = 'postgres'
148     else:
149         quoting_style = 'sqlite'
150
151     main(infile, args.encoding, outfile, output_encoding, not args.no_pbar,
152          quoting_style)