def hasReceivedRow(self,
                       timeout=1.0,
                       row=None,
                       rowIndex=None,
                       rowMax=None):
        """
		Waits to receive "response" event until the end of the timeout
		
		@param timeout: time max to wait to receive event in second (default=1s)
		@type timeout: float			

		@return: an event matching with the template or None otherwise
		@rtype: templatemessage
		"""
        TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(),
                                     timeout=timeout)

        db_event = templates.db(host=self.cfg['host'],
                                port=self.cfg['port'],
                                more=templates.response(row=row,
                                                        rowIndex=None,
                                                        rowMax=None))
        evt = self.received(expected=self.encapsule(db_event=db_event),
                            timeout=timeout)
        return evt
    def query(self, query, queryName=None):
        """
		Query the database
		
		@param query: sql query
		@type query: string			
		
		@param queryName: query identifier
		@type queryName: string/none	
		"""
        if self.connPtr is None:
            self.warning('connect first to the database')
        else:
            self.debug('query db')
            # log query event
            db_event = templates.db(host=self.cfg['host'],
                                    port=self.cfg['port'],
                                    user=self.cfg['user'],
                                    password=self.cfg['password'],
                                    more=templates.query(query=query))
            self.logSentEvent(shortEvt="query",
                              tplEvt=self.encapsule(db_event=db_event))

            if self.cfg['agent-support']:
                remote_cfg = {
                    'cmd': 'Query',
                    'query': query,
                    'user': self.cfg['user'],
                    'password': self.cfg['password'],
                    'host': self.cfg['host'],
                    'port': self.cfg['port']
                }
                self.sendNotifyToAgent(data=remote_cfg)
            else:
                try:
                    cursor = self.connPtr.cursor()
                    cursor.execute(query)

                    i = 0
                    # log response event
                    tpl = templates.db(host=self.cfg['host'],
                                       port=self.cfg['port'],
                                       user=self.cfg['user'],
                                       password=self.cfg['password'],
                                       more=templates.executed(
                                           nbChanged=str(cursor.rowcount)))
                    self.logRecvEvent(shortEvt="executed",
                                      tplEvt=self.encapsule(db_event=tpl))

                    try:
                        row = cursor.fetchone()
                        while row:
                            i += 1
                            self.debug(row)
                            # as dict
                            fields = map(lambda x: x[0], cursor.description)
                            ret = dict(zip(fields, row))

                            # each value as str
                            ret_str = {}
                            if queryName is not None:
                                ret_str['query-name'] = queryName
                            for k, v in ret.items():
                                ret_str[k] = str(v)

                            # log response event
                            tpl = templates.db(host=self.cfg['host'],
                                               port=self.cfg['port'],
                                               user=self.cfg['user'],
                                               password=self.cfg['password'],
                                               more=templates.response(
                                                   row=ret_str,
                                                   rowIndex=i,
                                                   rowMax=cursor.rowcount))
                            if self.logEventReceived:
                                self.logRecvEvent(
                                    shortEvt="row",
                                    tplEvt=self.encapsule(db_event=tpl))

                            self.handleIncomingRow(lower=self.encapsule(
                                db_event=tpl))

                            row = cursor.fetchone()
                    except MySQLdb.Error as e:
                        pass  # no more to read
                    # log response event
                    tpl = templates.db(host=self.cfg['host'],
                                       port=self.cfg['port'],
                                       user=self.cfg['user'],
                                       password=self.cfg['password'],
                                       more=templates.terminated(nbRow=i))
                    self.logRecvEvent(shortEvt="terminated",
                                      tplEvt=self.encapsule(db_event=tpl))

                    # close the cursor and commit
                    cursor.close()
                    self.connPtr.commit()
                except pymssql.Error as e:
                    self.onError(e)
                except Exception as e:
                    self.error(str(e))
                else:
                    pass
    def receivedNotifyFromAgent(self, data):
        """
		Function to reimplement
		"""
        self.debug(data)
        if 'cmd' in data:
            if data['cmd'] == AGENT_INITIALIZED:
                tpl = TestTemplatesLib.TemplateMessage()
                layer = TestTemplatesLib.TemplateLayer('AGENT')
                layer.addKey("ready", True)
                layer.addKey(name='name', data=self.cfg['agent']['name'])
                layer.addKey(name='type', data=self.cfg['agent']['type'])
                tpl.addLayer(layer=layer)
                self.logRecvEvent(shortEvt="Agent Is Ready", tplEvt=tpl)

            elif data['cmd'] == 'Connect':
                self.connPtr = FakePtr()
                tpl = templates.db(host=data['host'],
                                   port=data['port'],
                                   user=data['user'],
                                   password=data['password'],
                                   more=templates.connected())
                self.logRecvEvent(shortEvt="connected",
                                  tplEvt=self.encapsule(db_event=tpl))

            elif data['cmd'] == 'Disconnect':
                tpl = templates.db(host=data['host'],
                                   port=data['port'],
                                   user=data['user'],
                                   password=data['password'],
                                   more=templates.disconnected())
                self.logRecvEvent(shortEvt="disconnected",
                                  tplEvt=self.encapsule(db_event=tpl))
                self.connPtr = None

            elif data['cmd'] == 'Query':
                tpl = templates.db(host=data['host'],
                                   port=data['port'],
                                   user=data['user'],
                                   password=data['password'],
                                   more=templates.response(
                                       row=data['row'],
                                       rowIndex=data['row-index'],
                                       rowMax=data['row-max']))
                if self.logEventReceived:
                    self.logRecvEvent(shortEvt="row",
                                      tplEvt=self.encapsule(db_event=tpl))

                self.handleIncomingRow(lower=self.encapsule(db_event=tpl))

            elif data['cmd'] == 'Executed':
                tpl = templates.db(
                    host=data['host'],
                    port=data['port'],
                    user=data['user'],
                    password=data['password'],
                    more=templates.executed(nbChanged=data['nb-changed']))
                self.logRecvEvent(shortEvt="executed",
                                  tplEvt=self.encapsule(db_event=tpl))

            elif data['cmd'] == 'Terminated':
                tpl = templates.db(
                    host=data['host'],
                    port=data['port'],
                    user=data['user'],
                    password=data['password'],
                    more=templates.terminated(nbRow=['nb-rows']))
                self.logRecvEvent(shortEvt="terminated",
                                  tplEvt=self.encapsule(db_event=tpl))

            else:
                self.error("unknown command received: %s" % data["cmd"])
        else:
            self.error("no cmd detected %s" % data)