1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Database interface code for MySQL database servers.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* @addtogroup database
|
10
|
* @{
|
11
|
*/
|
12
|
|
13
|
class DatabaseConnection_mysql extends DatabaseConnection {
|
14
|
|
15
|
/**
|
16
|
* Flag to indicate if the cleanup function in __destruct() should run.
|
17
|
*
|
18
|
* @var boolean
|
19
|
*/
|
20
|
protected $needsCleanup = FALSE;
|
21
|
|
22
|
public function __construct(array $connection_options = array()) {
|
23
|
// This driver defaults to transaction support, except if explicitly passed FALSE.
|
24
|
$this->transactionSupport = !isset($connection_options['transactions']) || ($connection_options['transactions'] !== FALSE);
|
25
|
|
26
|
// MySQL never supports transactional DDL.
|
27
|
$this->transactionalDDLSupport = FALSE;
|
28
|
|
29
|
$this->connectionOptions = $connection_options;
|
30
|
|
31
|
// The DSN should use either a socket or a host/port.
|
32
|
if (isset($connection_options['unix_socket'])) {
|
33
|
$dsn = 'mysql:unix_socket=' . $connection_options['unix_socket'];
|
34
|
}
|
35
|
else {
|
36
|
// Default to TCP connection on port 3306.
|
37
|
$dsn = 'mysql:host=' . $connection_options['host'] . ';port=' . (empty($connection_options['port']) ? 3306 : $connection_options['port']);
|
38
|
}
|
39
|
$dsn .= ';dbname=' . $connection_options['database'];
|
40
|
// Allow PDO options to be overridden.
|
41
|
$connection_options += array(
|
42
|
'pdo' => array(),
|
43
|
);
|
44
|
$connection_options['pdo'] += array(
|
45
|
// So we don't have to mess around with cursors and unbuffered queries by default.
|
46
|
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE,
|
47
|
// Because MySQL's prepared statements skip the query cache, because it's dumb.
|
48
|
PDO::ATTR_EMULATE_PREPARES => TRUE,
|
49
|
);
|
50
|
|
51
|
parent::__construct($dsn, $connection_options['username'], $connection_options['password'], $connection_options['pdo']);
|
52
|
|
53
|
// Force MySQL to use the UTF-8 character set. Also set the collation, if a
|
54
|
// certain one has been set; otherwise, MySQL defaults to 'utf8_general_ci'
|
55
|
// for UTF-8.
|
56
|
if (!empty($connection_options['collation'])) {
|
57
|
$this->exec('SET NAMES utf8 COLLATE ' . $connection_options['collation']);
|
58
|
}
|
59
|
else {
|
60
|
$this->exec('SET NAMES utf8');
|
61
|
}
|
62
|
|
63
|
// Set MySQL init_commands if not already defined. Default Drupal's MySQL
|
64
|
// behavior to conform more closely to SQL standards. This allows Drupal
|
65
|
// to run almost seamlessly on many different kinds of database systems.
|
66
|
// These settings force MySQL to behave the same as postgresql, or sqlite
|
67
|
// in regards to syntax interpretation and invalid data handling. See
|
68
|
// http://drupal.org/node/344575 for further discussion. Also, as MySQL 5.5
|
69
|
// changed the meaning of TRADITIONAL we need to spell out the modes one by
|
70
|
// one.
|
71
|
$connection_options += array(
|
72
|
'init_commands' => array(),
|
73
|
);
|
74
|
$connection_options['init_commands'] += array(
|
75
|
'sql_mode' => "SET sql_mode = 'ANSI,STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER'",
|
76
|
);
|
77
|
// Set connection options.
|
78
|
$this->exec(implode('; ', $connection_options['init_commands']));
|
79
|
}
|
80
|
|
81
|
public function __destruct() {
|
82
|
if ($this->needsCleanup) {
|
83
|
$this->nextIdDelete();
|
84
|
}
|
85
|
}
|
86
|
|
87
|
public function queryRange($query, $from, $count, array $args = array(), array $options = array()) {
|
88
|
return $this->query($query . ' LIMIT ' . (int) $from . ', ' . (int) $count, $args, $options);
|
89
|
}
|
90
|
|
91
|
public function queryTemporary($query, array $args = array(), array $options = array()) {
|
92
|
$tablename = $this->generateTemporaryTableName();
|
93
|
$this->query('CREATE TEMPORARY TABLE {' . $tablename . '} Engine=MEMORY ' . $query, $args, $options);
|
94
|
return $tablename;
|
95
|
}
|
96
|
|
97
|
public function driver() {
|
98
|
return 'mysql';
|
99
|
}
|
100
|
|
101
|
public function databaseType() {
|
102
|
return 'mysql';
|
103
|
}
|
104
|
|
105
|
public function mapConditionOperator($operator) {
|
106
|
// We don't want to override any of the defaults.
|
107
|
return NULL;
|
108
|
}
|
109
|
|
110
|
public function nextId($existing_id = 0) {
|
111
|
$new_id = $this->query('INSERT INTO {sequences} () VALUES ()', array(), array('return' => Database::RETURN_INSERT_ID));
|
112
|
// This should only happen after an import or similar event.
|
113
|
if ($existing_id >= $new_id) {
|
114
|
// If we INSERT a value manually into the sequences table, on the next
|
115
|
// INSERT, MySQL will generate a larger value. However, there is no way
|
116
|
// of knowing whether this value already exists in the table. MySQL
|
117
|
// provides an INSERT IGNORE which would work, but that can mask problems
|
118
|
// other than duplicate keys. Instead, we use INSERT ... ON DUPLICATE KEY
|
119
|
// UPDATE in such a way that the UPDATE does not do anything. This way,
|
120
|
// duplicate keys do not generate errors but everything else does.
|
121
|
$this->query('INSERT INTO {sequences} (value) VALUES (:value) ON DUPLICATE KEY UPDATE value = value', array(':value' => $existing_id));
|
122
|
$new_id = $this->query('INSERT INTO {sequences} () VALUES ()', array(), array('return' => Database::RETURN_INSERT_ID));
|
123
|
}
|
124
|
$this->needsCleanup = TRUE;
|
125
|
return $new_id;
|
126
|
}
|
127
|
|
128
|
public function nextIdDelete() {
|
129
|
// While we want to clean up the table to keep it up from occupying too
|
130
|
// much storage and memory, we must keep the highest value in the table
|
131
|
// because InnoDB uses an in-memory auto-increment counter as long as the
|
132
|
// server runs. When the server is stopped and restarted, InnoDB
|
133
|
// reinitializes the counter for each table for the first INSERT to the
|
134
|
// table based solely on values from the table so deleting all values would
|
135
|
// be a problem in this case. Also, TRUNCATE resets the auto increment
|
136
|
// counter.
|
137
|
try {
|
138
|
$max_id = $this->query('SELECT MAX(value) FROM {sequences}')->fetchField();
|
139
|
// We know we are using MySQL here, no need for the slower db_delete().
|
140
|
$this->query('DELETE FROM {sequences} WHERE value < :value', array(':value' => $max_id));
|
141
|
}
|
142
|
// During testing, this function is called from shutdown with the
|
143
|
// simpletest prefix stored in $this->connection, and those tables are gone
|
144
|
// by the time shutdown is called so we need to ignore the database
|
145
|
// errors. There is no problem with completely ignoring errors here: if
|
146
|
// these queries fail, the sequence will work just fine, just use a bit
|
147
|
// more database storage and memory.
|
148
|
catch (PDOException $e) {
|
149
|
}
|
150
|
}
|
151
|
|
152
|
/**
|
153
|
* Overridden to work around issues to MySQL not supporting transactional DDL.
|
154
|
*/
|
155
|
protected function popCommittableTransactions() {
|
156
|
// Commit all the committable layers.
|
157
|
foreach (array_reverse($this->transactionLayers) as $name => $active) {
|
158
|
// Stop once we found an active transaction.
|
159
|
if ($active) {
|
160
|
break;
|
161
|
}
|
162
|
|
163
|
// If there are no more layers left then we should commit.
|
164
|
unset($this->transactionLayers[$name]);
|
165
|
if (empty($this->transactionLayers)) {
|
166
|
if (!PDO::commit()) {
|
167
|
throw new DatabaseTransactionCommitFailedException();
|
168
|
}
|
169
|
}
|
170
|
else {
|
171
|
// Attempt to release this savepoint in the standard way.
|
172
|
try {
|
173
|
$this->query('RELEASE SAVEPOINT ' . $name);
|
174
|
}
|
175
|
catch (PDOException $e) {
|
176
|
// However, in MySQL (InnoDB), savepoints are automatically committed
|
177
|
// when tables are altered or created (DDL transactions are not
|
178
|
// supported). This can cause exceptions due to trying to release
|
179
|
// savepoints which no longer exist.
|
180
|
//
|
181
|
// To avoid exceptions when no actual error has occurred, we silently
|
182
|
// succeed for MySQL error code 1305 ("SAVEPOINT does not exist").
|
183
|
if ($e->errorInfo[1] == '1305') {
|
184
|
// If one SAVEPOINT was released automatically, then all were.
|
185
|
// Therefore, clean the transaction stack.
|
186
|
$this->transactionLayers = array();
|
187
|
// We also have to explain to PDO that the transaction stack has
|
188
|
// been cleaned-up.
|
189
|
PDO::commit();
|
190
|
}
|
191
|
else {
|
192
|
throw $e;
|
193
|
}
|
194
|
}
|
195
|
}
|
196
|
}
|
197
|
}
|
198
|
}
|
199
|
|
200
|
|
201
|
/**
|
202
|
* @} End of "addtogroup database".
|
203
|
*/
|