Changeset 250 for trunk/trax/vendor

Show
Ignore:
Timestamp:
08/25/06 09:56:48 (6 years ago)
Author:
john
Message:

Converted AR to use MDB2 - DB is now deprecated

Location:
trunk/trax/vendor/trax
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/trax/vendor/trax/action_view/helpers/active_record_helper.php

    r242 r250  
    466466     *  @uses attribute_name 
    467467     *  @uses object() 
    468      *  @uses ActiveRecord::column_for_attribute() 
     468     *  @uses ActiveRecord::column_type() 
    469469     */ 
    470470    function column_type() { 
    471         $column = $this->object()->column_for_attribute($this->attribute_name); 
    472         return $column['type']; 
     471        return $this->object()->column_type($this->attribute_name); 
    473472    } 
    474473     
  • trunk/trax/vendor/trax/active_record.php

    r249 r250  
    3535 
    3636/** 
    37  *  Load the {@link http://pear.php.net/manual/en/package.database.db.php PEAR DB package} 
     37 *  Load the {@link http://pear.php.net/manual/en/package.database.mdb2.php PEAR MDB2 package} 
     38 *  PEAR::DB is now deprecated. 
     39 *  (This package(DB) been superseded by MDB2 but is still maintained for bugs and security fixes) 
    3840 */ 
    39 require_once('DB.php'); 
     41require_once('MDB2.php'); 
    4042 
    4143/** 
     
    6668     * 
    6769     *  Reference to the database object returned by 
    68      *  {@link http://pear.php.net/manual/en/package.database.db.db.connect.php  PEAR DB::Connect()} 
     70     *  {@link http://pear.php.net/manual/en/package.database.mdb2.intro-connect.php  PEAR MDB2::Connect()} 
    6971     *  @var object DB 
    7072     *  see 
    71      *  {@link http://pear.php.net/manual/en/package.database.db.php PEAR DB} 
     73     *  {@link http://pear.php.net/manual/en/package.database.mdb2.php PEAR MDB2} 
    7274     */ 
    7375    private static $db = null; 
     
    154156     *  @var integer 
    155157     */ 
    156     public $fetch_mode = DB_FETCHMODE_ASSOC; 
     158    public $fetch_mode = MDB2_FETCHMODE_ASSOC; 
    157159 
    158160    /** 
     
    951953        return null; 
    952954    } 
     955 
     956   /** 
     957    *  get the columns  data type. 
     958    *  @uses column_for_attribute() 
     959    *  @todo Document this API 
     960    */     
     961    function column_type($attribute) { 
     962        $column = $this->column_for_attribute($attribute); 
     963        if(isset($column['type'])) { 
     964            return $column['type'];     
     965        }             
     966        return null; 
     967    } 
    953968     
    954969    /** 
     
    9891004            $sql = "SELECT {$column} FROM {$this->table_name} WHERE {$conditions}"; 
    9901005            $this->log_query($sql); 
    991             $result = self::$db->getOne($sql); 
     1006            $result = self::$db->queryOne($sql); 
    9921007            if($this->is_error($result)) { 
    9931008                $this->raise($result->getMessage()); 
     
    10331048     * 
    10341049     *  @param string $sql  SQL for the query command 
    1035      *  @return DB_result {@link http://pear.php.net/manual/en/package.database.db.db-result.php} 
     1050     *  @return $mdb2->query {@link http://pear.php.net/manual/en/package.database.mdb2.intro-query.php} 
    10361051     *    Result set from query 
    10371052     *  @uses $db 
     
    10431058        # Run the query 
    10441059        $this->log_query($sql); 
    1045         $rs = self::$db->query($sql); 
     1060        $rs =& self::$db->query($sql); 
    10461061        if ($this->is_error($rs)) { 
    10471062            if(self::$use_transactions && self::$begin_executed) { 
     
    15811596     */ 
    15821597    private function add_record() { 
     1598        self::$db->loadModule('Extended', null, true);                 
     1599        # $primary_key_value may either be a quoted integer or php null 
     1600        $primary_key_value = self::$db->getBeforeID($this->table_name, $this->primary_keys[0]); 
     1601        if($this->is_error($primary_key_value)) { 
     1602            $this->raise($primary_key_value->getMessage()); 
     1603        } 
     1604 
    15831605        $attributes = $this->get_inserts(); 
    15841606        $fields = @implode(', ', array_keys($attributes)); 
    15851607        $values = @implode(', ', array_values($attributes)); 
    1586         $sql = "INSERT INTO $this->table_name ($fields) VALUES ($values)"; 
     1608        $sql = "INSERT INTO {$this->table_name} ($fields) VALUES ($values)"; 
    15871609        //echo "add_record: SQL: $sql<br>"; 
     1610        error_log("add_record: SQL: $sql"); 
    15881611        $result = $this->query($sql); 
     1612         
    15891613        if($this->is_error($result)) { 
    15901614            $this->raise($results->getMessage()); 
     
    15921616            $habtm_result = true; 
    15931617            $primary_key = $this->primary_keys[0]; 
    1594             $primary_key_value = $this->get_insert_id(); 
    1595              $this->$primary_key = $primary_key_value; 
     1618            # $id is now equivalent to the value in the id field that was inserted 
     1619            $primary_key_value = self::$db->getAfterID($primary_key_value, $this->table_name, $this->primary_keys[0]); 
     1620            if($this->is_error($primary_key_value)) { 
     1621                $this->raise($primary_key_value->getMessage()); 
     1622            }             
     1623            $this->$primary_key = $primary_key_value; 
    15961624            if($primary_key_value != '') { 
    15971625                if($this->auto_save_habtm) { 
     
    16311659        $sql = "UPDATE {$this->table_name} SET {$updates} WHERE {$conditions}"; 
    16321660        //echo "update_record:$sql<br>"; 
    1633         //error_log("update_record: SQL: $sql<br>"); 
     1661        error_log("update_record: SQL: $sql"); 
    16341662        $result = $this->query($sql); 
    16351663        if($this->is_error($result)) { 
     
    19621990                                return date($format); 
    19631991                            } elseif($this->preserve_null_dates && is_null($value) && !stristr($field_info['flags'], "not_null")) { 
    1964                                 return 'NULL';     
     1992                                return null;     
    19651993                            } 
    19661994                        } elseif(!$this->new_record) { 
     
    19681996                                return date($format); 
    19691997                            } elseif($this->preserve_null_dates && is_null($value) && !stristr($field_info['flags'], "not_null")) { 
    1970                                 return 'NULL';     
     1998                                return null;     
    19711999                            } 
    19722000                        } 
     
    21142142        } 
    21152143        $return = array(); 
    2116         foreach ($attributes as $key => $value) { 
     2144        foreach($attributes as $key => $value) { 
    21172145            $value = $this->check_datetime($key, $value); 
    2118             # If the value isn't a function, null, or numeric quote it. 
    2119             if(!(preg_match('/^\w+\(.*\)$/U', $value))  
    2120                && !(strcasecmp($value, 'NULL') == 0)  
    2121                && $this->attribute_is_string($key)) { 
    2122                //&& !is_numeric($value)) { 
    2123                 $value = str_replace("\\\"","\"", $value); 
    2124                 $value = str_replace("\'","'", $value); 
    2125                 $value = str_replace("\\\\","\\", $value); 
    2126                 $return[$key] = "'" . addslashes($value) . "'"; 
    2127             } else { 
    2128                 $return[$key] = $value; 
    2129             } 
    2130             //$return[$key] = $this->$db->quoteSmart($value); 
     2146            $type = $this->attribute_is_string($key) ? "Text" : "Integer"; 
     2147            $return[$key] = self::$db->quote($value, $type); 
    21312148        } 
    21322149        return $return; 
     
    22552272     */ 
    22562273    function set_content_columns($table_name) { 
    2257         $this->content_columns = self::$db->tableInfo($table_name); 
     2274        self::$db->loadModule('Reverse', null, true); 
     2275        $this->content_columns = self::$db->reverse->tableInfo($table_name); 
    22582276        if($this->is_error($this->content_columns)) { 
    22592277            $this->raise($this->content_columns->getMessage());         
     
    22762294     */ 
    22772295    function get_insert_id() { 
    2278         $id = self::$db->getOne("SELECT LAST_INSERT_ID();"); 
    2279         if ($this->is_error($id)) { 
    2280             $this->raise($id->getMessage()); 
    2281         } 
    2282         return $id; 
     2296        // fetch the last inserted id via autoincrement or current value of a sequence 
     2297        if(self::$db->supports('auto_increment') === true) { 
     2298            $id = self::$db->lastInsertID($this->table_name, $this->primary_keys[0]);    
     2299            if($this->is_error($id)) { 
     2300                $this->raise($id->getMessage()); 
     2301            }  
     2302            return $id;                 
     2303        } 
     2304 
     2305        return null; 
    22832306    } 
    22842307 
     
    23082331     */ 
    23092332    function establish_connection() { 
    2310         $connection = Trax::$active_record_connections[$this->connection_name]; 
     2333        $connection =& Trax::$active_record_connections[$this->connection_name]; 
    23112334        if(!is_object($connection) || $this->force_reconnect) { 
    23122335            if(array_key_exists($this->connection_name, Trax::$database_settings)) { 
     
    23342357            } 
    23352358            # Connect to the database and throw an error if the connect fails. 
    2336             $connection =& DB::Connect($connection_settings, $connection_options); 
     2359            $connection =& MDB2::Connect($connection_settings, $connection_options); 
     2360            //static $connect_cnt;  $connect_cnt++; error_log("connection #".$connect_cnt); 
    23372361        } 
    23382362        if(!$this->is_error($connection)) { 
     
    23472371 
    23482372    /** 
    2349      *  Test whether argument is a PEAR Error object or a DB Error object. 
     2373     *  Test whether argument is a PEAR Error object or a MDB2 Error object. 
    23502374     * 
    23512375     *  @param object $obj Object to test 
     
    23532377     */ 
    23542378    function is_error($obj) { 
    2355         if((PEAR::isError($obj)) || (DB::isError($obj))) { 
     2379        if((PEAR::isError($obj)) || (MDB2::isError($obj))) { 
    23562380            return true; 
    23572381        } else { 
     
    24312455     */     
    24322456    function attribute_is_string($attribute) { 
    2433         $column = $this->column_for_attribute($attribute); 
    2434         switch($column['type']) { 
     2457        $column_type = $this->column_type($attribute); 
     2458        switch($column_type) { 
    24352459            case 'string': 
    24362460            case 'varchar': 
     
    24382462            case 'text': 
    24392463            case 'blob': 
     2464            case 'clob': 
    24402465            case 'date': 
     2466            case 'time': 
    24412467            case 'datetime': 
    24422468            case 'timestamp':