Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Driver / SQLSrv / SQLSrvStatement.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\DBAL\Driver\SQLSrv;
21
22 use PDO;
23 use IteratorAggregate;
24 use Doctrine\DBAL\Driver\Statement;
25
26 /**
27  * SQL Server Statement
28  *
29  * @since 2.3
30  * @author Benjamin Eberlei <kontakt@beberlei.de>
31  */
32 class SQLSrvStatement implements IteratorAggregate, Statement
33 {
34     /**
35      * SQLSRV Resource
36      *
37      * @var resource
38      */
39     private $conn;
40
41     /**
42      * SQL Statement to execute
43      *
44      * @var string
45      */
46     private $sql;
47
48     /**
49      * SQLSRV Statement Resource
50      *
51      * @var resource
52      */
53     private $stmt;
54
55     /**
56      * Parameters to bind
57      *
58      * @var array
59      */
60     private $params = array();
61
62     /**
63      * Translations
64      *
65      * @var array
66      */
67     private static $fetchMap = array(
68         PDO::FETCH_BOTH => SQLSRV_FETCH_BOTH,
69         PDO::FETCH_ASSOC => SQLSRV_FETCH_ASSOC,
70         PDO::FETCH_NUM => SQLSRV_FETCH_NUMERIC,
71     );
72
73     /**
74      * Fetch Style
75      *
76      * @param int
77      */
78     private $defaultFetchMode = PDO::FETCH_BOTH;
79
80     /**
81      * @var int|null
82      */
83     private $lastInsertId;
84
85     /**
86      * Append to any INSERT query to retrieve the last insert id.
87      *
88      * @var string
89      */
90     const LAST_INSERT_ID_SQL = ';SELECT SCOPE_IDENTITY() AS LastInsertId;';
91
92     public function __construct($conn, $sql, $lastInsertId = null)
93     {
94         $this->conn = $conn;
95         $this->sql = $sql;
96
97         if (stripos($sql, 'INSERT INTO ') === 0) {
98             $this->sql .= self::LAST_INSERT_ID_SQL;
99             $this->lastInsertId = $lastInsertId;
100         }
101     }
102
103     public function bindValue($param, $value, $type = null)
104     {
105         return $this->bindParam($param, $value, $type,null);
106     }
107
108     /**
109      * {@inheritdoc}
110      */
111     public function bindParam($column, &$variable, $type = null, $length = null)
112     {
113         if (!is_numeric($column)) {
114             throw new SQLSrvException("sqlsrv does not support named parameters to queries, use question mark (?) placeholders instead.");
115         }
116
117         if ($type === \PDO::PARAM_LOB) {
118             $this->params[$column-1] = array($variable, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), SQLSRV_SQLTYPE_VARBINARY('max'));
119         } else {
120             $this->params[$column-1] = $variable;
121         }
122     }
123
124     public function closeCursor()
125     {
126         if ($this->stmt) {
127             sqlsrv_free_stmt($this->stmt);
128         }
129     }
130
131     public function columnCount()
132     {
133         return sqlsrv_num_fields($this->stmt);
134     }
135
136     /**
137      * {@inheritDoc}
138      */
139     public function errorCode()
140     {
141         $errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
142         if ($errors) {
143             return $errors[0]['code'];
144         }
145         return false;
146     }
147
148     /**
149      * {@inheritDoc}
150      */
151     public function errorInfo()
152     {
153         return sqlsrv_errors(SQLSRV_ERR_ERRORS);
154     }
155
156     public function execute($params = null)
157     {
158         if ($params) {
159             $hasZeroIndex = array_key_exists(0, $params);
160             foreach ($params as $key => $val) {
161                 $key = ($hasZeroIndex && is_numeric($key)) ? $key + 1 : $key;
162                 $this->bindValue($key, $val);
163             }
164         }
165
166         $this->stmt = sqlsrv_query($this->conn, $this->sql, $this->params);
167         if ( ! $this->stmt) {
168             throw SQLSrvException::fromSqlSrvErrors();
169         }
170
171         if ($this->lastInsertId) {
172             sqlsrv_next_result($this->stmt);
173             sqlsrv_fetch($this->stmt);
174             $this->lastInsertId->setId( sqlsrv_get_field($this->stmt, 0) );
175         }
176     }
177
178     public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
179     {
180         $this->defaultFetchMode = $fetchMode;
181     }
182
183     /**
184      * {@inheritdoc}
185      */
186     public function getIterator()
187     {
188         $data = $this->fetchAll();
189         return new \ArrayIterator($data);
190     }
191
192     /**
193      * {@inheritdoc}
194      */
195     public function fetch($fetchMode = null)
196     {
197         $fetchMode = $fetchMode ?: $this->defaultFetchMode;
198         if (isset(self::$fetchMap[$fetchMode])) {
199             return sqlsrv_fetch_array($this->stmt, self::$fetchMap[$fetchMode]);
200         } else if ($fetchMode == PDO::FETCH_OBJ || $fetchMode == PDO::FETCH_CLASS) {
201             $className = null;
202             $ctorArgs = null;
203             if (func_num_args() >= 2) {
204                 $args = func_get_args();
205                 $className = $args[1];
206                 $ctorArgs = (isset($args[2])) ? $args[2] : array();
207             }
208             return sqlsrv_fetch_object($this->stmt, $className, $ctorArgs);
209         }
210
211         throw new SQLSrvException("Fetch mode is not supported!");
212     }
213
214     /**
215      * {@inheritdoc}
216      */
217     public function fetchAll($fetchMode = null)
218     {
219         $className = null;
220         $ctorArgs = null;
221         if (func_num_args() >= 2) {
222             $args = func_get_args();
223             $className = $args[1];
224             $ctorArgs = (isset($args[2])) ? $args[2] : array();
225         }
226
227         $rows = array();
228         while ($row = $this->fetch($fetchMode, $className, $ctorArgs)) {
229             $rows[] = $row;
230         }
231         return $rows;
232     }
233
234     /**
235      * {@inheritdoc}
236      */
237     public function fetchColumn($columnIndex = 0)
238     {
239         $row = $this->fetch(PDO::FETCH_NUM);
240         return $row[$columnIndex];
241     }
242
243     /**
244      * {@inheritdoc}
245      */
246     public function rowCount()
247     {
248         return sqlsrv_rows_affected($this->stmt);
249     }
250 }
251