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

📄 sqlrelay.php

📁 适合于Unix/Linux下的一个持久数据库连接池
💻 PHP
📖 第 1 页 / 共 2 页
字号:
                if ($fp) {                    while (($buf = fread($fp, 4096)) != false) {                        $pdata[$i] .= $buf;                    }                }            }            sqlrcur_inputBind($sqlrcursor->cursor,"bind" . $i, $pdata[$i]);        }        # handle native SQL Relay binds...        if (!$typessize && $data) {            foreach ($data as $index=>$value) {                sqlrcur_inputBind($sqlrcursor->cursor, $index, $value);            }        }        if (!sqlrcur_executeQuery($sqlrcursor->cursor)) {            $error = sqlrcur_errorMessage($sqlrcursor->cursor);            $this->freeResult($sqlrcursor);            return $this->raiseError(DB_ERROR, null, null, null, $error);        }        /* If the query was a select, return a cursor, otherwise return DB_OK.        If there are any affected rows, then the query was definitely not a        select, otherwise there's no good way to know what kind of query it was        except by parsing it. */        if ($this->is_select[(int)$sqlrcursor->cursor]) {            return new DB_result($this,$sqlrcursor);        }        return DB_OK;    }    // }}}    // {{{ fetchInto()    /**     * Fetch a row and insert the data into an existing array.     *     * @param $cursor SQLRelay cursor     * @param $arr (reference) array where data from the row is stored     * @param $fetchmode how the array data should be indexed     * @param   $rownum the row number to fetch     * @access public     *     * @return int DB_OK on success, a DB error on failure     */    function fetchInto(&$sqlrcursor, &$arr, $fetchmode, $rownum = null)    {        if ($rownum != null) {            $sqlrcursor->rownum = $rownum;        }        if ($fetchmode & DB_FETCHMODE_ASSOC) {            $arr = sqlrcur_getRowAssoc($sqlrcursor->cursor,                                        $sqlrcursor->rownum);        } else {            $arr = sqlrcur_getRow($sqlrcursor->cursor,                                        $sqlrcursor->rownum);        }        if (!$arr) {            // See: http://bugs.php.net/bug.php?id=22328            // for why we can't check errors on fetching            return null;        }        $sqlrcursor->rownum++;        return DB_OK;    }    // }}}    // {{{ freeResult()    /**     * Free the internal resources associated with $cursor.     *     * @param $cursor SQLRelay cursor     *     * @access public     *     * @return bool TRUE     */    function freeResult(&$sqlrcursor)    {        if (is_resource($sqlrcursor)) {            unset($this->prepare_types[(int)$sqlrcursor->cursor]);            unset($this->is_select[(int)$sqlrcursor->cursor]);            sqlrcur_free($sqlrcursor->cursor);        }        return true;    }    // }}}    // {{{ freePrepared()    /**     * Frees the internal resources associated with a prepared query     *     * @param resource $stmt           the prepared statement's PHP resource     * @param bool     $free_resource  should the PHP resource be freed too?     *                                  Use false if you need to get data     *                                  from the result set later.     *     * @return bool  TRUE on success, FALSE if $result is invalid     *     * @see DB_common::prepare()     */    function freePrepared($stmt, $free_resource = true)    {        return true;    }    // {{{ numCols()    /**     * Get the number of columns in a cursor set.     *     * @param $cursor SQLRelay cursor     *     * @access public     *     * @return int the number of columns per row in $cursor     */    function numCols($sqlrcursor)    {        return sqlrcur_colCount($sqlrcursor->cursor);    }    // }}}    // {{{ numRows()    /**     * Get the number of rows in a result set.     *     * @param $cursor SQLRelay cursor     *     * @access public     *     * @return int the number of rows in $cursor     */    function numRows($sqlrcursor)    {        return sqlrcur_rowCount($sqlrcursor->cursor);    }    // }}}    // {{{ autoCommit()    /**     * Enable/disable automatic commits     */    function autoCommit($onoff = false)    {        if ($onoff == true) {            sqlrcon_autoCommitOn($this->connection);        } else {            sqlrcon_autoCommitOff($this->connection);        }        return DB_OK;    }    // }}}    // {{{ commit()    /**     * Commit the current transaction.     */    function commit()    {        if (sqlrcon_commit($this->connection) == 1) {            return DB_OK;        } else {            # FIXME: need some way to get a rollback error            return $this->raiseError(DB_ERROR, null, null, null,                                                "commit failed");        }    }    // }}}    // {{{ rollback()    /**     * Roll back (undo) the current transaction.     */    function rollback()    {        if (sqlrcon_rollback($this->connection) == 1) {            return DB_OK;        } else {            # FIXME: need some way to get a rollback error            return $this->raiseError(DB_ERROR, null, null, null,                                                "rollback failed");        }    }    // }}}    // {{{ affectedRows()    /**     * Gets the number of rows affected by the data manipulation     * query.  For other queries, this function returns 0.     *     * @return number of rows affected by the last query     */    function affectedRows()    {        return $this->affectedrows;    }    // }}}    // {{{ nextId()    /**     * Returns the next free id in a sequence     *     * @param string  $seq_name  name of the sequence     * @param boolean $ondemand  when true, the seqence is automatically     *                            created if it does not exist     *     * @return int  the next id number in the sequence.     *               A DB_Error object on failure.     *     */    function nextId($seq_name, $ondemand = true)    {        # get the sequence name        $seqname = $this->getSequenceName($seq_name);        # initialize the sequence query        $seqquery = "";        # figure out what kind of db we're using and build the appropriate query        if ($this->identity == "") {            $this->identity = sqlrcon_identify($this->connection);        }        if ($this->identity == "oracle8" || $this->identity == "oracle7") {            $seqquery = "SELECT ${seqname}.nextval FROM dual";        } else {            # FIXME: implement for other DB's            return $this->raiseError(DB_ERROR_NOT_CAPABLE);        }        # try to get the value from the sequence        $result =& $this->query($seqquery);        # if it failed and we're creating sequences on-demand, try to create        # the sequence and then re-run the query        if (DB::isError($result) && $ondemand) {            # FIXME: if we got an error, we assume that the sequence            # didn't exist and we try to create it that may not actually be the            # case, we need to check the error code            $result = $this->createSequence($seq_name);            if (!DB::isError($result)) {                $result =& $this->query($seqquery);            }        }        # if any of the previous stuff generated an error, return it        if (DB::isError($result)) {            return $this->raiseError($result);        }        # otherwise, return the first field        $arr = $result->fetchRow(DB_FETCHMODE_ORDERED);        return $arr[0];    }    // }}}    // {{{ createSequence()    /**     * Creates a new sequence     *     * @param string $seq_name  name of the new sequence     *     * @return int  DB_OK on success.  A DB_Error object on failure.     *     */    function createSequence($seq_name)    {        $seqquery="";        if ($this->identity == "") {            $this->identity = sqlrcon_identify($this->connection);        }        if ($this->identity == "oracle8" || $this->identity == "oracle7") {            $seqquery = "CREATE SEQUENCE " . $this->getSequenceName($seq_name);        } else {            # FIXME: implement for other DB's            return $this->raiseError(DB_ERROR_NOT_CAPABLE);        }        return $this->query($seqquery);    }    // }}}    // {{{ dropSequence()    /**     * Deletes a sequence     *     * @param string $seq_name  name of the sequence to be deleted     *     * @return int  DB_OK on success.  A DB_Error object on failure.     *     */    function dropSequence($seq_name)    {        $seqquery = "";        if ($this->identity == "") {            $this->identity = sqlrcon_identify($this->connection);        }        if ($this->identity == "oracle8" || $this->identity == "oracle7") {            $seqquery = "DROP SEQUENCE " . $this->getSequenceName($seq_name);        } else {            # FIXME: implement for other DB's            return $this->raiseError(DB_ERROR_NOT_CAPABLE);        }        return $this->query($seqquery);    }    // }}}    // {{{ tableInfo()    function tableInfo($result, $mode = null)    {        /*         * depending on $mode, metadata returns the following values:         *         * - mode is null (default):         * $result[]:         *   [0]["table"]  table name         *   [0]["name"]   field name         *   [0]["type"]   field type         *   [0]["len"]    field length         *   [0]["flags"]  field flags         *         * - mode is DB_TABLEINFO_ORDER         * $result[]:         *   ["num_fields"] number of metadata records         *   [0]["table"]  table name         *   [0]["name"]   field name         *   [0]["type"]   field type         *   [0]["len"]    field length         *   [0]["flags"]  field flags         *   ["order"][field name]  index of field named "field name"         *   The last one is used, if you have a field name, but no index.         *   Test:  if (isset($result['meta']['myfield'])) { ...         *         * - mode is DB_TABLEINFO_ORDERTABLE         *    the same as above. but additionally         *   ["ordertable"][table name][field name] index of field         *      named "field name"         *         *      this is, because if you have fields from different         *      tables with the same field name * they override each         *      other with DB_TABLEINFO_ORDER         *         *      you can combine DB_TABLEINFO_ORDER and         *      DB_TABLEINFO_ORDERTABLE with DB_TABLEINFO_ORDER |         *      DB_TABLEINFO_ORDERTABLE * or with DB_TABLEINFO_FULL         */        if (is_string($result)) {            // if $result is a string, then we want information about a            // table without a resultset else we want information about a            // resultset, this is not yet supported            return null;        } else if (empty($result)) {            return null;        } else if (!is_object($result)) {            return null;        }        # extract the sqlrcursor        $sqlrcursor = $result->result;        $count = sqlrcur_colCount($sqlrcursor->cursor);        if (empty($mode)) {            for ($i=0; $i<$count; $i++) {                $res[$i]['table'] = '';                $res[$i]['name'] = sqlrcur_getColumnName($sqlrcursor->cursor,$i);                $res[$i]['type'] = sqlrcur_getColumnType($sqlrcursor->cursor,$i);                $res[$i]['len'] = sqlrcur_getColumnLength($sqlrcursor->cursor,$i);                $res[$i]['flags'] = '';            }        } else {            $res['num_fields']= $count;            for ($i=0; $i<$count; $i++) {                $res[$i]['table'] = '';                $res[$i]['name'] = sqlrcur_getColumnName($sqlrcursor->cursor,$i);                $res[$i]['type'] = sqlrcur_getColumnType($sqlrcursor->cursor,$i);                $res[$i]['len'] = sqlrcur_getColumnLength($sqlrcursor->cursor,$i);                $res[$i]['flags'] = '';                if ($mode & DB_TABLEINFO_ORDER) {                    $res['order'][$res[$i]['name']] = $i;                }                if ($mode & DB_TABLEINFO_ORDERTABLE) {                    $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;                }            }        }        return $res;    }    // }}}    // {{{ errorNative()    /**     * Get the native error code of the last error (if any) that     * occured on the current connection.     *     * @access public     *     * @return int native SQLRelay error code     */    function errorNative()    {        # FIXME: sqlrelay doesn't have error codes, what do I do?        #return sqlrcon_errorMessage($this->connection);        return DB_ERROR;    }    // }}}}?>

⌨️ 快捷键说明

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