query.php

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

PHP
1,920
字号
        }        $ret = array();        switch (strtolower($what)) {            case 'table':            case 'tables':                foreach ($this->_join as $aJoin) {                    if (count($aJoin)) {                        $ret = array_merge($ret, array_keys($aJoin));                    }                }                break;            case 'right':   // return right-join data only            case 'left':    // return left join data only                if (count($this->_join[$what])) {                    $ret = array_merge($ret, $this->_join[$what]);                }                break;        }        return $ret;    }    // }}}    // {{{ addJoin()    /**     *   adds a table and a where clause that shall be used for the join     *   instead of calling     *       setJoin(array(table1,table2),'<where clause1> AND <where clause2>')     *   you can also call     *       setJoin(table1,'<where clause1>')     *       addJoin(table2,'<where clause2>')     *   or where it makes more sense is to build a query which is made out of a     *   left join and a standard join     *       setLeftJoin(table1,'<where clause1>')     *       // results in ... FROM $this->table LEFT JOIN table ON <where clause1>     *       addJoin(table2,'<where clause2>')     *       // results in ...  FROM $this->table,table2 LEFT JOIN table ON <where clause1> WHERE <where clause2>     *     * @access     public     * @param      string the table to be joined     * @param      string the where clause for the join     * @param      string the join type     */    function addJoin($table, $where, $type='default')    {        if ($table == $this->table) {            return;  //skip. Self joins are not supported.        }        // init value, to prevent E_ALL-warning        if (!isset($this->_join[$type]) || !$this->_join[$type]) {            $this->_join[$type] = array();        }        $this->_join[$type][$table] = $where;    }    // }}}    // {{{ setTable()    /**     * sets the table this class is currently working on     *     * @version    2002/07/11     * @access     public     * @author     Wolfram Kriesing <wk@visionp.de>     * @param      string  the table name     */    function setTable($table)    {        $this->table = $table;    }    // }}}    // {{{ getTable()    /**     * gets the table this class is currently working on     *     * @version    2002/07/11     * @access     public     * @author     Wolfram Kriesing <wk@visionp.de>     * @return     string  the table name     */    function getTable()    {        return $this->table;    }    // }}}    // {{{ setGroup()    /**     * sets the group-by condition     *     * @version    2002/07/22     * @access     public     * @author     Wolfram Kriesing <wk@visionp.de>     * @param      string  the group condition     */    function setGroup($group='')    {        $this->_group = $group;//FIXXME parse the condition and replace ambigious column names, such as "name='Deutschland'" with "country.name='Deutschland'"// then the users dont have to write that explicitly and can use the same name as in the setOrder i.e. setOrder('name,_net_name,_netPrefix_prefix');    }    // }}}    // {{{ getGroup()    /**     *   gets the group condition which is used for the current instance     *     * @version    2002/07/22     * @access     public     * @author     Wolfram Kriesing <wk@visionp.de>     * @return     string  the group condition     */    function getGroup()    {        return $this->_group;    }    // }}}    // {{{ setSelect()    /**     * limit the result to return only the columns given in $what     * @param string fields that shall be selected     */    function setSelect($what='*')    {        $this->_select = $what;    }    // }}}    // {{{ addSelect()    /**     * add a string to the select part of the query     *     * add a string to the select-part of the query and connects it to an existing     * string using the $connectString, which by default is a comma.     * (SELECT xxx FROM - xxx is the select-part of a query)     *     * @version    2003/01/08     * @access     public     * @author     Wolfram Kriesing <wk@visionp.de>     * @param      string  the string that shall be added to the select-part     * @param      string  the string to connect the new string with the existing one     * @return     void     */    function addSelect($what='*', $connectString=',')    {        // if the select string is not empty add the string, otherwise simply set it        if ($this->_select) {            $this->_select = $this->_select.$connectString.$what;        } else {            $this->_select = $what;        }    }    // }}}    // {{{ getSelect()    /**     * @return     string     */    function getSelect()    {        return $this->_select;    }    // }}}    // {{{ setDontSelect()    /**     * @param     string     */    function setDontSelect($what='')    {        $this->_dontSelect = $what;    }    // }}}    // {{{ getDontSelect()    /**     * @return     string     */    function getDontSelect()    {        return $this->_dontSelect;    }    // }}}    // {{{ reset()    /**     * reset all the set* settings; with no parameter given, it resets them all     *     * @version    2002/09/16     * @access     public     * @author     Wolfram Kriesing <wk@visionp.de>     * @return     void     */    function reset($what=array())    {        if (!sizeof($what)) {            $what = array(                'select',                'dontSelect',                'group',                'having',                'limit',                'where',                'index',                'order',                'join',                'leftJoin',                'rightJoin'            );        }        foreach ($what as $aReset) {            $this->{'set'.ucfirst($aReset)}();        }    }    // }}}    // {{{ setOption()    /**     * set mode the class shall work in     * currently we have the modes:     * 'raw'   does not quote the data before building the query     *     * @version    2002/09/17     * @access     public     * @author     Wolfram Kriesing <wk@visionp.de>     * @param      string      the mode to be set     * @param      mixed       the value of the mode     * @return     void     */    function setOption($option, $value)    {        $this->options[strtolower($option)] = $value;    }    // }}}    // {{{ getOption()    /**     * @return     string     */    function getOption($option)    {        return $this->options[strtolower($option)];    }    // }}}    // {{{ _quoteArray()    /**     * quotes all the data in this array if we are not in raw mode!     * @param array     */    function _quoteArray($data)    {        if (!$this->getOption('raw')) {            foreach ($data as $key => $val) {                $data[$key] = $this->db->quote($val);            }        }        return $data;    }    // }}}    // {{{ _checkColumns()    /**     * checks if the columns which are given as the array's indexes really exist     * if not it will be unset anyway     *     * @version    2002/04/16     * @access     public     * @author     Wolfram Kriesing <wk@visionp.de>     * @param      string  the actual message, first word should always be the method name,     *                       to build the message like this: className::methodname     * @param      integer the line number     */    function _checkColumns($newData, $method='unknown')    {        if (!$meta = $this->metadata()) {   // if no metadata available, return data as given            return $newData;        }        foreach ($newData as $colName => $x) {            if (!isset($meta[$colName])) {                $this->_errorLog("$method, column {$this->table}.$colName doesnt exist, value was removed before '$method'",__LINE__);                unset($newData[$colName]);            } else {                // if the current column exists, check the length too, not to write content that is too long                // prevent DB-errors here                // do only check the data length if this field is given                if (isset($meta[$colName]['len']) && ($meta[$colName]['len'] != -1) &&                    ($oldLength=strlen($newData[$colName])) > $meta[$colName]['len']                ) {                    $this->_errorLog("_checkColumns, had to trim column '$colName' from $oldLength to ".                                        $meta[$colName]['DATA_LENGTH'].' characters.', __LINE__);                    $newData[$colName] = substr($newData[$colName], 0, $meta[$colName]['len']);                }            }        }        return $newData;    }    // }}}    // {{{ debug()    /**     * overwrite this method and i.e. print the query $string     * to see the final query     *     * @param      string  the query mostly     */    function debug($string){}    //    //    //  ONLY ORACLE SPECIFIC, not very nice since it is DB dependent, but we need it!!!    //    //    // }}}    // {{{ metadata()    /**     * !!!! query COPIED FROM db_oci8.inc - from PHPLIB !!!!     *     * @access   public     * @see     * @version  2001/09     * @author   PHPLIB     * @param     * @return     */    function metadata($table='')    {        // is there an alias in the table name, then we have something like this: 'user ua'        // cut of the alias and return the table name        if (strpos($table, ' ') !== false) {            $split = explode(' ', trim($table));            $table = $split[0];        }        $full = false;        if (empty($table)) {            $table = $this->table;        }        // to prevent multiple selects for the same metadata        if (isset($this->_metadata[$table])) {            return $this->_metadata[$table];        }// FIXXXME use oci8 implementation of newer PEAR::DB-version        if ($this->db->phptype == 'oci8') {            $count = 0;            $id    = 0;            $res   = array();            //# This is a RIGHT OUTER JOIN: "(+)", if you want to see, what            //# this query results try the following:            //// $table = new Table; $this->db = new my_DB_Sql; // you have to make            ////                                          // your own class            //// $table->show_results($this->db->query(see query vvvvvv))            ////            $res = $this->db->getAll("SELECT T.column_name,T.table_name,T.data_type,".                "T.data_length,T.data_precision,T.data_scale,T.nullable,".                "T.char_col_decl_length,I.index_name".                " FROM ALL_TAB_COLUMNS T,ALL_IND_COLUMNS I".                " WHERE T.column_name=I.column_name (+)".                " AND T.table_name=I.table_name (+)".                " AND T.table_name=UPPER('$table') ORDER BY T.column_id");

⌨️ 快捷键说明

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