query.php

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

PHP
1,899
字号
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Contains the MDB_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    MDB_QueryTool
 * @author     Lorenzo Alberton <l dot alberton at quipo dot it>
 * @copyright  2004-2005 Lorenzo Alberton
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
 * @version    CVS: $Id: Query.php,v 1.45 2005/02/27 19:13:03 quipo Exp $
 * @link       http://pear.php.net/package/MDB_QueryTool
 */

/**
 * require the PEAR and MDB classes
 */
require_once 'PEAR.php';
require_once 'MDB.php';

/**
 * MDB_QueryTool_Query class
 *
 * This class should be extended
 *
 * @category   Database
 * @package    MDB_QueryTool
 * @author     Lorenzo Alberton <l dot alberton at quipo dot it>
 * @copyright  2004-2005 Lorenzo Alberton
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
 * @link       http://pear.php.net/package/MDB_QueryTool
 */
class MDB_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::Mdb-object 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 boolean     if to use the MDB_QueryTool_Result as a result or not
     * @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'
     *
     * @access private
     */
    var $_tableNameToShortNamePreg = '/^.*_/';

    /**
     * var array  this array caches queries that have already been built once
     *            to reduce the execution time
     * @access private
     */
    var $_queryCache = array();


    /**
     * The object that contains the log-instance
     * @access private
     */
    var $_logObject = null;

    /**
     * Some internal data the logging needs
     * @access private
     */
    var $_logData = array();

    // }}}
    // {{{ __construct

    /**
     * this is the constructor, as it will be implemented in ZE2 (php5)
     *
     * @param   object  db-object
     * @param   array   options array
     * @access  public
     */
/*
    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);
        }

        if (is_null($this->sequenceName)) {
            $this->sequenceName = $this->table;
        }
    }
*/

    // }}}
    // {{{ MDB_QueryTool_Query()

    /**
     * @param mixed $dsn DSN string, DSN array or MDB object
     * @param array $options
     * @access  public
     */
    function MDB_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 = &MDB::connect($dsn, $options);
        }
        if (MDB::isError($res)) {
    // FIXXME what shall we do here?
            $this->_errorLog($res->getUserInfo());
        } else {
            $this->db->setFetchMode(MDB_FETCHMODE_ASSOC);
        }
    }

    // }}}
    // {{{ getDbInstance()

    /**
     *   @return  object MDB Object
     *   @access  public
     */
    function &getDbInstance()
    {
        return $this->db;
    }

    // }}}
    // {{{ setDbInstance()

    /**
     * Setup using an existing connection.
     * this also sets the MDB_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(MDB_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!
     *
     *   @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
     *   @access  public
     */
    function get($id, $column='')
    {
        $id     = trim($id);
        $column = trim($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.'='.$this->_quote($id));
        $queryString     = $this->_buildSelectQuery($query);

        return $this->returnResult($this->execute($queryString, $getMethod));
    }

    // }}}
    // {{{ getMultiple()

    /**
     * gets the data of the given ids
     *
     * @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
     * @access  public
     */
    function getMultiple($ids, $column='')
    {
        $col = $this->primaryCol;
        if ($column) {
            $col = $column;

⌨️ 快捷键说明

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