Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| database_task | |
0.00% |
0 / 38 |
|
0.00% |
0 / 9 |
272 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| exec_sql | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| query | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| create_prepared_stmt | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| create_and_execute_prepared_stmt | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| exec_prepared_stmt | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| get_last_insert_id | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| report_error | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_doctrine_connection | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | namespace phpbb\install; |
| 15 | |
| 16 | use Doctrine\DBAL\Connection; |
| 17 | use Doctrine\DBAL\Driver\Exception as DriverException; |
| 18 | use Doctrine\DBAL\Driver\Statement as DriverStmt; |
| 19 | use Doctrine\DBAL\Exception; |
| 20 | use Doctrine\DBAL\Result; |
| 21 | use Doctrine\DBAL\Statement; |
| 22 | use phpbb\db\doctrine\connection_factory; |
| 23 | use phpbb\install\helper\config; |
| 24 | use phpbb\install\helper\database; |
| 25 | use phpbb\install\helper\iohandler\iohandler_interface; |
| 26 | |
| 27 | /** |
| 28 | * Abstract base class for common database manipulation tasks. |
| 29 | */ |
| 30 | abstract class database_task extends task_base |
| 31 | { |
| 32 | /** @var Connection */ |
| 33 | private $conn; |
| 34 | |
| 35 | /** @var iohandler_interface */ |
| 36 | private $io; |
| 37 | |
| 38 | /** |
| 39 | * Constructor. |
| 40 | * |
| 41 | * @param Connection $connection Doctrine DBAL connection. |
| 42 | * @param iohandler_interface $io IO handler to use. |
| 43 | * @param bool $essential Whether the task is essential. |
| 44 | */ |
| 45 | public function __construct(Connection $connection, iohandler_interface $io, bool $essential = true) |
| 46 | { |
| 47 | $this->conn = $connection; |
| 48 | $this->io = $io; |
| 49 | |
| 50 | parent::__construct($essential); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Execute a SQL query. |
| 55 | * |
| 56 | * @param string $sql The SQL to execute. |
| 57 | */ |
| 58 | protected function exec_sql(string $sql) |
| 59 | { |
| 60 | try |
| 61 | { |
| 62 | $this->conn->executeStatement($sql); |
| 63 | } |
| 64 | catch (Exception $e) |
| 65 | { |
| 66 | $this->report_error($e->getMessage()); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Run a query and return the result object. |
| 72 | * |
| 73 | * @param string $sql SQL query. |
| 74 | * |
| 75 | * @return Result|null Result of the query. |
| 76 | */ |
| 77 | protected function query(string $sql) : Result|null |
| 78 | { |
| 79 | try |
| 80 | { |
| 81 | return $this->conn->executeQuery($sql); |
| 82 | } |
| 83 | catch (Exception $e) |
| 84 | { |
| 85 | $this->report_error($e->getMessage()); |
| 86 | } |
| 87 | |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Creates a prepared statement. |
| 93 | * |
| 94 | * @param string $sql The SQL. |
| 95 | * |
| 96 | * @return Statement|null The prepared statement object or null if preparing failed |
| 97 | */ |
| 98 | protected function create_prepared_stmt(string $sql): Statement|null |
| 99 | { |
| 100 | try |
| 101 | { |
| 102 | return $this->conn->prepare($sql); |
| 103 | } |
| 104 | catch (Exception $e) |
| 105 | { |
| 106 | $this->report_error($e->getMessage()); |
| 107 | } |
| 108 | |
| 109 | return null; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Create and execute a prepared statement. |
| 114 | * |
| 115 | * @param string $sql The SQL to create the statement from. |
| 116 | * @param array $params The parameters to bind to it. |
| 117 | */ |
| 118 | protected function create_and_execute_prepared_stmt(string $sql, array $params) |
| 119 | { |
| 120 | try |
| 121 | { |
| 122 | $stmt = $this->conn->prepare($sql); |
| 123 | $this->exec_prepared_stmt($stmt, $params); |
| 124 | } |
| 125 | catch (Exception $e) |
| 126 | { |
| 127 | $this->report_error($e->getMessage()); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Bind values and execute a prepared statement. |
| 133 | * |
| 134 | * @param Statement|DriverStmt $stmt Prepared statement. |
| 135 | * @param array $params Parameters. |
| 136 | */ |
| 137 | protected function exec_prepared_stmt($stmt, array $params) |
| 138 | { |
| 139 | try |
| 140 | { |
| 141 | foreach ($params as $name => $val) |
| 142 | { |
| 143 | $stmt->bindValue($name, $val); |
| 144 | } |
| 145 | $stmt->execute(); |
| 146 | } |
| 147 | catch (DriverException $e) |
| 148 | { |
| 149 | $this->report_error($e->getMessage()); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Returns the last insert ID. |
| 155 | * |
| 156 | * @return int|null The last insert ID. |
| 157 | */ |
| 158 | protected function get_last_insert_id() : int|null |
| 159 | { |
| 160 | try |
| 161 | { |
| 162 | return (int) $this->conn->lastInsertId(); |
| 163 | } |
| 164 | catch (Exception $e) |
| 165 | { |
| 166 | $this->report_error($e->getMessage()); |
| 167 | } |
| 168 | |
| 169 | return null; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Report a database error. |
| 174 | * |
| 175 | * @param string $message The error message. |
| 176 | */ |
| 177 | private function report_error(string $message) |
| 178 | { |
| 179 | $this->io->add_error_message('INST_ERR_DB', $message); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Create a Doctrine connection in the installer context. |
| 184 | * |
| 185 | * @param database $db_helper Database helper. |
| 186 | * @param config $config Config options. |
| 187 | * |
| 188 | * @return Connection Doctrine DBAL connection object. |
| 189 | */ |
| 190 | protected static function get_doctrine_connection(database $db_helper, config $config) : Connection |
| 191 | { |
| 192 | $dbms = $db_helper->get_available_dbms($config->get('dbms')); |
| 193 | $dbms = $dbms[$config->get('dbms')]['DRIVER']; |
| 194 | |
| 195 | return connection_factory::get_connection_from_params( |
| 196 | $dbms, |
| 197 | $config->get('dbhost'), |
| 198 | $config->get('dbuser'), |
| 199 | $config->get('dbpasswd'), |
| 200 | $config->get('dbname'), |
| 201 | $config->get('dbport') |
| 202 | ); |
| 203 | } |
| 204 | } |