The day after I hyped the Getting Started with PyParsing book, I got to use it. Here’s the script :
from pyparsing import SkipTo, Suppress, CaselessLiteral import glob # Example to match # delete from TABLENAME # where CUSTOMER_ID = 'INTERNAL'; table_name = SkipTo("where") where_clause = SkipTo(';') delete_stmt = Suppress("delete") + Suppress("from") + table_name + Suppress("where") + where_clause + Suppress(";") for filename in glob.glob('*.sql'): f = open(filename) print '-- ', filename, ':' lines=f.read() for tokens, start,end in delete_stmt.scanString(lines): print 'select * from ' + tokens + ' where ' + tokens + ';' f.close()
This loops over all the files called *.sql in the current directory. Then it checks if there are any matches to the delete_stmt variable. So it has to match the literal “delete” and then the literal “from”. These are not put in the output, because they are in a Suppress() object.
After that we select everything up to ‘where’ as the table_name. Then the literal “where” has to be present. Everything up to the semicolon is then read into the where_clause variable.
Lastly the table name and where clause are used to create a select statement.
PS : The code was changed so that this
from pyparsing import *
from pyparsing import SkipTo, Suppress, CaselessLiteral
This way we don’t pollute the current namespace.