import sys
import sqlparse
from .checks import has_cross_join


def parse_file(filename):
    import json
    with open(filename, 'r') as fh:
        return json.load(fh)


CHECKS = (
    (has_cross_join, 'query contains cross join'),
)


def check_query(el):
    """
    Run each of the defined checks on a query.
    """
    stmt = sqlparse.parse(el)
    for check in CHECKS:
        if check[0](stmt[0]):
            return False, check[1]
    return True, ''


def main():
    argv = sys.argv
    try:
        queries = parse_file(argv[1])
    except IndexError:
        raise Exception('Filename required')
    failed = False
    for query, tests in queries.iteritems():
        passed, message = check_query(query)
        if not passed:
            failed = True
            print_message(message, tests, query)
    sys.exit(255 if failed else 0)


def print_message(message, tests, query):
    print "FAILED - %s" % (message)
    print "-----------------------------------------------------------------"
    print "Test Methods:"
    print "%s" % "\n".join(tests)
    print
    print "Query:"
    print "%s" % query

@@ -91,83 +91,45 @@
     
-import json%0A    with open(filename, 'r') as fh:%0A        return json.load(fh
+return open(filename, 'r').readlines(
 )%0A%0A%0A
@@ -599,23 +599,16 @@
 or query
-, tests
  in quer
@@ -614,20 +614,8 @@
 ries
-.iteritems()
 :%0A  
@@ -736,31 +736,24 @@
 age(message,
- tests,
  query)%0A    
@@ -815,15 +815,8 @@
 age,
- tests,
  que
@@ -938,68 +938,8 @@
 --%22%0A
-    print %22Test Methods:%22%0A    print %22%25s%22 %25 %22%5Cn%22.join(tests)%0A
     

Work with plain SQL files