Changeset 5826 in subversion


Ignore:
Timestamp:
Jan 26, 2012 7:18:26 AM (16 months ago)
Author:
alec
Message:
  • Remove unused sqlite functions, PHPCS fixes
Location:
branches/devel-framework/roundcubemail/program/include
Files:
1 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • branches/devel-framework/roundcubemail/program/include/rcube_mdb2.php

    r5822 r5826  
    5757     * @param  string $db_dsnr Optional DSN for read only operations 
    5858     */ 
    59     function __construct($db_dsnw, $db_dsnr='', $pconn=false) 
    60     { 
    61         if (empty($db_dsnr)) 
     59    public function __construct($db_dsnw, $db_dsnr='', $pconn=false) 
     60    { 
     61        if (empty($db_dsnr)) { 
    6262            $db_dsnr = $db_dsnw; 
     63        } 
    6364 
    6465        $this->db_dsnw = $db_dsnw; 
     
    8687            'debug'            => $this->debug_mode, 
    8788            'debug_handler'    => array($this, 'debug_handler'), 
    88             'portability'      => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL); 
     89            'portability'      => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL, 
     90        ); 
    8991 
    9092        if ($this->db_provider == 'pgsql') { 
     
    107109        else if ($this->db_provider == 'sqlite') { 
    108110            $dsn_array = MDB2::parseDSN($dsn); 
    109             if (!filesize($dsn_array['database']) && !empty($this->sqlite_initials)) 
    110                 $this->_sqlite_create_database($dbh, $this->sqlite_initials); 
    111         } 
    112         else if ($this->db_provider!='mssql' && $this->db_provider!='sqlsrv') 
     111            if (!filesize($dsn_array['database']) && !empty($this->sqlite_initials)) { 
     112                $this->sqlite_create_database($dbh, $this->sqlite_initials); 
     113            } 
     114        } 
     115        else if ($this->db_provider != 'mssql' && $this->db_provider != 'sqlsrv') { 
    113116            $dbh->setCharset('utf8'); 
     117        } 
    114118 
    115119        return $dbh; 
     
    121125     * 
    122126     * @param  string $mode Connection mode (r|w) 
    123      * @access public 
    124      */ 
    125     function db_connect($mode) 
     127     */ 
     128    public function db_connect($mode) 
    126129    { 
    127130        // previous connection failed, don't attempt to connect again 
     
    155158        } 
    156159 
    157         if ($this->db_connected) 
     160        if ($this->db_connected) { 
    158161            $this->db_mode = $mode; 
    159         else 
     162        } 
     163        else { 
    160164            $this->conn_failure = true; 
     165        } 
    161166    } 
    162167 
     
    166171     * 
    167172     * @param boolean $dbg True if SQL queries should be logged 
    168      * @access public 
    169      */ 
    170     function set_debug($dbg = true) 
     173     */ 
     174    public function set_debug($dbg = true) 
    171175    { 
    172176        $this->debug_mode = $dbg; 
     
    182186     * 
    183187     * @param  boolean  True on error 
    184      * @access public 
    185      */ 
    186     function is_error() 
     188     */ 
     189    public function is_error() 
    187190    { 
    188191        return $this->db_error ? $this->db_error_msg : false; 
     
    194197     * 
    195198     * @param  boolean  True if in connected state 
    196      * @access public 
    197      */ 
    198     function is_connected() 
     199     */ 
     200    public function is_connected() 
    199201    { 
    200202        return PEAR::isError($this->db_handle) ? false : $this->db_connected; 
     
    206208     * This returns true if dsnw != dsnr 
    207209     */ 
    208     function is_replicated() 
     210    public function is_replicated() 
    209211    { 
    210212      return !empty($this->db_dsnr) && $this->db_dsnw != $this->db_dsnr; 
     
    217219     * @param  string  SQL query to execute 
    218220     * @param  mixed   Values to be inserted in query 
     221     * 
    219222     * @return number  Query handle identifier 
    220      * @access public 
    221      */ 
    222     function query() 
     223     */ 
     224    public function query() 
    223225    { 
    224226        $params = func_get_args(); 
     
    226228 
    227229        // Support one argument of type array, instead of n arguments 
    228         if (count($params) == 1 && is_array($params[0])) 
     230        if (count($params) == 1 && is_array($params[0])) { 
    229231            $params = $params[0]; 
     232        } 
    230233 
    231234        return $this->_query($query, 0, 0, $params); 
     
    240243     * @param  number  Number of rows for LIMIT statement 
    241244     * @param  mixed   Values to be inserted in query 
     245     * 
    242246     * @return number  Query handle identifier 
    243      * @access public 
    244      */ 
    245     function limitquery() 
     247     */ 
     248    public function limitquery() 
    246249    { 
    247250        $params  = func_get_args(); 
     
    272275 
    273276        // check connection before proceeding 
    274         if (!$this->is_connected()) 
     277        if (!$this->is_connected()) { 
    275278            return null; 
    276  
    277         if ($this->db_provider == 'sqlite') 
    278             $this->_sqlite_prepare(); 
    279  
    280         if ($numrows || $offset) 
     279        } 
     280 
     281        if ($this->db_provider == 'sqlite') { 
     282            $this->sqlite_prepare(); 
     283        } 
     284 
     285        if ($numrows || $offset) { 
    281286            $result = $this->db_handle->setLimit($numrows,$offset); 
    282  
    283         if (empty($params)) 
     287        } 
     288 
     289        if (empty($params)) { 
    284290            $result = $mode == 'r' ? $this->db_handle->query($query) : $this->db_handle->exec($query); 
     291        } 
    285292        else { 
    286293            $params = (array)$params; 
     
    313320     * @param  number $res_id  Optional query handle identifier 
    314321     * @return mixed   Number of rows or false on failure 
    315      * @access public 
    316      */ 
    317     function num_rows($res_id=null) 
    318     { 
    319         if (!$this->db_connected) 
     322     */ 
     323    public function num_rows($res_id=null) 
     324    { 
     325        if (!$this->db_connected) { 
    320326            return false; 
    321  
    322         if ($result = $this->_get_result($res_id)) 
     327        } 
     328 
     329        if ($result = $this->_get_result($res_id)) { 
    323330            return $result->numRows(); 
    324         else 
    325             return false; 
     331        } 
     332 
     333        return false; 
    326334    } 
    327335 
     
    332340     * @param  number $res_id Optional query handle identifier 
    333341     * @return mixed   Number of rows or false on failure 
    334      * @access public 
    335      */ 
    336     function affected_rows($res_id = null) 
    337     { 
    338         if (!$this->db_connected) 
     342     */ 
     343    public function affected_rows($res_id = null) 
     344    { 
     345        if (!$this->db_connected) { 
    339346            return false; 
     347        } 
    340348 
    341349        return $this->_get_result($res_id); 
     
    348356     * 
    349357     * @param  string $table  Table name (to find the incremented sequence) 
     358     * 
    350359     * @return mixed   ID or false on failure 
    351      * @access public 
    352      */ 
    353     function insert_id($table = '') 
    354     { 
    355         if (!$this->db_connected || $this->db_mode == 'r') 
     360     */ 
     361    public function insert_id($table = '') 
     362    { 
     363        if (!$this->db_connected || $this->db_mode == 'r') { 
    356364            return false; 
     365        } 
    357366 
    358367        if ($table) { 
    359             if ($this->db_provider == 'pgsql') 
     368            if ($this->db_provider == 'pgsql') { 
    360369                // find sequence name 
    361370                $table = $this->sequence_name($table); 
    362             else 
     371            } 
     372            else { 
    363373                // resolve table name 
    364374                $table = $this->table_name($table); 
     375            } 
    365376        } 
    366377 
     
    376387     * 
    377388     * @param  number $res_id Optional query handle identifier 
     389     * 
    378390     * @return mixed   Array with col values or false on failure 
    379      * @access public 
    380      */ 
    381     function fetch_assoc($res_id=null) 
     391     */ 
     392    public function fetch_assoc($res_id = null) 
    382393    { 
    383394        $result = $this->_get_result($res_id); 
     
    391402     * 
    392403     * @param  number $res_id  Optional query handle identifier 
     404     * 
    393405     * @return mixed   Array with col values or false on failure 
    394      * @access public 
    395      */ 
    396     function fetch_array($res_id=null) 
     406     */ 
     407    public function fetch_array($res_id = null) 
    397408    { 
    398409        $result = $this->_get_result($res_id); 
     
    406417     * @param  MDB2_Result_Common Query $result result handle 
    407418     * @param  number                   $mode   Fetch mode identifier 
    408      * @return mixed   Array with col values or false on failure 
    409      * @access private 
     419     * 
     420     * @return mixed  Array with col values or false on failure 
    410421     */ 
    411422    private function _fetch_row($result, $mode) 
    412423    { 
    413         if ($result === false || PEAR::isError($result) || !$this->is_connected()) 
     424        if ($result === false || PEAR::isError($result) || !$this->is_connected()) { 
    414425            return false; 
     426        } 
    415427 
    416428        return $result->fetchRow($mode); 
     
    422434     * 
    423435     * @return array List of all tables of the current database 
    424      * @access public 
    425436     * @since 0.4-beta 
    426437     */ 
    427     function list_tables() 
     438    public function list_tables() 
    428439    { 
    429440        // get tables if not cached 
    430441        if (!$this->tables) { 
    431442            $this->db_handle->loadModule('Manager'); 
    432             if (!PEAR::isError($result = $this->db_handle->listTables())) 
     443            if (!PEAR::isError($result = $this->db_handle->listTables())) { 
    433444                $this->tables = $result; 
    434             else 
     445            } 
     446            else { 
    435447                $this->tables = array(); 
     448            } 
    436449        } 
    437450 
     
    444457     * 
    445458     * @param string Table name 
     459     * 
    446460     * @return array List of table cols 
    447461     */ 
    448     function list_cols($table) 
     462    public function list_cols($table) 
    449463    { 
    450464        $this->db_handle->loadModule('Manager'); 
     
    462476     * @param  mixed  $input  Value to quote 
    463477     * @param  string $type   Type of data 
     478     * 
    464479     * @return string  Quoted/converted string for use in query 
    465      * @access public 
    466      */ 
    467     function quote($input, $type = null) 
     480     */ 
     481    public function quote($input, $type = null) 
    468482    { 
    469483        // handle int directly for better performance 
    470         if ($type == 'integer') 
     484        if ($type == 'integer') { 
    471485            return intval($input); 
     486        } 
    472487 
    473488        // create DB handle if not available 
    474         if (!$this->db_handle) 
     489        if (!$this->db_handle) { 
    475490            $this->db_connect('r'); 
     491        } 
    476492 
    477493        return $this->db_connected ? $this->db_handle->quote($input, $type) : addslashes($input); 
     
    483499     * 
    484500     * @param  string $str Value to quote 
     501     * 
    485502     * @return string  Quoted string for use in query 
    486503     * @deprecated     Replaced by rcube_MDB2::quote_identifier 
    487504     * @see            rcube_mdb2::quote_identifier 
    488      * @access public 
    489      */ 
    490     function quoteIdentifier($str) 
     505     */ 
     506    public function quoteIdentifier($str) 
    491507    { 
    492508        return $this->quote_identifier($str); 
     
    498514     * 
    499515     * @param  string $str Value to quote 
     516     * 
    500517     * @return string  Quoted string for use in query 
    501      * @access public 
    502      */ 
    503     function quote_identifier($str) 
    504     { 
    505         if (!$this->db_handle) 
     518     */ 
     519    public function quote_identifier($str) 
     520    { 
     521        if (!$this->db_handle) { 
    506522            $this->db_connect('r'); 
     523        } 
    507524 
    508525        return $this->db_connected ? $this->db_handle->quoteIdentifier($str) : $str; 
     
    514531     * 
    515532     * @param  string $str The string to be escaped 
     533     * 
    516534     * @return string  The escaped string 
    517      * @access public 
    518535     * @since  0.1.1 
    519536     */ 
    520     function escapeSimple($str) 
    521     { 
    522         if (!$this->db_handle) 
     537    public function escapeSimple($str) 
     538    { 
     539        if (!$this->db_handle) { 
    523540            $this->db_connect('r'); 
     541        } 
    524542 
    525543        return $this->db_handle->escape($str); 
     
    531549     * 
    532550     * @return string SQL function to use in query 
    533      * @access public 
    534      */ 
    535     function now() 
     551     */ 
     552    public function now() 
    536553    { 
    537554        switch ($this->db_provider) { 
     
    551568     * @param  array  $arr  Input array 
    552569     * @param  string $type Type of data 
     570     * 
    553571     * @return string Comma-separated list of quoted values for use in query 
    554      * @access public 
    555      */ 
    556     function array2list($arr, $type = null) 
    557     { 
    558         if (!is_array($arr)) 
     572     */ 
     573    public function array2list($arr, $type = null) 
     574    { 
     575        if (!is_array($arr)) { 
    559576            return $this->quote($arr, $type); 
    560  
    561         foreach ($arr as $idx => $item) 
     577        } 
     578 
     579        foreach ($arr as $idx => $item) { 
    562580            $arr[$idx] = $this->quote($item, $type); 
     581        } 
    563582 
    564583        return implode(',', $arr); 
     
    573592     * 
    574593     * @param  string $field Field name 
     594     * 
    575595     * @return string  SQL statement to use in query 
    576596     * @deprecated 
    577597     */ 
    578     function unixtimestamp($field) 
     598    public function unixtimestamp($field) 
    579599    { 
    580600        switch($this->db_provider) { 
     
    596616     * 
    597617     * @param  string $timestamp Field name 
     618     * 
    598619     * @return string  SQL statement to use in query 
    599      * @access public 
    600      */ 
    601     function fromunixtime($timestamp) 
     620     */ 
     621    public function fromunixtime($timestamp) 
    602622    { 
    603623        return date("'Y-m-d H:i:s'", $timestamp); 
     
    610630     * @param  string $column  Field name 
    611631     * @param  string $value   Search value 
     632     * 
    612633     * @return string  SQL statement to use in query 
    613      * @access public 
    614      */ 
    615     function ilike($column, $value) 
     634     */ 
     635    public function ilike($column, $value) 
    616636    { 
    617637        // TODO: use MDB2's matchPattern() function 
    618         switch($this->db_provider) { 
     638        switch ($this->db_provider) { 
    619639            case 'pgsql': 
    620640                return $this->quote_identifier($column).' ILIKE '.$this->quote($value); 
     
    624644    } 
    625645 
     646 
    626647    /** 
    627648     * Abstract SQL statement for value concatenation 
    628649     * 
    629650     * @return string SQL statement to be used in query 
    630      * @access public 
    631      */ 
    632     function concat(/* col1, col2, ... */) 
     651     */ 
     652    public function concat(/* col1, col2, ... */) 
    633653    { 
    634654        $func = ''; 
    635655        $args = func_get_args(); 
    636656 
    637         switch($this->db_provider) { 
     657        switch ($this->db_provider) { 
    638658            case 'mysql': 
    639659            case 'mysqli': 
     
    657677     * 
    658678     * @param  mixed  $input Data to fix 
     679     * 
    659680     * @return mixed  Properly UTF-8 encoded data 
    660      * @access public 
    661      */ 
    662     function encode($input) 
     681     */ 
     682    public static function encode($input) 
    663683    { 
    664684        if (is_object($input)) { 
    665             foreach (get_object_vars($input) as $idx => $value) 
    666                 $input->$idx = $this->encode($value); 
     685            foreach (get_object_vars($input) as $idx => $value) { 
     686                $input->$idx = self::encode($value); 
     687            } 
    667688            return $input; 
    668689        } 
    669690        else if (is_array($input)) { 
    670             foreach ($input as $idx => $value) 
    671                 $input[$idx] = $this->encode($value); 
    672             return $input;       
     691            foreach ($input as $idx => $value) { 
     692                $input[$idx] = self::encode($value); 
     693            } 
     694            return $input; 
    673695        } 
    674696 
     
    681703     * 
    682704     * @param  mixed $input Input data 
     705     * 
    683706     * @return mixed  Decoded data 
    684      * @access public 
    685      */ 
    686     function decode($input) 
     707     */ 
     708    public static function decode($input) 
    687709    { 
    688710        if (is_object($input)) { 
    689             foreach (get_object_vars($input) as $idx => $value) 
    690                 $input->$idx = $this->decode($value); 
     711            foreach (get_object_vars($input) as $idx => $value) { 
     712                $input->$idx = self::decode($value); 
     713            } 
    691714            return $input; 
    692715        } 
    693716        else if (is_array($input)) { 
    694             foreach ($input as $idx => $value) 
    695                 $input[$idx] = $this->decode($value); 
    696             return $input;       
     717            foreach ($input as $idx => $value) { 
     718                $input[$idx] = self::decode($value); 
     719            } 
     720            return $input; 
    697721        } 
    698722 
     
    705729     * 
    706730     * @param  object $res Query handle 
     731     * 
    707732     * @return mixed   Handle ID 
    708      * @access private 
    709733     */ 
    710734    private function _add_result($res) 
     
    733757     * 
    734758     * @param  number $res_id Handle ID 
     759     * 
    735760     * @return mixed   Resource handle or false on failure 
    736      * @access private 
    737761     */ 
    738762    private function _get_result($res_id = null) 
    739763    { 
    740         if ($res_id == null) 
     764        if ($res_id == null) { 
    741765            $res_id = $this->last_res_id; 
    742  
    743         if (isset($this->a_query_results[$res_id])) 
    744             if (!PEAR::isError($this->a_query_results[$res_id])) 
     766        } 
     767 
     768        if (isset($this->a_query_results[$res_id])) { 
     769            if (!PEAR::isError($this->a_query_results[$res_id])) { 
    745770                return $this->a_query_results[$res_id]; 
     771            } 
     772        } 
    746773 
    747774        return false; 
     
    754781     * @param  MDB2   $dbh       SQLite database handle 
    755782     * @param  string $file_name File path to use for DB creation 
    756      * @access private 
    757      */ 
    758     private function _sqlite_create_database($dbh, $file_name) 
    759     { 
    760         if (empty($file_name) || !is_string($file_name)) 
     783     */ 
     784    private function sqlite_create_database($dbh, $file_name) 
     785    { 
     786        if (empty($file_name) || !is_string($file_name)) { 
    761787            return; 
     788        } 
    762789 
    763790        $data = file_get_contents($file_name); 
    764791 
    765         if (strlen($data)) 
    766             if (!sqlite_exec($dbh->connection, $data, $error) || MDB2::isError($dbh))  
     792        if (strlen($data)) { 
     793            if (!sqlite_exec($dbh->connection, $data, $error) || MDB2::isError($dbh)) { 
    767794                raise_error(array('code' => 500, 'type' => 'db', 
    768795                    'line' => __LINE__, 'file' => __FILE__, 
    769                     'message' => $error), true, false);  
     796                    'message' => $error), true, false); 
     797            } 
     798        } 
    770799    } 
    771800 
     
    774803     * Add some proprietary database functions to the current SQLite handle 
    775804     * in order to make it MySQL compatible 
    776      * 
    777      * @access private 
    778      */ 
    779     private function _sqlite_prepare() 
    780     { 
    781         include_once(INSTALL_PATH . 'program/include/rcube_sqlite.inc'); 
    782  
    783         // we emulate via callback some missing MySQL function 
     805     */ 
     806    private function sqlite_prepare() 
     807    { 
     808        // we emulate via callback some missing MySQL functions 
    784809        sqlite_create_function($this->db_handle->connection, 
    785             'from_unixtime', 'rcube_sqlite_from_unixtime'); 
     810            'unix_timestamp', array('rcube_mdb2', 'sqlite_unix_timestamp')); 
    786811        sqlite_create_function($this->db_handle->connection, 
    787             'unix_timestamp', 'rcube_sqlite_unix_timestamp'); 
    788         sqlite_create_function($this->db_handle->connection, 
    789             'now', 'rcube_sqlite_now'); 
    790         sqlite_create_function($this->db_handle->connection, 
    791             'md5', 'rcube_sqlite_md5'); 
     812            'now', array('rcube_mdb2', 'sqlite_now')); 
    792813    } 
    793814 
     
    850871    } 
    851872 
     873 
     874    /** 
     875     * Callback for sqlite: unix_timestamp() 
     876     */ 
     877    public static function sqlite_unix_timestamp($timestamp = '') 
     878    { 
     879        $timestamp = trim($timestamp); 
     880        if (!$timestamp) { 
     881            $ret = time(); 
     882        } 
     883        else if (!preg_match('/^[0-9]+$/s', $timestamp)) { 
     884            $ret = strtotime($timestamp); 
     885        } 
     886        else { 
     887            $ret = $timestamp; 
     888        } 
     889 
     890        return $ret; 
     891    } 
     892 
     893 
     894    /** 
     895     * Callback for sqlite: now() 
     896     */ 
     897    public static function sqlite_now() 
     898    { 
     899        return date("Y-m-d H:i:s"); 
     900    } 
     901 
    852902} 
Note: See TracChangeset for help on using the changeset viewer.