database.inc 2.98 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
// $Id$

/**
 * @file
 * Database interface code for MySQL database servers.
 */

/**
 * @ingroup database
 * @{
 */

class DatabaseConnection_mysql extends DatabaseConnection {

16
  public function __construct(array $connection_options = array()) {
17
    // This driver defaults to non transaction support.
18
    $this->transactionSupport = !empty($connection_options['transactions']);
Dries's avatar
Dries committed
19

20
21
    // MySQL never supports transactional DDL.
    $this->transactionalDDLSupport = FALSE;
22
23
24
25
26

    // Default to TCP connection on port 3306.
    if (empty($connection_options['port'])) {
      $connection_options['port'] = 3306;
    }
27
28
29
30
31
32
33

    $dsn = 'mysql:host=' . $connection_options['host'] . ';port=' . $connection_options['port'] . ';dbname=' . $connection_options['database'];
    parent::__construct($dsn, $connection_options['username'], $connection_options['password'], array(
      // So we don't have to mess around with cursors and unbuffered queries by default.
      PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE,
      // Because MySQL's prepared statements skip the query cache, because it's dumb.
      PDO::ATTR_EMULATE_PREPARES => TRUE,
34
35
      // Force column names to lower case.
      PDO::ATTR_CASE => PDO::CASE_LOWER,
36
    ));
37
38
39
40

    // Force MySQL to use the UTF-8 character set by default.
    $this->exec('SET NAMES "utf8"');

41
42
43
44
45
    // Force MySQL's behavior to conform more closely to SQL standards.
    // This allows Drupal to run almost seamlessly on many different
    // kinds of database systems. These settings force MySQL to behave
    // the same as postgresql, or sqlite in regards to syntax interpretation
    // and invalid data handling. See http://drupal.org/node/344575 for further disscussion.
46
    $this->exec("SET sql_mode='ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY'");
47
48
  }

49
  public function queryRange($query, array $args, $from, $count, array $options = array()) {
50
51
52
    return $this->query($query . ' LIMIT ' . $from . ', ' . $count, $args, $options);
  }

53
54
55
56
  public function queryTemporary($query, array $args, array $options = array()) {
    $tablename = $this->generateTemporaryTableName();
    $this->query(preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE {' . $tablename . '} Engine=MEMORY SELECT', $query), $args, $options);
    return $tablename;
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
  }

  public function driver() {
    return 'mysql';
  }

  public function databaseType() {
    return 'mysql';
  }

  public function mapConditionOperator($operator) {
    // We don't want to override any of the defaults.
    return NULL;
  }

  /**
   * @todo Remove this as soon as db_rewrite_sql() has been exterminated.
   */
  public function distinctField($table, $field, $query) {
    $field_to_select = 'DISTINCT(' . $table . '.' . $field . ')';
    // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT).
    return preg_replace('/(SELECT.*)(?:' . $table . '\.|\s)(?<!DISTINCT\()(?<!DISTINCT\(' . $table . '\.)' . $field . '(.*FROM )/AUsi', '\1 ' . $field_to_select . '\2', $query);
  }
}


/**
 * @} End of "ingroup database".
 */