forked from wattsjus/plinq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlcon.php
More file actions
89 lines (79 loc) · 2.54 KB
/
sqlcon.php
File metadata and controls
89 lines (79 loc) · 2.54 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
declare(strict_types=1);
namespace pLinq;
use PDO;
use PDOException;
class sqlcon extends dbconnbase implements dbconninf
{
private $conn;
private $stmt = null;
private $debug = false;
public function __construct($driver, $host, $dbname, $username, $password, $socket = null) {
try {
if ($socket) {
$dsn = "$driver:unix_socket=$socket;dbname=$dbname";
} else {
$dsn = "$driver:host=$host;dbname=$dbname";
}
$this->conn = new PDO($dsn, $username, $password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$this->logException('Failed to connect to database: ' . $e->getMessage());
}
}
public function getDBType(){
return $this->conn->getAttribute(PDO::ATTR_DRIVER_NAME);
}
public function query(string $sql) {
try {
if($this->debug) {
$start = microtime(true);
}
$stmt = $this->conn->prepare($sql);
$stmt->execute();
if($this->debug) {
$end = microtime(true);
$this->logQuery("=================PDO Wrapper=====================\n");
$this->logQuery($sql);
$this->logQuery($end - $start);
$this->logQuery("=======================End========================\n");
}
$this->stmt = $stmt;
return $this->stmt;
} catch (PDOException $e) {
$this->logException('Failed to connect to database: ' . $e->getMessage());
}
return false;
}
public function returnArray()
{
try {
if (!empty($this->stmt)) {
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}else{
return false;
}
} catch (PDOException $e) {
$this->logException("Error fetching all rows: " . $e->getMessage());
}
return false;
}
public function returnOneArray() {
try {
if (!empty($this->stmt)) {
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}else{
return false;
}
} catch (PDOException $e) {
$this->logException("Error fetching one rows: " . $e->getMessage());
}
return false;
}
public function escape(string $value) {
return $this->conn->quote($value);
}
public function close() {
$this->conn = null;
}
}