|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Db3v4l\Core\SqlExecutor\Forked; |
|
4
|
|
|
|
|
5
|
|
|
use Db3v4l\API\Interfaces\SqlExecutor\Forked\CommandExecutor; |
|
6
|
|
|
use Db3v4l\Service\DatabaseConfigurationManager; |
|
7
|
|
|
use Db3v4l\Util\Process; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Executes sql queries via a separate symfony console command |
|
11
|
|
|
*/ |
|
|
|
|
|
|
12
|
|
|
class PDO extends ForkedExecutor implements CommandExecutor |
|
13
|
|
|
{ |
|
14
|
|
|
const EXECUTION_STRATEGY = 'PDO'; |
|
15
|
|
|
|
|
16
|
|
|
protected $instanceName; |
|
17
|
|
|
/** @var DatabaseConfigurationManager $dbConfigurationManager */ |
|
|
|
|
|
|
18
|
|
|
protected $dbConfigurationManager; |
|
19
|
|
|
|
|
20
|
|
|
public function setInstanceName($instanceName) |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
$this->instanceName = $instanceName; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function setDbConfigurationManager(DatabaseConfigurationManager $dbConfigurationManager) |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
$this->dbConfigurationManager = $dbConfigurationManager; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function getExecuteStatementProcess($sql) |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
return $this->getProcess($sql, self::EXECUTE_COMMAND); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
|
|
|
|
|
36
|
|
|
* @param string $filename |
|
|
|
|
|
|
37
|
|
|
* @return Process |
|
|
|
|
|
|
38
|
|
|
*/ |
|
39
|
|
|
public function getExecuteFileProcess($filename) |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->getProcess($filename, self::EXECUTE_FILE); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function getProcess($sqlOrFilename, $action = self::EXECUTE_COMMAND) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
// pass on _all_ env vars, including PATH. Not doing so is deprecated... |
|
47
|
|
|
$env = null; |
|
48
|
|
|
|
|
49
|
|
|
$command = 'php'; |
|
50
|
|
|
$options = [ |
|
51
|
|
|
'bin/dbconsole', |
|
52
|
|
|
'sql:execute', |
|
53
|
|
|
'--only-instances=' . $this->instanceName, |
|
54
|
|
|
'--execute-in-process', |
|
55
|
|
|
'--execution-strategy=' . static::EXECUTION_STRATEGY, |
|
56
|
|
|
'--output-type=json', |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
switch ($action) { |
|
60
|
|
|
case self::EXECUTE_COMMAND: |
|
|
|
|
|
|
61
|
|
|
$options[] = '--sql=' . $sqlOrFilename; |
|
62
|
|
|
break; |
|
63
|
|
|
case self::EXECUTE_FILE: |
|
|
|
|
|
|
64
|
|
|
$options[] = '--file=' . $sqlOrFilename; |
|
65
|
|
|
break; |
|
66
|
|
|
default: |
|
|
|
|
|
|
67
|
|
|
throw new \OutOfBoundsException('Unsupported action: $action'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$instanceConfiguration = $this->dbConfigurationManager->getInstanceConfiguration($this->instanceName); |
|
71
|
|
|
|
|
72
|
|
|
/// @todo add support for charset ? |
|
73
|
|
|
if ($instanceConfiguration != $this->databaseConfiguration) { |
|
74
|
|
|
if (isset($this->databaseConfiguration['dbname'])) { |
|
75
|
|
|
$options[] = '--database=' . $this->databaseConfiguration['dbname']; |
|
76
|
|
|
} |
|
77
|
|
|
if (isset($this->databaseConfiguration['user'])) { |
|
78
|
|
|
$options[] = '--user=' . $this->databaseConfiguration['user']; |
|
79
|
|
|
} |
|
80
|
|
|
if (isset($this->databaseConfiguration['password'])) { |
|
81
|
|
|
$options[] = '--password=' . $this->databaseConfiguration['password']; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$commandLine = $this->buildCommandLine($command, $options); |
|
86
|
|
|
|
|
87
|
|
|
return new Process($commandLine, null, $env); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function resultSetToArray($data) |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
$data = json_encode($data); |
|
93
|
|
|
return $data; |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|