<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */


class DBWorkSachgebiete extends DBWorkGenerated
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'nr, typ, sachg';
    $this->_dataModulFields = array('nr', 'typ', 'sachg');
  }

  
  public function getNewObject()
  {
    return new Sachgebiete();
  }

  public function getTableName(): string
  {
    return 'sachgebiete';
  }

  
  public function findByTypAndSachgebiet($pTyp, $pSachgebiet)
  {
    $cleanTyp = $this->dboperation->getString4Sql($pTyp);
    $cleanSachgebiet = $this->dboperation->getInt4Sql($pSachgebiet);

    $sql = 'SELECT ' . $this->select . ' FROM ' . $this->getTableName() . ' sa'
           . ' WHERE ' . $this->dboperation->dbparser->caseInsensitiveLike('sa.', 'typ', $cleanTyp)
           . ' AND ' . $this->dboperation->dbparser->caseInsensitiveCompOperation('=', 'sa.', 'sachg', $cleanSachgebiet);

    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function findByTypAndSachgebietBereich($pTyp, $pSachgebietFrom, $pSachgebietTo)
  {
    $cleanTyp = $this->dboperation->getString4Sql($pTyp);
    $cleanSachgebietFrom = $this->dboperation->getInt4Sql($pSachgebietFrom);
    $cleanSachgebietTo = $this->dboperation->getInt4Sql($pSachgebietTo);

    $sql = 'SELECT ' . $this->select . ' FROM ' . $this->getTableName() . ' sa'
           . ' WHERE ' . $this->dboperation->dbparser->caseInsensitiveLike('sa.', 'typ', $cleanTyp)
           . ' AND ' . $this->dboperation->dbparser->caseInsensitiveBetween('sa.', 'sachg', $cleanSachgebietFrom, $cleanSachgebietTo);

    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  public function getSearchSQL($pPraeffix)
  {
    $praeffix = $this->dboperation->getString4Sql($pPraeffix);
    $qb = $this->getSqlSnippetBuilder();
    if ((isset($this->fromobject)) and (isset($this->toobject))) {
      if (trim($this->fromobject->sachg != '')) {
        $qb = $qb->add('AND sachg >= :sachg_lower: AND sachg <= :sachg_upper:')->paramInt('sachg_lower', $this->fromobject->sachg);
        $qb->paramInt('sachg_upper', $this->toobject->sachg);
      }
      if ($this->fromobject->typ != '') {
        $qb = $qb->add('AND typ = :typ:')->paramString('typ', $this->fromobject->typ);
      }
      if ($this->fromobject->nr != '') {
        $qb = $qb->add('AND nr = :nr:')->paramInt('nr', $this->fromobject->nr);
      }
    }

    return $qb->getSQL();
  }
}