123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- class DatabaseConnection_mysql extends DatabaseConnection {
-
- protected $needsCleanup = FALSE;
- public function __construct(array $connection_options = array()) {
-
- $this->transactionSupport = !isset($connection_options['transactions']) || ($connection_options['transactions'] !== FALSE);
-
- $this->transactionalDDLSupport = FALSE;
- $this->connectionOptions = $connection_options;
- $charset = 'utf8';
-
- if ($this->utf8mb4IsActive()) {
- $charset = 'utf8mb4';
- }
-
- if (isset($connection_options['unix_socket'])) {
- $dsn = 'mysql:unix_socket=' . $connection_options['unix_socket'];
- }
- else {
-
- $dsn = 'mysql:host=' . $connection_options['host'] . ';port=' . (empty($connection_options['port']) ? 3306 : $connection_options['port']);
- }
-
-
-
- $dsn .= ';charset=' . $charset;
- $dsn .= ';dbname=' . $connection_options['database'];
-
- $connection_options += array(
- 'pdo' => array(),
- );
- $connection_options['pdo'] += array(
-
- PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE,
-
- PDO::ATTR_EMULATE_PREPARES => TRUE,
- );
- if (defined('PDO::MYSQL_ATTR_MULTI_STATEMENTS')) {
-
-
- $connection_options['pdo'] += array(PDO::MYSQL_ATTR_MULTI_STATEMENTS => FALSE);
- }
- parent::__construct($dsn, $connection_options['username'], $connection_options['password'], $connection_options['pdo']);
-
-
-
- if (!empty($connection_options['collation'])) {
- $this->exec('SET NAMES ' . $charset . ' COLLATE ' . $connection_options['collation']);
- }
- else {
- $this->exec('SET NAMES ' . $charset);
- }
-
-
-
-
-
-
-
-
- $connection_options += array(
- 'init_commands' => array(),
- );
- $connection_options['init_commands'] += array(
- 'sql_mode' => "SET sql_mode = 'REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER'",
- );
-
- foreach ($connection_options['init_commands'] as $sql) {
- $this->exec($sql);
- }
- }
- public function __destruct() {
- if ($this->needsCleanup) {
- $this->nextIdDelete();
- }
- }
- public function queryRange($query, $from, $count, array $args = array(), array $options = array()) {
- return $this->query($query . ' LIMIT ' . (int) $from . ', ' . (int) $count, $args, $options);
- }
- public function queryTemporary($query, array $args = array(), array $options = array()) {
- $tablename = $this->generateTemporaryTableName();
- $this->query('CREATE TEMPORARY TABLE {' . $tablename . '} Engine=MEMORY ' . $query, $args, $options);
- return $tablename;
- }
- public function driver() {
- return 'mysql';
- }
- public function databaseType() {
- return 'mysql';
- }
- public function mapConditionOperator($operator) {
-
- return NULL;
- }
- public function nextId($existing_id = 0) {
- $new_id = $this->query('INSERT INTO {sequences} () VALUES ()', array(), array('return' => Database::RETURN_INSERT_ID));
-
- if ($existing_id >= $new_id) {
-
-
-
-
-
-
-
- $this->query('INSERT INTO {sequences} (value) VALUES (:value) ON DUPLICATE KEY UPDATE value = value', array(':value' => $existing_id));
- $new_id = $this->query('INSERT INTO {sequences} () VALUES ()', array(), array('return' => Database::RETURN_INSERT_ID));
- }
- $this->needsCleanup = TRUE;
- return $new_id;
- }
- public function nextIdDelete() {
-
-
-
-
-
-
-
-
- try {
- $max_id = $this->query('SELECT MAX(value) FROM {sequences}')->fetchField();
-
- $this->query('DELETE FROM {sequences} WHERE value < :value', array(':value' => $max_id));
- }
-
-
-
-
-
-
- catch (PDOException $e) {
- }
- }
-
- protected function popCommittableTransactions() {
-
- foreach (array_reverse($this->transactionLayers) as $name => $active) {
-
- if ($active) {
- break;
- }
-
- unset($this->transactionLayers[$name]);
- if (empty($this->transactionLayers)) {
- if (!PDO::commit()) {
- throw new DatabaseTransactionCommitFailedException();
- }
- }
- else {
-
- try {
- $this->query('RELEASE SAVEPOINT ' . $name);
- }
- catch (PDOException $e) {
-
-
-
-
-
-
-
- if ($e->errorInfo[1] == '1305') {
-
-
- $this->transactionLayers = array();
-
-
- PDO::commit();
- }
- else {
- throw $e;
- }
- }
- }
- }
- }
- public function utf8mb4IsConfigurable() {
- return TRUE;
- }
- public function utf8mb4IsActive() {
- return isset($this->connectionOptions['charset']) && $this->connectionOptions['charset'] === 'utf8mb4';
- }
- public function utf8mb4IsSupported() {
-
- $version = $this->getAttribute(PDO::ATTR_CLIENT_VERSION);
- if (strpos($version, 'mysqlnd') !== FALSE) {
-
- $version = preg_replace('/^\D+([\d.]+).*/', '$1', $version);
- if (version_compare($version, '5.0.9', '<')) {
- return FALSE;
- }
- }
- else {
-
- if (version_compare($version, '5.5.3', '<')) {
- return FALSE;
- }
- }
-
- try {
- $this->query("CREATE TABLE {drupal_utf8mb4_test} (id VARCHAR(255), PRIMARY KEY(id(255))) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ROW_FORMAT=DYNAMIC ENGINE=INNODB");
- }
- catch (Exception $e) {
- return FALSE;
- }
- $this->query("DROP TABLE {drupal_utf8mb4_test}");
- return TRUE;
- }
- }
|