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