123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- <?php
- /**
- * A class for creating SQL INSERT statements
- *
- * @author Ironpilot
- * @copyright Copywrite (c) 2011, STAPLE CODE
- *
- * This file is part of the STAPLE Framework.
- *
- * The STAPLE Framework is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by the
- * Free Software Foundation, either version 3 of the License, or (at your option)
- * any later version.
- *
- * The STAPLE Framework is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
- * more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
- */
- class Staple_Query_Insert
- {
- const LOW = "LOW_PRIORITY";
- const DELAYED = "DELAYED";
- const HIGH = "HIGH_PRIORITY";
-
- /**
- * The database object. A database object is required to properly escape input.
- * @var mysqli
- */
- protected $db;
- /**
- * The data to insert. May be a Select Statement Object or an array of DataSets
- * @var Staple_Query_DataSet | Staple_Query_Select
- */
- protected $data;
- /**
- * The Priority parameter of the SQL statement
- * @var string
- */
- protected $priority;
- /**
- * A boolean value used to set the IGNORE parameter
- * @var boolean
- */
- protected $ignore = false;
- /**
- * Table to update.
- * @var string
- */
- protected $table;
-
- /**
- * Boolean flag for ON DUPLICATE KEY UPDATE
- * @var boolean
- */
- protected $updateOnDuplicate = false;
- /**
- * The columns to update on a duplicate key.
- * @var array[string]
- */
- protected $updateColumns = array();
-
- public function __construct($table = NULL, $data = NULL, $db = NULL, $priority = NULL)
- {
- $this->data = new Staple_Query_DataSet();
-
- //Process Database connection
- if($db instanceof mysqli)
- {
- $this->setDb($db);
- }
- else
- {
- try {
- $this->setDb(Staple_DB::get());
- }
- catch (Exception $e)
- {
- $this->setDb(new mysqli());
- }
- }
- //No DB = Bad
- if(!($this->db instanceof mysqli))
- {
- throw new Exception('Unable to create database object', Staple_Error::DB_ERROR);
- }
-
- //Set Table
- if(isset($table))
- {
- $this->setTable($table);
- }
-
- //Set Data
- if(isset($data))
- {
- $this->setData($data);
- }
-
- //Set Priority
- if(isset($priority))
- {
- $this->setPriority($priority);
- }
- }
-
- /**
- * Execute the build function and return the result when converting to a string.
- */
- public function __toString()
- {
- try {
- $msg = $this->build();
- }
- catch (Exception $e)
- {
- $msg = $e->getMessage();
- }
- return $msg;
- }
-
- /**
- *
- * @see Staple_Query::build()
- */
- function build()
- {
- //Statement Start
- $stmt = "INSERT ";
-
- //Flags
- if(isset($this->priority))
- {
- $stmt .= $this->priority.' ';
- }
- if($this->ignore === TRUE)
- {
- $stmt .= 'IGNORE ';
- }
-
- //Table
- $stmt .= "\nINTO ".$this->table.' ';
-
- //Data
- if($this->data instanceof Staple_Query_DataSet)
- {
- $stmt .= $this->data->getInsertString();
- }
- elseif($this->data instanceof Staple_Query_Select)
- {
- $stmt .= "\n".$this->data;
- }
-
- //Duplicate Updates
- if($this->updateOnDuplicate === true)
- {
- $first = true;
- $stmt .= "\nON DUPLICATE KEY UPDATE ";
- foreach($this->updateColumns as $ucol)
- {
- if($first === true)
- {
- $first = false;
- }
- else
- {
- $stmt .= ',';
- }
- $stmt .= " $ucol=VALUES($ucol)";
- }
- }
-
- return $stmt;
- }
-
- /**
- * Executes the query.
- * @return mysqli_result | bool
- */
- public function Execute()
- {
- if($this->db instanceof mysqli)
- {
- return $this->db->query($this->build());
- }
- else
- {
- try
- {
- $this->db = Staple_DB::get();
- }
- catch (Exception $e)
- {
- //@todo try for a default connection if no staple connection
- throw new Exception('No Database Connection', Staple_Error::DB_ERROR);
- }
- if($this->db instanceof mysqli)
- {
- return $this->db->query($this->build());
- }
- }
- return false;
- }
-
-
- /**
- * Adds or replaces data in the insert dataset.
- * @param array $data
- * @throws Exception
- */
- public function addData(array $data)
- {
- if($this->data instanceof Staple_Query_Select)
- {
- throw new Exception('Cannot add data to an INSERT ... SELECT statement.', Staple_Error::DB_ERROR);
- }
- $this->data->addData($data);
- return $this;
- }
-
- /**
- * Adds or replaces a specific column value. Alias is set Data Column
- * @param string $column
- * @param mixed $data
- * @throws Exception
- * @see self::setDataColumn
- */
- public function addDataColumn($column, $data)
- {
- return $this->setDataColumn($column, $data);
- }
-
- /**
- * Adds a literal value to the dataset without conversion.
- * @param string $column
- * @param string $value
- */
- public function addLiteralColumn($column, $value)
- {
- return $this->setDataColumn($column, $value, true);
- }
-
- //----------------------------------------------GETTERS AND SETTERS----------------------------------------------
-
- /**
- * @return the $db
- */
- public function getDb()
- {
- return $this->db;
- }
-
- /**
- * @return the $data
- */
- public function getData()
- {
- return $this->data;
- }
-
- /**
- * @return the $priority
- */
- public function getPriority()
- {
- return $this->priority;
- }
- /**
- * @return the $ignore
- */
- public function getIgnore()
- {
- return $this->ignore;
- }
- /**
- * @return the $table
- */
- public function getTable()
- {
- return $this->table;
- }
- /**
- * @return the $updateOnDuplicate
- */
- public function getUpdateOnDuplicate()
- {
- return $this->updateOnDuplicate;
- }
- /**
- * @return the $updateColumns
- */
- public function getUpdateColumns()
- {
- return $this->updateColumns;
- }
-
- /**
- * @param mysqli $db
- */
- public function setDb(mysqli $db)
- {
- $this->db = $db;
- return $this;
- }
-
- /**
- * Sets the $data
- * @param Staple_Query_Select | Staple_Query_DataSet | array $data
- */
- public function setData($data)
- {
- if($data instanceof Staple_Query_Select || $data instanceof Staple_Query_DataSet)
- {
- $this->data = $data;
- }
- elseif(is_array($data))
- {
- $this->data = new Staple_Query_DataSet($data);
- }
- else
- {
- throw new Exception('Data must be an instance of Staple_Query_DataSet, an instance of Staple_Query_Select or an array', Staple_Error::APPLICATION_ERROR);
- }
- return $this;
- }
-
- /**
- * Sets the specified value for a specific column.
- * @param string $column
- * @param mixed $data
- * @param bool $literal
- * @throws Exception
- */
- public function setDataColumn($column,$data,$literal = false)
- {
- if($this->data instanceof Staple_Query_Select)
- {
- throw new Exception('Cannot add data to an INSERT ... SELECT statement.', Staple_Error::DB_ERROR);
- }
- if($literal === true)
- {
- $this->data->addLiteralColumn($column, $data);
- }
- else
- {
- $this->data[$column] = $data;
- }
- return $this;
- }
- /**
- * @param string $priority
- */
- public function setPriority($priority)
- {
- switch($priority)
- {
- case self::DELAYED:
- $this->priority = self::DELAYED;
- break;
- case self::HIGH:
- $this->priority = self::HIGH;
- case self::LOW:
- $this->priority = self::LOW;
- break;
- default: $this->priority = NULL;
- }
- return $this;
- }
- /**
- * @param boolean $ignore
- */
- public function setIgnore($ignore)
- {
- $this->ignore = (bool)$ignore;
- return $this;
- }
- /**
- * @param string $table
- */
- public function setTable($table)
- {
- $this->table = $table;
- return $this;
- }
- /**
- * @param bool $updateOnDuplicate
- */
- public function setUpdateOnDuplicate($updateOnDuplicate)
- {
- $this->updateOnDuplicate = (bool)$updateOnDuplicate;
- return $this;
- }
- /**
- * @param array[string] $updateColumns
- */
- public function setUpdateColumns(array $updateColumns)
- {
- $this->updateColumns = $updateColumns;
- return $this;
- }
-
- /**
- * Setup On Duplicate Key Update Syntax
- * @param bool $bool
- */
- public function onDuplicateKeyUpdate($bool = true)
- {
- $this->setUpdateOnDuplicate((bool)$bool);
- return $this;
- }
- }
- ?>
|