Projet

Général

Profil

Paste
Télécharger (3,38 ko) Statistiques
| Branche: | Révision:

root / drupal7 / includes / database / mysql / query.inc @ 01dfd3b5

1
<?php
2

    
3
/**
4
 * @addtogroup database
5
 * @{
6
 */
7

    
8
/**
9
 * @file
10
 * Query code for MySQL embedded database engine.
11
 */
12

    
13

    
14
class InsertQuery_mysql extends InsertQuery {
15

    
16
  public function execute() {
17
    if (!$this->preExecute()) {
18
      return NULL;
19
    }
20

    
21
    // If we're selecting from a SelectQuery, finish building the query and
22
    // pass it back, as any remaining options are irrelevant.
23
    if (empty($this->fromQuery)) {
24
      $max_placeholder = 0;
25
      $values = array();
26
      foreach ($this->insertValues as $insert_values) {
27
        foreach ($insert_values as $value) {
28
          $values[':db_insert_placeholder_' . $max_placeholder++] = $value;
29
        }
30
      }
31
    }
32
    else {
33
      $values = $this->fromQuery->getArguments();
34
    }
35

    
36
    $last_insert_id = $this->connection->query((string) $this, $values, $this->queryOptions);
37

    
38
    // Re-initialize the values array so that we can re-use this query.
39
    $this->insertValues = array();
40

    
41
    return $last_insert_id;
42
  }
43

    
44
  public function __toString() {
45
    // Create a sanitized comment string to prepend to the query.
46
    $comments = $this->connection->makeComment($this->comments);
47

    
48
    // Default fields are always placed first for consistency.
49
    $insert_fields = array_merge($this->defaultFields, $this->insertFields);
50

    
51
    if (method_exists($this->connection, 'escapeFields')) {
52
      $insert_fields = $this->connection->escapeFields($insert_fields);
53
    }
54

    
55
    // If we're selecting from a SelectQuery, finish building the query and
56
    // pass it back, as any remaining options are irrelevant.
57
    if (!empty($this->fromQuery)) {
58
      $insert_fields_string = $insert_fields ? ' (' . implode(', ', $insert_fields) . ') ' : ' ';
59
      return $comments . 'INSERT INTO {' . $this->table . '}' . $insert_fields_string . $this->fromQuery;
60
    }
61

    
62
    $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
63

    
64
    $max_placeholder = 0;
65
    $values = array();
66
    if (count($this->insertValues)) {
67
      foreach ($this->insertValues as $insert_values) {
68
        $placeholders = array();
69

    
70
        // Default fields aren't really placeholders, but this is the most convenient
71
        // way to handle them.
72
        $placeholders = array_pad($placeholders, count($this->defaultFields), 'default');
73

    
74
        $new_placeholder = $max_placeholder + count($insert_values);
75
        for ($i = $max_placeholder; $i < $new_placeholder; ++$i) {
76
          $placeholders[] = ':db_insert_placeholder_' . $i;
77
        }
78
        $max_placeholder = $new_placeholder;
79
        $values[] = '(' . implode(', ', $placeholders) . ')';
80
      }
81
    }
82
    else {
83
      // If there are no values, then this is a default-only query. We still need to handle that.
84
      $placeholders = array_fill(0, count($this->defaultFields), 'default');
85
      $values[] = '(' . implode(', ', $placeholders) . ')';
86
    }
87

    
88
    $query .= implode(', ', $values);
89

    
90
    return $query;
91
  }
92
}
93

    
94
class TruncateQuery_mysql extends TruncateQuery { }
95

    
96
class UpdateQuery_mysql extends UpdateQuery {
97
  public function __toString() {
98
    if (method_exists($this->connection, 'escapeField')) {
99
      $escapedFields = array();
100
      foreach ($this->fields as $field => $data) {
101
        $field = $this->connection->escapeField($field);
102
        $escapedFields[$field] = $data;
103
      }
104
      $this->fields = $escapedFields;
105
    }
106
    return parent::__toString();
107
  }
108
}
109

    
110
/**
111
 * @} End of "addtogroup database".
112
 */