1 <?php
2 3 4 5 6 7 8 9 10
11
12 namespace Neevo;
13
14 use InvalidArgumentException;
15 use Traversable;
16
17
18 19 20 21
22 class Statement extends BaseStatement {
23
24
25
26 protected $values = array();
27
28
29 protected $affectedRows;
30
31
32 33 34 35 36 37 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 53 54 55 56 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 72 73 74 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 99 100 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 117 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 129 130
131 public function getValues(){
132 return $this->values;
133 }
134
135
136 137 138
139 public function resetState(){
140 parent::resetState();
141 $this->affectedRows = null;
142 }
143
144
145 }
146