4 use Civi\Setup\Exception\SqlException
;
12 public static function parseDsn($dsn) {
13 $parsed = parse_url($dsn);
15 'server' => self
::encodeHostPort($parsed['host'], $parsed['port'] ??
NULL),
16 'username' => $parsed['user'] ?
: NULL,
17 'password' => $parsed['pass'] ?
: NULL,
18 'database' => $parsed['path'] ?
ltrim($parsed['path'], '/') : NULL,
19 'ssl_params' => self
::parseSSL($parsed['query'] ??
NULL),
24 * @todo Is this used anywhere? It doesn't support SSL as-is.
25 * Convert an datasource from array notation to URL notation.
30 public static function encodeDsn($db) {
31 return sprintf('mysql://%s:%s@%s/%s',
43 public static function softConnect($db) {
44 list($host, $port) = self
::decodeHostPort($db['server']);
45 if (empty($db['ssl_params'])) {
46 $conn = @mysqli_connect
($host, $db['username'], $db['password'], $db['database'], $port);
50 $init = mysqli_init();
53 $db['ssl_params']['key'] ??
NULL,
54 $db['ssl_params']['cert'] ??
NULL,
55 $db['ssl_params']['ca'] ??
NULL,
56 $db['ssl_params']['capath'] ??
NULL,
57 $db['ssl_params']['cipher'] ??
NULL
59 // @todo socket parameter, but if you're using sockets do you need SSL?
60 if (@mysqli_real_connect
($init, $host, $db['username'], $db['password'], $db['database'], $port, NULL, MYSQLI_CLIENT_SSL
)) {
70 * @throws SqlException
72 public static function connect($db) {
73 $conn = self
::softConnect($db);
74 if (mysqli_connect_errno()) {
75 throw new SqlException(sprintf("Connection failed: %s\n", mysqli_connect_error()));
86 * Ex: '[1234:abcd]:123'
88 * Combination: [0 => string $host, 1 => numeric|NULL $port].
89 * Ex: ['localhost', NULL].
90 * Ex: ['127.0.0.1', 3306]
92 public static function decodeHostPort($host) {
93 $hostParts = explode(':', $host);
94 if (count($hostParts) > 1 && strrpos($host, ']') !== strlen($host) - 1) {
95 $port = array_pop($hostParts);
96 $host = implode(':', $hostParts);
101 return array($host, $port);
105 * Combine a host and port number.
107 * @param string $host
108 * @param int|NULL $port
111 * Ex: '127.0.0.1:3307'.
113 public static function encodeHostPort($host, $port) {
114 return $host . ($port ?
(':' . $port) : '');
118 * For SSL you can have client certificates, which has some required and
119 * optional parameters, or you can have anonymous SSL, which just requires
120 * some indication that you want that.
122 * @param string $query_string
125 public static function parseSSL($query_string) {
126 if (empty($query_string)) {
129 parse_str($query_string, $parsed_query);
130 $sensible_parameters = [
131 // ssl=1 alone means no client certificate - it's not a real mysqli option
139 // Only want to include a param if it's in our list of sensibles, e.g.
140 // we don't want new_link=true.
141 return array_intersect_key($parsed_query, $sensible_parameters);
146 * @param string $SQLcontent
147 * @param bool $lineMode
148 * What does this mean? Seems weird.
150 public static function sourceSQL($db, $SQLcontent, $lineMode = FALSE) {
151 $conn = self
::connect($db);
153 $conn->query('SET NAMES ' . ($conn->server_version
< 50503 ?
'utf8' : 'utf8mb4'));
156 $string = $SQLcontent;
158 // change \r\n to fix windows issues
159 $string = str_replace("\r\n", "\n", $string);
161 //get rid of comments starting with # and --
163 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
164 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
166 $queries = preg_split('/;\s*$/m', $string);
167 foreach ($queries as $query) {
168 $query = trim($query);
169 if (!empty($query)) {
170 if ($result = $conn->query($query)) {
171 if (is_object($result)) {
172 mysqli_free_result($result);
176 throw new SqlException("Cannot execute $query: " . mysqli_error($conn));
182 throw new \
RuntimeException("Not implemented: lineMode");
183 // $fd = fopen($SQLcontent, "r");
184 // while ($string = fgets($fd)) {
185 // $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
186 // $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
188 // $string = trim($string);
189 // if (!empty($string)) {
190 // if ($result = $conn->query($string)) {
191 // if (is_object($result)) {
192 // mysqli_free_result($result);
196 // throw new SqlException("Cannot execute $string: " . mysqli_error($conn));
204 * Execute query. Ignore the results.
206 * @param \mysqli|array $conn
207 * The DB to query. Either a mysqli connection, or credentials for
210 * @throws SqlException
212 public static function execute($conn, $sql) {
213 $conn = is_array($conn) ? self
::connect($conn) : $conn;
214 $result = $conn->query($sql);
216 throw new SqlException("Cannot execute $sql: " . $conn->error
);
219 if ($result && $result !== TRUE) {
220 $result->free_result();
226 * Get all the results of a SQL query, as an array.
228 * @param \mysqli|array $conn
229 * The DB to query. Either a mysqli connection, or credentials for
235 public static function fetchAll($conn, $sql) {
236 $conn = is_array($conn) ? self
::connect($conn) : $conn;
237 $result = $conn->query($sql);
239 throw new SqlException("Cannot execute $sql: " . $conn->error
);
243 while ($row = $result->fetch_assoc()) {
246 $result->free_result();
252 * Get a list of views in the given database.
254 * @param \mysqli|array $conn
255 * The DB to query. Either a mysqli connection, or credentials for
257 * @param string $databaseName
259 * Ex: ['civicrm_view1', 'civicrm_view2']
261 public static function findViews($conn, $databaseName) {
262 $sql = sprintf("SELECT table_name FROM information_schema.TABLES WHERE TABLE_SCHEMA='%s' AND TABLE_TYPE = 'VIEW'",
263 $conn->escape_string($databaseName));
265 return array_map(function($arr) {
266 return $arr['table_name'];
267 }, self
::fetchAll($conn, $sql));
271 * Get a list of concrete tables in the given database.
273 * @param \mysqli|array $conn
274 * The DB to query. Either a mysqli connection, or credentials for
276 * @param string $databaseName
278 * Ex: ['civicrm_view1', 'civicrm_view2']
280 public static function findTables($conn, $databaseName) {
281 $sql = sprintf("SELECT table_name FROM information_schema.TABLES WHERE TABLE_SCHEMA='%s' AND TABLE_TYPE = 'BASE TABLE'",
282 $conn->escape_string($databaseName));
284 return array_map(function($arr) {
285 return $arr['table_name'];
286 }, self
::fetchAll($conn, $sql));