<commit_before><?php

/*
 * This file is part of the Stinger Doctrine-Commons package.
 *
 * (c) <NAME> <<EMAIL>>
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace StingerSoft\DoctrineCommons\Utils\Helper;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use JsonStreamingParser\Listener\IdleListener;
use StingerSoft\DoctrineCommons\Utils\JsonStreamImporter;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Json Stream Listener to import data into the database
 */
class ImportListener extends IdleListener {

	/**
	 *
	 * @var integer
	 */
	protected $entries = 0;

	/**
	 *
	 * @var integer
	 */
	protected $level = 0;

	/**
	 *
	 * @var string
	 */
	protected $currentTable = null;

	/**
	 *
	 * @var string
	 */
	protected $currentField = null;

	/**
	 *
	 * @var string[]
	 */
	protected $nonExistingTables = array();

	/**
	 *
	 * @var string[]
	 */
	protected $existingTables = array();

	/**
	 *
	 * @var \Doctrine\DBAL\Query\QueryBuilder
	 */
	protected $currentTableQuery = null;

	/**
	 * The console output channel
	 *
	 * @var OutputInterface
	 */
	protected $output;

	/**
	 * The schema manager to check if a table exists
	 *
	 * @var AbstractSchemaManager
	 */
	protected $schemaManager;

	/**
	 * The database connection
	 *
	 * @var Connection
	 */
	protected $connection;

	/**
	 * Console helper to render a progress bar
	 *
	 * @var ProgressBar
	 */
	protected $progressbar;

	/**
	 *
	 * @var ImportDatabaseCommand
	 */
	protected $importCommand;

	/**
	 * Constructor
	 *
	 * @param ImportDatabaseCommand $parent
	 *        	The command using this listener
	 * @param OutputInterface $output
	 *        	The console output channel
	 * @param AbstractSchemaManager $schemaManager
	 *        	The schema manager to check if a table exists
	 * @param Connection $connection
	 *        	The database connection
	 * @param int $maxEntries
	 *        	The maximum amount of entries which should be imported
	 */
	public function __construct(JsonStreamImporter $parent, AbstractSchemaManager $schemaManager, Connection $connection, $maxEntries, OutputInterface $output = null) {
		if($this->output) {
			$this->progressbar = new ProgressBar($output, $maxEntries);
		}
		$this->connection = $connection;
		$this->schemaManager = $schemaManager;
		$this->output = $output;
		$this->importCommand = $parent;
	}

	/**
	 *
	 * {@inheritdoc}
	 *
	 * @see \JsonStreamingParser\Listener\IdleListener::startDocument()<commit_msg>fixing: adding output only if necessary<commit_after><?php

/*
 * This file is part of the Stinger Doctrine-Commons package.
 *
 * (c) <NAME> <<EMAIL>>
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace StingerSoft\DoctrineCommons\Utils\Helper;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use JsonStreamingParser\Listener\IdleListener;
use StingerSoft\DoctrineCommons\Utils\JsonStreamImporter;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Json Stream Listener to import data into the database
 */
class ImportListener extends IdleListener {

	/**
	 *
	 * @var integer
	 */
	protected $entries = 0;

	/**
	 *
	 * @var integer
	 */
	protected $level = 0;

	/**
	 *
	 * @var string
	 */
	protected $currentTable = null;

	/**
	 *
	 * @var string
	 */
	protected $currentField = null;

	/**
	 *
	 * @var string[]
	 */
	protected $nonExistingTables = array();

	/**
	 *
	 * @var string[]
	 */
	protected $existingTables = array();

	/**
	 *
	 * @var \Doctrine\DBAL\Query\QueryBuilder
	 */
	protected $currentTableQuery = null;

	/**
	 * The console output channel
	 *
	 * @var OutputInterface
	 */
	protected $output;

	/**
	 * The schema manager to check if a table exists
	 *
	 * @var AbstractSchemaManager
	 */
	protected $schemaManager;

	/**
	 * The database connection
	 *
	 * @var Connection
	 */
	protected $connection;

	/**
	 * Console helper to render a progress bar
	 *
	 * @var ProgressBar
	 */
	protected $progressbar;

	/**
	 *
	 * @var ImportDatabaseCommand
	 */
	protected $importCommand;

	/**
	 * Constructor
	 *
	 * @param ImportDatabaseCommand $parent
	 *        	The command using this listener
	 * @param OutputInterface $output
	 *        	The console output channel
	 * @param AbstractSchemaManager $schemaManager
	 *        	The schema manager to check if a table exists
	 * @param Connection $connection
	 *        	The database connection
	 * @param int $maxEntries
	 *        	The maximum amount of entries which should be imported
	 */
	public function __construct(JsonStreamImporter $parent, AbstractSchemaManager $schemaManager, Connection $connection, $maxEntries = null, OutputInterface $output = null) {
		$this->output = $output;
		if($this->output != null && $maxEntries !== null) {
			$this->progressbar = new ProgressBar($output, $maxEntries);
		}
		$this->connection = $connection;
		$this->schemaManager = $schemaManager;
		$this->importCommand = $parent;
	}

	/**
	 *
	 * {@inheritdoc}
	 *
	 * @see \JsonStreamingParser\Listener\IdleListener::startDocument()
{'fraction_non_alphanumeric': 0.11896326682826776, 'fraction_numerical': 0.0007458512026850643, 'mean_word_length': 3.375203915171289, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, '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': '6613849', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1513, 'n_words': 436}