query.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,920 行 · 第 1/5 页

PHP
1,920
字号
<?php/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: *//** * Contains the DB_QueryTool_Query class * * PHP versions 4 and 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt.  If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category   Database * @package    DB_QueryTool * @author     Wolfram Kriesing <wk@visionp.de> * @author     Paolo Panto <wk@visionp.de> * @author     Lorenzo Alberton <l dot alberton at quipo dot it> * @copyright  2003-2005 Wolfram Kriesing, Paolo Panto, Lorenzo Alberton * @license    http://www.php.net/license/3_0.txt  PHP License 3.0 * @version    CVS: $Id: Query.php,v 1.57 2005/02/27 19:13:19 quipo Exp $ * @link       http://pear.php.net/package/DB_QueryTool *//** * require the PEAR and DB classes */require_once 'PEAR.php';require_once 'DB.php';/** * DB_QueryTool_Query class * * This class should be extended * * @category   Database * @package    DB_QueryTool * @author     Wolfram Kriesing <wk@visionp.de> * @author     Paolo Panto <wk@visionp.de> * @author     Lorenzo Alberton <l dot alberton at quipo dot it> * @copyright  2003-2005 Wolfram Kriesing, Paolo Panto, Lorenzo Alberton * @license    http://www.php.net/license/3_0.txt  PHP License 3.0 * @link       http://pear.php.net/package/DB_QueryTool */class DB_QueryTool_Query{    // {{{ class vars    /**     * @var string  the name of the primary column     */    var $primaryCol = 'id';    /**     * @var string  the current table the class works on     */    var $table      = '';    /**     * @var string  the name of the sequence for this table     */    var $sequenceName = null;    /**     * @var object  the db-object, a PEAR::DB instance     */    var $db = null;    /**     * @var string  the where condition     * @access private     */    var $_where = '';    /**     * @var string  the order condition     * @access private     */    var $_order = '';    /**     * @var    string  the having definition     * @access private     */    var $_having = '';    /**     * @var array   contains the join content     *              the key is the join type, for now we have 'default' and 'left'     *              inside each key 'table' contains the table     *                          key 'where' contains the where clause for the join     * @access private     */    var $_join = array();    /**     * @var    string  which column to index the result by     * @access private     */    var $_index = null;    /**     * @var    string  the group-by clause     * @access private     */    var $_group = '';    /**     * @var    array   the limit     * @access private     */    var $_limit = array();    /**     * @var    string  type of result to return     * @access private     */    var $_resultType = 'none';    /**     * @var    array   the metadata temporary saved     * @access private     */    var $_metadata = array();    /**     * @var    string     * @access private     */    var $_lastQuery = null;    /**     * @var    string   the rows that shall be selected     * @access private     */    var $_select = '*';    /**     * @var    string   the rows that shall not be selected     * @access private     */    var $_dontSelect = '';    /**     * @var array  this array saves different modes in which this class works     *             i.e. 'raw' means no quoting before saving/updating data     * @access private     */    var $options = array(        'raw'      =>  false,        'verbose'  =>  true,    // set this to false in a productive environment                                // it will produce error-logs if set to true        'useCache' =>  false,        'logFile'  =>  false,    );    /**     * this array contains information about the tables     * those are     * - 'name' => the real table name     * - 'shortName' => the short name used, so that when moving the table i.e.     *                  onto a provider's db and u have to rename the tables to     *                  longer names this name will be relevant, i.e. when     *                  autoJoining, i.e. a table name on your local machine is:     *                  'user' but online it has to be 'applName_user' then the     *                  shortName will be used to determine if a column refers to     *                  another table, if the colName is 'user_id', it knows the     *                  shortName 'user' refers to the table 'applName_user'     */    var $tableSpec = array();    /**     * this is the regular expression that shall be used to find a table's shortName     * in a column name, the string found by using this regular expression will be removed     * from the column name and it will be checked if it is a table name     * i.e. the default '/_id$/' would find the table name 'user' from the column name 'user_id'     */    var $_tableNameToShortNamePreg = '/^.*_/';    /**     * @var array this array caches queries that have already been built once     *            to reduce the execution time     */    var $_queryCache = array();    /**     * The object that contains the log-instance     */    var $_logObject = null;    /**     * Some internal data the logging needs     */    var $_logData = array();    // }}}    // {{{ __construct()    /**     * this is the constructor, as it will be implemented in ZE2 (php5)     *     * @version    2002/04/02     * @access     public     * @author     Wolfram Kriesing <wk@visionp.de>     * @param      object  db-object     *//*    function __construct($dsn=false, $options=array())    {        if (!isset($options['autoConnect'])) {            $autoConnect = true;        } else {            $autoConnect = $options['autoConnect'];        }        if (isset($options['errorCallback'])) {            $this->setErrorCallback($options['errorCallback']);        }        if (isset($options['errorSetCallback'])) {            $this->setErrorSetCallback($options['errorSetCallback']);        }        if (isset($options['errorLogCallback'])) {            $this->setErrorLogCallback($options['errorLogCallback']);        }        if ($autoConnect && $dsn) {            $this->connect($dsn, $options);        }        //we would need to parse the dsn first ... i dont feel like now :-)        // oracle has all column names in upper case//FIXXXME make the class work only with upper case when we work with oracle        //if ($this->db->phptype=='oci8' && !$this->primaryCol) {        //    $this->primaryCol = 'ID';        //}        if ($this->sequenceName == null) {            $this->sequenceName = $this->table;        }    }*/    // }}}    // {{{ DB_QueryTool_Query()    /**     * @version    2002/04/02     * @access     public     * @author     Wolfram Kriesing <wk@visionp.de>     * @param mixed $dsn DSN string, DSN array or DB object     * @param array $options     */    function DB_QueryTool_Query($dsn=false, $options=array())    {        //$this->__construct($dsn, $options);        if (!isset($options['autoConnect'])) {            $autoConnect = true;        } else {            $autoConnect = $options['autoConnect'];            unset($options['autoConnect']);        }        if (isset($options['errorCallback'])) {            $this->setErrorCallback($options['errorCallback']);            unset($options['errorCallback']);        }        if (isset($options['errorSetCallback'])) {            $this->setErrorSetCallback($options['errorSetCallback']);            unset($options['errorSetCallback']);        }        if (isset($options['errorLogCallback'])) {            $this->setErrorLogCallback($options['errorLogCallback']);            unset($options['errorLogCallback']);        }        if ($autoConnect && $dsn) {            $this->connect($dsn, $options);        }        if (is_null($this->sequenceName)) {            $this->sequenceName = $this->table;        }    }    // }}}    // {{{ connect()    /**     * use this method if you want to connect manually     * @param mixed $dsn DSN string, DSN array or MDB object     * @param array $options     */    function connect($dsn, $options=array())    {        if (is_object($dsn)) {            $res = $this->db =& $dsn;        } else {            $res = $this->db = DB::connect($dsn, $options);        }        if (DB::isError($res)) {// FIXXME what shall we do here?            $this->_errorLog($res->getUserInfo());        } else {            $this->db->setFetchMode(DB_FETCHMODE_ASSOC);        }    }    // }}}    // {{{ getDbInstance()    /**     * @return reference to current DB instance     */    function &getDbInstance()    {        return $this->db;    }    // }}}    // {{{ setDbInstance()    /**     * Setup using an existing connection.     * this also sets the DB_FETCHMODE_ASSOC since this class     * needs this to be set!     *     * @param object a reference to an existing DB-object     * @return void     */    function setDbInstance(&$dbh)    {        $this->db =& $dbh;        $this->db->setFetchMode(DB_FETCHMODE_ASSOC);    }    // }}}    // {{{ get()    /**     * get the data of a single entry     * if the second parameter is only one column the result will be returned     * directly not as an array!     *     * @version    2002/03/05     * @access     public     * @author     Wolfram Kriesing <wk@visionp.de>     * @param      integer the id of the element to retrieve     * @param      string  if this is given only one row shall be returned, directly, not an array     * @return     mixed   (1) an array of the retrieved data     *                     (2) if the second parameter is given and its only one column,     *                         only this column's data will be returned     *                     (3) false in case of failure     */    function get($id, $column='')    {        $table = $this->table;        $getMethod = 'getRow';        if ($column && !strpos($column, ',')) {   // if only one column shall be selected            $getMethod = 'getOne';        }        // we dont use 'setSelect' here, since this changes the setup of the class, we        // build the query directly        // if $column is '' then _buildSelect selects '*' anyway, so that's the same behaviour as before        $query['select'] = $this->_buildSelect($column);        $query['where']  = $this->_buildWhere($this->table.'.'.$this->primaryCol.'='.$id);        $queryString = $this->_buildSelectQuery($query);        return $this->returnResult($this->execute($queryString,$getMethod));    }    // }}}    // {{{ getMultiple()    /**     * gets the data of the given ids     *     * @version    2002/04/23     * @access     public     * @author     Wolfram Kriesing <wk@visionp.de>     * @param      array   this is an array of ids to retreive     * @param      string  the column to search in for     * @return     mixed   an array of the retreived data, or false in case of failure     *                       when failing an error is set in $this->_error     */    function getMultiple($ids, $column='')    {        $col = $this->primaryCol;        if ($column) {

⌨️ 快捷键说明

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