Changeset 1701

Show
Ignore:
Timestamp:
04/05/07 15:40:25 (20 months ago)
Author:
mbonetti
Message:

we can avoid using procedural style here

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/gregarius/cls/db/db.mysqli.php

    r1700 r1701  
    3131class MysqliDB extends DB { 
    3232 
    33     // we could as well set this to private and use PHP5-syntax __construct for  
     33    // we could as well set this to private and use PHP5-syntax __construct for 
    3434    // the constructor, as Mysqli is only supported by PHP5, afaik. 
    35      
    36     var $_db; 
    3735 
    38     function  MysqliDB() { 
    39         parent::DB(); 
    40     } 
     36    var $_db; 
    4137 
    42      
    43     function DBConnect($dbserver, $dbuname, $dbpass) { 
    44     $this -> _db = new mysqli($dbserver, $dbuname, $dbpass); 
     38    function  MysqliDB() { 
     39        parent::DB(); 
     40    } 
    4541 
    46         if(mysqli_connect_errno()) { 
    47               die( "<h1>Error connecting to the database!</h1>\n" 
    48                      ."<p>Have you edited dbinit.php and correctly defined " 
    49                      ."the database username and password?</p>\n" );           
    50         } 
    51     } 
    52      
    53     function DBSelectDB($dbname) { 
    54         if (!$this -> _db -> select_db($dbname)) { 
    55               die( "<h1>Error selecting the database!</h1>\n" 
    56                      ."<p>Does your database exist?" 
    57                      . "Have you edited dbinit.php and correctly defined " 
    58                      ."the database username and password?</p>\n" 
    59                      ."<p>Refer to the <a href=\"INSTALL\">INSTALL</a> document " 
    60                      ."if in doubt</p>\n" ); 
    61         } 
    62     } 
    63      
    64     function rss_query ($query, $dieOnError=true, $preventRecursion=false) { 
    65          $ret =  $this -> _db -> query($query); 
    66      
    67          if ($error = $this -> rss_sql_error()) { 
    68               $errorString = $this -> rss_sql_error_message(); 
    69          } 
    70      
    71          // if we got a missing table error, look for missing tables in the schema 
    72          // and try to create them 
    73          if ($error == 1146 && !$preventRecursion && $dieOnError) { 
    74                 require_once(dirname(__FILE__) . '/../../init.php'); 
    75                 rss_require('schema.php'); 
    76               checkSchema(); 
    77               return $this -> rss_query ($query, $dieOnError, true); 
    78          } elseif ($error == 1054 && !$preventRecursion && $dieOnError) { 
    79               if (preg_match("/^[^']+'([^']+)'.*$/",$errorString,$matches)) { 
    80                 require_once(dirname(__FILE__) . '/../../init.php'); 
    81                 rss_require('schema.php'); 
    82                     checkSchemaColumns($matches[1]); 
    83                     return $this -> rss_query ($query, $dieOnError, true); 
    84               } 
    85          } 
    86      
    87         if ($error && $dieOnError) { 
    88               die ("<p>Failed to execute the SQL query <pre>$query</pre> </p>" 
    89                      ."<p>Error $error: $errorString</p>"); 
    90          } 
    91          return $ret; 
    92     } 
    93      
    94     function rss_fetch_row($rs) { 
    95         return  $rs -> fetch_row(); 
    96     } 
    97      
    98     function rss_fetch_assoc($rs) { 
    99         return $rs -> fetch_assoc(); 
    100     } 
    101     function rss_num_rows($rs) { 
    102         return $rs -> num_rows; 
    103     } 
    104      
    105     function rss_sql_error() { 
    106          return $this -> _db -> errno; 
    107     } 
    108      
    109     function rss_sql_error_message () { 
    110          return $this -> _db -> error; 
    111     } 
    112      
    113     function rss_insert_id() { 
    114         return $this -> _db -> insert_id; 
    115     } 
    116      
    117     function rss_real_escape_string($string) { 
    118          if (method_exists($this -> _db, 'real_escape_string')) { 
    119               return $this -> _db -> real_escape_string($string); 
    120          } elseif (method_exists($this -> _db, 'escape_string')) { 
    121               return $this -> _db -> escape_string($string); 
    122          } else { 
    123               die("<p class=\"error\">Your PHP version doesn't meet Gregarius' minimal requirements, please consider upgrading!</p>"); 
    124          } 
    125     } 
    126      
    127     function rss_is_sql_error($kind) { 
    128         switch ($kind) { 
    129             case RSS_SQL_ERROR_NO_ERROR: 
    130                 return ($this -> rss_sql_error() == 0); 
    131                 break; 
    132             case RSS_SQL_ERROR_DUPLICATE_ROW: 
    133                 return ($this -> rss_sql_error() == 1062); 
    134                 break; 
    135             default: 
    136                 return false; 
    137         } 
    138     } 
     42 
     43    function DBConnect($dbserver, $dbuname, $dbpass) { 
     44        $this -> _db = new mysqli($dbserver, $dbuname, $dbpass); 
     45 
     46        if(!$this -> _db) { 
     47            die( "<h1>Error connecting to the database!</h1>\n" 
     48                 ."<p>Have you edited dbinit.php and correctly defined " 
     49                 ."the database username and password?</p>\n" ); 
     50        } 
     51    } 
     52 
     53    function DBSelectDB($dbname) { 
     54        if (!$this -> _db -> select_db($dbname)) { 
     55            die( "<h1>Error selecting the database!</h1>\n" 
     56                 ."<p>Does your database exist?" 
     57                 . "Have you edited dbinit.php and correctly defined " 
     58                 ."the database username and password?</p>\n" 
     59                 ."<p>Refer to the <a href=\"INSTALL\">INSTALL</a> document " 
     60                 ."if in doubt</p>\n" ); 
     61        } 
     62    } 
     63 
     64    function rss_query ($query, $dieOnError=true, $preventRecursion=false) { 
     65        $ret =  $this -> _db -> query($query); 
     66 
     67        if ($error = $this -> rss_sql_error()) { 
     68            $errorString = $this -> rss_sql_error_message(); 
     69        } 
     70 
     71        // if we got a missing table error, look for missing tables in the schema 
     72        // and try to create them 
     73        if ($error == 1146 && !$preventRecursion && $dieOnError) { 
     74            require_once(dirname(__FILE__) . '/../../init.php'); 
     75            rss_require('schema.php'); 
     76            checkSchema(); 
     77            return $this -> rss_query ($query, $dieOnError, true); 
     78        } 
     79        elseif ($error == 1054 && !$preventRecursion && $dieOnError) { 
     80            if (preg_match("/^[^']+'([^']+)'.*$/",$errorString,$matches)) { 
     81                require_once(dirname(__FILE__) . '/../../init.php'); 
     82                rss_require('schema.php'); 
     83                checkSchemaColumns($matches[1]); 
     84                return $this -> rss_query ($query, $dieOnError, true); 
     85            } 
     86        } 
     87 
     88        if ($error && $dieOnError) { 
     89            die ("<p>Failed to execute the SQL query <pre>$query</pre> </p>" 
     90                 ."<p>Error $error: $errorString</p>"); 
     91        } 
     92        return $ret; 
     93    } 
     94 
     95    function rss_fetch_row($rs) { 
     96        return  $rs -> fetch_row(); 
     97    } 
     98 
     99    function rss_fetch_assoc($rs) { 
     100        return $rs -> fetch_assoc(); 
     101    } 
     102    function rss_num_rows($rs) { 
     103        return $rs -> num_rows; 
     104    } 
     105 
     106    function rss_sql_error() { 
     107        return $this -> _db -> errno; 
     108    } 
     109 
     110    function rss_sql_error_message () { 
     111        return $this -> _db -> error; 
     112    } 
     113 
     114    function rss_insert_id() { 
     115        return $this -> _db -> insert_id; 
     116    } 
     117 
     118    function rss_real_escape_string($string) { 
     119        if (method_exists($this -> _db, 'real_escape_string')) { 
     120            return $this -> _db -> real_escape_string($string); 
     121        } 
     122        elseif (method_exists($this -> _db, 'escape_string')) { 
     123            return $this -> _db -> escape_string($string); 
     124        } 
     125        else { 
     126            die("<p class=\"error\">Your PHP version doesn't meet Gregarius' minimal requirements, please consider upgrading!</p>"); 
     127        } 
     128    } 
     129 
     130    function rss_is_sql_error($kind) { 
     131        switch ($kind) { 
     132        case RSS_SQL_ERROR_NO_ERROR: 
     133                return ($this -> rss_sql_error() == 0); 
     134            break; 
     135        case RSS_SQL_ERROR_DUPLICATE_ROW: 
     136            return ($this -> rss_sql_error() == 1062); 
     137            break; 
     138        default: 
     139            return false; 
     140        } 
     141    } 
    139142} 
    140143?>