'Process the query, modify it if necessary based on NO_SYNC_MAP rules
and execute it on the remote side.'

def execute_passive(self, query, params=None):

    global execute_sync
    if query.lower().startswith('select'):
        return
    try:
        from freenasUI.middleware.notifier import notifier
        if (not (hasattr(notifier, 'failover_status') and (notifier().failover_status() == 'MASTER'))):
            return
    except:
        return
    parse = sqlparse.parse(query)
    for p in parse:
        if (p.tokens[0].normalized not in ('DELETE', 'INSERT', 'UPDATE')):
            continue
        cparams = list(params)
        if (p.tokens[0].normalized == 'INSERT'):
            into = p.token_next_by(m=(sqlparse.tokens.Keyword, 'INTO'))
            if (not into):
                continue
            next_ = p.token_next(into[0])
            if (next_[1].value in NO_SYNC_MAP):
                continue
        elif (p.tokens[0].normalized == 'DELETE'):
            from_ = p.token_next_by(m=(sqlparse.tokens.Keyword, 'FROM'))
            if (not from_):
                continue
            next_ = p.token_next(from_[0])
            if (next_[1].value in NO_SYNC_MAP):
                continue
        elif (p.tokens[0].normalized == 'UPDATE'):
            name = p.token_next(0)[1].value
            no_sync = NO_SYNC_MAP.get(name)
            if ((no_sync is None) and (name in NO_SYNC_MAP)):
                continue
            set_ = p.token_next_by(m=(sqlparse.tokens.Keyword, 'SET'))
            if (not set_):
                continue
            next_ = p.token_next(set_[0])
            if (not next_):
                continue
            if (no_sync is None):
                lookup = []
            else:
                if ('fields' not in no_sync):
                    continue
                if issubclass(next_[1].__class__, sqlparse.sql.IdentifierList):
                    lookup = list(next_[1].get_sublists())
                elif issubclass(next_[1].__class__, sqlparse.sql.Comparison):
                    lookup = [next_[1]]
                placeholders = [a for a in p.flatten() if (a.value in ('%s', '?'))]
            delete_idx = []
            for l in lookup:
                if (l.value not in no_sync['fields']):
                    continue
                try:
                    idx = placeholders.index(l.tokens[(-1)])
                    if cparams:
                        delete_idx.append(idx)
                except ValueError:
                    pass
                t_index = l.parent.token_index(l)
                prev_ = l.parent.token_prev(t_index)
                next_ = l.parent.token_next(t_index)
                if (next_ and issubclass(next_[1].__class__, sqlparse.sql.Token) and (next_[1].value == ',')):
                    del l.parent.tokens[next_[0]]
                elif (prev_ and issubclass(prev_[1].__class__, sqlparse.sql.Token) and (prev_[1].value == ',')):
                    del l.parent.tokens[prev_[0]]
                del l.parent.tokens[l.parent.token_index(l)]
            delete_idx.sort(reverse=True)
            for i in delete_idx:
                del cparams[i]
        if (params is not None):
            sql = self.convert_query(str(p))
        else:
            sql = str(p)
        rsr = RunSQLRemote(sql=sql, params=cparams)
        rsr.start()
        if execute_sync:
            rsr.join()
