silverstripe-framework/model/queries/SQLAssignmentRow.php

221 lines
6.2 KiB
PHP
Raw Normal View History

<?php
/**
* Represents a list of updates / inserts made to a single row in a table
2014-08-15 08:53:05 +02:00
*
* @package framework
* @subpackage model
*/
class SQLAssignmentRow {
2014-08-15 08:53:05 +02:00
/**
* List of field values to store for this query
2014-08-15 08:53:05 +02:00
*
* Each item in this array will be in the form of a single-length array
* in the format array('sql' => array($parameters)).
* The field name is stored as the key
2014-08-15 08:53:05 +02:00
*
* E.g.
2014-08-15 08:53:05 +02:00
*
* <code>$assignments['ID'] = array('?' => array(1));</code>
2014-08-15 08:53:05 +02:00
*
* This allows for complex, parameterised updates, or explict field values set
* without any prameters
2014-08-15 08:53:05 +02:00
*
* @var array
*/
protected $assignments = array();
2014-08-15 08:53:05 +02:00
/**
* Instantiate a new SQLAssignmentRow object with the given values
2014-08-15 08:53:05 +02:00
*
* @param array $values
*/
function __construct(array $values = array()) {
$this->setAssignments($values);
}
2014-08-15 08:53:05 +02:00
/**
* Given a key / value pair, extract the predicate and any potential paramaters
* in a format suitable for storing internally as a list of paramaterised conditions.
2014-08-15 08:53:05 +02:00
*
* @param mixed $value Either a literal field value, or an array with
* placeholder => parameter(s) as a pair
* @return array A single item array in the format array($sql => array($parameters))
*/
protected function parseAssignment($value) {
// Assume string values (or values saved as customised array objects)
// represent simple assignment
if(!is_array($value) || isset($value['type'])) {
return array('?' => array($value));
}
2014-08-15 08:53:05 +02:00
// If given as array then extract and check both the SQL as well as the parameter(s)
// Note that there could be multiple parameters, e.g.
// array('MAX(?,?)' => array(1,2)) although the container should
// have a single item
if(count($value) == 1) {
foreach($value as $sql => $parameters) {
if(!is_string($sql)) continue;
if(!is_array($parameters)) $parameters = array($parameters);
2014-08-15 08:53:05 +02:00
// @todo Some input sanitisation checking the key contains the
// correct number of ? placeholders as the number of parameters
return array($sql => $parameters);
}
}
2014-08-15 08:53:05 +02:00
user_error("Nested field assignments should be given as a single parameterised item array in "
. "array('?' => array('value')) format)", E_USER_ERROR);
}
2014-08-15 08:53:05 +02:00
/**
* Given a list of assignments in any user-acceptible format, normalise the
* value to a common array('SQL' => array(parameters)) format
2014-08-15 08:53:05 +02:00
*
* @param array $predicates List of assignments.
* The key of this array should be the field name, and the value the assigned
* literal value, or an array with parameterised information.
* @return array List of normalised assignments
*/
protected function normaliseAssignments(array $assignments) {
$normalised = array();
foreach($assignments as $field => $value) {
$normalised[$field] = $this->parseAssignment($value);
}
return $normalised;
}
2014-08-15 08:53:05 +02:00
/**
* Adds assignments for a list of several fields
2014-08-15 08:53:05 +02:00
*
* Note that field values must not be escaped, as these will be internally
* parameterised by the database engine.
2014-08-15 08:53:05 +02:00
*
* <code>
2014-08-15 08:53:05 +02:00
*
* // Basic assignments
* $query->addAssignments(array(
* '"Object"."Title"' => 'Bob',
* '"Object"."Description"' => 'Bob was here'
* ))
2014-08-15 08:53:05 +02:00
*
* // Parameterised assignments
* $query->addAssignments(array(
* '"Object"."Title"' => array('?' => 'Bob')),
* '"Object"."Description"' => array('?' => null))
* ))
2014-08-15 08:53:05 +02:00
*
* // Complex parameters
* $query->addAssignments(array(
* '"Object"."Score"' => array('MAX(?,?)' => array(1, 3))
* ));
2014-08-15 08:53:05 +02:00
*
* // Assigment of literal SQL for a field. The empty array is
* // important to denote the zero-number paramater list
* $query->addAssignments(array(
* '"Object"."Score"' => array('NOW()' => array())
* ));
2014-08-15 08:53:05 +02:00
*
* </code>
2014-08-15 08:53:05 +02:00
*
* @param array $assignments The list of fields to assign
* @return self The self reference to this row
*/
public function addAssignments(array $assignments) {
$assignments = $this->normaliseAssignments($assignments);
$this->assignments = array_merge($this->assignments, $assignments);
return $this;
}
2014-08-15 08:53:05 +02:00
/**
* Sets the list of assignments to the given list
2014-08-15 08:53:05 +02:00
*
* @see SQLWriteExpression::addAssignments() for syntax examples
2014-08-15 08:53:05 +02:00
*
* @param array $assignments
* @return self The self reference to this row
*/
public function setAssignments(array $assignments) {
return $this->clear()->addAssignments($assignments);
}
2014-08-15 08:53:05 +02:00
/**
* Retrieves the list of assignments in parameterised format
2014-08-15 08:53:05 +02:00
*
* @return array List of assigments. The key of this array will be the
* column to assign, and the value a parameterised array in the format
* array('SQL' => array(parameters));
*/
public function getAssignments() {
return $this->assignments;
}
2014-08-15 08:53:05 +02:00
/**
* Set the value for a single field
2014-08-15 08:53:05 +02:00
*
* E.g.
* <code>
2014-08-15 08:53:05 +02:00
*
* // Literal assignment
* $query->assign('"Object"."Description"', 'lorum ipsum');
2014-08-15 08:53:05 +02:00
*
* // Single parameter
* $query->assign('"Object"."Title"', array('?' => 'Bob'));
2014-08-15 08:53:05 +02:00
*
* // Complex parameters
* $query->assign('"Object"."Score"', array('MAX(?,?)' => array(1, 3));
* </code>
2014-08-15 08:53:05 +02:00
*
* @param string $field The field name to update
* @param mixed $value The value to assign to this field. This could be an
* array containing a parameterised SQL query of any number of parameters,
* or a single literal value.
* @return self The self reference to this row
*/
public function assign($field, $value) {
return $this->addAssignments(array($field => $value));
}
2014-08-15 08:53:05 +02:00
/**
2014-08-15 08:53:05 +02:00
* Assigns a value to a field using the literal SQL expression, rather than
* a value to be escaped
2014-08-15 08:53:05 +02:00
*
* @param string $field The field name to update
* @param string $sql The SQL to use for this update. E.g. "NOW()"
* @return self The self reference to this row
*/
public function assignSQL($field, $sql) {
return $this->assign($field, array($sql => array()));
}
2014-08-15 08:53:05 +02:00
/**
* Determine if this assignment is empty
2014-08-15 08:53:05 +02:00
*
* @return boolean Flag indicating that this assignment is empty
*/
public function isEmpty() {
return empty($this->assignments);
}
2014-08-15 08:53:05 +02:00
/**
* Retrieves the list of columns updated
2014-08-15 08:53:05 +02:00
*
* @return array
*/
public function getColumns() {
return array_keys($this->assignments);
}
2014-08-15 08:53:05 +02:00
/**
* Clears all assignment values
2014-08-15 08:53:05 +02:00
*
* @return self The self reference to this row
*/
public function clear() {
$this->assignments = array();
return $this;
}
}