]> git.phdru.name Git - sqlconvert.git/blobdiff - scripts/mysql-to-sql.py
Add script mysql-to-sql.py
[sqlconvert.git] / scripts / mysql-to-sql.py
diff --git a/scripts/mysql-to-sql.py b/scripts/mysql-to-sql.py
new file mode 100755 (executable)
index 0000000..add0a8c
--- /dev/null
@@ -0,0 +1,48 @@
+#! /usr/bin/env python
+
+import argparse
+import sys
+
+from mysql2sql.print_tokens import print_tokens
+from mysql2sql.process_tokens import requote_names, StatementGrouper
+
+
+def main(infile, outfile):
+    grouper = StatementGrouper()
+    for line in infile:
+        grouper.process_line(line)
+        if grouper.statements:
+            for statement in grouper.get_statements():
+                requote_names(statement)
+                print_tokens(statement, outfile=outfile)
+    tokens = grouper.close()
+    if tokens:
+        for token in tokens:
+            print_tokens(token, outfile=outfile)
+
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser(description='Convert MySQL to SQL')
+    parser.add_argument('-i', '--infile', help='input file name')
+    parser.add_argument('-o', '--outfile', help='output file name')
+    args = parser.parse_args()
+
+    if args.infile:
+        infile = open(args.infile, 'rt')
+    else:
+        infile = sys.stdin
+        if infile.isatty():
+            parser.print_help()
+            sys.exit()
+
+    if args.outfile:
+        try:
+            outfile = open(args.outfile, 'wt')
+        except:
+            if infile is not sys.stdin:
+                infile.close()
+            raise
+    else:
+        outfile = sys.stdout
+
+    main(infile, outfile)