2008-08-21 19:36:39 +00:00
|
|
|
<?php
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup database
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2009-04-20 20:02:31 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Query code for MySQL embedded database engine.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2008-08-21 19:36:39 +00:00
|
|
|
class InsertQuery_mysql extends InsertQuery {
|
|
|
|
|
|
|
|
public function execute() {
|
2009-07-21 01:56:36 +00:00
|
|
|
if (!$this->preExecute()) {
|
2009-04-15 13:30:17 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-07-21 01:56:36 +00:00
|
|
|
// If we're selecting from a SelectQuery, finish building the query and
|
|
|
|
// pass it back, as any remaining options are irrelevant.
|
|
|
|
if (empty($this->fromQuery)) {
|
|
|
|
$max_placeholder = 0;
|
|
|
|
$values = array();
|
|
|
|
foreach ($this->insertValues as $insert_values) {
|
|
|
|
foreach ($insert_values as $value) {
|
|
|
|
$values[':db_insert_placeholder_' . $max_placeholder++] = $value;
|
|
|
|
}
|
2008-08-21 19:36:39 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-21 01:56:36 +00:00
|
|
|
else {
|
|
|
|
$values = $this->fromQuery->getArguments();
|
|
|
|
}
|
2008-08-21 19:36:39 +00:00
|
|
|
|
2010-05-06 05:59:31 +00:00
|
|
|
$last_insert_id = $this->connection->query((string) $this, $values, $this->queryOptions);
|
2008-08-21 19:36:39 +00:00
|
|
|
|
|
|
|
// Re-initialize the values array so that we can re-use this query.
|
|
|
|
$this->insertValues = array();
|
|
|
|
|
|
|
|
return $last_insert_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __toString() {
|
2010-05-15 07:04:21 +00:00
|
|
|
// Create a comments string to prepend to the query.
|
|
|
|
$comments = (!empty($this->comments)) ? '/* ' . implode('; ', $this->comments) . ' */ ' : '';
|
2008-08-21 19:36:39 +00:00
|
|
|
|
|
|
|
// Default fields are always placed first for consistency.
|
|
|
|
$insert_fields = array_merge($this->defaultFields, $this->insertFields);
|
2008-09-15 20:48:10 +00:00
|
|
|
|
2009-07-21 01:56:36 +00:00
|
|
|
// If we're selecting from a SelectQuery, finish building the query and
|
|
|
|
// pass it back, as any remaining options are irrelevant.
|
2009-06-05 16:55:45 +00:00
|
|
|
if (!empty($this->fromQuery)) {
|
2010-05-15 07:04:21 +00:00
|
|
|
return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') ' . $this->fromQuery;
|
2009-06-05 16:55:45 +00:00
|
|
|
}
|
|
|
|
|
2010-05-15 07:04:21 +00:00
|
|
|
$query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
|
2008-08-21 19:36:39 +00:00
|
|
|
|
|
|
|
$max_placeholder = 0;
|
|
|
|
$values = array();
|
2008-11-08 04:18:08 +00:00
|
|
|
if (count($this->insertValues)) {
|
2008-08-21 19:36:39 +00:00
|
|
|
foreach ($this->insertValues as $insert_values) {
|
|
|
|
$placeholders = array();
|
2008-09-15 20:48:10 +00:00
|
|
|
|
2008-08-21 19:36:39 +00:00
|
|
|
// Default fields aren't really placeholders, but this is the most convenient
|
|
|
|
// way to handle them.
|
|
|
|
$placeholders = array_pad($placeholders, count($this->defaultFields), 'default');
|
2008-09-15 20:48:10 +00:00
|
|
|
|
2008-08-21 19:36:39 +00:00
|
|
|
$new_placeholder = $max_placeholder + count($insert_values);
|
|
|
|
for ($i = $max_placeholder; $i < $new_placeholder; ++$i) {
|
2009-04-20 20:02:31 +00:00
|
|
|
$placeholders[] = ':db_insert_placeholder_' . $i;
|
2008-08-21 19:36:39 +00:00
|
|
|
}
|
|
|
|
$max_placeholder = $new_placeholder;
|
2009-04-20 20:02:31 +00:00
|
|
|
$values[] = '(' . implode(', ', $placeholders) . ')';
|
2008-08-21 19:36:39 +00:00
|
|
|
}
|
|
|
|
}
|
2008-11-08 04:18:08 +00:00
|
|
|
else {
|
2008-12-20 18:24:41 +00:00
|
|
|
// If there are no values, then this is a default-only query. We still need to handle that.
|
2008-08-21 19:36:39 +00:00
|
|
|
$placeholders = array_fill(0, count($this->defaultFields), 'default');
|
2009-04-20 20:02:31 +00:00
|
|
|
$values[] = '(' . implode(', ', $placeholders) . ')';
|
2008-08-21 19:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$query .= implode(', ', $values);
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MergeQuery_mysql extends MergeQuery {
|
|
|
|
|
|
|
|
public function execute() {
|
2008-12-26 14:23:38 +00:00
|
|
|
|
|
|
|
// A merge query without any key field is invalid.
|
|
|
|
if (count($this->keyFields) == 0) {
|
|
|
|
throw new InvalidMergeQueryException("You need to specify key fields before executing a merge query");
|
|
|
|
}
|
|
|
|
|
2008-08-21 19:36:39 +00:00
|
|
|
// Set defaults.
|
|
|
|
if ($this->updateFields) {
|
|
|
|
$update_fields = $this->updateFields;
|
|
|
|
}
|
|
|
|
else {
|
2008-09-27 20:10:26 +00:00
|
|
|
// When update fields are derived from insert fields, we don't need
|
|
|
|
// placeholders since we can tell MySQL to reuse insert supplied
|
|
|
|
// values using the VALUES(col_name) function.
|
|
|
|
$update_fields = array();
|
2008-08-21 19:36:39 +00:00
|
|
|
}
|
2008-09-15 20:48:10 +00:00
|
|
|
|
2008-08-21 19:36:39 +00:00
|
|
|
$insert_fields = $this->insertFields + $this->keyFields;
|
|
|
|
|
|
|
|
$max_placeholder = 0;
|
|
|
|
$values = array();
|
2008-12-20 18:24:41 +00:00
|
|
|
// We assume that the order here is the same as in __toString(). If that's
|
2008-08-21 19:36:39 +00:00
|
|
|
// not the case, then we have serious problems.
|
|
|
|
foreach ($insert_fields as $value) {
|
|
|
|
$values[':db_insert_placeholder_' . $max_placeholder++] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expressions take priority over literal fields, so we process those first
|
|
|
|
// and remove any literal fields that conflict.
|
|
|
|
foreach ($this->expressionFields as $field => $data) {
|
|
|
|
if (!empty($data['arguments'])) {
|
|
|
|
$values += $data['arguments'];
|
|
|
|
}
|
|
|
|
unset($update_fields[$field]);
|
|
|
|
}
|
2008-09-15 20:48:10 +00:00
|
|
|
|
2008-08-21 19:36:39 +00:00
|
|
|
// Because we filter $fields the same way here and in __toString(), the
|
|
|
|
// placeholders will all match up properly.
|
|
|
|
$max_placeholder = 0;
|
|
|
|
foreach ($update_fields as $field => $value) {
|
|
|
|
$values[':db_update_placeholder_' . ($max_placeholder++)] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-11 13:43:37 +00:00
|
|
|
// MySQL's INSERT ... ON DUPLICATE KEY UPDATE queries return 1
|
|
|
|
// (MergeQuery::STATUS_INSERT) for an INSERT operation or 2
|
|
|
|
// (MergeQuery::STATUS_UPDATE) for an UPDATE operation.
|
|
|
|
//
|
|
|
|
// @link http ://dev.mysql.com/doc/refman/5.0/en/mysql-affected-rows.html
|
|
|
|
$this->queryOptions['return'] = Database::RETURN_AFFECTED;
|
2010-05-06 05:59:31 +00:00
|
|
|
return $this->connection->query((string) $this, $values, $this->queryOptions);
|
2008-08-21 19:36:39 +00:00
|
|
|
}
|
|
|
|
|
2008-09-15 20:48:10 +00:00
|
|
|
|
2008-08-21 19:36:39 +00:00
|
|
|
public function __toString() {
|
2010-05-15 07:04:21 +00:00
|
|
|
// Create a comments string to prepend to the query.
|
|
|
|
$comments = (!empty($this->comments)) ? '/* ' . implode('; ', $this->comments) . ' */ ' : '';
|
2008-08-21 19:36:39 +00:00
|
|
|
|
|
|
|
// Set defaults.
|
|
|
|
if ($this->updateFields) {
|
|
|
|
$update_fields = $this->updateFields;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$update_fields = $this->insertFields;
|
|
|
|
// If there are no exclude fields, this is a no-op.
|
|
|
|
foreach ($this->excludeFields as $exclude_field) {
|
|
|
|
unset($update_fields[$exclude_field]);
|
|
|
|
}
|
|
|
|
}
|
2008-09-15 20:48:10 +00:00
|
|
|
|
2009-02-22 16:31:24 +00:00
|
|
|
// If the merge query has no fields to update, add the first key as an
|
|
|
|
// update field so the query will not fail if a duplicate key is found.
|
|
|
|
if (!$update_fields && !$this->expressionFields) {
|
|
|
|
$update_fields = array_slice($this->keyFields, 0, 1, TRUE);
|
|
|
|
}
|
|
|
|
|
2008-08-21 19:36:39 +00:00
|
|
|
$insert_fields = $this->insertFields + $this->keyFields;
|
|
|
|
|
2010-05-15 07:04:21 +00:00
|
|
|
$query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', array_keys($insert_fields)) . ') VALUES ';
|
2008-08-21 19:36:39 +00:00
|
|
|
|
|
|
|
$max_placeholder = 0;
|
|
|
|
$values = array();
|
|
|
|
// We don't need the $field, but this is a convenient way to count.
|
|
|
|
foreach ($insert_fields as $field) {
|
|
|
|
$values[] = ':db_insert_placeholder_' . $max_placeholder++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query .= '(' . implode(', ', $values) . ') ON DUPLICATE KEY UPDATE ';
|
2008-09-15 20:48:10 +00:00
|
|
|
|
2008-08-21 19:36:39 +00:00
|
|
|
// Expressions take priority over literal fields, so we process those first
|
|
|
|
// and remove any literal fields that conflict.
|
|
|
|
$max_placeholder = 0;
|
|
|
|
$update = array();
|
|
|
|
foreach ($this->expressionFields as $field => $data) {
|
|
|
|
$update[] = $field . '=' . $data['expression'];
|
|
|
|
unset($update_fields[$field]);
|
|
|
|
}
|
|
|
|
|
2008-09-27 20:10:26 +00:00
|
|
|
// Build update fields clauses based on caller supplied list, or derived
|
|
|
|
// from insert supplied values using the VALUES(col_name) function.
|
2008-08-21 19:36:39 +00:00
|
|
|
foreach ($update_fields as $field => $value) {
|
2008-09-27 20:10:26 +00:00
|
|
|
if ($this->updateFields) {
|
|
|
|
$update[] = ($field . '=:db_update_placeholder_' . $max_placeholder++);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$update[] = ($field . '=VALUES(' . $field . ')');
|
|
|
|
}
|
2008-08-21 19:36:39 +00:00
|
|
|
}
|
2008-09-15 20:48:10 +00:00
|
|
|
|
2008-08-21 19:36:39 +00:00
|
|
|
$query .= implode(', ', $update);
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @} End of "ingroup database".
|
|
|
|
*/
|