@@ -87,6 +87,7 @@ default_formatting = get_option_with_default(p, 'string', 'formatting', None)
8787default_encoding = get_option_with_default (p , 'string' , 'encoding' , 'UTF-8' )
8888default_output_encoding = get_option_with_default (p , 'string' , 'encoding' , None )
8989default_query_encoding = get_option_with_default (p , 'string' , 'query_encoding' , locale .getpreferredencoding ())
90+ default_output_header = get_option_with_default (p , 'string' , 'output_header' , False )
9091
9192parser = OptionParser (usage = """
9293 q allows performing SQL-like statements on tabular text data.
@@ -130,6 +131,7 @@ parser.add_option("-T", "--tab-delimited-output", dest="tab_delimited_output", d
130131 help = "Same as -D <tab>. Just a shorthand for outputing tab delimited output. You can use -D $'\t ' if you want." )
131132parser .add_option ("-H" , "--skip-header" , dest = "skip_header" , default = default_skip_header , action = "store_true" ,
132133 help = "Skip header row. This has been changed from earlier version - Only one header row is supported, and the header row is used for column naming" )
134+ parser .add_option ("-O" , "--output-header" , dest = "output_header" , default = default_output_header , action = "store_true" ,help = "Output header line. Output column-names are determined from the query itself. Use column aliases in order to set your column names in the query. For example, 'select name FirstName,value1/value2 MyCalculation from ...'. This can be used even if there was no header in the input." )
133135parser .add_option ("-f" , "--formatting" , dest = "formatting" , default = default_formatting ,
134136 help = "Output-level formatting, in the format X=fmt,Y=fmt etc, where X,Y are output column numbers (e.g. 1 for first SELECT column etc." )
135137parser .add_option ("-e" , "--encoding" , dest = "encoding" , default = default_encoding ,
@@ -155,6 +157,10 @@ parser.add_option("-k", "--keep-leading-whitespace", dest="keep_leading_whitespa
155157def regexp (regular_expression , data ):
156158 return re .search (regular_expression , data ) is not None
157159
160+ class Sqlite3DBResults (object ):
161+ def __init__ (self ,query_column_names ,results ):
162+ self .query_column_names = query_column_names
163+ self .results = results
158164
159165class Sqlite3DB (object ):
160166
@@ -184,12 +190,17 @@ class Sqlite3DB(object):
184190 def execute_and_fetch (self , q ):
185191 try :
186192 if self .show_sql :
187- print q
193+ print repr ( q )
188194 self .cursor .execute (q )
195+ if self .cursor .description is not None :
196+ # we decode the column names, so they can be encoded to any output format later on
197+ query_column_names = [c [0 ].decode ('utf-8' ) for c in self .cursor .description ]
198+ else :
199+ query_column_names = None
189200 result = self .cursor .fetchall ()
190201 finally :
191202 pass # cursor.close()
192- return result
203+ return Sqlite3DBResults ( query_column_names , result )
193204
194205 def _get_as_list_str (self , l ):
195206 return "," .join (['"%s"' % x .replace ('"' , '""' ) for x in l ])
@@ -299,6 +310,8 @@ class Sql(object):
299310 # names
300311 self .qtable_name_effective_table_names = {}
301312
313+ self .query_column_names = None
314+
302315 # Go over all sql parts
303316 idx = 0
304317 while idx < len (self .sql_parts ):
@@ -358,7 +371,8 @@ class Sql(object):
358371 return " " .join (effective_sql )
359372
360373 def execute_and_fetch (self , db ):
361- return db .execute_and_fetch (self .get_effective_sql ())
374+ db_results_obj = db .execute_and_fetch (self .get_effective_sql ())
375+ return db_results_obj
362376
363377
364378class LineSplitter (object ):
971985 sys .exit (0 )
972986
973987 # Execute the query and fetch the data
974- m = sql_object .execute_and_fetch (db )
988+ db_results_obj = sql_object .execute_and_fetch (db )
989+ m = db_results_obj .results
990+ output_column_name_list = db_results_obj .query_column_names
975991except EmptyDataException :
976992 print >> sys .stderr , "Warning - data is empty"
977993 sys .exit (0 )
@@ -1023,6 +1039,8 @@ else:
10231039 formatting_dict = None
10241040
10251041try :
1042+ if options .output_header and output_column_name_list is not None :
1043+ m .insert (0 ,output_column_name_list )
10261044 for rownum , row in enumerate (m ):
10271045 row_str = []
10281046 for i , col in enumerate (row ):
0 commit comments