def Print_Human_Readable()

in src/SimpleReplay/PreviousVersion/ParseUserActivityLog.py [0:0]


def Print_Human_Readable(queryObjList, creds, rewrites, readonly):
    creds_list = ['COPY ','UNLOAD ']
    rw_list = ['CREATE TABLE ','INSERT ','DELETE ','UPDATE ','DROP TABLE ','ALTER TABLE ','COPY ','UNLOAD ', 'VACUUM ','ANALYZE ', 'TRUNCATE ', 'GRANT ']
    rw_files = []
    skip_ends = [ 'BEGIN;', 'COMMIT;' ]
    skip_rewrites = [ 'SELECT ', 'UPDATE ', 'INSERT INTO ', 'DELETE FROM ', 'CREATE TEMP TABLE ','WITH ']
    avoid_skipping = [ 'SELECT *', ' BETWEEN ', 'LIKE ' ]
    dedupe_these = ['set ', 'select', 'create', 'delete', 'update', 'insert', 'SET ', 'SELECT', 'CREATE', 'DELETE', 'UPDATE', 'INSERT']
    for i in range(0, len(queryObjList), 1):
        if queryObjList[i].querystatement.endswith(tuple(skip_ends)):
           continue
        fname = queryObjList[i].databasename + '-' + queryObjList[i].username + '-' + queryObjList[i].pid + '.sql'
        rwname = 'rw-'+ fname
        if rewrites and ';' in queryObjList[i].querystatement and queryObjList[i].querystatement.count('\n') == 1 and queryObjList[i].querystatement.startswith(tuple(skip_rewrites)) and not any(word in queryObjList[i].querystatement for word in avoid_skipping):
           #print fname 
           #print queryObjList[i].querystatement 
           continue
        #
        if readonly and any(word.lower() in queryObjList[i].querystatement.lower() for word in rw_list):
           if fname not in rw_files:
              rw_files.append(fname)
           rwname = 'rw-'+ fname
           if os.path.isfile(fname):
             os.rename(fname,rwname)
        #
        if readonly and fname in rw_files:
          fname = rwname
        #
        firstTime = False
        if not os.path.isfile(fname):
          setUser = 'set session_authorization to ' + queryObjList[i].username + ';\n'
          firstTime = True 
        else:
          if  any(word in queryObjList[i].querystatement for word in dedupe_these):
             if queryObjList[i].querystatement.endswith(tuple(';')) or queryObjList[i].querystatement in open(fname).read():
                continue
        #
        f = open ( fname, 'a' )
        if firstTime:
          f.write ('--Starttime: ' + queryObjList[i].startdatetime + '\n')
          f.write (setUser)
        if  (creds and any(word in queryObjList[i].querystatement for word in creds_list)):
           f.write(queryObjList[i].querystatement.replace("CREDENTIALS ''","CREDENTIALS '"+ creds + "'") + ";\n" )
        else:
           f.write( queryObjList[i].querystatement + ";\n" )
        f.close()