# This file was generated by mettle.genes.db.GenPy3 [ver 2.1] on Thu Jul  7 10:20:58 2022
#  Target Database = postgresql
#
import datetime
import uuid
import time
import mettle.lib
import mettle.io
import mettle.db

from bs_loco.db.tables.imsg_hist_search import iMsgHistSearch
from bs_loco.db.tables.msg_hist import tMsgHist

class dMsgHistSearch:

    def __init__(self, dbcon: mettle.db.IConnect):
        """
        Constructor.

        :param dbcon: Mettle database connection object.
        """
        self._dbcon   = dbcon
        self._dbstmnt = None
        self.irec     = iMsgHistSearch()
        self.orec     = None
    def __enter__(self):
        """
        With statement enter.
        """
        return self

    def __exit__(self, type, value, traceback):
        """
        With statement exit.
        """
        self._destroy()

    def _destroy(self):
        self._dbstmnt = None

    def exec_deft(self,
                  date_from: datetime.datetime,
                  date_to: datetime.datetime) -> "dMsgHistSearch":
        """
        Execute the query by setting all the inputs.

        :param date_from: datetime.datetime
        :param date_to: datetime.datetime
        :return: Self for convenience.
        """
        self.irec.date_from = date_from
        self.irec.date_to = date_to

        return self.exec()

    def exec(self, irec: iMsgHistSearch = None) -> "dMsgHistSearch":
        """
        Execute the query, optionally passing in the input rec.

        :param irec:
        :return: Self for convenience.
        """
        if irec:
            self.irec._copy_from(irec)

        self._destroy()

        self._dbstmnt = self._dbcon.statement("MsgHistSearch", self._dbcon.STMNT_TYPE_READ)

        self._dbstmnt.sql("""select
  m.*
from
  loco.msghist m
where
  m.msgdate  >= :datefrom and
  m.msgdate  <= :dateto
order by
  m.id""")

        self._dbstmnt.bind_in("dateFrom", self.irec.date_from, datetime.datetime)
        self._dbstmnt.bind_in("dateTo", self.irec.date_to, datetime.datetime)

        self._dbstmnt.bind_out("id", int, 8)
        self._dbstmnt.bind_out("status", str, 1)
        self._dbstmnt.bind_out("msgdate", datetime.datetime)
        self._dbstmnt.bind_out("msg", dict)

        self._dbcon.execute(self._dbstmnt)

        if irec:
            irec._copy_from(self.irec)

        return self

    def fetch(self) -> bool:
        """
        Fetches the next row from the executed query.

        :return: True if a row was fetched, else False.
        """
        if not self._dbcon.fetch(self._dbstmnt):
            return False

        self.orec = tMsgHist()

        self.orec.id = self._dbstmnt.result[0]
        self.orec.status = self._dbstmnt.result[1]
        self.orec.msgdate = self._dbstmnt.result[2]
        self.orec.msg = self._dbstmnt.result[3]

        return True

    def fetch_iter(self) -> tMsgHist:
        """
        Yield all the rows one at a time.

        :return: Iterator for output rows
        """
        while self.fetch():
            yield self.orec

    def fetch_all(self, out_list: tMsgHist.List, clearList: bool = True) -> int:
        """
        Fetch all the rows into the specified list and return the list count.

        :param out_list:
        :param clearList:
        :return: Number of rows fetched.
        """
        if clearList:
            out_list.clear()

        while self.fetch():
            out_list.append(self.orec)

        return len(out_list)

