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
|
|
|
* Relies on the 'sql:execute' dbconsole command |
12
|
|
|
*/ |
|
|
|
|
13
|
|
|
class PDO extends ForkedExecutor implements CommandExecutor |
14
|
|
|
{ |
15
|
|
|
const EXECUTION_STRATEGY = 'PDO'; |
16
|
|
|
|
17
|
|
|
protected $instanceName; |
18
|
|
|
/** @var DatabaseConfigurationManager $dbConfigurationManager */ |
|
|
|
|
19
|
|
|
protected $dbConfigurationManager; |
20
|
|
|
|
21
|
|
|
public function setInstanceName($instanceName) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$this->instanceName = $instanceName; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function setDbConfigurationManager(DatabaseConfigurationManager $dbConfigurationManager) |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$this->dbConfigurationManager = $dbConfigurationManager; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getExecuteStatementProcess($sql) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
return $this->getProcess($sql, self::EXECUTE_COMMAND); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
|
|
|
|
37
|
|
|
* @param string $filename |
|
|
|
|
38
|
|
|
* @return Process |
|
|
|
|
39
|
|
|
*/ |
40
|
|
|
public function getExecuteFileProcess($filename) |
41
|
|
|
{ |
42
|
|
|
return $this->getProcess($filename, self::EXECUTE_FILE); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function getProcess($sqlOrFilename, $action = self::EXECUTE_COMMAND) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
// pass on _all_ env vars, including PATH. Not doing so is deprecated... |
48
|
|
|
$env = null; |
49
|
|
|
|
50
|
|
|
$command = 'php'; |
51
|
|
|
$options = [ |
52
|
|
|
'bin/dbconsole', |
53
|
|
|
'sql:execute', |
54
|
|
|
'--only-instances=' . $this->instanceName, |
55
|
|
|
'--execute-in-process', |
56
|
|
|
'--execution-strategy=' . static::EXECUTION_STRATEGY, |
57
|
|
|
'--output-type=json', |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
switch ($action) { |
61
|
|
|
case self::EXECUTE_COMMAND: |
|
|
|
|
62
|
|
|
$options[] = '--sql=' . $sqlOrFilename; |
63
|
|
|
break; |
64
|
|
|
case self::EXECUTE_FILE: |
|
|
|
|
65
|
|
|
$options[] = '--file=' . $sqlOrFilename; |
66
|
|
|
break; |
67
|
|
|
default: |
|
|
|
|
68
|
|
|
throw new \OutOfBoundsException('Unsupported action: $action'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$instanceConfiguration = $this->dbConfigurationManager->getInstanceConfiguration($this->instanceName); |
72
|
|
|
|
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
|
|
|
/// @todo create Process from array |
88
|
|
|
return new Process($commandLine, null, $env); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function resultSetToArray($data) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$data = json_encode($data); |
94
|
|
|
return $data; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|