Permalink
Browse files

[Generator] Fixed SQL insertion

  • Loading branch information...
1 parent a2cc82a commit 1efcab9e73ac7965fa933c9411cf546861ca9ae7 @willdurand willdurand committed Mar 7, 2012
@@ -33,12 +33,11 @@ class SqlInsert extends AbstractCommand
*/
protected function configure()
{
- parent::configure();
-
$this
+ ->addOption('output-dir', null, InputOption::VALUE_REQUIRED, 'The output directory', self::DEFAULT_OUTPUT_DIRECTORY)
->addOption('connection', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Connection to use', array())
->setName('sql:insert')
- ->setDescription('Insert SQL')
+ ->setDescription('Insert SQL statements')
;
}
@@ -50,9 +49,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$manager = new SqlManager();
$connections = array();
- foreach ($this->getOption('connection') as $connection) {
+ foreach ($input->getOption('connection') as $connection) {
list($name, $dsn) = $this->parseConnection($connection);
- $connections[$name] = $dsn;
+ $connections[$name] = array('dsn' => $dsn);
}
$manager->setConnections($connections);
@@ -42,29 +42,23 @@ class TestPrepare extends AbstractCommand
* @var array
*/
protected $fixtures = array(
- 'bookstore' => 'bookstore',
- 'bookstore_packaged' => 'bookstore-packaged',
- 'namespaced' => 'namespaced',
- '' => 'reverse/mysql',
- 'bookstore' => 'schemas',
+ 'bookstore' => array('bookstore', 'bookstore-cms', 'bookstore-behavior'),
+ 'bookstore-packaged' => array('bookstore-packaged', 'bookstore-log'),
+ 'namespaced' => 'bookstore_namespaced',
+ 'reverse/mysql' => 'reverse-bookstore',
+ 'schemas' => 'bookstore',
);
/**
* @var string
*/
protected $root = null;
- /**
- * @var string
- */
- protected $propelgen = null;
-
public function __construct()
{
parent::__construct();
$this->root = realpath(__DIR__.'/../../../../');
- $this->propelgen = $this->root.'/tools/generator/bin/propel-gen';
}
/**
@@ -89,23 +83,23 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
- foreach ($this->fixtures as $databaseName => $fixturesDir) {
- $this->buildFixtures(sprintf('%s/%s', self::FIXTURES_DIR, $fixturesDir), $databaseName, $input, $output);
+ foreach ($this->fixtures as $fixturesDir => $database) {
+ $this->buildFixtures(sprintf('%s/%s', self::FIXTURES_DIR, $fixturesDir), $database, $input, $output);
}
}
/**
* @param string $fixturesDir
*/
- protected function buildFixtures($fixturesDir, $databaseName, InputInterface $input, OutputInterface $output)
+ protected function buildFixtures($fixturesDir, $database, InputInterface $input, OutputInterface $output)
{
if (!file_exists($fixturesDir)) {
$output->writeln(sprintf('<error>Directory "%s" not found.</error>', $fixturesDir));
return;
}
- $output->write(sprintf('Building fixtures in <info>%-40s</info> ', $fixturesDir));
+ $output->writeln(sprintf('Building fixtures in <info>%-40s</info> ', $fixturesDir));
chdir($fixturesDir);
@@ -131,61 +125,69 @@ protected function buildFixtures($fixturesDir, $databaseName, InputInterface $in
}
}
- if (0 < count((array) $this->getSchemas('.'))) {
+ if (0 < count((array) $this->getSchemas('.')) || false === strstr($fixturesDir, 'reverse')) {
$in = new ArrayInput(array(
- 'command' => 'config:build',
- '--input-dir' => '.',
- '--output-file' => sprintf('build/conf/%s-conf.php', $databaseName),
- '--verbose' => $input->getOption('verbose'),
- ));
-
- $command = $this->getApplication()->find('config:build');
- $command->run($in, $output);
-
- $in = new ArrayInput(array(
- 'command' => 'model:build',
+ 'command' => 'sql:build',
'--input-dir' => '.',
- '--output-dir' => 'build/classes/',
+ '--output-dir' => 'build/sql/',
'--platform' => ucfirst($input->getOption('vendor')) . 'Platform',
'--verbose' => $input->getOption('verbose'),
));
- $command = $this->getApplication()->find('model:build');
+ $command = $this->getApplication()->find('sql:build');
$command->run($in, $output);
+ $connections = array();
+ if (is_array($database)) {
+ foreach ($database as $db) {
+ $connections[] = sprintf(
+ '%s=%s;username=%s;password=%s',
+ $db, $input->getOption('dsn'),
+ $input->getOption('user'), $input->getOption('password')
+ );
+ }
+ } else {
+ $connections[] = sprintf(
+ '%s=%s;username=%s;password=%s',
+ $database, $input->getOption('dsn'),
+ $input->getOption('user'), $input->getOption('password')
+ );
+ }
+
$in = new ArrayInput(array(
'command' => 'sql:insert',
- '--input-dir' => '.',
'--output-dir' => 'build/sql/',
- '--platform' => ucfirst($input->getOption('vendor')) . 'Platform',
- '--connection' => sprintf(
- '%s=%s;username=%s;password=%s',
- $databaseName, $input->getOption('dsn'),
- $input->getOption('user'), $input->getOption('password')
- ),
+ '--connection' => $connections,
'--verbose' => $input->getOption('verbose'),
));
$command = $this->getApplication()->find('sql:insert');
$command->run($in, $output);
}
- if (0 < count((array) $this->getSchemas('.')) || false !== strpos('reverse', $fixturesDir)) {
- // use new commands
+ if (0 < count((array) $this->getSchemas('.'))) {
$in = new ArrayInput(array(
- 'command' => 'sql:build',
+ 'command' => 'config:build',
'--input-dir' => '.',
- '--output-dir' => 'build/sql/',
+ '--output-file' => sprintf('build/conf/%s-conf.php', is_array($database) ? $database[0] : $database),
+ '--verbose' => $input->getOption('verbose'),
+ ));
+
+ $command = $this->getApplication()->find('config:build');
+ $command->run($in, $output);
+
+ $in = new ArrayInput(array(
+ 'command' => 'model:build',
+ '--input-dir' => '.',
+ '--output-dir' => 'build/classes/',
'--platform' => ucfirst($input->getOption('vendor')) . 'Platform',
'--verbose' => $input->getOption('verbose'),
));
- $command = $this->getApplication()->find('sql:build');
+ $command = $this->getApplication()->find('model:build');
$command->run($in, $output);
}
- $output->writeln('OK');
-
chdir($this->root);
}
}
@@ -11,6 +11,7 @@
namespace Propel\Generator\Manager;
use Propel\Generator\Exception\InvalidArgumentException;
+use Propel\Generator\Util\SqlParser;
use \PDO;
use \PDOException;
@@ -136,22 +137,27 @@ public function buildSql()
public function insertSql($datasource = null)
{
$statementsToInsert = array();
+
foreach ($this->getProperties($this->getSqlDbMapFilename()) as $sqlFile => $database) {
if (null !== $datasource && $database !== $datasource) {
// skip
+ $this->log(sprintf('Skipping %s.', $sqlFile));
break;
}
if (!isset($statementsToInsert[$database])) {
$statementsToInsert[$database] = array();
}
- if (null === $database || (null !== $database && $database === $datasource)) {
+
+ if (null === $datasource || (null !== $database && $database === $datasource)) {
$filename = $this->getWorkingDirectory() . DIRECTORY_SEPARATOR . $sqlFile;
if (file_exists($filename)) {
foreach (SqlParser::parseFile($filename) as $sql) {
$statementsToInsert[$database][] = $sql;
}
+ } else {
+ $this->log(sprintf("File %s doesn't exist", $filename));
}
}
}

0 comments on commit 1efcab9

Please sign in to comment.