summaryrefslogtreecommitdiff
path: root/classes/db/pgsql.php
blob: 87c2abefddd896d6f1be1d4f83046f6f72c1b0f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
class Db_Pgsql implements IDb {
	private $link;

	function connect($host, $user, $pass, $db, $port) {
		$string = "dbname=$db user=$user";

		if ($pass) {
			$string .= " password=$pass";
		}

		if ($host) {
			$string .= " host=$host";
		}

		if (is_numeric($port) && $port > 0) {
			$string = "$string port=" . $port;
		}

		$this->link = pg_connect($string);

		if (!$this->link) {
			die("Unable to connect to database (as $user to $host, database $db):" . pg_last_error());
		}

		return $this->link;
	}

	function escape_string($s, $strip_tags = true) {
		if ($strip_tags) $s = strip_tags($s);

		return pg_escape_string($this->link, $s);
	}

	function query($query, $die_on_error = true) {
		$result = pg_query($this->link, $query);

		if (!$result) {
			$query = htmlspecialchars($query); // just in case
			if ($die_on_error) {
				die("Query <i>$query</i> failed [$result]: " . ($this->link ? pg_last_error($this->link) : "No connection"));
			}
		}
		return $result;
	}

	function fetch_assoc($result) {
		return pg_fetch_assoc($result);
	}


	function num_rows($result) {
		return pg_num_rows($result);
	}

	function fetch_result($result, $row, $param) {
		return pg_fetch_result($result, $row, $param);
	}

	function close() {
		return pg_close($this->link);
	}

	function affected_rows($result) {
		return pg_affected_rows($result);
	}

	function last_error() {
		return pg_last_error($this->link);
	}

}
?>