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

namespace Kufer\Web\Classes\Db;

class DBWorkHLAusbildungsSchablone extends \DBWorkGenerated
{
  public function __construct($pDBnr = 1, $pNewInstance = false)
  {
    parent::__construct($pDBnr, $pNewInstance);

    $this->select = array_map(
      \FunctionOperations::prefixString(':table:.'),
      array('id', 'ausbildung_schablone', 'kuerzel', 'beschreibung', 'fachbereich', $this->clob('bemerkung'))
    );

    $this->_dataModulFields = array('id', 'ausbildung_schablone', 'kuerzel', 'beschreibung', 'fachbereich', 'bemerkung');
  }

  public function getNewObject()
  {
    return new \Kufer\Web\Classes\Datamodul\HLAusbildungsSchablone();
  }

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

  public function getAllQuery(): \SelectQueryBuilder
  {
    return $this->getSelectQueryBuilder()
      ->select($this->select)
      ->from($this->getTableName(), 'hlas')
      ->paramRaw('table', 'hlas');
  }

  public function findByFilterQuery(\Kufer\Web\Classes\Dbfilter\HLAusbildungSchablonenSQLFilter $filter): \SelectQueryBuilder
  {
    $condition = $filter->createSql($this->dboperation);

    return $this->getAllQuery()
      ->and($condition);
  }

  
  public function findByFilter(\Kufer\Web\Classes\Dbfilter\HLAusbildungSchablonenSQLFilter $filter): array
  {
    $builder = $this->findByFilterQuery($filter);

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRows();
  }

  
  public function findFirstByFilter(\Kufer\Web\Classes\Dbfilter\HLAusbildungSchablonenSQLFilter $filter): ?\Kufer\Web\Classes\Datamodul\HLAusbildungsSchablone
  {
    $builder = $this->findByFilterQuery($filter);

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    $result = $this->getFirstRow();

    return empty($result->id) ? null : $result;
  }

  
  public function findBeschreibungByFilter(\Kufer\Web\Classes\Dbfilter\HLAusbildungSchablonenSQLFilter $filter): array
  {
    $builder = $this->findByFilterQuery($filter)
      ->select(':table:.beschreibung');

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRowsSingleColumn('beschreibung');
  }

  
  public function findAusbildungsSchabloneByFilter(\Kufer\Web\Classes\Dbfilter\HLAusbildungSchablonenSQLFilter $filter): array
  {
    $builder = $this->findByFilterQuery($filter)
      ->select(':table:.ausbildung_schablone');

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRowsSingleColumn('ausbildung_schablone');
  }

  
  public function findMappedByIdByFilter(\Kufer\Web\Classes\Dbfilter\HLAusbildungSchablonenSQLFilter $filter): array
  {
    $builder = $this->findByFilterQuery($filter);

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRowsMappedByAttribute('id');
  }

  
  public function countByFilter(\Kufer\Web\Classes\Dbfilter\HLAusbildungSchablonenSQLFilter $filter): int
  {
    $builder = $this->findByFilterQuery($filter)
      ->select('COUNT(*) AS anzahl');

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQLForInnerQuery());

    $this->dboperation->selectrow(0);

    return (int)$this->getValue('anzahl', \DatamodulVarType::TYPE_INT);
  }
}