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