abstract.php

来自「Bug tracker, and reporter.」· PHP 代码 · 共 1,118 行 · 第 1/3 页

PHP
1,118
字号
    public function getProfiler()    {        return $this->_profiler;    }    /**     * Prepares and executes an SQL statement with bound data.     *     * @param  mixed  $sql  The SQL statement with placeholders.     *                      May be a string or Zend_Db_Select.     * @param  mixed  $bind An array of data to bind to the placeholders.     * @return Zend_Db_Statement_Interface     */    public function query($sql, $bind = array())    {        // connect to the database if needed        $this->_connect();        // is the $sql a Zend_Db_Select object?        if ($sql instanceof Zend_Db_Select) {            $sql = $sql->__toString();        }        // make sure $bind to an array;        // don't use (array) typecasting because        // because $bind may be a Zend_Db_Expr object        if (!is_array($bind)) {            $bind = array($bind);        }        // prepare and execute the statement with profiling        $stmt = $this->prepare($sql);        $stmt->execute($bind);        // return the results embedded in the prepared statement object        $stmt->setFetchMode($this->_fetchMode);        return $stmt;    }    /**     * Leave autocommit mode and begin a transaction.     *     * @return bool True     */    public function beginTransaction()    {        $this->_connect();        $q = $this->_profiler->queryStart('begin', Zend_Db_Profiler::TRANSACTION);        $this->_beginTransaction();        $this->_profiler->queryEnd($q);        return true;    }    /**     * Commit a transaction and return to autocommit mode.     *     * @return bool True     */    public function commit()    {        $this->_connect();        $q = $this->_profiler->queryStart('commit', Zend_Db_Profiler::TRANSACTION);        $this->_commit();        $this->_profiler->queryEnd($q);        return true;    }    /**     * Roll back a transaction and return to autocommit mode.     *     * @return bool True     */    public function rollBack()    {        $this->_connect();        $q = $this->_profiler->queryStart('rollback', Zend_Db_Profiler::TRANSACTION);        $this->_rollBack();        $this->_profiler->queryEnd($q);        return true;    }    /**     * Inserts a table row with specified data.     *     * @param mixed $table The table to insert data into.     * @param array $bind Column-value pairs.     * @return int The number of affected rows.     */    public function insert($table, array $bind)    {        // extract and quote col names from the array keys        $cols = array();        $vals = array();        foreach ($bind as $col => $val) {            $cols[] = $this->quoteIdentifier($col, true);            if ($val instanceof Zend_Db_Expr) {                $vals[] = $val->__toString();                unset($bind[$col]);            } else {                $vals[] = '?';            }        }        // build the statement        $sql = "INSERT INTO "             . $this->quoteIdentifier($table, true)             . ' (' . implode(', ', $cols) . ') '             . 'VALUES (' . implode(', ', $vals) . ')';        // execute the statement and return the number of affected rows        $stmt = $this->query($sql, array_values($bind));        $result = $stmt->rowCount();        return $result;    }    /**     * Updates table rows with specified data based on a WHERE clause.     *     * @param  mixed        $table The table to update.     * @param  array        $bind  Column-value pairs.     * @param  mixed        $where UPDATE WHERE clause(s).     * @return int          The number of affected rows.     */    public function update($table, array $bind, $where = '')    {        /**         * Build "col = ?" pairs for the statement,         * except for Zend_Db_Expr which is treated literally.         */        $set = array();        foreach ($bind as $col => $val) {            if ($val instanceof Zend_Db_Expr) {                $val = $val->__toString();                unset($bind[$col]);            } else {                $val = '?';            }            $set[] = $this->quoteIdentifier($col, true) . ' = ' . $val;        }        $where = $this->_whereExpr($where);        /**         * Build the UPDATE statement         */        $sql = "UPDATE "             . $this->quoteIdentifier($table, true)             . ' SET ' . implode(', ', $set)             . (($where) ? " WHERE $where" : '');        /**         * Execute the statement and return the number of affected rows         */        $stmt = $this->query($sql, array_values($bind));        $result = $stmt->rowCount();        return $result;    }    /**     * Deletes table rows based on a WHERE clause.     *     * @param  mixed        $table The table to update.     * @param  mixed        $where DELETE WHERE clause(s).     * @return int          The number of affected rows.     */    public function delete($table, $where = '')    {        $where = $this->_whereExpr($where);        /**         * Build the DELETE statement         */        $sql = "DELETE FROM "             . $this->quoteIdentifier($table, true)             . (($where) ? " WHERE $where" : '');        /**         * Execute the statement and return the number of affected rows         */        $stmt = $this->query($sql);        $result = $stmt->rowCount();        return $result;    }    /**     * Convert an array, string, or Zend_Db_Expr object     * into a string to put in a WHERE clause.     *     * @param mixed $where     * @return string     */    protected function _whereExpr($where)    {        if (empty($where)) {            return $where;        }        if (!is_array($where)) {            $where = array($where);        }        foreach ($where as &$term) {            if ($term instanceof Zend_Db_Expr) {                $term = $term->__toString();            }            $term = '(' . $term . ')';        }        $where = implode(' AND ', $where);        return $where;    }    /**     * Creates and returns a new Zend_Db_Select object for this adapter.     *     * @return Zend_Db_Select     */    public function select()    {        return new Zend_Db_Select($this);    }    /**     * Get the fetch mode.     *     * @return int     */    public function getFetchMode()    {        return $this->_fetchMode;    }    /**     * Fetches all SQL result rows as a sequential array.     * Uses the current fetchMode for the adapter.     *     * @param string|Zend_Db_Select $sql  An SQL SELECT statement.     * @param mixed                 $bind Data to bind into SELECT placeholders.     * @param mixed                 $fetchMode Override current fetch mode.     * @return array     */    public function fetchAll($sql, $bind = array(), $fetchMode = null)    {        if ($fetchMode === null) {            $fetchMode = $this->_fetchMode;        }        $stmt = $this->query($sql, $bind);        $result = $stmt->fetchAll($fetchMode);        return $result;    }    /**     * Fetches the first row of the SQL result.     * Uses the current fetchMode for the adapter.     *     * @param string|Zend_Db_Select $sql An SQL SELECT statement.     * @param mixed $bind Data to bind into SELECT placeholders.     * @param mixed                 $fetchMode Override current fetch mode.     * @return array     */    public function fetchRow($sql, $bind = array(), $fetchMode = null)    {        if ($fetchMode === null) {            $fetchMode = $this->_fetchMode;        }        $stmt = $this->query($sql, $bind);        $result = $stmt->fetch($fetchMode);        $stmt->closeCursor();        return $result;    }    /**     * Fetches all SQL result rows as an associative array.     *     * The first column is the key, the entire row array is the     * value.  You should construct the query to be sure that     * the first column contains unique values, or else     * rows with duplicate values in the first column will     * overwrite previous data.     *     * @param string|Zend_Db_Select $sql An SQL SELECT statement.     * @param mixed $bind Data to bind into SELECT placeholders.     * @return string     */    public function fetchAssoc($sql, $bind = array())    {        $stmt = $this->query($sql, $bind);        $data = array();        while ($row = $stmt->fetch(Zend_Db::FETCH_ASSOC)) {            $tmp = array_values(array_slice($row, 0, 1));            $data[$tmp[0]] = $row;        }        return $data;    }    /**     * Fetches the first column of all SQL result rows as an array.     *     * The first column in each row is used as the array key.     *     * @param string|Zend_Db_Select $sql An SQL SELECT statement.     * @param mixed $bind Data to bind into SELECT placeholders.     * @return array     */    public function fetchCol($sql, $bind = array())    {        $stmt = $this->query($sql, $bind);        $result = $stmt->fetchAll(Zend_Db::FETCH_COLUMN, 0);        return $result;    }    /**     * Fetches all SQL result rows as an array of key-value pairs.     *     * The first column is the key, the second column is the     * value.     *     * @param string|Zend_Db_Select $sql An SQL SELECT statement.     * @param mixed $bind Data to bind into SELECT placeholders.     * @return string     */    public function fetchPairs($sql, $bind = array())    {        $stmt = $this->query($sql, $bind);        $data = array();        while ($row = $stmt->fetch(Zend_Db::FETCH_NUM)) {            $data[$row[0]] = $row[1];        }        return $data;    }    /**     * Fetches the first column of the first row of the SQL result.     *     * @param string|Zend_Db_Select $sql An SQL SELECT statement.     * @param mixed $bind Data to bind into SELECT placeholders.     * @return string     */    public function fetchOne($sql, $bind = array())    {        $stmt = $this->query($sql, $bind);        $result = $stmt->fetchColumn(0);        $stmt->closeCursor();        return $result;    }    /**     * Quote a raw string.     *     * @param string $value     Raw string     * @return string           Quoted string     */    protected function _quote($value)    {        if (is_int($value) || is_float($value)) {            return $value;        }        return "'" . addcslashes($value, "\000\n\r\\'\"\032") . "'";    }    /**     * Safely quotes a value for an SQL statement.     *     * If an array is passed as the value, the array values are quoted     * and then returned as a comma-separated string.     *     * @param mixed $value The value to quote.     * @param mixed $type  OPTIONAL the SQL datatype name, or constant, or null.     * @return mixed An SQL-safe quoted value (or string of separated values).     */    public function quote($value, $type = null)    {        $this->_connect();        if ($value instanceof Zend_Db_Select) {            return '(' . $value->__toString() . ')';

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?