Move busui to seperate repository
[bus.git] / MySqlDatabase.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
 
/* - - - - - - - - - - - - - - - - - - - - -
 
 Title : PHP Quick Profiler MySQL Class
 Author : Created by Ryan Campbell
 URL : http://particletree.com/features/php-quick-profiler/
 
 Last Updated : April 22, 2009
 
 Description : A simple database wrapper that includes
 logging of queries.
 
- - - - - - - - - - - - - - - - - - - - - */
 
class MySqlDatabase {
 
        private $host;                  
        private $user;          
        private $password;      
        private $database;      
        public $queryCount = 0;
        public $queries = array();
        public $conn;
        
        /*------------------------------------
                  CONFIG CONNECTION
        ------------------------------------*/
        
        function __construct($host, $user, $password) {
                $this->host = $host;
                $this->user = $user;
                $this->password = $password;
        }
        
        function connect($new = false) {
                $this->conn = mysql_connect($this->host, $this->user, $this->password, $new);
                if(!$this->conn) {
                        throw new Exception('We\'re working on a few connection issues.');
                }
        }
        
        function changeDatabase($database) {
                $this->database = $database;
                if($this->conn) {
                        if(!mysql_select_db($database, $this->conn)) {
                                throw new CustomException('We\'re working on a few connection issues.');
                        }
                }
        }
        
        function lazyLoadConnection() {
                $this->connect(true);
                if($this->database) $this->changeDatabase($this->database);
        }
        
        /*-----------------------------------
                                        QUERY
        ------------------------------------*/
        
        function query($sql) {
                if(!$this->conn) $this->lazyLoadConnection();
                $start = $this->getTime();
                $rs = mysql_query($sql, $this->conn);
                $this->queryCount += 1;
                $this->logQuery($sql, $start);
                if(!$rs) {
                        throw new Exception('Could not execute query.');
                }
                return $rs;
        }
        
        /*-----------------------------------
                        DEBUGGING
        ------------------------------------*/
        
        function logQuery($sql, $start) {
                $query = array(
                                'sql' => $sql,
                                'time' => ($this->getTime() - $start)*1000
                        );
                array_push($this->queries, $query);
        }
        
        function getTime() {
                $time = microtime();
                $time = explode(' ', $time);
                $time = $time[1] + $time[0];
                $start = $time;
                return $start;
        }
        
        public function getReadableTime($time) {
                $ret = $time;
                $formatter = 0;
                $formats = array('ms', 's', 'm');
                if($time >= 1000 && $time < 60000) {
                        $formatter = 1;
                        $ret = ($time / 1000);
                }
                if($time >= 60000) {
                        $formatter = 2;
                        $ret = ($time / 1000) / 60;
                }
                $ret = number_format($ret,3,'.','') . ' ' . $formats[$formatter];
                return $ret;
        }
        
        function __destruct()  {
                @mysql_close($this->conn);
        }
        
}
 
?>