Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Statement.php 1532 2007-05-31 17:45:07Z zYne $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Doctrine_Connection_Statement
24
 *
25
 * @package     Doctrine
26
 * @subpackage  Connection
27
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
28
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
29
 * @link        www.doctrine-project.org
30
 * @since       1.0
31
 * @version     $Revision: 1532 $
32
 */
33
class Doctrine_Connection_Statement implements Doctrine_Adapter_Statement_Interface
34
{
35
    /**
36
     * @var Doctrine_Connection $conn       Doctrine_Connection object, every connection
37
     *                                      statement holds an instance of Doctrine_Connection
38
     */
39
    protected $_conn;
40
 
41
    /**
42
     * @var mixed $_stmt                    PDOStatement object, boolean false or Doctrine_Adapter_Statement object
43
     */
44
    protected $_stmt;
45
 
46
    /**
47
     * constructor
48
     *
49
     * @param Doctrine_Connection $conn     Doctrine_Connection object, every connection
50
     *                                      statement holds an instance of Doctrine_Connection
51
     * @param mixed $stmt
52
     */
53
    public function __construct(Doctrine_Connection $conn, $stmt)
54
    {
55
        $this->_conn = $conn;
56
        $this->_stmt = $stmt;
57
 
58
        if ($stmt === false) {
59
            throw new Doctrine_Exception('Unknown statement object given.');
60
        }
61
    }
62
 
63
    /**
64
     * getConnection
65
     * returns the connection object this statement uses
66
     *
67
     * @return Doctrine_Connection
68
     */
69
    public function getConnection()
70
    {
71
        return $this->_conn;
72
    }
73
 
74
    public function getStatement()
75
    {
76
        return $this->_stmt;
77
    }
78
 
79
    public function getQuery()
80
    {
81
        return $this->_stmt->queryString;
82
    }
83
 
84
    /**
85
     * bindColumn
86
     * Bind a column to a PHP variable
87
     *
88
     * @param mixed $column         Number of the column (1-indexed) or name of the column in the result set.
89
     *                              If using the column name, be aware that the name should match
90
     *                              the case of the column, as returned by the driver.
91
     *
92
     * @param string $param         Name of the PHP variable to which the column will be bound.
93
     * @param integer $type         Data type of the parameter, specified by the Doctrine_Core::PARAM_* constants.
94
     * @return boolean              Returns TRUE on success or FALSE on failure
95
     */
96
    public function bindColumn($column, $param, $type = null)
97
    {
98
        if ($type === null) {
99
            return $this->_stmt->bindColumn($column, $param);
100
        } else {
101
            return $this->_stmt->bindColumn($column, $param, $type);
102
        }
103
    }
104
 
105
    /**
106
     * bindValue
107
     * Binds a value to a corresponding named or question mark
108
     * placeholder in the SQL statement that was use to prepare the statement.
109
     *
110
     * @param mixed $param          Parameter identifier. For a prepared statement using named placeholders,
111
     *                              this will be a parameter name of the form :name. For a prepared statement
112
     *                              using question mark placeholders, this will be the 1-indexed position of the parameter
113
     *
114
     * @param mixed $value          The value to bind to the parameter.
115
     * @param integer $type         Explicit data type for the parameter using the Doctrine_Core::PARAM_* constants.
116
     *
117
     * @return boolean              Returns TRUE on success or FALSE on failure.
118
     */
119
    public function bindValue($param, $value, $type = null)
120
    {
121
        if ($type === null) {
122
            return $this->_stmt->bindValue($param, $value);
123
        } else {
124
            return $this->_stmt->bindValue($param, $value, $type);
125
        }
126
    }
127
 
128
    /**
129
     * bindParam
130
     * Binds a PHP variable to a corresponding named or question mark placeholder in the
131
     * SQL statement that was use to prepare the statement. Unlike Doctrine_Adapter_Statement_Interface->bindValue(),
132
     * the variable is bound as a reference and will only be evaluated at the time
133
     * that Doctrine_Adapter_Statement_Interface->execute() is called.
134
     *
135
     * Most parameters are input parameters, that is, parameters that are
136
     * used in a read-only fashion to build up the query. Some drivers support the invocation
137
     * of stored procedures that return data as output parameters, and some also as input/output
138
     * parameters that both send in data and are updated to receive it.
139
     *
140
     * @param mixed $param          Parameter identifier. For a prepared statement using named placeholders,
141
     *                              this will be a parameter name of the form :name. For a prepared statement
142
     *                              using question mark placeholders, this will be the 1-indexed position of the parameter
143
     *
144
     * @param mixed $variable       Name of the PHP variable to bind to the SQL statement parameter.
145
     *
146
     * @param integer $type         Explicit data type for the parameter using the Doctrine_Core::PARAM_* constants. To return
147
     *                              an INOUT parameter from a stored procedure, use the bitwise OR operator to set the
148
     *                              Doctrine_Core::PARAM_INPUT_OUTPUT bits for the data_type parameter.
149
     *
150
     * @param integer $length       Length of the data type. To indicate that a parameter is an OUT parameter
151
     *                              from a stored procedure, you must explicitly set the length.
152
     * @param mixed $driverOptions
153
     * @return boolean              Returns TRUE on success or FALSE on failure.
154
     */
155
    public function bindParam($column, &$variable, $type = null, $length = null, $driverOptions = array())
156
    {
157
        if ($type === null) {
158
            return $this->_stmt->bindParam($column, $variable);
159
        } else {
160
            return $this->_stmt->bindParam($column, $variable, $type, $length, $driverOptions);
161
        }
162
    }
163
 
164
    /**
165
     * closeCursor
166
     * Closes the cursor, enabling the statement to be executed again.
167
     *
168
     * @return boolean              Returns TRUE on success or FALSE on failure.
169
     */
170
    public function closeCursor()
171
    {
172
        return $this->_stmt->closeCursor();
173
    }
174
 
175
    /**
176
     * columnCount
177
     * Returns the number of columns in the result set
178
     *
179
     * @return integer              Returns the number of columns in the result set represented
180
     *                              by the Doctrine_Adapter_Statement_Interface object. If there is no result set,
181
     *                              this method should return 0.
182
     */
183
    public function columnCount()
184
    {
185
        return $this->_stmt->columnCount();
186
    }
187
 
188
    /**
189
     * errorCode
190
     * Fetch the SQLSTATE associated with the last operation on the statement handle
191
     *
192
     * @see Doctrine_Adapter_Interface::errorCode()
193
     * @return string       error code string
194
     */
195
    public function errorCode()
196
    {
197
        return $this->_stmt->errorCode();
198
    }
199
 
200
    /**
201
     * errorInfo
202
     * Fetch extended error information associated with the last operation on the statement handle
203
     *
204
     * @see Doctrine_Adapter_Interface::errorInfo()
205
     * @return array        error info array
206
     */
207
    public function errorInfo()
208
    {
209
        return $this->_stmt->errorInfo();
210
    }
211
 
212
    /**
213
     * execute
214
     * Executes a prepared statement
215
     *
216
     * If the prepared statement included parameter markers, you must either:
217
     * call PDOStatement->bindParam() to bind PHP variables to the parameter markers:
218
     * bound variables pass their value as input and receive the output value,
219
     * if any, of their associated parameter markers or pass an array of input-only
220
     * parameter values
221
     *
222
     *
223
     * @param array $params             An array of values with as many elements as there are
224
     *                                  bound parameters in the SQL statement being executed.
225
     * @return boolean                  Returns TRUE on success or FALSE on failure.
226
     */
227
    public function execute($params = null)
228
    {
229
        try {
230
            $event = new Doctrine_Event($this, Doctrine_Event::STMT_EXECUTE, $this->getQuery(), $params);
231
            $this->_conn->getListener()->preStmtExecute($event);
232
 
233
            $result = true;
234
            if ( ! $event->skipOperation) {
235
 
236
                if ($this->_conn->getAttribute(Doctrine_Core::ATTR_PORTABILITY) & Doctrine_Core::PORTABILITY_EMPTY_TO_NULL) {
237
                    foreach ($params as $key => $value) {
238
                        if ($value === '') {
239
                            $params[$key] = null;
240
                        }
241
                    }
242
                }
243
 
244
                if ($params) {
245
                    $pos = 0;
246
                    foreach ($params as $key => $value) {
247
                        $pos++;
248
                        $param = is_numeric($key) ?  $pos : $key;
249
                        if (is_resource($value)) {
250
                            $this->_stmt->bindParam($param, $params[$key], Doctrine_Core::PARAM_LOB);
251
                        } else {
252
                            $this->_stmt->bindParam($param, $params[$key]);
253
                        }
254
                    }
255
                }
256
 
257
                $result = $this->_stmt->execute();
258
 
259
                $this->_conn->incrementQueryCount();
260
            }
261
 
262
            $this->_conn->getListener()->postStmtExecute($event);
263
 
264
            return $result;
265
        } catch (PDOException $e) {
266
        } catch (Doctrine_Adapter_Exception $e) {
267
        }
268
 
269
        $this->_conn->rethrowException($e, $this);
270
 
271
        return false;
272
    }
273
 
274
    /**
275
     * fetch
276
     *
277
     * @see Doctrine_Core::FETCH_* constants
278
     * @param integer $fetchStyle           Controls how the next row will be returned to the caller.
279
     *                                      This value must be one of the Doctrine_Core::FETCH_* constants,
280
     *                                      defaulting to Doctrine_Core::FETCH_BOTH
281
     *
282
     * @param integer $cursorOrientation    For a PDOStatement object representing a scrollable cursor,
283
     *                                      this value determines which row will be returned to the caller.
284
     *                                      This value must be one of the Doctrine_Core::FETCH_ORI_* constants, defaulting to
285
     *                                      Doctrine_Core::FETCH_ORI_NEXT. To request a scrollable cursor for your
286
     *                                      Doctrine_Adapter_Statement_Interface object,
287
     *                                      you must set the Doctrine_Core::ATTR_CURSOR attribute to Doctrine_Core::CURSOR_SCROLL when you
288
     *                                      prepare the SQL statement with Doctrine_Adapter_Interface->prepare().
289
     *
290
     * @param integer $cursorOffset         For a Doctrine_Adapter_Statement_Interface object representing a scrollable cursor for which the
291
     *                                      $cursorOrientation parameter is set to Doctrine_Core::FETCH_ORI_ABS, this value specifies
292
     *                                      the absolute number of the row in the result set that shall be fetched.
293
     *
294
     *                                      For a Doctrine_Adapter_Statement_Interface object representing a scrollable cursor for
295
     *                                      which the $cursorOrientation parameter is set to Doctrine_Core::FETCH_ORI_REL, this value
296
     *                                      specifies the row to fetch relative to the cursor position before
297
     *                                      Doctrine_Adapter_Statement_Interface->fetch() was called.
298
     *
299
     * @return mixed
300
     */
301
    public function fetch($fetchMode = Doctrine_Core::FETCH_BOTH,
302
                          $cursorOrientation = Doctrine_Core::FETCH_ORI_NEXT,
303
                          $cursorOffset = null)
304
    {
305
        $event = new Doctrine_Event($this, Doctrine_Event::STMT_FETCH, $this->getQuery());
306
 
307
        $event->fetchMode = $fetchMode;
308
        $event->cursorOrientation = $cursorOrientation;
309
        $event->cursorOffset = $cursorOffset;
310
 
311
        $data = $this->_conn->getListener()->preFetch($event);
312
 
313
        if ( ! $event->skipOperation) {
314
            $data = $this->_stmt->fetch($fetchMode, $cursorOrientation, $cursorOffset);
315
        }
316
 
317
        $this->_conn->getListener()->postFetch($event);
318
 
319
        return $data;
320
    }
321
 
322
    /**
323
     * fetchAll
324
     * Returns an array containing all of the result set rows
325
     *
326
     * @param integer $fetchMode            Controls how the next row will be returned to the caller.
327
     *                                      This value must be one of the Doctrine_Core::FETCH_* constants,
328
     *                                      defaulting to Doctrine_Core::FETCH_BOTH
329
     *
330
     * @param integer $columnIndex          Returns the indicated 0-indexed column when the value of $fetchStyle is
331
     *                                      Doctrine_Core::FETCH_COLUMN. Defaults to 0.
332
     *
333
     * @return array
334
     */
335
    public function fetchAll($fetchMode = Doctrine_Core::FETCH_BOTH,
336
                             $columnIndex = null)
337
    {
338
        $event = new Doctrine_Event($this, Doctrine_Event::STMT_FETCHALL, $this->getQuery());
339
        $event->fetchMode = $fetchMode;
340
        $event->columnIndex = $columnIndex;
341
 
342
        $this->_conn->getListener()->preFetchAll($event);
343
 
344
        if ( ! $event->skipOperation) {
345
            if ($columnIndex !== null) {
346
                $data = $this->_stmt->fetchAll($fetchMode, $columnIndex);
347
            } else {
348
                $data = $this->_stmt->fetchAll($fetchMode);
349
            }
350
 
351
            $event->data = $data;
352
        }
353
 
354
        $this->_conn->getListener()->postFetchAll($event);
355
 
356
        return $data;
357
    }
358
 
359
    /**
360
     * fetchColumn
361
     * Returns a single column from the next row of a
362
     * result set or FALSE if there are no more rows.
363
     *
364
     * @param integer $columnIndex          0-indexed number of the column you wish to retrieve from the row. If no
365
     *                                      value is supplied, Doctrine_Adapter_Statement_Interface->fetchColumn()
366
     *                                      fetches the first column.
367
     *
368
     * @return string                       returns a single column in the next row of a result set.
369
     */
370
    public function fetchColumn($columnIndex = 0)
371
    {
372
        return $this->_stmt->fetchColumn($columnIndex);
373
    }
374
 
375
    /**
376
     * fetchObject
377
     * Fetches the next row and returns it as an object.
378
     *
379
     * Fetches the next row and returns it as an object. This function is an alternative to
380
     * Doctrine_Adapter_Statement_Interface->fetch() with Doctrine_Core::FETCH_CLASS or Doctrine_Core::FETCH_OBJ style.
381
     *
382
     * @param string $className             Name of the created class, defaults to stdClass.
383
     * @param array $args                   Elements of this array are passed to the constructor.
384
     *
385
     * @return mixed                        an instance of the required class with property names that correspond
386
     *                                      to the column names or FALSE in case of an error.
387
     */
388
    public function fetchObject($className = 'stdClass', $args = array())
389
    {
390
        return $this->_stmt->fetchObject($className, $args);
391
    }
392
 
393
    /**
394
     * getAttribute
395
     * Retrieve a statement attribute
396
     *
397
     * @param integer $attribute
398
     * @see Doctrine_Core::ATTR_* constants
399
     * @return mixed                        the attribute value
400
     */
401
    public function getAttribute($attribute)
402
    {
403
        return $this->_stmt->getAttribute($attribute);
404
    }
405
 
406
    /**
407
     * getColumnMeta
408
     * Returns metadata for a column in a result set
409
     *
410
     * @param integer $column               The 0-indexed column in the result set.
411
     *
412
     * @return array                        Associative meta data array with the following structure:
413
     *
414
     *          native_type                 The PHP native type used to represent the column value.
415
     *          driver:decl_                type The SQL type used to represent the column value in the database. If the column in the result set is the result of a function, this value is not returned by PDOStatement->getColumnMeta().
416
     *          flags                       Any flags set for this column.
417
     *          name                        The name of this column as returned by the database.
418
     *          len                         The length of this column. Normally -1 for types other than floating point decimals.
419
     *          precision                   The numeric precision of this column. Normally 0 for types other than floating point decimals.
420
     *          pdo_type                    The type of this column as represented by the PDO::PARAM_* constants.
421
     */
422
    public function getColumnMeta($column)
423
    {
424
        return $this->_stmt->getColumnMeta($column);
425
    }
426
 
427
    /**
428
     * nextRowset
429
     * Advances to the next rowset in a multi-rowset statement handle
430
     *
431
     * Some database servers support stored procedures that return more than one rowset
432
     * (also known as a result set). The nextRowset() method enables you to access the second
433
     * and subsequent rowsets associated with a PDOStatement object. Each rowset can have a
434
     * different set of columns from the preceding rowset.
435
     *
436
     * @return boolean                      Returns TRUE on success or FALSE on failure.
437
     */
438
    public function nextRowset()
439
    {
440
        return $this->_stmt->nextRowset();
441
    }
442
 
443
    /**
444
     * rowCount
445
     * rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
446
     * executed by the corresponding object.
447
     *
448
     * If the last SQL statement executed by the associated Statement object was a SELECT statement,
449
     * some databases may return the number of rows returned by that statement. However,
450
     * this behaviour is not guaranteed for all databases and should not be
451
     * relied on for portable applications.
452
     *
453
     * @return integer                      Returns the number of rows.
454
     */
455
    public function rowCount()
456
    {
457
        return $this->_stmt->rowCount();
458
    }
459
 
460
    /**
461
     * setAttribute
462
     * Set a statement attribute
463
     *
464
     * @param integer $attribute
465
     * @param mixed $value                  the value of given attribute
466
     * @return boolean                      Returns TRUE on success or FALSE on failure.
467
     */
468
    public function setAttribute($attribute, $value)
469
    {
470
        return $this->_stmt->setAttribute($attribute, $value);
471
    }
472
 
473
    /**
474
     * setFetchMode
475
     * Set the default fetch mode for this statement
476
     *
477
     * @param integer $mode                 The fetch mode must be one of the Doctrine_Core::FETCH_* constants.
478
     * @return boolean                      Returns 1 on success or FALSE on failure.
479
     */
480
    public function setFetchMode($mode, $arg1 = null, $arg2 = null)
481
    {
482
        return $this->_stmt->setFetchMode($mode, $arg1, $arg2);
483
    }
484
}