Skip to content

Commit 8ac134e

Browse files
committed
mysqli: support UNIX sockets
1 parent 3d1e878 commit 8ac134e

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

lib/Skeleton/Database/Driver/Mysqli/Proxy.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,30 @@ public function __construct($dsn) {
6262
*/
6363
public function connect() : bool {
6464
mysqli_report(MYSQLI_REPORT_OFF);
65+
6566
$settings = parse_url($this->dsn);
6667

6768
// If we can't even parse the DSN, don't bother
6869
if (!isset($settings['path']) OR !isset($settings['host']) OR !isset($settings['user'])) {
6970
throw new \Skeleton\Database\Exception\Connection('Could not connect to database: DSN incorrect');
7071
}
7172

72-
// We don't support connecting to UNIX sockets the traditional way
73+
// UNIX sockets can be used by setting host to unix(/path/to/socket)
7374
if ($settings['host'] == 'unix(') {
74-
throw new \Skeleton\Database\Exception\Connection('Could not connect to database: UNIX socket syntax is wrong');
75+
$settings['socket'] = strtok($settings['path'], ')');
76+
$settings['path'] = strtok('');
77+
$settings['host'] = 'localhost';
78+
} else {
79+
$settings['socket'] = null;
80+
}
81+
82+
if (!isset($settings['port'])) {
83+
$settings['port'] = null;
7584
}
7685

7786
$settings['path'] = substr($settings['path'], 1);
78-
$this->database = @new \Mysqli($settings['host'], $settings['user'], $settings['pass'], $settings['path']);
87+
88+
$this->database = @new \Mysqli($settings['host'], $settings['user'], $settings['pass'], $settings['path'], $settings['port'], $settings['socket']);
7989

8090
// If there is an error connecting to the database, stop doing what you're doing
8191
if ($this->database->connect_errno != 0) {

0 commit comments

Comments
 (0)