import sys
import re
__version__ = "0.2"
def extract_sqls(filenames, for_explain=False):
for filename in filenames:
extract_sql(filename, for_explain)
def extract_sql(filename, for_explain=False):
'''Extract sql statments from the file and print them to std.'''
f = None
try:
f = file(filename)
except IOError:
print '%s: %s: No such file' % (sys.argv[0], filename)
return
print '\n%s> %s <%s\n' % ('=' * 25, filename, '=' * 25)
sql_set = set()
while True:
line = f.readline()
if len(line) == 0:
break
explain_sql_pres = ['(^| )(SELECT )', '(^| )(UPDATE )', '(^| )(DELETE )']
all_sql_pres = explain_sql_pres + ['(^| )(INSERT INTO )']
sql_pres = explain_sql_pres if for_explain is True else all_sql_pres
for sql_pre in sql_pres:
m = re.search(sql_pre, line, re.I)
if m is not None:
break
if m is None:
continue
raw_sql = line[m.start(2):-1]
base_sql = re.sub("'.*'|\d+", '?', raw_sql)
if for_explain is True:
if base_sql not in sql_set:
sql_set.add(base_sql)
print_sql(raw_sql)
else:
print_sql(raw_sql)
f.close()
print
def print_sql(sql):
if sql[-1] == ';':
print '%s' % sql
else:
print '%s;' % sql
if len(sys.argv) < 2:
print '''\
%s: too few arguments
Try `%s --help\' for more information.''' % (sys.argv[0], sys.argv[0])
sys.exit()
if sys.argv[1].startswith('--'):
option = sys.argv[1][2:]
if option == 'for-explain':
extract_sqls(sys.argv[2:], True)
elif option == 'version':
print 'sql_extractor', __version__
sys.exit()
elif option == 'help':
print '''
Usage: %s [OPTION] FILE...
Extract sql statments from file then print them to console.
Options include:
--for-explain Only print one sql for which with different parameters
--version Prints the version number
--help Display this help''' % sys.argv[0]
else:
print '''\
%s: invalid option -- %s
Try `%s --help\' for more information.''' % (sys.argv[0], option, sys.argv[0])
sys.exit()
else:
extract_sqls(sys.argv[1:])