def createRequest(self, fields):
        '''Creates request

        Creates the request with the given data in the persistence

        Args:
            fields: the data of the request

        Raises:
            PersistenceException: There is DB error
        '''
        try:
            mapping = {}
            requestTablename = 'PartnerRequests'
            requestColumns = ['request_id', 'study_id', 'query', 'expiration_time',
                              'cache_available', 'cache_timestamp', 'status']
            
            for i in range(len(fields)):
                mapping[requestColumns[i]] = fields[i]
            id = self.connection.incr(requestTablename + ":lastid")
            self.cursor.sadd(requestTablename, id)
            self.cursor.hmset(requestTablename + ":" + str(id), mapping)
            for i in range(len(fields)):
                # All columns are varchar
                self.cursor.sadd(requestTablename + ":" + requestColumns[i] + ":" + fields[i], id)
        except RedisError as e:
            raise PersistenceException(*e.args)
    def queryRequests(self, query, pageNumber, pageSize):
        '''Queries requests

        Queries the data according the the given filtering options, and returns the list of
        records (as lists) of data.

        Args:
            query: the query string
            pageNumber: the page number
            pageSize: the page size

        Returns:
            The list of records of data

        Raises:
            PersistenceException: There is DB error
        '''
        try:
            requestTablename = 'PartnerRequests'
            requestColumns = ['id', 'request_id', 'study_id', 'query', 'expiration_time',
                              'cache_available', 'cache_timestamp', 'status']
            columns = "`%s`" % "`, `".join(requestColumns)
            tablename = requestTablename
            where = query
            limit = ""
            if pageNumber:
                offset = (pageNumber-1) * pageSize
                limit = "limit %s,%s" % (offset, pageSize)
            sql = "select %s from %s where %s %s" % (columns, tablename, where, limit)
            self.cursor.execute(sql)
            requests = self.cursor.fetchall()
            return requests
        except DatabaseError as e:
            raise PersistenceException(*e.args)
    def createRequest(self, fields):
        '''Creates request

        Creates the request with the given data in the persistence

        Args:
            fields: the data of the request

        Raises:
            PersistenceException: There is DB error
        '''
        try:
            requestTablename = 'PartnerRequests'
            requestColumns = ['request_id', 'study_id', 'query', 'expiration_time',
                              'cache_available', 'cache_timestamp', 'status']
            columns = []
            data = []
            for i in range(len(fields)):
                if fields[i]:
                    data.append(fields[i])
                    columns.append(requestColumns[i])
            sql = "insert into %s(`%s`) values('%s')" % (requestTablename, "`, `".join(columns), "', '".join(data))
            print(sql)
            self.cursor.execute(sql)
        except DatabaseError as e:
            raise PersistenceException(*e.args)
    def commit(self):
        '''Commits the sssion

        Raises:
            PersistenceException: There is DB error
        '''
        try:
            self.cursor.execute()
        except RedisError as e:
            raise PersistenceException(*e.args)
    def rollback(self):
        '''Rolls back the session

        Raises:
            PersistenceException: There is DB error
        '''
        try:
            self.connection.rollback()
        except DatabaseError as e:
            raise PersistenceException(*e.args)
    def commit(self):
        '''Commits the sssion

        Raises:
            PersistenceException: There is DB error
        '''
        try:
            self.connection.commit()
        except DatabaseError as e:
            raise PersistenceException(*e.args)
    def queryRequests(self, query, pageNumber, pageSize):
        '''Queries requests

        Queries the data according the the given filtering options, and returns the list of
        records (as lists) of data.

        Args:
            query: the query string
            pageNumber: the page number
            pageSize: the page size

        Returns:
            The list of records of data

        Raises:
            PersistenceException: There is DB error
        '''
        try:
            claimType = ClaimTypeFactory.load("partner_request")

            query_parser = RedisQueryParser(claimType)
            parse_tree = query_parser.parse(query)

            query_executor = RedisQueryExecutor(self.connection, claimType)
            object_ids = query_executor.visit(parse_tree)
            if pageNumber:
                start = (pageNumber-1) * pageSize
                end = start + pageSize
                object_ids = object_ids[start:end]

            for id in object_ids:
                self.cursor.hgetall(claimType.tablename + ":" + id.decode())
            claims = self.cursor.execute()

            for i in range(len(claims)):
                claims[i] = self._convert_to_tuple(claimType, claims[i])
            return claims
        except RedisQueryParserError as e:
            raise PersistenceException(*e.args)
        except RedisError as e:
            raise PersistenceException(*e.args)
    def updateRequests(self, setStr, query):
        '''Updates requests

        Updates the data according the the given filtering options.

        Args:
            setStr: the updated fields and values
            query: the query string

        Raises:
            PersistenceException: There is DB error
        '''
        try:
            claimType = ClaimTypeFactory.load("partner_request")

            query_parser = RedisQueryParser(claimType)
            parse_tree = query_parser.parse(query)

            query_executor = RedisQueryExecutor(self.connection, claimType)
            object_ids = query_executor.visit(parse_tree)

            set_key = setStr.split('=')[0].encode()
            set_val = setStr.split('=')[1].encode()

            for id in object_ids:
                self.cursor.hgetall(claimType.tablename + ":" + id.decode())
                claims = self.cursor.execute()
                for claim in claims:
                    if claim is not None:
                        self.cursor.srem(claimType.tablename + ":" + set_key.decode() + ":" + claim[set_key].decode(), int(id.decode()))
                        self.cursor.sadd(claimType.tablename + ":" + set_key.decode() + ":" + set_val.decode(), int(id.decode()))
                        claim[set_key] = set_val
                        self.cursor.hmset(claimType.tablename + ":" + id.decode(), self._convert_to_decode(claim))
            
        except RedisQueryParserError as e:
            raise PersistenceException(*e.args)
        except RedisError as e:
            raise PersistenceException(*e.args)
    def close(self):
        '''Closes the session

        Raises:
            PersistenceException: There is DB error
        '''
        try:
            if self.cursor:
                self.cursor.reset()
                self.cursor = None
            if self.connection:
                self.connection = None
        except RedisError as e:
            raise PersistenceException(*e.args)
    def begin(self):
        '''Begins the persistence connection session

        Raises:
            PersistenceException: There is DB error
        '''
        try:
            self.connection = redis.StrictRedis(host=self.connectionConfig["host"],
                                                port=self.connectionConfig["port"],
                                                db=self.connectionConfig["db"])
            self.connection.ping()
            self.cursor = self.connection.pipeline()
            self.cursor.multi()
        except RedisError as e:
            raise PersistenceException(*e.args)
    def begin(self):
        '''Begins the persistence connection session

        Raises:
            PersistenceException: There is DB error
        '''
        try:
            self.connection = dbdriver.connect(host=self.connectionConfig["host"],
                                               user=self.connectionConfig["user"],
                                               passwd=self.connectionConfig["passwd"],
                                               db=self.connectionConfig["db"],
                                               port=self.connectionConfig["port"],
                                               connect_timeout=self.connectionConfig["connect_timeout"])
            self.cursor = self.connection.cursor()
        except DatabaseError as e:
            raise PersistenceException(*e.args)
    def updateRequests(self, setStr, query):
        '''Updates requests

        Updates the data according the the given filtering options.

        Args:
            setStr: the updated fields and values
            query: the query string

        Raises:
            PersistenceException: There is DB error
        '''
        try:
            requestTablename = 'PartnerRequests'
            tablename = requestTablename
            where = query
            sql = "update %s set %s where %s" % (tablename, setStr, where)
            self.cursor.execute(sql)
        except DatabaseError as e:
            raise PersistenceException(*e.args)