]> git.phdru.name Git - sqlconvert.git/blob - scripts/mysql-to-sql.py
Print error message
[sqlconvert.git] / scripts / mysql-to-sql.py
1 #! /usr/bin/env python
2 from __future__ import print_function
3
4 import argparse
5 import sys
6
7 from mysql2sql.print_tokens import print_tokens
8 from mysql2sql.process_tokens import requote_names, StatementGrouper
9
10
11 def main(infile, outfile):
12     grouper = StatementGrouper()
13     for line in infile:
14         grouper.process_line(line)
15         if grouper.statements:
16             for statement in grouper.get_statements():
17                 requote_names(statement)
18                 print_tokens(statement, outfile=outfile)
19     tokens = grouper.close()
20     if tokens:
21         for token in tokens:
22             print_tokens(token, outfile=outfile)
23
24
25 if __name__ == '__main__':
26     parser = argparse.ArgumentParser(description='Convert MySQL to SQL')
27     parser.add_argument('-i', '--infile', help='input file name')
28     parser.add_argument('-o', '--outfile', help='output file name')
29     args = parser.parse_args()
30
31     if args.infile:
32         infile = open(args.infile, 'rt')
33     else:
34         infile = sys.stdin
35         if infile.isatty():
36             print("Error: cannot input from console", file=sys.stderr)
37             parser.print_help()
38             sys.exit()
39
40     if args.outfile:
41         try:
42             outfile = open(args.outfile, 'wt')
43         except:
44             if infile is not sys.stdin:
45                 infile.close()
46             raise
47     else:
48         outfile = sys.stdout
49
50     main(infile, outfile)