<commit_before><?php namespace Zephyrus\Database;

use stdClass;
use Zephyrus\Application\Configuration;
use Zephyrus\Database\Core\Database;
use Zephyrus\Database\Core\DatabaseStatement;
use Zephyrus\Database\Core\Filterable;
use Zephyrus\Database\Core\Pageable;
use Zephyrus\Exceptions\DatabaseException;
use Zephyrus\Security\Cryptography;

abstract class DatabaseBroker
{
    /**
     * @var Database
     */
    private $database;

    /**
     * @var callable
     */
    private $sanitizeCallback = null;

    /**
     * @var array
     */
    private $encryptedFields = [];

    use Pageable;
    use Filterable { filterQuery as private; }

    /**
     * Broker constructor called by children. Simply get the database reference for further use.
     *
     * @param null|Database $database
     * @throws DatabaseException
     */
    public function __construct(?Database $database = null)
    {
        $this->database = $database;
        if (is_null($this->database)) {
            $this->database = DatabaseFactory::buildFromConfigurations();
        }
    }

    /**
     * @return Database
     */
    protected function getDatabase(): Database
    {
        return $this->database;
    }

    /**
     * @param Database $database
     */
    protected function setDatabase(Database $database)
    {
        $this->database = $database;
    }

    /**
     * Proceeds to include the given variable into the database environnement so that the executed queries, triggers or
     * stored procedures could have access to the variable. Useful for example to pass a user id to register for
     * automated log triggers.
     *
     * @param string $name
     * @param string $value
     */
    protected function addSessionVariable(string $name, string $value)
    {
        $this->query($this->database->getAdapter()->getAddEnvironmentVariableClause($name, $value));
    }

    public function isFieldEncrypted(string $field): bool
    {
        return in_array($field, $this->encryptedFields);
    }

    /**
     * @return array
     */
    public function getEncryptedFields(): array
    {
        return $this->encryptedFields;
    }
<commit_msg>Added guard for non existant database session environnement<commit_after><?php namespace Zephyrus\Database;

use stdClass;
use Zephyrus\Application\Configuration;
use Zephyrus\Database\Core\Database;
use Zephyrus\Database\Core\DatabaseStatement;
use Zephyrus\Database\Core\Filterable;
use Zephyrus\Database\Core\Pageable;
use Zephyrus\Exceptions\DatabaseException;
use Zephyrus\Security\Cryptography;

abstract class DatabaseBroker
{
    /**
     * @var Database
     */
    private $database;

    /**
     * @var callable
     */
    private $sanitizeCallback = null;

    /**
     * @var array
     */
    private $encryptedFields = [];

    use Pageable;
    use Filterable { filterQuery as private; }

    /**
     * Broker constructor called by children. Simply get the database reference for further use.
     *
     * @param null|Database $database
     * @throws DatabaseException
     */
    public function __construct(?Database $database = null)
    {
        $this->database = $database;
        if (is_null($this->database)) {
            $this->database = DatabaseFactory::buildFromConfigurations();
        }
    }

    /**
     * @return Database
     */
    protected function getDatabase(): Database
    {
        return $this->database;
    }

    /**
     * @param Database $database
     */
    protected function setDatabase(Database $database)
    {
        $this->database = $database;
    }

    /**
     * Proceeds to include the given variable into the database environnement so that the executed queries, triggers or
     * stored procedures could have access to the variable. Useful for example to pass a user id to register for
     * automated log triggers.
     *
     * @param string $name
     * @param string $value
     */
    protected function addSessionVariable(string $name, string $value)
    {
        $sql = $this->database->getAdapter()->getAddEnvironmentVariableClause($name, $value);
        if (empty($sql)) {
            // Guard for non existant session environnement feature (e.g. sqlite).
            return;
        }
        $this->query($sql);
    }

    public function isFieldEncrypted(string $field): bool
    {
        return in_array($field, $this->encryptedFields);
    }

    /**
     * @return array
     */
    public function getEncryptedFields(): array
    {
        return $this->encryptedFields;
    }

{'fraction_non_alphanumeric': 0.10346348996249724, 'fraction_numerical': 0.0, 'mean_word_length': 2.5785319652722967, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2388052', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1216, 'n_words': 335}