Skip to content

Commit

Permalink
mysqli: support UNIX sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
gerryd committed Feb 5, 2024
1 parent 3d1e878 commit 8ac134e
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/Skeleton/Database/Driver/Mysqli/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,30 @@ public function __construct($dsn) {
*/
public function connect() : bool {
mysqli_report(MYSQLI_REPORT_OFF);

$settings = parse_url($this->dsn);

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

// We don't support connecting to UNIX sockets the traditional way
// UNIX sockets can be used by setting host to unix(/path/to/socket)
if ($settings['host'] == 'unix(') {
throw new \Skeleton\Database\Exception\Connection('Could not connect to database: UNIX socket syntax is wrong');
$settings['socket'] = strtok($settings['path'], ')');
$settings['path'] = strtok('');
$settings['host'] = 'localhost';
} else {
$settings['socket'] = null;
}

if (!isset($settings['port'])) {
$settings['port'] = null;
}

$settings['path'] = substr($settings['path'], 1);
$this->database = @new \Mysqli($settings['host'], $settings['user'], $settings['pass'], $settings['path']);

$this->database = @new \Mysqli($settings['host'], $settings['user'], $settings['pass'], $settings['path'], $settings['port'], $settings['socket']);

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

0 comments on commit 8ac134e

Please sign in to comment.