Changeset 2


Ignore:
Timestamp:
11/22/2007 01:37:21 AM (19 years ago)
Author:
mdawaffe
Message:

update db class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/hyper-db.php

    r1 r2  
    22
    33define('HYPERDB', true);
     4
    45define('OBJECT', 'OBJECT', true);
    56define('ARRAY_A', 'ARRAY_A', false);
    … …  
    8182        }
    8283
    83         /**
    84          * Escapes content for insertion into the database, for security
    85          *
    86          * @param string $string
    87          * @return string query safe string
    88          */
    89         function escape($string) {
    90                 return addslashes( $string ); // Disable rest for now, causing problems
    91                 if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' )
    92                         return mysql_escape_string( $string );
    93                 else
    94                         return mysql_real_escape_string( $string, $this->dbh );
    95         }
    96 
    9784        // ==================================================================
    9885        //      Print SQL/DB error.
    … …  
    180167                return '';
    181168        }
    182        
     169
    183170        function is_write_query( $q ) {
    184171                If( substr( $q, -1 ) == ';' )
    185172                        $q = substr( $q, 0, -1 );
    186173                if ( preg_match('/^\s*SELECT.*?\s+FROM\s+`?(\w+)`?\s*/is', $q, $maybe) )
     174                        return false;
     175                if ( preg_match('/^\s*SHOW DATABASES\s*/is', $q, $maybe) )
    187176                        return false;
    188177                if ( preg_match('/^\s*UPDATE\s+`?(\w+)`?\s*/is', $q, $maybe) )
    … …  
    399388
    400389        /**
     390         * Escapes content for insertion into the database, for security
     391         *
     392         * @param string $string
     393         * @return string query safe string
     394         */
     395        function escape($string) {
     396                return addslashes( $string ); // Disable rest for now, causing problems
     397                if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' )
     398                        return mysql_escape_string( $string );
     399                else
     400                        return mysql_real_escape_string( $string, $this->dbh );
     401        }
     402
     403        /**
    401404         * Escapes content by reference for insertion into the database, for security
    402405         * @param string $s
    … …  
    404407        function escape_by_ref( &$s ) {
    405408                $s = $this->escape( $s );
     409        }
     410
     411        function escape_deep( $array ) {
     412                return is_array($array) ? array_map(array(&$this, 'escape_deep'), $array) : $this->escape( $array );
    406413        }
    407414
    … …  
    416423                array_walk($args, array(&$this, 'escape_by_ref'));
    417424                return @call_user_func_array('sprintf', array_merge(array($query), $args));
     425        }
     426
     427        /**
     428         * Insert an array of data into a table
     429         * @param string $table WARNING: not sanitized!
     430         * @param array $data should not already be SQL-escaped
     431         * @return mixed results of $this->query()
     432         */
     433        function insert($table, $data) {
     434                $data = $this->escape_deep($data);
     435                $fields = array_keys($data);
     436                return $this->query("INSERT INTO $table (`" . implode('`,`',$fields) . "`) VALUES ('".implode("','",$data)."')");
     437        }
     438
     439        /**
     440         * Update a row in the table with an array of data
     441         * @param string $table WARNING: not sanitized!
     442         * @param array $data should not already be SQL-escaped
     443         * @param array $where a named array of WHERE column => value relationships.  Multiple member pairs will be joined with ANDs.  WARNING: the column names are not currently sanitized!
     444         * @return mixed results of $this->query()
     445         */
     446        function update($table, $data, $where){
     447                $data = $this->escape_deep($data);
     448                $bits = $wheres = array();
     449                foreach ( array_keys($data) as $k )
     450                        $bits[] = "`$k` = '$data[$k]'";
     451
     452                if ( is_array( $where ) )
     453                        foreach ( $where as $c => $v )
     454                                $wheres[] = "$c = '" . $this->escape( $v ) . "'";
     455                else
     456                        return false;
     457                return $this->query( "UPDATE $table SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ) . ' LIMIT 1' );
    418458        }
    419459
    … …  
    695735        }
    696736
     737        /**
     738         * Checks wether of not the database version is high enough to support the features backPress
     739         * @global $wp_version
     740         */
     741        function check_database_version() {
     742                // Make sure the server has MySQL 4.0
     743                $mysql_version = $this->db_version();
     744                if ( version_compare($mysql_version, '4.0.0', '<') )
     745                        return new WP_Error('database_version', __('<strong>ERROR</strong>: MySQL 4.0.0 is required') );
     746        }
     747
     748        // Store just a few DB caps here - nothing fancy
     749        function has_cap( $db_cap, $dbh_or_table = false ) {
     750                $version = $this->db_version( $dbh_or_table );
     751
     752                $db_cap = strtolower( $db_cap );
     753
     754                switch ( $db_cap ) :
     755                case 'group_concat' :
     756                case 'collation' :
     757                        return version_compare($version, '4.1', '>=');
     758                        break;
     759                endswitch;
     760
     761                return false;
     762        }
     763
     764        // table name or mysql resource
     765        function db_version( $dbh_or_table = false ) {
     766                if ( !is_resource( $dbh_or_table ) )
     767                        $dbh_or_table = $this->db_connect( "DESCRIBE $dbh_or_table" );
     768                // $dbh_or_table is now a $dbh
     769
     770                if ( $dbh_or_table )
     771                        return mysql_get_server_info( $dbh_or_table );
     772                return false;
     773        }
    697774}
    698775
Note: See TracChangeset for help on using the changeset viewer.