<commit_before><?php
namespace Mathielen\ImportEngine\Storage;

use Doctrine\ORM\Query;
use Mathielen\DataImport\Reader\DoctrineQueryReader;
use Doctrine\ORM\EntityManagerInterface;
use Ddeboer\DataImport\Writer\DoctrineWriter;
use Mathielen\ImportEngine\Exception\InvalidConfigurationException;

class DoctrineStorage implements StorageInterface
{

    /**
     * @var EntityManagerInterface
     */
    private $entityManager;

    /**
     * @var Query
     */
    private $query;

    private $entityName;

    public function __construct(EntityManagerInterface $entityManager, $entityName=null, Query $query=null)
    {
        $this->entityManager = $entityManager;
        $this->entityName = $entityName;

        if (is_null($query) && is_string($entityName) && class_exists($entityName)) {
            $query = $this->entityManager->createQueryBuilder()
                ->select('o')
                ->from($this->entityName, 'o')
                ->getQuery();
        }

        $this->query = $query;
    }

    /**
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::reader()
     */
    public function reader()
    {
        return new DoctrineQueryReader($this->query);
    }

    /**
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::writer()
     */
    public function writer()
    {
        if (empty($this->entityName)) {
            throw new InvalidConfigurationException("Can only use doctrine for writing if entityName is given.");
        }

        return new DoctrineWriter($this->entityManager, $this->entityName);
    }

    /**
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::info()
     */
    public function info()
    {
        $count = count($this->reader());

        return new StorageInfo(array(
            'name' => $this->query->getDQL(),
            'type' => 'DQL Query',
            'count' => $count
        ));
    }

    /**
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::getFields()
     */
    public function getFields()
    {
        return $this->reader()->getFields();
    }
}<commit_msg>doctrinewriter - set default: do not truncate first<commit_after><?php
namespace Mathielen\ImportEngine\Storage;

use Ddeboer\DataImport\Writer\DoctrineWriter;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Mathielen\DataImport\Reader\DoctrineQueryReader;
use Mathielen\ImportEngine\Exception\InvalidConfigurationException;

class DoctrineStorage implements StorageInterface
{

    /**
     * @var EntityManagerInterface
     */
    private $entityManager;

    /**
     * @var Query
     */
    private $query;

    private $entityName;

    public function __construct(EntityManagerInterface $entityManager, $entityName=null, Query $query=null)
    {
        $this->entityManager = $entityManager;
        $this->entityName = $entityName;

        if (is_null($query) && is_string($entityName) && class_exists($entityName)) {
            $query = $this->entityManager->createQueryBuilder()
                ->select('o')
                ->from($this->entityName, 'o')
                ->getQuery();
        }

        $this->query = $query;
    }

    /**
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::writer()
     */
    public function writer()
    {
        if (empty($this->entityName)) {
            throw new InvalidConfigurationException("Can only use doctrine for writing if entityName is given.");
        }

        $writer = new DoctrineWriter($this->entityManager, $this->entityName);
        $writer->setTruncate(false);

        return $writer;
    }

    /**
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::info()
     */
    public function info()
    {
        $count = count($this->reader());

        return new StorageInfo(array(
            'name' => $this->query->getDQL(),
            'type' => 'DQL Query',
            'count' => $count
        ));
    }

    /**
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::reader()
     */
    public function reader()
    {
        return new DoctrineQueryReader($this->query);
    }

    /**
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::getFields()
     */
    public function getFields()
    {
        return $this->reader()->getFields();
    }
}
{'fraction_non_alphanumeric': 0.1356703847029365, 'fraction_numerical': 0.0, 'mean_word_length': 2.5636658556366587, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 46, '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': '4819539', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1226, 'n_words': 227}