Skip to content
This repository has been archived by the owner on Apr 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4 from vend/feature-debug-query
Browse files Browse the repository at this point in the history
Code deduplication
  • Loading branch information
dominics committed Jun 16, 2014
2 parents 621e3a5 + 8ed99fe commit 9468ecf
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 63 deletions.
4 changes: 4 additions & 0 deletions src/Doxport/Action/Base/Action.php
Expand Up @@ -127,6 +127,10 @@ public function setChunk(Chunk $chunk)
*/
protected function debugMemory()
{
if (!$this->options['verbose']) {
return;
}

$a = [
memory_get_usage(),
memory_get_peak_usage(),
Expand Down
21 changes: 21 additions & 0 deletions src/Doxport/Action/Base/QueryAction.php
Expand Up @@ -3,6 +3,7 @@
namespace Doxport\Action\Base;

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query;
use Doxport\Doctrine\AliasGenerator;
use Doxport\Doctrine\JoinWalk;
use Doxport\File\AbstractFile;
Expand Down Expand Up @@ -121,6 +122,26 @@ protected function entityToArray($entity, array $fields = [])
return $array;
}

/**
* Prints debugging information about the given query
*
* @param Query $query
*/
protected function debugQuery(Query $query)
{
if (!$this->options['verbose']) {
return;
}

$sql = $query->getSQL();

if (is_array($sql)) {
$sql = implode('; ', $sql);
}

$this->logger->info($sql);
}

/**
* @param JoinWalk $walk
* @return mixed
Expand Down
4 changes: 2 additions & 2 deletions src/Doxport/Action/Delete.php
Expand Up @@ -23,7 +23,7 @@ protected function processQuery(JoinWalk $walk)
// Get query
$this->logger->notice('Getting select query for {target}', ['target' => $walk->getTargetId()]);
$query = $walk->getQuery();
$this->logger->info($query->getSQL());
$this->debugQuery($query);

// Output join information
$this->logger->info((string)$walk);
Expand Down Expand Up @@ -116,7 +116,7 @@ public function processClear(Walk $path, array $fields, array $joinFields)
// Get query
$this->logger->notice('Getting select query for {target}', ['target' => $walk->getTargetId()]);
$query = $walk->getQuery();
$this->logger->info($query->getSQL());
$this->debugQuery($query);

// Output join information
$this->logger->info((string)$walk);
Expand Down
4 changes: 1 addition & 3 deletions src/Doxport/Action/Export.php
Expand Up @@ -25,9 +25,7 @@ class Export extends QueryAction
*/
protected function processQuery(JoinWalk $walk)
{
if ($this->options['verbose']) {
$this->debugMemory();
}
$this->debugMemory();

if (!empty($this->clear[$walk->getTargetId()])) {
$clearFile = $this->getClearFile($walk);
Expand Down
5 changes: 2 additions & 3 deletions src/Doxport/Action/Import.php
Expand Up @@ -138,9 +138,7 @@ protected function flush($count = null)

$this->logger->notice(' changes flushed.');

if ($this->options['verbose']) {
$this->debugMemory();
}
$this->debugMemory();
}

/**
Expand Down Expand Up @@ -178,6 +176,7 @@ protected function processUpdate($entityName, array $updates)

if (empty($entity)) {
$this->logger->warning('Cannot find {entity} to update, skipping update', ['entity' => $entityName]);
continue;
}

// @todo Doesn't support foreign keys in identifier
Expand Down
@@ -1,8 +1,9 @@
<?php

namespace Doxport\Console;
namespace Doxport\Console\Base;

use Doxport\Action\Base\Action;
use Doxport\Console\Base\Command;
use Doxport\File\Factory;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Input\InputInterface;
Expand Down
@@ -1,6 +1,6 @@
<?php

namespace Doxport\Console;
namespace Doxport\Console\Base;

use Doxport\Doxport;
use Doxport\Log\OutputLogger;
Expand Down
@@ -1,8 +1,9 @@
<?php

namespace Doxport\Console;
namespace Doxport\Console\Base;

use Doxport\Action\Base\QueryAction;
use Doxport\Console\Base\ActionCommand;
use InvalidArgumentException;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -37,7 +38,19 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);

$this->validateInput($input);

$pass = $this->doxport->getConstraintPass();
$vertices = $pass->run();

$pass = $this->doxport->getClearPass($vertices);
$pass->run();

$pass = $this->doxport->getJoinPass($vertices);
$pass->run();

$this->logger->notice('All done.');
}

protected function configureDoxport(InputInterface $input)
Expand Down
27 changes: 2 additions & 25 deletions src/Doxport/Console/DeleteCommand.php
Expand Up @@ -4,8 +4,7 @@

use Doxport\Action\Base\Action;
use Doxport\Action\Delete;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doxport\Console\Base\QueryActionCommand;

/**
* Deletes a dataset, writing it to files and syncing them before committing
Expand All @@ -20,32 +19,10 @@ protected function configure()
{
parent::configure();

$this
->setName('delete')
$this->setName('delete')
->setDescription('Deletes a set of data from the database, beginning with a specified type, but not including it');
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);

$pass = $this->doxport->getConstraintPass();
$vertices = $pass->run();

$pass = $this->doxport->getClearPass($vertices);
$pass->run();

$pass = $this->doxport->getJoinPass($vertices);
$pass->run();

$this->logger->notice('All done.');
}

/**
* @return Action
*/
Expand Down
28 changes: 2 additions & 26 deletions src/Doxport/Console/ExportCommand.php
Expand Up @@ -4,8 +4,7 @@

use Doxport\Action\Base\Action;
use Doxport\Action\Export;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doxport\Console\Base\QueryActionCommand;

/**
* Exports a dataset, writing it to files
Expand All @@ -19,33 +18,10 @@ protected function configure()
{
parent::configure();

$this
->setName('export')
$this->setName('export')
->setDescription('Exports a set of data from the database, beginning with a specified type');
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);

$pass = $this->doxport->getConstraintPass();
$vertices = $pass->run();

$pass = $this->doxport->getClearPass($vertices);
$pass->run();

$pass = $this->doxport->getJoinPass($vertices);
$pass->run();

$this->logger->notice('All done.');
}


/**
* @return Action
*/
Expand Down
1 change: 1 addition & 0 deletions src/Doxport/Console/ImportCommand.php
Expand Up @@ -3,6 +3,7 @@
namespace Doxport\Console;

use Doxport\Action\Import;
use Doxport\Console\Base\ActionCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down
3 changes: 2 additions & 1 deletion src/Doxport/Exception/IOException.php
Expand Up @@ -3,4 +3,5 @@
namespace Doxport\Exception;

class IOException extends \Exception
{}
{
}
1 change: 1 addition & 0 deletions test/Doxport/Console/CommandTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Doxport\Console;

use Doxport\Console\Base\Command;
use Doxport\Test\AbstractTest;
use Symfony\Component\Console\Tester\CommandTester;

Expand Down
2 changes: 2 additions & 0 deletions test/Doxport/Console/ImportCommandTest.php
Expand Up @@ -2,6 +2,8 @@

namespace Doxport\Console;

use Doxport\Console\Base\Command;

class ImportCommandTest extends ActionCommandTest
{
/**
Expand Down
38 changes: 38 additions & 0 deletions test/Doxport/Metadata/PropertyTest.php
@@ -0,0 +1,38 @@
<?php

namespace Doxport\Metadata;

use Doxport\Annotation\Exclude;
use Doxport\Test\AbstractTest;
use LogicException;

class PropertyTest extends AbstractTest
{
public function testHasAnnotation()
{
$property = new Property('something', [new Exclude()], []);

$this->assertTrue($property->hasAnnotation('Doxport\Annotation\Exclude'));
$this->assertFalse($property->hasAnnotation('Doxport\Annotation\Clear'));
}

public function testGetTargetEntity()
{
$target = uniqid();

$property = new Property('something', [new Exclude()], [
'targetEntity' => $target
]);

$this->assertEquals($target, $property->getTargetEntity());
}

/**
* @expectedException LogicException
*/
public function testNoTargetEntity()
{
$property = new Property('something', [], []);
$property->getTargetEntity();
}
}

0 comments on commit 9468ecf

Please sign in to comment.