summaryrefslogtreecommitdiff
path: root/includes/db/DatabaseMysql.php
blob: 5b15147777c68ae7034fc88783f9358874f15deb (plain)
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
<?php
/**
 * This is the MySQL database abstraction layer.
 *
 * 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Database
 */

/**
 * Database abstraction object for PHP extension mysql.
 *
 * @ingroup Database
 * @see Database
 */
class DatabaseMysql extends DatabaseMysqlBase {
	/**
	 * @param string $sql
	 * @return resource False on error
	 */
	protected function doQuery( $sql ) {
		$conn = $this->getBindingHandle();

		if ( $this->bufferResults() ) {
			$ret = mysql_query( $sql, $conn );
		} else {
			$ret = mysql_unbuffered_query( $sql, $conn );
		}

		return $ret;
	}

	/**
	 * @param string $realServer
	 * @return bool|resource MySQL Database connection or false on failure to connect
	 * @throws DBConnectionError
	 */
	protected function mysqlConnect( $realServer ) {
		# Avoid a suppressed fatal error, which is very hard to track down
		if ( !extension_loaded( 'mysql' ) ) {
			throw new DBConnectionError(
				$this,
				"MySQL functions missing, have you compiled PHP with the --with-mysql option?\n"
			);
		}

		$connFlags = 0;
		if ( $this->mFlags & DBO_SSL ) {
			$connFlags |= MYSQL_CLIENT_SSL;
		}
		if ( $this->mFlags & DBO_COMPRESS ) {
			$connFlags |= MYSQL_CLIENT_COMPRESS;
		}

		if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
			$numAttempts = 2;
		} else {
			$numAttempts = 1;
		}

		$conn = false;

		# The kernel's default SYN retransmission period is far too slow for us,
		# so we use a short timeout plus a manual retry. Retrying means that a small
		# but finite rate of SYN packet loss won't cause user-visible errors.
		for ( $i = 0; $i < $numAttempts && !$conn; $i++ ) {
			if ( $i > 1 ) {
				usleep( 1000 );
			}
			if ( $this->mFlags & DBO_PERSISTENT ) {
				$conn = mysql_pconnect( $realServer, $this->mUser, $this->mPassword, $connFlags );
			} else {
				# Create a new connection...
				$conn = mysql_connect( $realServer, $this->mUser, $this->mPassword, true, $connFlags );
			}
		}

		return $conn;
	}

	/**
	 * @param string $charset
	 * @return bool
	 */
	protected function mysqlSetCharset( $charset ) {
		$conn = $this->getBindingHandle();

		if ( function_exists( 'mysql_set_charset' ) ) {
			return mysql_set_charset( $charset, $conn );
		} else {
			return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
		}
	}

	/**
	 * @return bool
	 */
	protected function closeConnection() {
		$conn = $this->getBindingHandle();

		return mysql_close( $conn );
	}

	/**
	 * @return int
	 */
	function insertId() {
		$conn = $this->getBindingHandle();

		return mysql_insert_id( $conn );
	}

	/**
	 * @return int
	 */
	function lastErrno() {
		if ( $this->mConn ) {
			return mysql_errno( $this->mConn );
		} else {
			return mysql_errno();
		}
	}

	/**
	 * @return int
	 */
	function affectedRows() {
		$conn = $this->getBindingHandle();

		return mysql_affected_rows( $conn );
	}

	/**
	 * @param string $db
	 * @return bool
	 */
	function selectDB( $db ) {
		$conn = $this->getBindingHandle();

		$this->mDBname = $db;

		return mysql_select_db( $db, $conn );
	}

	protected function mysqlFreeResult( $res ) {
		return mysql_free_result( $res );
	}

	protected function mysqlFetchObject( $res ) {
		return mysql_fetch_object( $res );
	}

	protected function mysqlFetchArray( $res ) {
		return mysql_fetch_array( $res );
	}

	protected function mysqlNumRows( $res ) {
		return mysql_num_rows( $res );
	}

	protected function mysqlNumFields( $res ) {
		return mysql_num_fields( $res );
	}

	protected function mysqlFetchField( $res, $n ) {
		return mysql_fetch_field( $res, $n );
	}

	protected function mysqlFieldName( $res, $n ) {
		return mysql_field_name( $res, $n );
	}

	protected function mysqlFieldType( $res, $n ) {
		return mysql_field_type( $res, $n );
	}

	protected function mysqlDataSeek( $res, $row ) {
		return mysql_data_seek( $res, $row );
	}

	protected function mysqlError( $conn = null ) {
		return ( $conn !== null ) ? mysql_error( $conn ) : mysql_error(); // avoid warning
	}

	protected function mysqlRealEscapeString( $s ) {
		$conn = $this->getBindingHandle();

		return mysql_real_escape_string( $s, $conn );
	}

	protected function mysqlPing() {
		$conn = $this->getBindingHandle();

		return mysql_ping( $conn );
	}
}