1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Tools\Console\Command; |
21
|
|
|
|
22
|
|
|
use Symfony\Component\Console\Command\Command; |
23
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
24
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
25
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Task for executing arbitrary SQL that can come from a file or directly from |
29
|
|
|
* the command line. |
30
|
|
|
* |
31
|
|
|
* @link www.doctrine-project.org |
32
|
|
|
* @since 2.0 |
33
|
|
|
* @author Benjamin Eberlei <[email protected]> |
34
|
|
|
* @author Guilherme Blanco <[email protected]> |
35
|
|
|
* @author Jonathan Wage <[email protected]> |
36
|
|
|
* @author Roman Borschel <[email protected]> |
37
|
|
|
*/ |
38
|
|
|
class ImportCommand extends Command |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
protected function configure() |
44
|
|
|
{ |
45
|
|
|
$this |
46
|
|
|
->setName('dbal:import') |
47
|
|
|
->setDescription('Import SQL file(s) directly to Database.') |
48
|
|
|
->setDefinition([ |
49
|
|
|
new InputArgument( |
50
|
|
|
'file', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'File path(s) of SQL to be executed.' |
51
|
|
|
) |
52
|
|
|
]) |
53
|
|
|
->setHelp(<<<EOT |
54
|
|
|
Import SQL file(s) directly to Database. |
55
|
|
|
EOT |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
63
|
|
|
{ |
64
|
|
|
$conn = $this->getHelper('db')->getConnection(); |
65
|
|
|
|
66
|
|
|
if (($fileNames = $input->getArgument('file')) !== null) { |
67
|
|
|
foreach ((array) $fileNames as $fileName) { |
68
|
|
|
$filePath = realpath($fileName); |
69
|
|
|
|
70
|
|
|
// Phar compatibility. |
71
|
|
|
if (false === $filePath) { |
72
|
|
|
$filePath = $fileName; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ( ! file_exists($filePath)) { |
76
|
|
|
throw new \InvalidArgumentException( |
77
|
|
|
sprintf("SQL file '<info>%s</info>' does not exist.", $filePath) |
78
|
|
|
); |
79
|
|
|
} elseif ( ! is_readable($filePath)) { |
80
|
|
|
throw new \InvalidArgumentException( |
81
|
|
|
sprintf("SQL file '<info>%s</info>' does not have read permissions.", $filePath) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$output->write(sprintf("Processing file '<info>%s</info>'... ", $filePath)); |
86
|
|
|
$sql = file_get_contents($filePath); |
87
|
|
|
|
88
|
|
|
if ($conn instanceof \Doctrine\DBAL\Driver\PDOConnection) { |
89
|
|
|
// PDO Drivers |
90
|
|
|
try { |
91
|
|
|
$lines = 0; |
92
|
|
|
|
93
|
|
|
$stmt = $conn->prepare($sql); |
94
|
|
|
$stmt->execute(); |
95
|
|
|
|
96
|
|
|
do { |
97
|
|
|
// Required due to "MySQL has gone away!" issue |
98
|
|
|
$stmt->fetch(); |
99
|
|
|
$stmt->closeCursor(); |
100
|
|
|
|
101
|
|
|
$lines++; |
102
|
|
|
} while ($stmt->nextRowset()); |
103
|
|
|
|
104
|
|
|
$output->write(sprintf('%d statements executed!', $lines) . PHP_EOL); |
105
|
|
|
} catch (\PDOException $e) { |
106
|
|
|
$output->write('error!' . PHP_EOL); |
107
|
|
|
|
108
|
|
|
throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); |
109
|
|
|
} |
110
|
|
|
} else { |
111
|
|
|
// Non-PDO Drivers (ie. OCI8 driver) |
112
|
|
|
$stmt = $conn->prepare($sql); |
113
|
|
|
$rs = $stmt->execute(); |
114
|
|
|
|
115
|
|
|
if ($rs) { |
116
|
|
|
$output->writeln('OK!' . PHP_EOL); |
117
|
|
|
} else { |
118
|
|
|
$error = $stmt->errorInfo(); |
119
|
|
|
|
120
|
|
|
$output->write('error!' . PHP_EOL); |
121
|
|
|
|
122
|
|
|
throw new \RuntimeException($error[2], $error[0]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$stmt->closeCursor(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|