'Take in input a query string and return its processed nulled,
casted and concatenated query string.
Examples:
MySQL input:  SELECT user, password FROM mysql.user
MySQL output: CONCAT(\'mMvPxc\',IFNULL(CAST(user AS CHAR(10000)), \' \'),\'nXlgnR\',IFNULL(CAST(password AS CHAR(10000)), \' \'),\'YnCzLl\') FROM mysql.user
PostgreSQL input:  SELECT usename, passwd FROM pg_shadow
PostgreSQL output: \'HsYIBS\'||COALESCE(CAST(usename AS CHARACTER(10000)), \' \')||\'KTBfZp\'||COALESCE(CAST(passwd AS CHARACTER(10000)), \' \')||\'LkhmuP\' FROM pg_shadow
Oracle input:  SELECT COLUMN_NAME, DATA_TYPE FROM SYS.ALL_TAB_COLUMNS WHERE TABLE_NAME=\'USERS\'
Oracle output: \'GdBRAo\'||NVL(CAST(COLUMN_NAME AS VARCHAR(4000)), \' \')||\'czEHOf\'||NVL(CAST(DATA_TYPE AS VARCHAR(4000)), \' \')||\'JVlYgS\' FROM SYS.ALL_TAB_COLUMNS WHERE TABLE_NAME=\'USERS\'
Microsoft SQL Server input:  SELECT name, master.dbo.fn_varbintohexstr(password) FROM master..sysxlogins
Microsoft SQL Server output: \'QQMQJO\'+ISNULL(CAST(name AS VARCHAR(8000)), \' \')+\'kAtlqH\'+ISNULL(CAST(master.dbo.fn_varbintohexstr(password) AS VARCHAR(8000)), \' \')+\'lpEqoi\' FROM master..sysxlogins
@param query: query string to be processed
@type query: C{str}
@return: query string nulled, casted and concatenated
@rtype: C{str}'

def concatQuery(self, query, unpack=True):

    if unpack:
        concatenatedQuery = ''
        query = query.replace(',    ', ',')
        (fieldsSelectFrom, fieldsSelect, fieldsNoSelect, fieldsSelectTop, fieldsSelectCase, _, fieldsToCastStr, fieldsExists) = self.getFields(query)
        castedFields = self.nullCastConcatFields(fieldsToCastStr)
        concatenatedQuery = query.replace(fieldsToCastStr, castedFields, 1)
    else:
        return query
    if Backend.isDbms(DBMS.MYSQL):
        if fieldsExists:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("CONCAT('%s'," % kb.chars.start), 1)
            concatenatedQuery += (",'%s')" % kb.chars.stop)
        elif fieldsSelectCase:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("CONCAT('%s'," % kb.chars.start), 1)
            concatenatedQuery += (",'%s')" % kb.chars.stop)
        elif fieldsSelectFrom:
            _ = unArrayizeValue(zeroDepthSearch(concatenatedQuery, '    FROM    '))
            concatenatedQuery = ("%s,'%s')%s" % (concatenatedQuery[:_].replace('SELECT    ', ("CONCAT('%s'," % kb.chars.start), 1), kb.chars.stop, concatenatedQuery[_:]))
        elif fieldsSelect:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("CONCAT('%s'," % kb.chars.start), 1)
            concatenatedQuery += (",'%s')" % kb.chars.stop)
        elif fieldsNoSelect:
            concatenatedQuery = ("CONCAT('%s',%s,'%s')" % (kb.chars.start, concatenatedQuery, kb.chars.stop))
    elif (Backend.getIdentifiedDbms() in (DBMS.PGSQL, DBMS.ORACLE, DBMS.SQLITE, DBMS.DB2, DBMS.FIREBIRD, DBMS.HSQLDB)):
        if fieldsExists:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("'%s'||" % kb.chars.start), 1)
            concatenatedQuery += ("||'%s'" % kb.chars.stop)
        elif fieldsSelectCase:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("'%s'||(SELECT    " % kb.chars.start), 1)
            concatenatedQuery += (")||'%s'" % kb.chars.stop)
        elif fieldsSelectFrom:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("'%s'||" % kb.chars.start), 1)
            _ = unArrayizeValue(zeroDepthSearch(concatenatedQuery, '    FROM    '))
            concatenatedQuery = ("%s||'%s'%s" % (concatenatedQuery[:_], kb.chars.stop, concatenatedQuery[_:]))
            concatenatedQuery = re.sub(("('%s'\\|\\|)(.+)(%s)" % (kb.chars.start, re.escape(castedFields))), '\\g<2>\\g<1>\\g<3>', concatenatedQuery)
        elif fieldsSelect:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("'%s'||" % kb.chars.start), 1)
            concatenatedQuery += ("||'%s'" % kb.chars.stop)
        elif fieldsNoSelect:
            concatenatedQuery = ("'%s'||%s||'%s'" % (kb.chars.start, concatenatedQuery, kb.chars.stop))
    elif (Backend.getIdentifiedDbms() in (DBMS.MSSQL, DBMS.SYBASE)):
        if fieldsExists:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("'%s'+" % kb.chars.start), 1)
            concatenatedQuery += ("+'%s'" % kb.chars.stop)
        elif fieldsSelectTop:
            topNum = re.search('\\ASELECT\\s+TOP\\s+([\\d]+)\\s+', concatenatedQuery, re.I).group(1)
            concatenatedQuery = concatenatedQuery.replace(('SELECT    TOP    %s    ' % topNum), ("TOP    %s    '%s'+" % (topNum, kb.chars.start)), 1)
            concatenatedQuery = concatenatedQuery.replace('    FROM    ', ("+'%s'    FROM    " % kb.chars.stop), 1)
        elif fieldsSelectCase:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("'%s'+" % kb.chars.start), 1)
            concatenatedQuery += ("+'%s'" % kb.chars.stop)
        elif fieldsSelectFrom:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("'%s'+" % kb.chars.start), 1)
            _ = unArrayizeValue(zeroDepthSearch(concatenatedQuery, '    FROM    '))
            concatenatedQuery = ("%s+'%s'%s" % (concatenatedQuery[:_], kb.chars.stop, concatenatedQuery[_:]))
        elif fieldsSelect:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("'%s'+" % kb.chars.start), 1)
            concatenatedQuery += ("+'%s'" % kb.chars.stop)
        elif fieldsNoSelect:
            concatenatedQuery = ("'%s'+%s+'%s'" % (kb.chars.start, concatenatedQuery, kb.chars.stop))
    elif Backend.isDbms(DBMS.ACCESS):
        if fieldsExists:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("'%s'&" % kb.chars.start), 1)
            concatenatedQuery += ("&'%s'" % kb.chars.stop)
        elif fieldsSelectCase:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("'%s'&(SELECT    " % kb.chars.start), 1)
            concatenatedQuery += (")&'%s'" % kb.chars.stop)
        elif fieldsSelectFrom:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("'%s'&" % kb.chars.start), 1)
            _ = unArrayizeValue(zeroDepthSearch(concatenatedQuery, '    FROM    '))
            concatenatedQuery = ("%s&'%s'%s" % (concatenatedQuery[:_], kb.chars.stop, concatenatedQuery[_:]))
        elif fieldsSelect:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("'%s'&" % kb.chars.start), 1)
            concatenatedQuery += ("&'%s'" % kb.chars.stop)
        elif fieldsNoSelect:
            concatenatedQuery = ("'%s'&%s&'%s'" % (kb.chars.start, concatenatedQuery, kb.chars.stop))
    else:
        warnMsg = 'applying    generic    concatenation    (CONCAT)'
        singleTimeWarnMessage(warnMsg)
        if fieldsExists:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("CONCAT(CONCAT('%s'," % kb.chars.start), 1)
            concatenatedQuery += ("),'%s')" % kb.chars.stop)
        elif fieldsSelectCase:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("CONCAT(CONCAT('%s'," % kb.chars.start), 1)
            concatenatedQuery += ("),'%s')" % kb.chars.stop)
        elif fieldsSelectFrom:
            _ = unArrayizeValue(zeroDepthSearch(concatenatedQuery, '    FROM    '))
            concatenatedQuery = ("%s),'%s')%s" % (concatenatedQuery[:_].replace('SELECT    ', ("CONCAT(CONCAT('%s'," % kb.chars.start), 1), kb.chars.stop, concatenatedQuery[_:]))
        elif fieldsSelect:
            concatenatedQuery = concatenatedQuery.replace('SELECT    ', ("CONCAT(CONCAT('%s'," % kb.chars.start), 1)
            concatenatedQuery += ("),'%s')" % kb.chars.stop)
        elif fieldsNoSelect:
            concatenatedQuery = ("CONCAT(CONCAT('%s',%s),'%s')" % (kb.chars.start, concatenatedQuery, kb.chars.stop))
    return concatenatedQuery
