/
class.Database.php
171 lines (140 loc) · 3.34 KB
/
class.Database.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
class Database
{
private static $instance = array();
private $connection;
private $result;
private $row;
private $sql;
private $error;
const STATS_QUERY = false;
private static $__exe_queries = array();
private function __construct($index)
{
global $dbdetails;
$this->connection = new mysqli($dbdetails[$index]['host'], $dbdetails[$index]['user'], $dbdetails[$index]['password'], $dbdetails[$index]['database']);
}
private function __clone()
{
$this->connection->close();
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
public function __toString()
{
$this->connection->close();
trigger_error('Print is not allowed.', E_USER_ERROR);
}
public static function getConnection($index = DB_SLAVE)
{
$index = (!empty($index) ? $index : DB_SLAVE);
if (empty(self::$instance[$index]) || empty(self::$instance[$index]->connection->client_info) || !is_object(self::$instance[$index]) )
{
self::$instance[$index] = new Database($index);
mysqli_options( self::$instance[$index] , MYSQLI_OPT_CONNECT_TIMEOUT , 10);
}
return self::$instance[$index];
}
public function query($sql)
{
if ( is_object($this->result) )
{
$this->result->close();
}
$this->sql = $sql;
//self::$__exe_queries[] = $sql;
if ( $this->result = $this->connection->query($this->sql) )
{
return true;
}
else
{
$this->error = $this->connection->error;
return false;
}
}
public function getRowCount()
{
if ( is_object($this->result) )
{
return $this->result->num_rows;
}
}
public function getInsertedAutoId()
{
return $this->connection->insert_id;
}
public function getAffectedRowCount()
{
return $this->connection->affected_rows;
}
public function fetch()
{
if ( is_object($this->result) )
{
return $this->result->fetch_array();
}
}
public function fetchAssoc()
{
if ( is_object($this->result) )
{
return $this->result->fetch_array(MYSQLI_ASSOC);
}
}
public function getResultSet()
{
$resultSet = array();
if ( is_object($this->result) )
{
while ( $row = $this->result->fetch_array(MYSQLI_ASSOC) )
{
$resultSet[] = $row;
}
}
return $resultSet;
}
public function close()
{
if ( is_object($this->result) )
{
$this->result->close();
}
if ( is_object($this->connection) )
{
$this->connection->close();
}
self::debug_queries();
}
private static function debug_queries()
{
if ( self::STATS_QUERY )
{
print "Total Quries: " . count(self::$__exe_queries) . "<br \>\n";
foreach ( self::$__exe_queries as $q_key => $v_query )
{
print ($q_key + 1) . ') ' . $v_query . "<br />\n";
}
self::$__exe_queries = array();
}
}
public function db_escape($string)
{
if ( !is_array($string) )
{
return $this->connection->real_escape_string(trim($string));
}
}
function __destruct(){
if ( is_object($this->result) )
{
$this->result->close();
unset($this->result);
}
if ( is_object($this->connection) )
{
$this->connection->close();
unset($this->connection);
}
}
}
// eof class