# HG changeset patch # User jjohnson # Date 1443970341 14400 # Node ID ffa5e34a55c1013aceb6b04428fbc70795c0a28a # Parent 24f0911f75eec478af2fb34ec508bd253289698a Uploaded diff -r 24f0911f75ee -r ffa5e34a55c1 query_tabular.py --- a/query_tabular.py Sat Oct 03 09:23:08 2015 -0400 +++ b/query_tabular.py Sun Oct 04 10:52:21 2015 -0400 @@ -111,6 +111,7 @@ parser.add_option( '-s', '--sqlitedb', dest='sqlitedb', default=None, help='The SQLite Database' ) parser.add_option( '-t', '--table', dest='tables', action="append", default=[], help='Tabular file: file_path[=table_name[:column_name,...]' ) parser.add_option( '-q', '--query', dest='query', default=None, help='SQL query' ) + parser.add_option( '-Q', '--query_file', dest='query_file', default=None, help='SQL query file' ) parser.add_option( '-n', '--no_header', dest='no_header', action='store_true', default=False, help='Include a column headers line' ) parser.add_option( '-o', '--output', dest='output', default=None, help='Output file for query results' ) (options, args) = parser.parse_args() @@ -143,7 +144,16 @@ create_table(conn,path,table_name,column_names=column_names) conn.close() - if (options.query is None): + query = None + if (options.query_file != None): + with open(options.query_file,'r') as fh: + query = '' + for line in fh: + query += line + elif (options.query != None): + query = options.query + + if (query is None): try: conn = sqlite.connect(options.sqlitedb) c = conn.cursor() @@ -161,13 +171,13 @@ except Exception, exc: print >> sys.stderr, "Error: %s" % exc exit(0) - #if not sqlite.is_read_only_query(options.query): + #if not sqlite.is_read_only_query(query): # print >> sys.stderr, "Error: Must be a read only query" # exit(2) try: conn = sqlite.connect(options.sqlitedb) cur = conn.cursor() - results = cur.execute(options.query) + results = cur.execute(query) if not options.no_header: outputFile.write("#%s\n" % '\t'.join([str(col[0]) for col in cur.description])) # yield [col[0] for col in cur.description] diff -r 24f0911f75ee -r ffa5e34a55c1 query_tabular.xml --- a/query_tabular.xml Sat Oct 03 09:23:08 2015 -0400 +++ b/query_tabular.xml Sun Oct 04 10:52:21 2015 -0400 @@ -6,7 +6,11 @@ - + + +$sqlquery + + ^([A-Za-z]\w*)?(,([A-Za-z]\w*)?)*$ - + By default, tables will be named: t1,t2,...,tn ^(?i)\s*select\s+.*\s+from\s+.*$ @@ -60,10 +64,10 @@ - save_db or not (query and len(query) > 0) + save_db or not (sqlquery and len(sqlquery) > 0) - query and len(query) > 0 + sqlquery and len(sqlquery) > 0 @@ -79,7 +83,7 @@ - + @@ -91,7 +95,7 @@ - + @@ -106,7 +110,7 @@ - +