Code coverage for /20081101/includes/database/mysql/query.inc

Line #Times calledCode
1
<?php
2
// $Id: query.inc,v 1.3 2008/09/27 20:10:26 dries Exp $
3
4
/**
5
 * @ingroup database
6
 * @{
7
 */
8
91966
class InsertQuery_mysql extends InsertQuery {
10
11
  public function execute() {
12
13
    // Confirm that the user did not try to specify an identical
14
    //  field and default field.
15731
    if (array_intersect($this->insertFields, $this->defaultFields)) {
160
      throw new PDOException('You may not specify the same field to have a
value and a schema-default value.');
170
    }
18
19731
    $last_insert_id = 0;
20
21731
    $max_placeholder = 0;
22731
    $values = array();
23731
    foreach ($this->insertValues as $insert_values) {
24731
      foreach ($insert_values as $value) {
25731
        $values[':db_insert_placeholder_' . $max_placeholder++] = $value;
26731
      }
27731
    }
28
29731
    $last_insert_id = $this->connection->query((string)$this, $values,
$this->queryOptions);
30
31
    // Re-initialize the values array so that we can re-use this query.
32731
    $this->insertValues = array();
33
34731
    return $last_insert_id;
350
  }
36
37
  public function __toString() {
38
39731
    $delay = $this->queryOptions['delay'] ? 'DELAYED' : '';
40
41
    // Default fields are always placed first for consistency.
42731
    $insert_fields = array_merge($this->defaultFields,
$this->insertFields);
43
44731
    $query = "INSERT $delay INTO {" . $this->table . '} (' . implode(', ',
$insert_fields) . ') VALUES ';
45
46731
    $max_placeholder = 0;
47731
    $values = array();
48731
    if (count($this->insertValues)) {
49731
      foreach ($this->insertValues as $insert_values) {
50731
        $placeholders = array();
51
52
        // Default fields aren't really placeholders, but this is the most
convenient
53
        // way to handle them.
54731
        $placeholders = array_pad($placeholders,
count($this->defaultFields), 'default');
55
56731
        $new_placeholder = $max_placeholder + count($insert_values);
57731
        for ($i = $max_placeholder; $i < $new_placeholder; ++$i) {
58731
          $placeholders[] = ':db_insert_placeholder_'. $i;
59731
        }
60731
        $max_placeholder = $new_placeholder;
61731
        $values[] = '('. implode(', ', $placeholders) .')';
62731
      }
63731
    }
64
    else {
65
      // If there are no values, then this is a default-only query.  We
still need to handle that.
664
      $placeholders = array_fill(0, count($this->defaultFields),
'default');
674
      $values[] = '(' . implode(', ', $placeholders) .')';
68
    }
69
70731
    $query .= implode(', ', $values);
71
72731
    return $query;
730
  }
74
}
75
761966
class MergeQuery_mysql extends MergeQuery {
77
78
  public function execute() {
79
    // Set defaults.
801630
    if ($this->updateFields) {
811
      $update_fields = $this->updateFields;
821
    }
83
    else {
84
      // When update fields are derived from insert fields, we don't need
85
      // placeholders since we can tell MySQL to reuse insert supplied
86
      // values using the VALUES(col_name) function.
871630
      $update_fields = array();
88
    }
89
901630
    $insert_fields = $this->insertFields + $this->keyFields;
91
921630
    $max_placeholder = 0;
931630
    $values = array();
94
    // We assume that the order here is the same as in __toString().  If
that's
95
    // not the case, then we have serious problems.
961630
    foreach ($insert_fields as $value) {
971630
      $values[':db_insert_placeholder_' . $max_placeholder++] = $value;
981630
    }
99
100
    // Expressions take priority over literal fields, so we process those
first
101
    // and remove any literal fields that conflict.
1021630
    foreach ($this->expressionFields as $field => $data) {
1039
      if (!empty($data['arguments'])) {
1044
        $values += $data['arguments'];
1054
      }
1069
      unset($update_fields[$field]);
1079
    }
108
109
    // Because we filter $fields the same way here and in __toString(), the
110
    // placeholders will all match up properly.
1111630
    $max_placeholder = 0;
1121630
    foreach ($update_fields as $field => $value) {
1131
      $values[':db_update_placeholder_' . ($max_placeholder++)] = $value;
1141
    }
115
1161630
    $last_insert_id = $this->connection->query((string)$this, $values,
$this->queryOptions);
117
1181630
    return $last_insert_id;
1190
  }
120
121
122
  public function __toString() {
123
124
    // Set defaults.
1251630
    if ($this->updateFields) {
1261
      $update_fields = $this->updateFields;
1271
    }
128
    else {
1291630
      $update_fields = $this->insertFields;
130
      // If there are no exclude fields, this is a no-op.
1311630
      foreach ($this->excludeFields as $exclude_field) {
1321
        unset($update_fields[$exclude_field]);
1331
      }
134
    }
135
1361630
    $insert_fields = $this->insertFields + $this->keyFields;
137
1381630
    $query = "INSERT INTO {" . $this->table . '} (' . implode(', ',
array_keys($insert_fields)) . ') VALUES ';
139
1401630
    $max_placeholder = 0;
1411630
    $values = array();
142
    // We don't need the $field, but this is a convenient way to count.
1431630
    foreach ($insert_fields as $field) {
1441630
      $values[] = ':db_insert_placeholder_' . $max_placeholder++;
1451630
    }
146
1471630
    $query .= '(' . implode(', ', $values) . ') ON DUPLICATE KEY UPDATE ';
148
149
    // Expressions take priority over literal fields, so we process those
first
150
    // and remove any literal fields that conflict.
1511630
    $max_placeholder = 0;
1521630
    $update = array();
1531630
    foreach ($this->expressionFields as $field => $data) {
1549
      $update[] = $field . '=' . $data['expression'];
1559
      unset($update_fields[$field]);
1569
    }
157
158
    // Build update fields clauses based on caller supplied list, or
derived
159
    // from insert supplied values using the VALUES(col_name) function.
1601630
    foreach ($update_fields as $field => $value) {
1611630
      if ($this->updateFields) {
1621
        $update[] = ($field . '=:db_update_placeholder_' .
$max_placeholder++);
1631
      }
164
      else {
1651630
        $update[] = ($field . '=VALUES(' . $field . ')');
166
      }
1671630
    }
168
1691630
    $query .= implode(', ', $update);
170
1711630
    return $query;
1720
  }
173
}
174
175
/**
176
 * @} End of "ingroup database".
177
 */
1781966