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