1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Doctrine\DBAL\Tools\Console\Command;
use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Driver\PDOStatement;
use InvalidArgumentException;
use PDOException;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use const PHP_EOL;
use function assert;
use function error_get_last;
use function file_exists;
use function file_get_contents;
use function is_readable;
use function realpath;
use function sprintf;
/**
* Task for executing arbitrary SQL that can come from a file or directly from
* the command line.
*
* @deprecated Use a database client application instead
*/
class ImportCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('dbal:import')
->setDescription('Import SQL file(s) directly to Database.')
->setDefinition([new InputArgument(
'file',
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
'File path(s) of SQL to be executed.'
),
])
->setHelp(<<<EOT
Import SQL file(s) directly to Database.
EOT
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$conn = $this->getHelper('db')->getConnection();
$fileNames = $input->getArgument('file');
if ($fileNames === null) {
return null;
}
foreach ((array) $fileNames as $fileName) {
$filePath = realpath($fileName);
// Phar compatibility.
if ($filePath === false) {
$filePath = $fileName;
}
if (! file_exists($filePath)) {
throw new InvalidArgumentException(
sprintf("SQL file '<info>%s</info>' does not exist.", $filePath)
);
} elseif (! is_readable($filePath)) {
throw new InvalidArgumentException(
sprintf("SQL file '<info>%s</info>' does not have read permissions.", $filePath)
);
}
$output->write(sprintf("Processing file '<info>%s</info>'... ", $filePath));
$sql = @file_get_contents($filePath);
if ($sql === false) {
throw new RuntimeException(
sprintf("Unable to read SQL file '<info>%s</info>': %s", $filePath, error_get_last()['message'])
);
}
if ($conn instanceof PDOConnection) {
// PDO Drivers
try {
$lines = 0;
$stmt = $conn->prepare($sql);
assert($stmt instanceof PDOStatement);
$stmt->execute();
do {
// Required due to "MySQL has gone away!" issue
$stmt->fetch();
$stmt->closeCursor();
$lines++;
} while ($stmt->nextRowset());
$output->write(sprintf('%d statements executed!', $lines) . PHP_EOL);
} catch (PDOException $e) {
$output->write('error!' . PHP_EOL);
throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
}
} else {
// Non-PDO Drivers (ie. OCI8 driver)
$stmt = $conn->prepare($sql);
$rs = $stmt->execute();
if (! $rs) {
$error = $stmt->errorInfo();
$output->write('error!' . PHP_EOL);
throw new RuntimeException($error[2], $error[0]);
}
$output->writeln('OK!' . PHP_EOL);
$stmt->closeCursor();
}
}
return null;
}
}