| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 | <?php/** * * Cube Framework $Id$ YJ6QIPQLwvHGSqBT8khG6zOIOs40LWzpMnK9ALvjNQs= * * @link        http://codecu.be/framework * @copyright   Copyright (c) 2015 CodeCube SRL * @license     http://codecu.be/framework/license Commercial License * * @version     1.5 *//** * pdo statement class = proxy to \PDOStatement */namespace Cube\Db\Statement;use Cube\Exception;class Pdo extends AbstractStatement{    /**     *     * statement resource     *     * @var \PDOStatement     */    protected $_stmt = null;    /**     *     * fetch mode     *     * @var int     */    protected $_fetchMode = \PDO::FETCH_ASSOC;    /**     *     * prepare a string SQL statement and create a statement object.     *     * @param string $sql     *     * @return void     * @throws \Cube\Exception     */    protected function _prepare($sql)    {        try {            $this->_stmt = $this->_adapter->getConnection()->prepare($sql);        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }    /**     *     * bind a column to a php variable     *     * @param string $column name or position of the column in the result set     * @param mixed  $param  reference to the php variable to which the column will be bound     * @param int    $type   data type of the parameter     *     * @return bool     * @throws \Cube\Exception     */    public function bindColumn($column, &$param, $type = null)    {        $this->_bindColumn[$column] = &$param;        try {            return $this->_stmt->bindColumn($column, $param, $type);        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }    /**     *     * binds a parameter to the specified variable name     *     * @param int|string $parameter parameter identifier     * @param mixed      $variable  name of the PHP variable to bind to the SQL statement parameter     * @param int        $type      data type of the parameter     * @param int        $length    length of the data type     * @param mixed      $options   other driver options     *     * @throws \InvalidArgumentException     * @throws \Cube\Exception     * @return bool     */    public function bindParam($parameter, &$variable, $type = null, $length = null, $options = null)    {        if (!is_int($parameter) && !is_string($parameter)) {            throw new \InvalidArgumentException('Invalid bind-variable position');        }        $this->_bindParam[$parameter] = &$variable;        try {            if ($type === null) {                if (is_bool($variable)) {                    $type = \PDO::PARAM_BOOL;                }                elseif ($variable === null) {                    $type = \PDO::PARAM_NULL;                }                elseif (is_integer($variable)) {                    $type = \PDO::PARAM_INT;                }                else {                    $type = \PDO::PARAM_STR;                }            }            return $this->_stmt->bindParam($parameter, $variable, $type, $length, $options);        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }    /**     *     * binds a value to a parameter     *     * @param int|string $parameter parameter identifier     * @param mixed      $value     the value to bind to the parameter     * @param string     $type      data type of the parameter     *     * @return bool     * @throws \Cube\Exception     */    public function bindValue($parameter, &$value, $type = null)    {        if (is_string($parameter) && $parameter[0] != ':') {            $parameter = ":$parameter";        }        $this->_bindParam[$parameter] = $value;        try {            return $this->_stmt->bindValue($parameter, $value, $type);        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }    /**     *     * closes the cursor, allowing the statement to be executed again     *     * @return bool     * @throws \Cube\Exception     */    public function closeCursor()    {        try {            return $this->_stmt->closeCursor();        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }    /**     *     * returns the number of columns in the result set     *     * @return int     * @throws \Cube\Exception     */    public function columnCount()    {        try {            return $this->_stmt->columnCount();        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }    /**     *     * fetch the error code associated with the last operation on the statement handle     *     * @return string     * @throws \Cube\Exception     */    public function errorCode()    {        try {            return $this->_stmt->errorCode();        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }    /**     *     * fetch extended error information associated with the last operation on the statement handle     *     * @return array     * @throws \Cube\Exception     */    public function errorInfo()    {        try {            return $this->_stmt->errorCode();        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }    /**     *     * execute a prepared statement     *     * @param array $params array of values with as many elements as there are bound parameters in the SQL statement being executed     *     * @return bool     * @throws \Cube\Exception     */    public function execute(array $params = null)    {        try {            return $this->_stmt->execute($params);        } catch (\PDOException $e) {            $errorMessage = $e->getMessage() . ' [Query]: ' . $this->_stmt->queryString;            throw new Exception($errorMessage, $e->getCode());        }    }    /**     *     * fetch the next row from the result set     *     * @param int $style  fetch mode     * @param int $cursor scrollable cursor     * @param int $offset number of cursors     *     * @return mixed     * @throws \Cube\Exception     */    public function fetch($style = null, $cursor = null, $offset = null)    {        if ($style === null) {            $style = $this->_fetchMode;        }        try {            return $this->_stmt->fetch($style, $cursor, $offset);        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }    /**     *     * returns an array containing all of the result set rows     *     * @param int $style  fetch mode     * @param int $column column number, if fetch mode is by column     *     * @return array        collection of rows, each in a format by fetch mode     * @throws \Cube\Exception     */    public function fetchAll($style = null, $column = null)    {        if ($style === null) {            $style = $this->_fetchMode;        }        try {            if ($style === \PDO::FETCH_COLUMN) {                return $this->_stmt->fetchAll($style, (int)$column);            }            else {                return $this->_stmt->fetchAll($style);            }        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }    /**     *     * returns a single column from the next row of a result set     *     * @param int $column column number from the fetch row or the first column if no column is set     *     * @return string     * @throws \Cube\Exception     */    public function fetchColumn($column = null)    {        try {            return $this->_stmt->fetchColumn((int)$column);        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }    /**     *     * fetches the next row and returns it as an object     *     * @param string $class  name of the class to create     * @param array  $config constructor arguments to add to the class     *     * @return mixed            one object instance of the specified class     * @throws \Cube\Exception     */    public function fetchObject($class = 'stdClass', array $config = array())    {        try {            return $this->_stmt->fetchObject($class, $config);        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }    /**     *     * retrieve a statement attribute     *     * @param string $attribute the attribute name     *     * @return mixed     * @throws \Cube\Exception     */    public function getAttribute($attribute)    {        try {            return $this->_stmt->getAttribute($attribute);        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }    /**     *     * advances to the next rowset in a multi-rowset statement handle     *     * @return bool     * @throws \Cube\Exception     */    public function nextRowset()    {        try {            return $this->_stmt->nextRowset();        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }    /**     *     * returns the number of rows affected by the last SQL statement     *     * @return int     the number of rows affected     * @throws \Cube\Exception     */    public function rowCount()    {        try {            return $this->_stmt->rowCount();        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }    /**     *     * set a statement attribute     *     * @param string $key   attribute name     * @param mixed  $value attribute value     *     * @return bool     * @throws \Cube\Exception     */    public function setAttribute($key, $value)    {        $this->_attributes[$key] = $value;        try {            return $this->_stmt->setAttribute($key, $value);        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }    /**     *     * set the default fetch mode for this statement.     *     * @param int $mode the fetch mode     *     * @return bool     * @throws \Cube\Exception     */    public function setFetchMode($mode)    {        try {            return $this->_stmt->setFetchMode($mode);        } catch (\PDOException $e) {            throw new Exception($e->getMessage(), $e->getCode());        }    }}
 |