silverstripe-framework/model/queries/SQLInsert.php
Damian Mooyman d8e9af8af8 API New Database abstraction layer. Ticket #7429
Database abstraction broken up into controller, connector, query builder, and schema manager, each independently configurable via YAML / Injector
Creation of new DBQueryGenerator for database specific generation of SQL
Support for parameterised queries, move of code base to use these over escaped conditions
Refactor of SQLQuery into separate query classes for each of INSERT UPDATE DELETE and SELECT
Support for PDO
Installation process upgraded to use new ORM
SS_DatabaseException created to handle database errors, maintaining details of raw sql and parameter details for user code designed interested in that data.
Renamed DB static methods to conform correctly to naming conventions (e.g. DB::getConn -> DB::get_conn)
3.2 upgrade docs
Performance Optimisation and simplification of code to use more concise API
API Ability for database adapters to register extensions to ConfigureFromEnv.php
2014-07-09 18:04:05 +12:00

196 lines
4.2 KiB
PHP

<?php
/**
* Object representing a SQL INSERT query.
* The various parts of the SQL query can be manipulated individually.
*
* @package framework
* @subpackage model
*/
class SQLInsert extends SQLExpression implements SQLWriteExpression {
/**
* List of rows to be inserted
*
* @var array[SQLAssignmentRow]
*/
protected $rows = array();
/**
* The table name to insert into
*
* @var string
*/
protected $into = null;
/**
* Construct a new SQLInsert object
*
* @param string $into Table name to insert into
* @param array $assignments List of column assignments
* @return static
*/
public static function create($into = null, $assignments = array()) {
return Injector::inst()->createWithArgs(__CLASS__, func_get_args());
}
/**
* Construct a new SQLInsert object
*
* @param string $into Table name to insert into
* @param array $assignments List of column assignments
*/
function __construct($into = null, $assignments = array()) {
$this->setInto($into);
if(!empty($assignments)) {
$this->setAssignments($assignments);
}
}
/**
* Sets the table name to insert into
*
* @param string $into Single table name
* @return self The self reference to this query
*/
public function setInto($into) {
$this->into = $into;
return $this;
}
/**
* Gets the table name to insert into
*
* @return string Single table name
*/
public function getInto() {
return $this->into;
}
public function isEmpty() {
return empty($this->into) || empty($this->rows);
}
/**
* Appends a new row to insert
*
* @param array|SQLAssignmentRow $data A list of data to include for this row
* @return self The self reference to this query
*/
public function addRow($data = null) {
// Clear existing empty row
if(($current = $this->currentRow()) && $current->isEmpty()) {
array_pop($this->rows);
}
// Append data
if($data instanceof SQLAssignmentRow) {
$this->rows[] = $data;
} else {
$this->rows[] = new SQLAssignmentRow($data);
}
return $this;
}
/**
* Returns the current list of rows
*
* @return array[SQLAssignmentRow]
*/
public function getRows() {
return $this->rows;
}
/**
* Returns the list of distinct column names used in this insert
*
* @return array
*/
public function getColumns() {
$columns = array();
foreach($this->getRows() as $row) {
$columns = array_merge($columns, $row->getColumns());
}
return array_unique($columns);
}
/**
* Sets all rows to the given array
*
* @param array $rows the list of rows
* @return self The self reference to this query
*/
public function setRows(array $rows) {
return $this->clear()->addRows($rows);
}
/**
* Adds the list of rows to the array
*
* @param array $rows the list of rows
* @return self The self reference to this query
*/
public function addRows(array $rows) {
foreach($rows as $row) $this->addRow($row);
return $this;
}
/**
* Returns the currently set row
*
* @param boolean $create Flag to indicate if a row should be created if none exists
* @return SQLAssignmentRow|false The row, or false if none exists
*/
public function currentRow($create = false) {
$current = end($this->rows);
if($create && !$current) {
$this->rows[] = $current = new SQLAssignmentRow();
}
return $current;
}
public function addAssignments(array $assignments) {
$this->currentRow(true)->addAssignments($assignments);
return $this;
}
public function setAssignments(array $assignments) {
$this->currentRow(true)->setAssignments($assignments);
return $this;
}
public function getAssignments() {
return $this->currentRow(true)->getAssignments();
}
public function assign($field, $value) {
$this->currentRow(true)->assign($field, $value);
return $this;
}
public function assignSQL($field, $sql) {
$this->currentRow(true)->assignSQL($field, $sql);
return $this;
}
/**
* Clears all currently set assigment values on the current row
*
* @return self The self reference to this query
*/
public function clearRow() {
$this->currentRow(true)->clear();
return $this;
}
/**
* Clears all rows
*
* @return self The self reference to this query
*/
public function clear() {
$this->rows = array();
return $this;
}
}