-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTR_MysqlConnector.php
167 lines (142 loc) · 4.57 KB
/
TR_MysqlConnector.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
<?php
/*
Mysql connector
*/
/**
* Copyright (C) 2008 - Ian Homer & bemoko
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, see <http://www.gnu.org/licenses>.
*/
class TR_MysqlConnector {
protected $context;
protected $error;
protected $connected = false;
protected $db = null;
function TR_MysqlConnector( $context ) {
$this->setContext($context);
}
public function setContext($context) {
$this->context=$context;
}
public function getContext() {
return $this->context;
}
public function isConnected() {
return $this->connected;
}
public function connect() {
$this->db=mysql_connect($this->context->host,
$this->context->dbuser, $this->context->password);
$this->connected = false;
/*
* Set character encoding
*/
mysql_query("SET NAMES '".$this->context->dbencoding."';", $this->db);
mysql_query("SET CHARACTER SET '".$this->context->dbencoding."';", $this->db);
if (!$this->db) {
$this->setError($this->context
->getErrorMessage('trReport_noconnection',
$this->context->dbuser,
$this->context->host,mysql_error()));
return false;
}
/*
* Test the connection early - note that we can't switch to the db
* with mysql_select_db since if this is a shared database connection
* with mediawiki then we will have changed the db for the mediawiki
* access.
*/
$sql="select count(id) from ".$this->context->database.
".priority;";
$result=mysql_query($sql,$this->db);
if (!$result) {
$this->setError($this->context->getErrorMessage('trReport_nodb',
"Can't find test table 'priority' in database '".
$this->context->database."' using ".$sql.
" - this probably means your username and password set in the variable wgBugzillaReports are not correct."));
$this->db=null;
} else if (mysql_error($this->db)) {
$this->setError($this->context->getErrorMessage('trReport_nodb'),
mysql_error($this->db));
$this->db=null;
} else if ($this->getRowCount($result) != 1) {
$this->setError($this->context->getErrorMessage('trReport_nodb',
$this->context->database."-".$this->getRowCount($result)));
$this->db=null;
}
$this->free($result);
$this->connected = true;
return $this->connected;
}
public function execute($sql) {
$result = mysql_query($sql, $this->db);
$errNo = mysql_errno();
$error = mysql_error();
if ($errNo != "") {
$this->setError("MySQL Error:".$errNo." - ".$error);
} else {
$this->setError("");
}
return $result;
}
public function getRowCount($result) {
return mysql_num_rows($result);
}
public function getFieldCount($result) {
return mysql_num_fields($result);
}
public function seek($result, $row) {
$rows = $this->getRowCount($result);
if ($rows > 0 and $row < $rows) {
mysql_data_seek( $result, $row);
}
}
/**
* fetching meta data for a field
*/
public function getField($result, $fieldNum) {
return mysql_fetch_field($result, $fieldNum);
}
public function fetch($result) {
return mysql_fetch_array($result, MYSQL_ASSOC);
}
public function free($result) {
mysql_free_result($result);
}
public function close() {
/*
* In PHP you should rely on script termination to close mysql
* and not explicitly call mysql_close($db) - see
* http://uk.php.net/manual/en/function.mysql-close.php
* This is because the implementation may reuse connections. This
* does happen if the connection details for the Bugzila database are
* the same as the wiki database. Setting to null is good practice
* to free up the resource early.
*/
$this->connected = false;
$this->db=null;
}
public function getTable($table) {
return $this->context->database.".".$table;
}
public function setError($message) {
$this->message=$message;
}
public function getError() {
return $this->message;
}
public function getDbError() {
return mysql_error($this->db);
}
}