Neevo Public API
  • Namespace
  • Class
  • Tree

Namespaces

  • Neevo
    • Cache
    • Drivers
    • Nette
  • PHP

Classes

  • BaseStatement
  • Connection
  • Literal
  • Manager
  • Parser
  • Result
  • ResultIterator
  • Row
  • Statement

Interfaces

  • DriverInterface
  • ObservableInterface
  • ObserverInterface

Exceptions

  • DriverException
  • ImplementationException
  • NeevoException
  1 <?php
  2 /**
  3  * Neevo - Tiny database layer for PHP. (http://neevo.smasty.net)
  4  *
  5  * This source file is subject to the MIT license that is bundled
  6  * with this package in the file license.txt.
  7  *
  8  * Copyright (c) 2012 Smasty (http://smasty.net)
  9  *
 10  */
 11 
 12 namespace Neevo;
 13 
 14 use InvalidArgumentException;
 15 use Traversable;
 16 
 17 
 18 /**
 19  * Class for data manipulation statements (INSERT, UPDATE, DELETE)
 20  * @author Smasty
 21  */
 22 class Statement extends BaseStatement {
 23 
 24 
 25     /** @var array */
 26     protected $values = array();
 27 
 28     /** @var int */
 29     protected $affectedRows;
 30 
 31 
 32     /**
 33      * Creates UPDATE statement.
 34      * @param Connection $connection
 35      * @param string $table
 36      * @param array|Traversable $data
 37      * @return Statement fluent interface
 38      */
 39     public static function createUpdate(Connection $connection, $table, $data){
 40         if(!($data instanceof Traversable || (is_array($data) && !empty($data))))
 41             throw new InvalidArgumentException('Data must be a non-empty array or Traversable.');
 42 
 43         $obj = new self($connection);
 44         $obj->type = Manager::STMT_UPDATE;
 45         $obj->source = $table;
 46         $obj->values = $data instanceof Traversable ? iterator_to_array($data) : $data;
 47         return $obj;
 48     }
 49 
 50 
 51     /**
 52      * Creates INSERT statement.
 53      * @param Connection $connection
 54      * @param string $table
 55      * @param array|Traversable $values
 56      * @return Statement fluent interface
 57      */
 58     public static function createInsert(Connection $connection, $table, $values){
 59         if(!($values instanceof Traversable || (is_array($values) && !empty($values))))
 60             throw new InvalidArgumentException('Values must be a non-empty array or Traversable.');
 61 
 62         $obj = new self($connection);
 63         $obj->type = Manager::STMT_INSERT;
 64         $obj->source = $table;
 65         $obj->values = $values instanceof Traversable ? iterator_to_array($values) : $values;
 66         return $obj;
 67     }
 68 
 69 
 70     /**
 71      * Creates DELETE statement.
 72      * @param Connection $connection
 73      * @param string $table
 74      * @return Statement fluent interface
 75      */
 76     public static function createDelete(Connection $connection, $table){
 77         $obj = new self($connection);
 78         $obj->type = Manager::STMT_DELETE;
 79         $obj->source = $table;
 80         return $obj;
 81     }
 82 
 83 
 84     public function run(){
 85         $result = parent::run();
 86 
 87         try{
 88             $this->affectedRows = $this->connection->getDriver()->getAffectedRows();
 89         } catch(DriverException $e){
 90             $this->affectedRows = false;
 91         }
 92 
 93         return $result;
 94     }
 95 
 96 
 97     /**
 98      * Returns the ID generated in the last INSERT statement.
 99      * @return int|bool
100      * @throws NeevoException on non-INSERT statements.
101      */
102     public function insertId(){
103         if($this->type !== Manager::STMT_INSERT)
104             throw new NeevoException(__METHOD__ . ' can be called only on INSERT statements.');
105 
106         $this->performed || $this->run();
107         try{
108             return $this->connection->getDriver()->getInsertId();
109         } catch(ImplementationException $e){
110             return false;
111         }
112     }
113 
114 
115     /**
116      * Returns the number of rows affected by the statement.
117      * @return int
118      */
119     public function affectedRows(){
120         $this->performed || $this->run();
121         if($this->affectedRows === false)
122             throw new DriverException('Affected rows are not supported by this driver.');
123         return $this->affectedRows;
124     }
125 
126 
127     /**
128      * Returns the values of statement.
129      * @return array
130      */
131     public function getValues(){
132         return $this->values;
133     }
134 
135 
136     /**
137      * Resets the state of the statement.
138      */
139     public function resetState(){
140         parent::resetState();
141         $this->affectedRows = null;
142     }
143 
144 
145 }
146 
Neevo Public API API documentation generated by ApiGen 2.8.0