Total Complexity | 44 |
Total Lines | 253 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like NativeClient often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NativeClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class NativeClient extends ForkedExecutor implements CommandExecutor, FileExecutor, ShellExecutor |
||
14 | { |
||
15 | const EXECUTION_STRATEGY = 'NativeClient'; |
||
16 | const EXECUTE_SHELL = 2; |
||
17 | |||
18 | /** |
||
19 | * @param string $sql |
||
20 | * @return Process |
||
21 | */ |
||
22 | public function getExecuteStatementProcess($sql) |
||
23 | { |
||
24 | return $this->getProcess($sql, self::EXECUTE_COMMAND); |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @param string $filename |
||
29 | * @return Process |
||
30 | */ |
||
31 | public function getExecuteFileProcess($filename) |
||
32 | { |
||
33 | return $this->getProcess($filename, self::EXECUTE_FILE); |
||
34 | } |
||
35 | |||
36 | public function getExecuteShellProcess() |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param string $sqlOrFilename |
||
43 | * @param int $action |
||
44 | * @return Process |
||
45 | */ |
||
46 | protected function getProcess($sqlOrFilename, $action = self::EXECUTE_COMMAND) |
||
47 | { |
||
48 | $clientType = $this->getDbClientType($this->databaseConfiguration); |
||
49 | |||
50 | // pass on _all_ env vars, including PATH. Not doing so is deprecated... |
||
51 | $env = null; |
||
52 | |||
53 | switch ($clientType) { |
||
54 | case 'mysql': |
||
55 | $command = 'mysql'; |
||
56 | $options = [ |
||
57 | '--host=' . $this->databaseConfiguration['host'], |
||
58 | '--port=' . $this->databaseConfiguration['port'] ?? '3306', |
||
59 | '--user=' . $this->databaseConfiguration['user'], |
||
60 | '-p' . $this->databaseConfiguration['password'], |
||
61 | '--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)' |
||
62 | '-t', |
||
63 | ]; |
||
64 | if (isset($this->databaseConfiguration['dbname'])) { |
||
65 | $options[] = $this->databaseConfiguration['dbname']; |
||
66 | } |
||
67 | if ($action == self::EXECUTE_COMMAND) { |
||
68 | $options[] = '--execute=' . $sqlOrFilename; |
||
69 | } |
||
70 | // $env = [ |
||
71 | // problematic when wrapping the process in a call to `time`... |
||
72 | //'MYSQL_PWD' => $this->databaseConfiguration['password'], |
||
73 | //]; |
||
74 | break; |
||
75 | |||
76 | case 'psql': |
||
77 | $command = 'psql'; |
||
78 | $connectString = "postgresql://".$this->databaseConfiguration['user'].":".$this->databaseConfiguration['password']. |
||
79 | "@{$this->databaseConfiguration['host']}:".($this->databaseConfiguration['port'] ?? '5432').'/'; |
||
80 | if (isset($this->databaseConfiguration['dbname'])) { |
||
81 | $connectString .= $this->databaseConfiguration['dbname']; |
||
82 | } |
||
83 | $options = [ |
||
84 | $connectString, |
||
85 | '-Pfooter=off' |
||
86 | ]; |
||
87 | // NB: this triggers a different behaviour that piping multiple commands to stdin, namely |
||
88 | // it wraps all of the commands in a transaction and allows either sql commands or a single meta-command |
||
89 | if ($action == self::EXECUTE_COMMAND) { |
||
90 | $options[] = '--command=' . $sqlOrFilename; |
||
91 | } |
||
92 | //$env = [ |
||
93 | // problematic when wrapping the process in a call to `time`... |
||
94 | //'PGPASSWORD' => $this->databaseConfiguration['password'], |
||
95 | //]; |
||
96 | break; |
||
97 | |||
98 | case 'sqlcmd': |
||
99 | $command = 'sqlcmd'; |
||
100 | $options = [ |
||
101 | '-S' . $this->databaseConfiguration['host'] . ($this->databaseConfiguration['port'] != '' ? ',' . $this->databaseConfiguration['port'] : ''), |
||
102 | '-U' . $this->databaseConfiguration['user'], |
||
103 | '-P' . $this->databaseConfiguration['password'], |
||
104 | '-r1', |
||
105 | '-b', |
||
106 | ]; |
||
107 | if (isset($this->databaseConfiguration['dbname'])) { |
||
108 | $options[] = '-d' . $this->databaseConfiguration['dbname']; |
||
109 | } |
||
110 | if ($action == self::EXECUTE_FILE) { |
||
111 | $options[] = '-i' . $sqlOrFilename; |
||
112 | } elseif ($action == self::EXECUTE_COMMAND) { |
||
113 | $options[] = '-Q' . $sqlOrFilename; |
||
114 | } |
||
115 | break; |
||
116 | |||
117 | case 'sqlite': |
||
118 | $command = 'sqlite3'; |
||
119 | // 'path' is the full path to the 'master' db (for Doctrine compatibility). |
||
120 | // non-master dbs are supposed to reside in the same directory |
||
121 | if (isset($this->databaseConfiguration['dbname'])) { |
||
122 | $options[] = dirname($this->databaseConfiguration['path']) . '/' . $this->databaseConfiguration['dbname'] . '.sqlite'; |
||
123 | } else { |
||
124 | $options[] = $this->databaseConfiguration['path']; |
||
125 | } |
||
126 | |||
127 | if ($action == self::EXECUTE_COMMAND) { |
||
128 | $options[] = $sqlOrFilename; |
||
129 | } |
||
130 | break; |
||
131 | |||
132 | case 'sqlplus': |
||
133 | /// @todo disable execution of dangerous (or all) SQLPLUS commands. |
||
134 | /// See the list at https://docs.oracle.com/en/database/oracle/oracle-database/18/sqpug/SQL-Plus-command-reference.html#GUID-177F24B7-D154-4F8B-A05B-7568079800C6 |
||
135 | $command = 'sqlplus'; |
||
136 | $connectionIdentifier = '//' . $this->databaseConfiguration['host'] . |
||
137 | ($this->databaseConfiguration['port'] != '' ? ':' . $this->databaseConfiguration['port'] : ''); |
||
138 | // nb: for oracle, if we use pdbs to map 'databases', they get a new service name |
||
139 | /// @todo allow support for _not_ doing that, and using schemas as 'databases' |
||
140 | if (isset($this->databaseConfiguration['servicename'])) { |
||
141 | $connectionIdentifier .= '/' . $this->databaseConfiguration['servicename']; |
||
142 | } else { |
||
143 | if ($this->databaseConfiguration['dbname'] != '') { |
||
144 | $connectionIdentifier .= '/' . $this->databaseConfiguration['dbname']; |
||
145 | } |
||
146 | } |
||
147 | $options = [ |
||
148 | '-L', // 'attempts to log in just once, instead of reprompting on error' |
||
149 | '-NOLOGINTIME', |
||
150 | '-S', |
||
151 | $this->databaseConfiguration['user'] . '/' . $this->databaseConfiguration['password'] . '@' . $connectionIdentifier, |
||
152 | ]; |
||
153 | /// @todo make this optional somehow / allow SYSOPER as alternative |
||
154 | if (strtolower($this->databaseConfiguration['user']) === 'sys') { |
||
155 | $options[] = 'AS'; |
||
156 | $options[] = 'SYSDBA'; |
||
157 | } |
||
158 | if ($action == self::EXECUTE_FILE) { |
||
159 | /// @todo wouldn't it be better to create another temp file with prefixed the sqlplus commands? |
||
160 | //$options[] = '@' . $sqlOrFilename; |
||
161 | $sqlOrFilename = "WHENEVER OSERROR EXIT FAILURE;\nWHENEVER SQLERROR EXIT SQL.SQLCODE;\nSET PAGESIZE 50000;\nSET FEEDBACK OFF;\n" . file_get_contents($sqlOrFilename); |
||
162 | $action = self::EXECUTE_COMMAND; |
||
163 | } else { |
||
164 | $sqlOrFilename = "WHENEVER OSERROR EXIT FAILURE;\nWHENEVER SQLERROR EXIT SQL.SQLCODE;\nSET PAGESIZE 50000;\nSET FEEDBACK OFF;\n" . $sqlOrFilename; |
||
165 | } |
||
166 | |||
167 | break; |
||
168 | |||
169 | default: |
||
170 | throw new \OutOfBoundsException("Unsupported db client '$clientType'"); |
||
171 | } |
||
172 | |||
173 | $commandLine = $this->buildCommandLine($command, $options); |
||
174 | |||
175 | /// @todo investigate: for psql is this better done via --file ? |
||
176 | if ($action == self::EXECUTE_FILE && $clientType != 'sqlsrv' && $clientType != 'sqlplus') { |
||
177 | $commandLine .= ' < ' . escapeshellarg($sqlOrFilename); |
||
178 | } |
||
179 | |||
180 | if ($action == self::EXECUTE_COMMAND && $clientType == 'sqlplus') { |
||
181 | $commandLine .= " << 'SQLEOF'\n" . $sqlOrFilename . "\nSQLEOF"; |
||
182 | } |
||
183 | |||
184 | $process = Process::fromShellCommandline($commandLine, null, $env); |
||
185 | |||
186 | if ($action == self::EXECUTE_COMMAND && $clientType == 'sqlplus') { |
||
187 | // The way Symfony Process deals with sighchildEnabled breaks EOF handling. We disable it |
||
188 | $process->forceSigchildEnabledIndividually(false); |
||
189 | } |
||
190 | |||
191 | return $process; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @see https://www.doctrine-project.org/projects/doctrine-dbal/en/2.10/reference/configuration.html for supported aliases |
||
196 | * @param array $connectionConfiguration |
||
197 | * @return string |
||
198 | */ |
||
199 | protected function getDbClientType(array $connectionConfiguration) |
||
206 | ); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Transforms a resultSet string, formatted as per the default way of the db client, into an array |
||
211 | * @todo tested on single-column SELECTs so far |
||
212 | * @param $string |
||
213 | * @return string[] |
||
214 | */ |
||
215 | public function resultSetToArray($string) |
||
269 |