mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
In commit baf8a22
(git-interface: Support SQLite as database backend,
2016-08-03), conf/config.proto was changed so that dsn_prefix was
changed to backend and this fixes this in web/lib/DB.class.php.
Since SQLite's dsn is different, this adds a check of which backend is
desired and will quit if MySQL or SQLite are not the backend selected.
SQLite2 may be supported, but is untested and will trigger an error if
used.
Signed-off-by: Mark Weiman <mark.weiman@markzz.com>
Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
51 lines
1.2 KiB
PHP
51 lines
1.2 KiB
PHP
<?php
|
|
|
|
include_once("confparser.inc.php");
|
|
|
|
class DB {
|
|
|
|
/**
|
|
* A database object
|
|
*/
|
|
private static $dbh = null;
|
|
|
|
/**
|
|
* Return an already existing database object or newly instantiated object
|
|
*
|
|
* @return \PDO A database connection using PDO
|
|
*/
|
|
public static function connect() {
|
|
if (self::$dbh === null) {
|
|
try {
|
|
$backend = config_get('database', 'backend');
|
|
$host = config_get('database', 'host');
|
|
$socket = config_get('database', 'socket');
|
|
$name = config_get('database', 'name');
|
|
$user = config_get('database', 'user');
|
|
$password = config_get('database', 'password');
|
|
|
|
if ($backend == "mysql") {
|
|
$dsn = $backend .
|
|
':host=' . $host .
|
|
';unix_socket=' . $socket .
|
|
';dbname=' . $name;
|
|
|
|
self::$dbh = new PDO($dsn, $user, $password);
|
|
self::$dbh->exec("SET NAMES 'utf8' COLLATE 'utf8_general_ci';");
|
|
} else if ($backend == "sqlite") {
|
|
$dsn = $backend .
|
|
":" . $name;
|
|
|
|
self::$dbh = new PDO($dsn, null, null);
|
|
} else {
|
|
die("Error - " . $backend . " is not supported by aurweb");
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
die('Error - Could not connect to AUR database');
|
|
}
|
|
}
|
|
|
|
return self::$dbh;
|
|
}
|
|
}
|