|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* - - - - - - - - - - - - - - - - - - - - - |
|
4
|
|
|
|
|
5
|
|
|
Title : PHP Quick Profiler MySQL Class |
|
6
|
|
|
Author : Created by Ryan Campbell |
|
7
|
|
|
URL : http://particletree.com/features/php-quick-profiler/ |
|
8
|
|
|
|
|
9
|
|
|
Last Updated : April 22, 2009 |
|
10
|
|
|
|
|
11
|
|
|
Description : A simple database wrapper that includes |
|
12
|
|
|
logging of queries. |
|
13
|
|
|
|
|
14
|
|
|
- - - - - - - - - - - - - - - - - - - - - */ |
|
15
|
|
|
|
|
16
|
|
|
namespace Particletree\Pqp; |
|
17
|
|
|
|
|
18
|
|
|
class MySqlDatabase { |
|
19
|
|
|
|
|
20
|
|
|
private $host; |
|
21
|
|
|
private $user; |
|
22
|
|
|
private $password; |
|
23
|
|
|
private $database; |
|
24
|
|
|
public $queryCount = 0; |
|
25
|
|
|
public $queries = array(); |
|
26
|
|
|
public $conn; |
|
27
|
|
|
|
|
28
|
|
|
/*------------------------------------ |
|
29
|
|
|
CONFIG CONNECTION |
|
30
|
|
|
------------------------------------*/ |
|
31
|
|
|
|
|
32
|
|
|
function __construct($host, $user, $password) { |
|
|
|
|
|
|
33
|
|
|
$this->host = $host; |
|
34
|
|
|
$this->user = $user; |
|
35
|
|
|
$this->password = $password; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
function connect($new = false) { |
|
|
|
|
|
|
39
|
|
|
$this->conn = mysql_connect($this->host, $this->user, $this->password, $new); |
|
40
|
|
|
if(!$this->conn) { |
|
41
|
|
|
throw new Exception('We\'re working on a few connection issues.'); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
function changeDatabase($database) { |
|
|
|
|
|
|
46
|
|
|
$this->database = $database; |
|
47
|
|
|
if($this->conn) { |
|
48
|
|
|
if(!mysql_select_db($database, $this->conn)) { |
|
49
|
|
|
throw new CustomException('We\'re working on a few connection issues.'); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
function lazyLoadConnection() { |
|
|
|
|
|
|
55
|
|
|
$this->connect(true); |
|
56
|
|
|
if($this->database) $this->changeDatabase($this->database); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/*----------------------------------- |
|
60
|
|
|
QUERY |
|
61
|
|
|
------------------------------------*/ |
|
62
|
|
|
|
|
63
|
|
|
function query($sql) { |
|
|
|
|
|
|
64
|
|
|
if(!$this->conn) $this->lazyLoadConnection(); |
|
65
|
|
|
$start = $this->getTime(); |
|
66
|
|
|
$rs = mysql_query($sql, $this->conn); |
|
67
|
|
|
$this->queryCount += 1; |
|
68
|
|
|
$this->logQuery($sql, $start); |
|
69
|
|
|
if(!$rs) { |
|
70
|
|
|
throw new Exception('Could not execute query.'); |
|
71
|
|
|
} |
|
72
|
|
|
return $rs; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/*----------------------------------- |
|
76
|
|
|
DEBUGGING |
|
77
|
|
|
------------------------------------*/ |
|
78
|
|
|
|
|
79
|
|
|
function logQuery($sql, $start) { |
|
|
|
|
|
|
80
|
|
|
$query = array( |
|
81
|
|
|
'sql' => $sql, |
|
82
|
|
|
'time' => ($this->getTime() - $start)*1000 |
|
83
|
|
|
); |
|
84
|
|
|
array_push($this->queries, $query); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
function getTime() { |
|
|
|
|
|
|
88
|
|
|
$time = microtime(); |
|
89
|
|
|
$time = explode(' ', $time); |
|
90
|
|
|
$time = $time[1] + $time[0]; |
|
91
|
|
|
$start = $time; |
|
92
|
|
|
return $start; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
public function getReadableTime($time) { |
|
|
|
|
|
|
96
|
|
|
$ret = $time; |
|
97
|
|
|
$formatter = 0; |
|
98
|
|
|
$formats = array('ms', 's', 'm'); |
|
99
|
|
|
if($time >= 1000 && $time < 60000) { |
|
100
|
|
|
$formatter = 1; |
|
101
|
|
|
$ret = ($time / 1000); |
|
102
|
|
|
} |
|
103
|
|
|
if($time >= 60000) { |
|
104
|
|
|
$formatter = 2; |
|
105
|
|
|
$ret = ($time / 1000) / 60; |
|
106
|
|
|
} |
|
107
|
|
|
$ret = number_format($ret,3,'.','') . ' ' . $formats[$formatter]; |
|
108
|
|
|
return $ret; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
function __destruct() { |
|
|
|
|
|
|
112
|
|
|
@mysql_close($this->conn); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
?> |
|
|
|
|
|
|
118
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.