| Conditions | 28 |
| Paths | 385 |
| Total Lines | 146 |
| Code Lines | 87 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |
||
| 269 |