cms/drupal/includes/database/mysql/query.inc
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     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 we're selecting from a SelectQuery, finish building the query and
       
    52     // pass it back, as any remaining options are irrelevant.
       
    53     if (!empty($this->fromQuery)) {
       
    54       $insert_fields_string = $insert_fields ? ' (' . implode(', ', $insert_fields) . ') ' : ' ';
       
    55       return $comments . 'INSERT INTO {' . $this->table . '}' . $insert_fields_string . $this->fromQuery;
       
    56     }
       
    57 
       
    58     $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
       
    59 
       
    60     $max_placeholder = 0;
       
    61     $values = array();
       
    62     if (count($this->insertValues)) {
       
    63       foreach ($this->insertValues as $insert_values) {
       
    64         $placeholders = array();
       
    65 
       
    66         // Default fields aren't really placeholders, but this is the most convenient
       
    67         // way to handle them.
       
    68         $placeholders = array_pad($placeholders, count($this->defaultFields), 'default');
       
    69 
       
    70         $new_placeholder = $max_placeholder + count($insert_values);
       
    71         for ($i = $max_placeholder; $i < $new_placeholder; ++$i) {
       
    72           $placeholders[] = ':db_insert_placeholder_' . $i;
       
    73         }
       
    74         $max_placeholder = $new_placeholder;
       
    75         $values[] = '(' . implode(', ', $placeholders) . ')';
       
    76       }
       
    77     }
       
    78     else {
       
    79       // If there are no values, then this is a default-only query. We still need to handle that.
       
    80       $placeholders = array_fill(0, count($this->defaultFields), 'default');
       
    81       $values[] = '(' . implode(', ', $placeholders) . ')';
       
    82     }
       
    83 
       
    84     $query .= implode(', ', $values);
       
    85 
       
    86     return $query;
       
    87   }
       
    88 }
       
    89 
       
    90 class TruncateQuery_mysql extends TruncateQuery { }
       
    91 
       
    92 /**
       
    93  * @} End of "addtogroup database".
       
    94  */