DB.class.php 1.37 KB
Newer Older
1
2
<?php

3
4
include_once("confparser.inc.php");

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 {
20
				$backend = config_get('database', 'backend');
21
22
				$host = config_get('database', 'host');
				$socket = config_get('database', 'socket');
Leonidas Spyropoulos's avatar
WIP2    
Leonidas Spyropoulos committed
23
				$port = config_get('database', 'port');
24
25
26
27
				$name = config_get('database', 'name');
				$user = config_get('database', 'user');
				$password = config_get('database', 'password');

28
				if ($backend == "mysql") {
Leonidas Spyropoulos's avatar
WIP2    
Leonidas Spyropoulos committed
29
30
31
32
33
34
35
36
37
38
39
					if ($port != '') {
						$dsn = $backend .
							':host=' . $host .
							';port=' . $port .
							';dbname=' . $name;
					} else {
						$dsn = $backend .
							':host=' . $host .
							';unix_socket=' . $socket .
							';dbname=' . $name;
					}
40
41
42
43
44
45
46
47
48
49
50

					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");
				}
51

52
53
54
55
56
57
58
59
			} catch (PDOException $e) {
				die('Error - Could not connect to AUR database');
			}
		}

		return self::$dbh;
	}
}