Skip to content

Instantly share code, notes, and snippets.

@doctrinebot
Created December 13, 2015 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctrinebot/58375ca36e51bcecb47d to your computer and use it in GitHub Desktop.
Save doctrinebot/58375ca36e51bcecb47d to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-300 - https://github.com/doctrine/doctrine2/issues/3766
Index: RunSqlTask.php
===================================================================
--- RunSqlTask.php (revision 7103)
+++ RunSqlTask.php (working copy)
@@ -99,7 +99,37 @@
$arguments = $this->getArguments();
if (isset($arguments['file'])) {
- //TODO
+ $fileName = $arguments['file'];
+ if (!file_exists($fileName)) {
+ throw new CliException(sprintf('The SQL file [%s] does not exist.', $fileName));
+ } elseif ( ! is_readable($fileName)) {
+ throw new CliException(sprintf('The SQL file [%s] does not have read permissions.', $fileName));
+ }
+
+ $em = $this->getConfiguration()->getAttribute('em');
+
+ $resultSet = array();
+
+ $executedCount = 0;
+
+ $this->getPrinter()->writeln('Processing file...');
+
+ foreach (file($fileName) as $sql) {
+ if (preg_match('/^select/i', $sql)) {
+ $stmt = $em->getConnection()->execute($sql);
+ $resultSet[] = $stmt->fetchAll(\Doctrine\DBAL\Connection::FETCH_ASSOC);
+ } else {
+ $resultSet[] = $em->getConnection()->executeUpdate($sql);
+ }
+ $executedCount ++;
+ }
+
+ $this->getPrinter()->writeln(sprintf('%d statements executed.', $executedCount));
+
+ $maxDepth = isset($arguments['depth']) ? $arguments['depth'] : 7;
+
+ Debug::dump($resultSet);
+
} else if (isset($arguments['sql'])) {
$em = $this->getConfiguration()->getAttribute('em');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment