Neevo Public API
  • Namespace
  • Class
  • Tree

Namespaces

  • Neevo
    • Nette
  • None
  • PHP

Classes

  • Neevo
  • NeevoBaseStmt
  • NeevoCacheFile
  • NeevoCacheMemcache
  • NeevoCacheMemory
  • NeevoCacheSession
  • NeevoConnection
  • NeevoDriverMySQL
  • NeevoDriverMySQLi
  • NeevoDriverPgSQL
  • NeevoDriverSQLite2
  • NeevoDriverSQLite3
  • NeevoLiteral
  • NeevoLoader
  • NeevoObserverMap
  • NeevoParser
  • NeevoResult
  • NeevoResultIterator
  • NeevoRow
  • NeevoStmt

Interfaces

  • INeevoCache
  • INeevoDriver
  • INeevoObservable
  • INeevoObserver

Exceptions

  • NeevoDriverException
  • NeevoException
  • NeevoImplementationException
  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) 2011 Martin Srank (http://smasty.net)
  9  *
 10  */
 11 
 12 
 13 /**
 14  * Class for data manipulation statements (INSERT, UPDATE, DELETE)
 15  * @author Martin Srank
 16  * @package Neevo
 17  */
 18 class NeevoStmt extends NeevoBaseStmt {
 19 
 20 
 21     /** @var array */
 22     protected $values = array();
 23 
 24     /** @var int */
 25     protected $affectedRows;
 26 
 27 
 28     /*  ************  Statement factories  ************  */
 29 
 30 
 31     /**
 32      * Create UPDATE statement.
 33      * @param NeevoConnection $connection
 34      * @param string $table
 35      * @param array|Traversable $data
 36      * @return NeevoStmt fluent interface
 37      */
 38     public static function createUpdate(NeevoConnection $connection, $table, $data){
 39         if(!($data instanceof Traversable || (is_array($data) && !empty($data))))
 40             throw new InvalidArgumentException('Data must be a non-empty array or Traversable.');
 41 
 42         $obj = new self($connection);
 43         $obj->type = Neevo::STMT_UPDATE;
 44         $obj->source = $table;
 45         $obj->values = $data instanceof Traversable ? iterator_to_array($data) : $data;
 46         return $obj;
 47     }
 48 
 49 
 50     /**
 51      * Create INSERT statement.
 52      * @param NeevoConnection $connection
 53      * @param string $table
 54      * @param array $values
 55      * @return NeevoStmt fluent interface
 56      */
 57     public static function createInsert(NeevoConnection $connection, $table, array $values){
 58         if(!($values instanceof Traversable || (is_array($values) && !empty($values))))
 59             throw new InvalidArgumentException('Values must be a non-empty array or Traversable.');
 60 
 61         $obj = new self($connection);
 62         $obj->type = Neevo::STMT_INSERT;
 63         $obj->source = $table;
 64         $obj->values = $values instanceof Traversable ? iterator_to_array($values) : $values;
 65         return $obj;
 66     }
 67 
 68 
 69     /**
 70      * Create DELETE statement.
 71      * @param NeevoConnection $connection
 72      * @param string $table
 73      * @return NeevoStmt fluent interface
 74      */
 75     public static function createDelete(NeevoConnection $connection, $table){
 76         $obj = new self($connection);
 77         $obj->type = Neevo::STMT_DELETE;
 78         $obj->source = $table;
 79         return $obj;
 80     }
 81 
 82 
 83     public function run(){
 84         $result = parent::run();
 85 
 86         try{
 87             $this->affectedRows = $this->connection->getDriver()->getAffectedRows();
 88         } catch(NeevoDriverException $e){
 89             $this->affectedRows = false;
 90         }
 91 
 92         return $result;
 93     }
 94 
 95 
 96     /**
 97      * Get the ID generated in the last INSERT statement.
 98      * @return int|FALSE
 99      * @throws NeevoException on non-INSERT statements.
100      */
101     public function insertId(){
102         if($this->type !== Neevo::STMT_INSERT)
103             throw new NeevoException(__METHOD__.' can be called only on INSERT statements.');
104 
105         $this->performed || $this->run();
106         try{
107             return $this->connection->getDriver()->getInsertId();
108         } catch(NeevoImplemenationException $e){
109             return false;
110         }
111     }
112 
113 
114     /**
115      * Get the number of rows affected by the statement.
116      * @return int
117      */
118     public function affectedRows(){
119         $this->performed || $this->run();
120         if($this->affectedRows === false)
121             throw new NeevoDriverException('Affected rows are not supported by this driver.');
122         return $this->affectedRows;
123     }
124 
125 
126     /**
127      * Get values of statement.
128      * @return array
129      */
130     public function getValues(){
131         return $this->values;
132     }
133 
134 
135     /**
136      * Reset state of the statement.
137      * @return void
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