-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql5.php
241 lines (202 loc) · 5.59 KB
/
mysql5.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* UniDB Driver Mysql5
* @file: mysql5.php
* @package: UniDB - MySQL,PostgreSQL, SQLite
* @author Samnan ur Rehman
*
* @license GNU GPLv3
*/
require_once( 'core.php' );
class Mysql5_Database extends Core_Database {
function __construct() {
parent::__construct();
}
public function connect($ip, $user, $password, $db="") {
if (!function_exists('mysql_connect')) {
return $this->log_error('Mysql5 client library is not installed');
}
$this->conn = @mysql_connect($ip, $user, $password);
if (!$this->conn)
return $this->log_error('Database connection failed to the server');
if ($db && !@mysql_select_db($db, $this->conn))
return $this->log_error(mysql_error($this->conn));
$this->ip = $ip;
$this->user = $user;
$this->password = $password;
$this->db = $db;
return true;
}
public function disconnect() {
@mysql_close($this->conn);
$this->conn = false;
return true;
}
public function get_features() {
$this->query("SHOW VARIABLES LIKE 'version'", '_features');
if ($this->num_rows('_features') == 1) {
$row = $this->fetch_row('_features');
$version = (float) $row['Value'];
if ($version >= 5.0) {
$this->features['views'] = TRUE;
$this->features['functions'] = TRUE;
$this->features['procedures'] = TRUE;
$this->features['triggers'] = TRUE;
$this->query("SHOW VARIABLES LIKE 'event_scheduler'", '_features');
if ($this->num_rows('_features') == 1) {
$this->features['events'] = TRUE;
}
}
}
return $this->features;
}
public function select_db($db) {
$this->db = $db;
return mysql_select_db($this->db);
}
public function query($sql, $stack=0) {
if (!$this->conn) {
return $this->log_error("DB: Connection has been closed");
return false;
}
$this->result[$stack] = "";
$this->lastQuery = $sql;
$this->queryTime = $this->get_time();
$this->result[$stack] = @mysql_query($sql, $this->conn);
$this->queryTime = $this->get_time() - $this->queryTime;
if (!$this->result[$stack]) {
return $this->log_error( mysql_error($this->conn) );
}
return true;
}
public function get_insert_id() {
return mysql_insert_id($this->conn);
}
public function fetch_row($stack=0, $type="") {
if($type == "")
$type = $this->options['result_type'];
if ($type == "both")
$type = MYSQL_BOTH;
else if ($type == "num")
$type = MYSQL_NUM;
else if ($type == "assoc")
$type = MYSQL_ASSOC;
if (!$this->result[$stack]) {
return $this->log_error("fetch_row[$stack] failed");
}
return @mysql_fetch_array($this->result[$stack], $type);
}
public function fetch_row_num($num, $stack=0, $type="") {
if($type == "")
$type = $this->options['result_type'];
if ($type == "both")
$type = MYSQL_BOTH;
else if ($type == "num")
$type = MYSQL_NUM;
else if ($type == "assoc")
$type = MYSQL_ASSOC;
if (!$this->result[$stack]) {
return $this_log_error("fetch_row_num[$stack] failed");
}
mysql_data_seek($this->result[$stack], $num);
return @mysql_fetch_array($this->result[$stack], $type);
}
public function num_rows($stack=0) {
return mysql_num_rows($this->result[$stack]);
}
public function num_affected_rows() {
return mysql_affected_rows($this->conn);
}
public function get_databases() {
$res = $this->query("show databases", '_databases');
$ret = array();
while($row = $this->fetch_row('_databases'))
$ret[] = $row[0];
return $ret;
}
public function get_tables() {
if (!$this->db)
return array();
$res = $this->query("show table status from `$this->db` where engine is NOT null", '_tables');
$ret = array();
while($row = $this->fetch_row('_tables')) {
$ret[] = $row['Name'];
}
return $ret;
}
public function get_views() {
if (!$this->db)
return array();
$res = $this->query("show table status from `$this->db` where engine is null", '_views');
if (!$res)
return array();
$ret = array();
while($row = $this->fetch_row('_views'))
$ret[] = $row[0];
return $ret;
}
public function get_procedures() {
if (!$this->db)
return array();
$res = $this->query("show procedure status where db = '$this->db'", '_procs');
if (!$res)
return array();
$ret = array();
while($row = $this->fetch_row('_procs'))
$ret[] = $row[1];
return $ret;
}
public function get_functions() {
if (!$this->db)
return array();
$res = $this->query("show function status where db = '$this->db'", '_funcs');
if (!$res)
return array();
$ret = array();
while($row = $this->fetch_row('_funcs'))
$ret[] = $row[1];
return $ret;
}
public function get_triggers() {
if (!$this->db)
return array();
$res = $this->query("select `TRIGGER_NAME` from `INFORMATION_SCHEMA`.`TRIGGERS` where `TRIGGER_SCHEMA` = '$this->db'", '_triggers');
if (!$res)
return array();
$ret = array();
while($row = $this->fetch_row('_triggers'))
$ret[] = $row[0];
return $ret;
}
public function get_events() {
if (!$this->db)
return array();
$res = $this->query("select `EVENT_NAME` from `INFORMATION_SCHEMA`.`EVENTS` where `EVENT_SCHEMA` = '$this->db'", '_events');
if (!$res)
return array();
$ret = array();
while($row = $this->fetch_row('_events'))
$ret[] = $row[0];
return $ret;
}
public function escape($str) {
return "'".mysql_escape_string($str)."'";
}
public function quote($str) {
if(strpos($str, '.') === false)
return '`' . $str . '`';
return '`' . str_replace('.', '`.`', $str) . '`';
}
protected function get_expr_string($str) {
switch($str) {
case 'datetime':
return 'NOW()';
case 'date':
return 'CURDATE()';
case 'time':
return 'CURTIME()';
}
return $str;
}
}
?>