-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.pdo-pgsql.php
71 lines (65 loc) · 1.84 KB
/
db.pdo-pgsql.php
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
<?php
class db {
private $db;
private $sql = '';
private $qry;
private $rows = array();
private $row = array();
private $nullValue = null; // wtf really
public function __construct() {
try {
$this->db = new PDO("pgsql:host=".file_get_contents('./dbhost')." dbname=idol user=idol password=".file_get_contents('./dbpass'));
} catch(PDOException $e) {
exit("Database connection failed: {$e->getMessage()}");
}
}
// public function sql(string $sql): void
// {
// $this->sql = $sql;
// }
public function prepare(string $sql) {
try {
$this->qry = $this->db->prepare($sql);
// $this->qry->bindParam(':null', $this->nullValue, PDO::PARAM_INT);
} catch(PDOException $e) {
exit("Error running query: {$e->getMessage()}");
}
}
public function execute(array $fieldMaps = array()) {
if(count($fieldMaps) == 0) $fieldMaps = null;
try {
$result = $this->qry->execute($fieldMaps);
$err = $this->db->errorInfo();
if(false === $result && $err[0] != '00000') {
$s = $this->sql;
foreach($fieldMaps as $k => $v) $s = strtr($s, array($k => "'".strtr($v, array("'" => "''"))."'"));
message("Execution failed: ".print_r($err, true)."\n\nSQL was: {$s}", true);
}
} catch(PDOException $e) {
exit("Execution failed: {$e->getMessage()}");
}
}
public function fetchAll(): array
{
try {
$rows = $this->qry->fetchAll(PDO::FETCH_ASSOC);
if(false === $rows) $rows = array();
} catch(PDOException $e) {
exit("Fetch failed: {$e->getMessage()}");
}
return($rows);
}
public function query(string $sql, array $fieldMaps = array()): array
{
try {
// if(count($fieldMaps) > 0) exit(print_r($fieldMaps, true));
$this->sql = $sql;
$this->prepare($sql);
$this->execute($fieldMaps);
} catch(PDOException $e) {
exit("Full query failed: {$e->getMessage()}");
}
return($this->fetchAll());
}
}
?>