1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Db3v4l\Core\SqlExecutor\Forked; |
4
|
|
|
|
5
|
|
|
use Db3v4l\API\Interfaces\SqlExecutor\Forked\CommandExecutor; |
6
|
|
|
use Db3v4l\API\Interfaces\SqlExecutor\Forked\FileExecutor; |
7
|
|
|
use Db3v4l\API\Interfaces\SqlExecutor\Forked\ShellExecutor; |
8
|
|
|
use Db3v4l\Util\Process; |
9
|
|
|
|
10
|
|
|
/** |
|
|
|
|
11
|
|
|
* @todo allow to inject path of db clients via setter/constructor |
12
|
|
|
*/ |
|
|
|
|
13
|
|
|
class NativeClient extends ForkedExecutor implements CommandExecutor, FileExecutor, ShellExecutor |
14
|
|
|
{ |
15
|
|
|
const EXECUTE_COMMAND = 0; |
16
|
|
|
const EXECUTE_FILE = 1; |
17
|
|
|
const EXECUTE_SHELL = 2; |
18
|
|
|
|
19
|
|
|
/** |
|
|
|
|
20
|
|
|
* @param string $sql |
|
|
|
|
21
|
|
|
* @return Process |
|
|
|
|
22
|
|
|
*/ |
23
|
|
|
public function getExecuteStatementProcess($sql) |
24
|
|
|
{ |
25
|
|
|
return $this->getProcess($sql, self::EXECUTE_COMMAND); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
|
|
|
|
29
|
|
|
* @param string $filename |
|
|
|
|
30
|
|
|
* @return Process |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
public function getExecuteFileProcess($filename) |
33
|
|
|
{ |
34
|
|
|
return $this->getProcess($filename, self::EXECUTE_FILE); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getExecuteShellProcess() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
return $this->getProcess(null, self::EXECUTE_SHELL); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
|
|
|
|
43
|
|
|
* @param string $sqlOrFilename |
|
|
|
|
44
|
|
|
* @param int $action |
|
|
|
|
45
|
|
|
* @return Process |
|
|
|
|
46
|
|
|
*/ |
47
|
|
|
protected function getProcess($sqlOrFilename, $action = self::EXECUTE_COMMAND) |
48
|
|
|
{ |
49
|
|
|
$clientType = $this->getDbClientType($this->databaseConfiguration); |
50
|
|
|
|
51
|
|
|
// pass on _all_ env vars, including PATH. Not doing so is deprecated... |
52
|
|
|
$env = null; |
53
|
|
|
|
54
|
|
|
switch ($clientType) { |
55
|
|
|
case 'mysql': |
|
|
|
|
56
|
|
|
$command = 'mysql'; |
57
|
|
|
$options = [ |
58
|
|
|
'--host=' . $this->databaseConfiguration['host'], |
59
|
|
|
'--port=' . $this->databaseConfiguration['port'] ?? '3306', |
60
|
|
|
'--user=' . $this->databaseConfiguration['user'], |
61
|
|
|
'-p' . $this->databaseConfiguration['password'], |
62
|
|
|
'--binary-mode', // 'It also disables all mysql commands except charset and delimiter in non-interactive mode (for input piped to mysql or loaded using the source command)' |
63
|
|
|
'-t', |
64
|
|
|
]; |
65
|
|
|
if (isset($this->databaseConfiguration['dbname'])) { |
|
|
|
|
66
|
|
|
$options[] = $this->databaseConfiguration['dbname']; |
67
|
|
|
} |
|
|
|
|
68
|
|
|
if ($action == self::EXECUTE_COMMAND) { |
|
|
|
|
69
|
|
|
$options[] = '--execute=' . $sqlOrFilename; |
70
|
|
|
} |
|
|
|
|
71
|
|
|
// $env = [ |
72
|
|
|
// problematic when wrapping the process in a call to `time`... |
73
|
|
|
//'MYSQL_PWD' => $this->databaseConfiguration['password'], |
74
|
|
|
//]; |
75
|
|
|
break; |
76
|
|
|
|
77
|
|
|
case 'psql': |
|
|
|
|
78
|
|
|
$command = 'psql'; |
79
|
|
|
$connectString = "postgresql://".$this->databaseConfiguration['user'].":".$this->databaseConfiguration['password']. |
80
|
|
|
"@{$this->databaseConfiguration['host']}:".($this->databaseConfiguration['port'] ?? '5432').'/'; |
81
|
|
|
if (isset($this->databaseConfiguration['dbname'])) { |
|
|
|
|
82
|
|
|
$connectString .= $this->databaseConfiguration['dbname']; |
83
|
|
|
} |
|
|
|
|
84
|
|
|
$options = [ |
85
|
|
|
$connectString, |
86
|
|
|
'-Pfooter=off' |
87
|
|
|
]; |
88
|
|
|
// NB: this triggers a different behaviour that piping multiple commands to stdin, namely |
89
|
|
|
// it wraps all of the commands in a transaction and allows either sql commands or a single meta-command |
90
|
|
|
if ($action == self::EXECUTE_COMMAND) { |
|
|
|
|
91
|
|
|
$options[] = '--command=' . $sqlOrFilename; |
92
|
|
|
} |
|
|
|
|
93
|
|
|
//$env = [ |
94
|
|
|
// problematic when wrapping the process in a call to `time`... |
95
|
|
|
//'PGPASSWORD' => $this->databaseConfiguration['password'], |
96
|
|
|
//]; |
97
|
|
|
break; |
98
|
|
|
|
99
|
|
|
case 'sqlcmd': |
|
|
|
|
100
|
|
|
$command = 'sqlcmd'; |
101
|
|
|
$options = [ |
102
|
|
|
'-S' . $this->databaseConfiguration['host'] . ($this->databaseConfiguration['port'] != '' ? ',' . $this->databaseConfiguration['port'] : ''), |
103
|
|
|
'-U' . $this->databaseConfiguration['user'], |
104
|
|
|
'-P' . $this->databaseConfiguration['password'], |
105
|
|
|
'-r1', |
106
|
|
|
'-b', |
107
|
|
|
]; |
108
|
|
|
if (isset($this->databaseConfiguration['dbname'])) { |
|
|
|
|
109
|
|
|
$options[] = '-d' . $this->databaseConfiguration['dbname']; |
110
|
|
|
} |
|
|
|
|
111
|
|
|
if ($action == self::EXECUTE_FILE) { |
|
|
|
|
112
|
|
|
$options[] = '-i' . $sqlOrFilename; |
113
|
|
|
} elseif ($action == self::EXECUTE_COMMAND) { |
|
|
|
|
114
|
|
|
$options[] = '-Q' . $sqlOrFilename; |
115
|
|
|
} |
|
|
|
|
116
|
|
|
break; |
117
|
|
|
|
118
|
|
|
case 'sqlite': |
|
|
|
|
119
|
|
|
$command = 'sqlite3'; |
120
|
|
|
// 'path' is the full path to the 'master' db (for Doctrine compatibility). |
121
|
|
|
// non-master dbs are supposed to reside in the same directory |
122
|
|
|
if (isset($this->databaseConfiguration['dbname'])) { |
|
|
|
|
123
|
|
|
$options[] = dirname($this->databaseConfiguration['path']) . '/' . $this->databaseConfiguration['dbname'] . '.sqlite'; |
|
|
|
|
124
|
|
|
} else { |
|
|
|
|
125
|
|
|
$options[] = $this->databaseConfiguration['path']; |
126
|
|
|
} |
|
|
|
|
127
|
|
|
|
128
|
|
|
if ($action == self::EXECUTE_COMMAND) { |
|
|
|
|
129
|
|
|
$options[] = $sqlOrFilename; |
130
|
|
|
} |
|
|
|
|
131
|
|
|
break; |
132
|
|
|
|
133
|
|
|
// case 'sqlplus': |
134
|
|
|
// $command = 'sqlplus'; |
135
|
|
|
// // pass on _all_ env vars, including PATH |
136
|
|
|
// $env = null; |
137
|
|
|
// break; |
138
|
|
|
default: |
|
|
|
|
139
|
|
|
throw new \OutOfBoundsException("Unsupported db client '$clientType'"); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$commandLine = $this->buildCommandLine($command, $options); |
143
|
|
|
|
144
|
|
|
/// @todo investigate: for psql is this better done via --file ? |
145
|
|
|
if ($action == self::EXECUTE_FILE && $clientType != 'sqlsrv') { |
146
|
|
|
$commandLine .= ' < ' . escapeshellarg($sqlOrFilename); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return new Process($commandLine, null, $env); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
|
|
|
|
153
|
|
|
* @see https://www.doctrine-project.org/projects/doctrine-dbal/en/2.10/reference/configuration.html for supported aliases |
|
|
|
|
154
|
|
|
* @param array $connectionConfiguration |
|
|
|
|
155
|
|
|
* @return string |
|
|
|
|
156
|
|
|
*/ |
157
|
|
|
protected function getDbClientType(array $connectionConfiguration) |
158
|
|
|
{ |
159
|
|
|
$vendor = $connectionConfiguration['vendor']; |
160
|
|
|
return str_replace( |
161
|
|
|
array('mariadb', 'mssql', 'oracle', 'postgresql'), |
162
|
|
|
array('mysql', 'sqlcmd', 'sqlplus', 'psql'), |
163
|
|
|
$vendor |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Transforms a resultSet string, formatted as per the default way of the db client, into an array |
169
|
|
|
* @todo tested on single-column SELECTs so far |
|
|
|
|
170
|
|
|
* @param $string |
|
|
|
|
171
|
|
|
* @return string[] |
|
|
|
|
172
|
|
|
*/ |
173
|
|
|
public function resultSetToArray($string) |
174
|
|
|
{ |
175
|
|
|
switch ($this->databaseConfiguration['vendor']) { |
176
|
|
|
case 'mariadb': |
|
|
|
|
177
|
|
|
case 'mysql': |
|
|
|
|
178
|
|
|
// 'table format', triggered by using the -t option for the client |
179
|
|
|
$output = explode("\n", $string); |
180
|
|
|
array_shift($output); // '+--+' |
181
|
|
|
array_shift($output); // headers |
182
|
|
|
array_shift($output); // '+--+' |
183
|
|
|
array_pop($output); // '+--+' |
184
|
|
|
foreach($output as &$line) { |
|
|
|
|
185
|
|
|
$line = trim($line, '|'); |
186
|
|
|
$line = trim($line); |
187
|
|
|
} |
|
|
|
|
188
|
|
|
return $output; |
189
|
|
|
//case 'oracle': |
190
|
|
|
case 'postgresql': |
|
|
|
|
191
|
|
|
$output = explode("\n", $string); |
192
|
|
|
array_shift($output); // headers |
193
|
|
|
array_shift($output); // '---' |
194
|
|
|
//array_pop($output); // '(N rows)' |
195
|
|
|
foreach($output as &$line) { |
|
|
|
|
196
|
|
|
$line = trim($line); |
197
|
|
|
} |
|
|
|
|
198
|
|
|
return $output; |
199
|
|
|
case 'sqlite': |
|
|
|
|
200
|
|
|
$output = explode("\n", $string); |
201
|
|
|
return $output; |
202
|
|
|
case 'mssql': |
|
|
|
|
203
|
|
|
$output = explode("\n", $string); |
204
|
|
|
array_shift($output); |
205
|
|
|
array_shift($output); // '---' |
206
|
|
|
array_pop($output); // blank line |
207
|
|
|
array_pop($output); // '(N rows affected)' |
208
|
|
|
foreach($output as &$line) { |
|
|
|
|
209
|
|
|
$line = trim($line); |
210
|
|
|
} |
|
|
|
|
211
|
|
|
return $output; |
212
|
|
|
default: |
|
|
|
|
213
|
|
|
throw new \OutOfBoundsException("Unsupported database type '{$this->databaseConfiguration['vendor']}'"); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|