⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 db2.php

📁 Bug tracker, and reporter.
💻 PHP
📖 第 1 页 / 共 2 页
字号:
     * PRIMARY          => boolean; true if column is part of the primary key     * PRIMARY_POSITION => integer; position of column in primary key     * IDENTITY         => integer; true if column is auto-generated with unique values     *     * @todo Discover integer unsigned property.     *     * @param string $tableName     * @param string $schemaName OPTIONAL     * @return array     */    public function describeTable($tableName, $schemaName = null)    {        $sql = "SELECT DISTINCT c.tabschema, c.tabname, c.colname, c.colno,              c.typename, c.default, c.nulls, c.length, c.scale,              c.identity, tc.type AS tabconsttype, k.colseq            FROM syscat.columns c              LEFT JOIN (syscat.keycoluse k JOIN syscat.tabconst tc                ON (k.tabschema = tc.tabschema                  AND k.tabname = tc.tabname                  AND tc.type = 'P'))              ON (c.tabschema = k.tabschema                AND c.tabname = k.tabname                AND c.colname = k.colname)            WHERE "            . $this->quoteInto('UPPER(c.tabname) = UPPER(?)', $tableName);        if ($schemaName) {            $sql .= $this->quoteInto(' AND UPPER(c.tabschema) = UPPER(?)', $schemaName);        }        $sql .= " ORDER BY c.colno";        $desc = array();        $stmt = $this->query($sql);        /**         * To avoid case issues, fetch using FETCH_NUM         */        $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);        /**         * The ordering of columns is defined by the query so we can map         * to variables to improve readability         */        $tabschema      = 0;        $tabname        = 1;        $colname        = 2;        $colno          = 3;        $typename       = 4;        $default        = 5;        $nulls          = 6;        $length         = 7;        $scale          = 8;        $identityCol    = 9;        $tabconstype    = 10;        $colseq         = 11;        foreach ($result as $key => $row) {            list ($primary, $primaryPosition, $identity) = array(false, null, false);            if ($row[$tabconstype] == 'P') {                $primary = true;                $primaryPosition = $row[$colseq];            }            /**             * In IBM DB2, an column can be IDENTITY             * even if it is not part of the PRIMARY KEY.             */            if ($row[$identityCol] == 'Y') {                $identity = true;            }            // only colname needs to be case adjusted            $desc[$this->foldCase($row[$colname])] = array(                'SCHEMA_NAME'      => $this->foldCase($row[$tabschema]),                'TABLE_NAME'       => $this->foldCase($row[$tabname]),                'COLUMN_NAME'      => $this->foldCase($row[$colname]),                'COLUMN_POSITION'  => $row[$colno]+1,                'DATA_TYPE'        => $row[$typename],                'DEFAULT'          => $row[$default],                'NULLABLE'         => (bool) ($row[$nulls] == 'Y'),                'LENGTH'           => $row[$length],                'SCALE'            => $row[$scale],                'PRECISION'        => ($row[$typename] == 'DECIMAL' ? $row[$length] : 0),                'UNSIGNED'         => null, // @todo                'PRIMARY'          => $primary,                'PRIMARY_POSITION' => $primaryPosition,                'IDENTITY'         => $identity            );        }        return $desc;    }    /**     * Return the most recent value from the specified sequence in the database.     * This is supported only on RDBMS brands that support sequences     * (e.g. Oracle, PostgreSQL, DB2).  Other RDBMS brands return null.     *     * @param string $sequenceName     * @return string     */    public function lastSequenceId($sequenceName)    {        $this->_connect();        $sql = 'SELECT PREVVAL FOR '.$this->quoteIdentifier($sequenceName, true).' AS VAL FROM SYSIBM.SYSDUMMY1';        $value = $this->fetchOne($sql);        return (string) $value;    }    /**     * Generate a new value from the specified sequence in the database, and return it.     * This is supported only on RDBMS brands that support sequences     * (e.g. Oracle, PostgreSQL, DB2).  Other RDBMS brands return null.     *     * @param string $sequenceName     * @return string     */    public function nextSequenceId($sequenceName)    {        $this->_connect();        $sql = 'SELECT NEXTVAL FOR '.$this->quoteIdentifier($sequenceName, true).' AS VAL FROM SYSIBM.SYSDUMMY1';        $value = $this->fetchOne($sql);        return (string) $value;    }    /**     * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.     *     * As a convention, on RDBMS brands that support sequences     * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence     * from the arguments and returns the last id generated by that sequence.     * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method     * returns the last value generated for such a column, and the table name     * argument is disregarded.     *     * The IDENTITY_VAL_LOCAL() function gives the last generated identity value     * in the current process, even if it was for a GENERATED column.     *     * @param string $tableName OPTIONAL     * @param string $primaryKey OPTIONAL     * @return string     */    public function lastInsertId($tableName = null, $primaryKey = null)    {        $this->_connect();        if ($tableName !== null) {            $sequenceName = $tableName;            if ($primaryKey) {                $sequenceName .= "_$primaryKey";            }            $sequenceName .= '_seq';            return $this->lastSequenceId($sequenceName);        }        $sql = 'SELECT IDENTITY_VAL_LOCAL() AS VAL FROM SYSIBM.SYSDUMMY1';        $value = $this->fetchOne($sql);        return (string) $value;    }    /**     * Begin a transaction.     *     * @return void     */    protected function _beginTransaction()    {        $this->_setExecuteMode(DB2_AUTOCOMMIT_OFF);    }    /**     * Commit a transaction.     *     * @return void     */    protected function _commit()    {        if (!db2_commit($this->_connection)) {            /**             * @see Zend_Db_Adapter_Db2_Exception             */            require_once 'Zend/Db/Adapter/Db2/Exception.php';            throw new Zend_Db_Adapter_Db2_Exception(                db2_conn_errormsg($this->_connection),                db2_conn_error($this->_connection));        }        $this->_setExecuteMode(DB2_AUTOCOMMIT_ON);    }    /**     * Rollback a transaction.     *     * @return void     */    protected function _rollBack()    {        if (!db2_rollback($this->_connection)) {            /**             * @see Zend_Db_Adapter_Db2_Exception             */            require_once 'Zend/Db/Adapter/Db2/Exception.php';            throw new Zend_Db_Adapter_Db2_Exception(                db2_conn_errormsg($this->_connection),                db2_conn_error($this->_connection));        }        $this->_setExecuteMode(DB2_AUTOCOMMIT_ON);    }    /**     * Set the fetch mode.     *     * @param integer $mode     * @return void     * @throws Zend_Db_Adapter_Db2_Exception     */    public function setFetchMode($mode)    {        switch ($mode) {            case Zend_Db::FETCH_NUM:   // seq array            case Zend_Db::FETCH_ASSOC: // assoc array            case Zend_Db::FETCH_BOTH:  // seq+assoc array            case Zend_Db::FETCH_OBJ:   // object                $this->_fetchMode = $mode;                break;            case Zend_Db::FETCH_BOUND:   // bound to PHP variable                /**                 * @see Zend_Db_Adapter_Db2_Exception                 */                require_once 'Zend/Db/Adapter/Db2/Exception.php';                throw new Zend_Db_Adapter_Db2_Exception('FETCH_BOUND is not supported yet');                break;            default:                /**                 * @see Zend_Db_Adapter_Db2_Exception                 */                require_once 'Zend/Db/Adapter/Db2/Exception.php';                throw new Zend_Db_Adapter_Db2_Exception("Invalid fetch mode '$mode' specified");                break;        }    }    /**     * Adds an adapter-specific LIMIT clause to the SELECT statement.     *     * @param string $sql     * @param integer $count     * @param integer $offset OPTIONAL     * @return string     */    public function limit($sql, $count, $offset = 0)    {        $count = intval($count);        if ($count <= 0) {            /**             * @see Zend_Db_Adapter_Db2_Exception             */            require_once 'Zend/Db/Adapter/Db2/Exception.php';            throw new Zend_Db_Adapter_Db2_Exception("LIMIT argument count=$count is not valid");        }        $offset = intval($offset);        if ($offset < 0) {            /**             * @see Zend_Db_Adapter_Db2_Exception             */            require_once 'Zend/Db/Adapter/Db2/Exception.php';            throw new Zend_Db_Adapter_Db2_Exception("LIMIT argument offset=$offset is not valid");        }        if ($offset == 0) {            $limit_sql = $sql . " FETCH FIRST $count ROWS ONLY";            return $limit_sql;        }        /**         * DB2 does not implement the LIMIT clause as some RDBMS do.         * We have to simulate it with subqueries and ROWNUM.         * Unfortunately because we use the column wildcard "*",         * this puts an extra column into the query result set.         */        $limit_sql = "SELECT z2.*            FROM (                SELECT ROW_NUMBER() OVER() AS \"ZEND_DB_ROWNUM\", z1.*                FROM (                    " . $sql . "                ) z1            ) z2            WHERE z2.zend_db_rownum BETWEEN " . ($offset+1) . " AND " . ($offset+$count);        return $limit_sql;    }    /**     * Check if the adapter supports real SQL parameters.     *     * @param string $type 'positional' or 'named'     * @return bool     */    public function supportsParameters($type)    {        switch ($type) {            case 'positional':                return true;            case 'named':            default:                return false;        }    }}

⌨️ 快捷键说明

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