Changeset 315 for trunk/trax/vendor

Show
Ignore:
Timestamp:
07/08/09 01:40:57 (3 years ago)
Author:
john
Message:

AR multiple connection/read only connections

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/trax/vendor/trax/active_record.php

    r313 r315  
    177177     * What environment to run in. 
    178178     */ 
    179     public static $environment = 'development'; 
     179    public static $environment = 'production'; 
    180180 
    181181    /** 
     
    502502     */     
    503503    public static $query_log = array(); 
     504                                    
     505    /** 
     506     *  Log all queries to the query_log array even not in development mode 
     507     */ 
     508    public static $log_all = false; 
    504509 
    505510    /** 
     
    771776            if(@array_key_exists("join_table", $parameters)) { 
    772777                $join_table = $parameters['join_table']; 
    773             }  
     778            } 
     779            if(@array_key_exists("index_on", $parameters)) { 
     780                $index_on = $parameters['index_on']; 
     781            }              
    774782            if(@array_key_exists("foreign_key", $parameters)) { 
    775783                $this_foreign_key = $parameters['foreign_key']; 
     
    792800        # Instantiate an object to access find_all 
    793801        $other_class_object = new $other_class_name(); 
     802        if(!is_null($index_on)) { 
     803            $other_class_object->index_on = $index_on; 
     804        } 
    794805 
    795806        # If finder_sql is specified just use it instead of determining the joins/sql 
     
    876887            if(@array_key_exists("per_page", $parameters)) { 
    877888                $options['per_page'] = $parameters['per_page']; 
    878             } 
     889            }       
     890            if(@array_key_exists("index_on", $parameters)) { 
     891                $index_on = $parameters['index_on']; 
     892            }            
    879893            if(@array_key_exists("foreign_key", $parameters)) { 
    880894                $foreign_key = $parameters['foreign_key']; 
     
    898912 
    899913        # Instantiate an object to access find_all 
    900         $other_class_object = new $other_class_name(); 
     914        $other_class_object = new $other_class_name();  
     915        if(!is_null($index_on)) { 
     916            $other_class_object->index_on = $index_on; 
     917        }         
    901918         
    902919        # If finder_sql is specified just use it instead of determining the association 
     
    10861103        #($parameters[0]) ? $field = $parameters[0] : $field = "*"; 
    10871104        $field = (stristr($parameters[0], ".") ? $parameters[0] : "{$this->table_prefix}{$this->table_name}.".$parameters[0]); 
    1088         $sql = "SELECT {$aggregate_type}({$distinct}{$field}) AS agg_result FROM {$this->table_prefix}{$this->table_name} ";         
     1105        $sql = "SELECT {$distinct}{$aggregate_type}({$field}) AS agg_result FROM {$this->table_prefix}{$this->table_name} ";         
    10891106        # Use any passed-in parameters 
    10901107        if(is_array($parameters[1])) { 
     
    10951112        } 
    10961113        if(!empty($joins)) $sql .= " $joins "; 
    1097         if(!empty($conditions)) $sql .= " WHERE $conditions "; 
    1098         # echo "$aggregate_type sql:$sql<br>"; 
     1114        if(!empty($conditions)) $sql .= " WHERE $conditions ";   
     1115        if(!empty($group)) $sql .= " GROUP BY {$group} "; 
     1116        if(!empty($having)) $sql .= " HAVING {$having} ";        
     1117         
     1118        #echo "$aggregate_type sql:$sql<br>"; 
    10991119        //print_r($parameters[0]); 
    11001120        //echo $sql; 
     
    12131233     */ 
    12141234    function begin() { 
    1215         # check if transaction are supported by this driver 
    1216         if(self::$db->supports('transactions')) {         
    1217             $rs = self::$db->beginTransaction(); 
     1235        # check if transaction are supported by this driver     
     1236        $db =& $this->get_connection(); 
     1237        if($db->supports('transactions')) {         
     1238            $rs = $db->beginTransaction(); 
    12181239            if($this->is_error($rs)) { 
    12191240                $this->raise($rs->getMessage()); 
     
    12301251     */     
    12311252    function save_point($save_point) { 
    1232         if(!is_null($save_point)) { 
     1253        if(!is_null($save_point)) {           
     1254            $db =& $this->get_connection(); 
    12331255            # check if transaction are supported by this driver 
    1234             if(self::$db->supports('transactions')) {             
     1256            if($db->supports('transactions')) {             
    12351257                # check if we are inside a transaction and if savepoints are supported 
    1236                 if(self::$db->inTransaction() && self::$db->supports('savepoints')) { 
     1258                if($db->inTransaction() && $db->supports('savepoints')) { 
    12371259                    # Set a savepoint 
    1238                     $rs = self::$db->beginTransaction($save_point);  
     1260                    $rs = $db->beginTransaction($save_point);  
    12391261                    if($this->is_error($rs)) { 
    12401262                        $this->raise($rs->getMessage()); 
     
    12511273     *  @todo Document this API 
    12521274     */ 
    1253     function commit() {       
     1275    function commit() {           
     1276        $db =& $this->get_connection(); 
    12541277        # check if transaction are supported by this driver 
    1255         if(self::$db->supports('transactions')) { 
     1278        if($db->supports('transactions')) { 
    12561279            # check if we are inside a transaction 
    1257             if(self::$db->inTransaction()) { 
    1258                 $rs = self::$db->commit();  
     1280            if($db->inTransaction()) { 
     1281                $rs = $db->commit();  
    12591282                if($this->is_error($rs)) { 
    12601283                    $this->raise($rs->getMessage()); 
     
    12711294     *  @todo Document this API 
    12721295     */     
    1273     function rollback() { 
     1296    function rollback() {      
     1297        $db =& $this->get_connection(true); 
    12741298        # check if transaction are supported by this driver 
    1275         if(self::$db->supports('transactions')) { 
    1276             $rs = self::$db->rollback();  
     1299        if($db->supports('transactions')) { 
     1300            $rs = $db->rollback();  
    12771301            if($this->is_error($rs)) { 
    12781302                $this->raise($rs->getMessage()); 
     
    12981322        $db =& $this->get_connection($read_only); 
    12991323        $rs =& $db->query($sql); 
    1300         if ($this->is_error($rs)) { 
     1324        if($this->is_error($rs)) { 
    13011325            if(self::$auto_rollback && self::$in_transaction) { 
    13021326                $this->rollback(); 
     
    13451369                    $conditions .= " AND "; 
    13461370                    $param_index++; 
    1347                 } else { 
    1348                     $value = $this->attribute_is_string($part) ?  
    1349                         "'".$parameters[$param_index]."'" :  
    1350                         $parameters[$param_index];                     
     1371                } else {     
     1372                    $value = $this->quote_attribute($part, $parameters[$param_index]); 
     1373                    #$value = $this->attribute_is_string($part) ?  
     1374                    #    "'".$parameters[$param_index]."'" :  
     1375                    #    $parameters[$param_index];  
     1376                    #error_log("find_by: $part = $value") ;                  
    13511377                    $create_fields[$part] = $parameters[$param_index];   
    13521378                    $conditions .= "{$part} = {$value}"; 
     
    14091435        $per_page = null; 
    14101436        $select = null; 
    1411         $paginate = false; 
     1437        $paginate = false;  
     1438        $group = null; 
     1439        $having = null; 
    14121440 
    14131441        # this is if they passed in an associative array to emulate 
     
    14261454        } 
    14271455 
    1428         # Test source of SQL for query 
    1429         if(stristr($conditions, "SELECT")) { 
     1456        # Test source of SQL for query  
     1457        if(stristr($conditions, "SELECT ")) { 
    14301458            # SQL completely specified in argument so use it as is 
    14311459            $sql = $conditions; 
    1432         } else { 
     1460        }  
     1461        else { 
    14331462 
    14341463            # If select fields not specified just do a SELECT * 
     
    16541683        $find_all = false; 
    16551684        if(is_array($id)) { 
    1656             if($id[0]) { 
     1685            if(isset($id[0])) { 
    16571686                # passed in array of numbers array(1,2,4,23) 
    16581687                $primary_key = $this->primary_keys[0]; 
     
    19031932     *  @throws {@link ActiveRecordError} 
    19041933     */ 
    1905     private function add_record() { 
    1906         self::$db->loadModule('Extended', null, true);                 
     1934    private function add_record() {  
     1935        $db =& $this->get_connection(); 
     1936        $db->loadModule('Extended', null, true);                 
    19071937        # $primary_key_value may either be a quoted integer or php null 
    1908         $primary_key_value = self::$db->getBeforeID("{$this->table_prefix}{$this->table_name}", $this->primary_keys[0]); 
     1938        $primary_key_value = $db->getBeforeID("{$this->table_prefix}{$this->table_name}", $this->primary_keys[0]); 
    19091939        if($this->is_error($primary_key_value)) { 
    19101940            $this->raise($primary_key_value->getMessage()); 
     
    19201950        $habtm_result = true; 
    19211951        $primary_key = $this->primary_keys[0]; 
    1922         # $id is now equivalent to the value in the id field that was inserted 
    1923         $primary_key_value = self::$db->getAfterID($primary_key_value, "{$this->table_prefix}{$this->table_name}", $this->primary_keys[0]); 
     1952        # $primary_key_value is now equivalent to the value in the id field that was inserted 
     1953        $primary_key_value = $db->getAfterID($primary_key_value, "{$this->table_prefix}{$this->table_name}", $this->primary_keys[0]); 
    19241954        if($this->is_error($primary_key_value)) { 
    19251955            $this->raise($primary_key_value->getMessage()); 
     
    21892219     *  @throws {@link ActiveRecordError} 
    21902220     */ 
    2191     function delete_all($conditions = null) { 
     2221    function delete_all($conditions = null, $limit = null) { 
    21922222        if(is_null($conditions)) { 
    21932223            $this->add_error("No conditions specified to delete on."); 
    21942224            return false; 
     2225        }  
     2226        if(!is_null($limit)) { 
     2227            $limit = "LIMIT {$limit}"; 
    21952228        } 
    21962229        # Delete the record(s) 
    2197         $this->query("DELETE FROM {$this->table_prefix}{$this->table_name} WHERE {$conditions}"); 
     2230        $this->query("DELETE FROM {$this->table_prefix}{$this->table_name} WHERE {$conditions} {$limit}"); 
    21982231        # reset this to a new record     
    21992232        $this->new_record = true; 
     
    22682301                        $values = @implode(', ', array_values($attributes)); 
    22692302                        $sql = "INSERT INTO $table_name ($fields) VALUES ($values)"; 
    2270                         error_log("add_habtm_records: SQL: $sql"); 
     2303                        #error_log("add_habtm_records: SQL: $sql"); 
    22712304                        $this->query($sql); 
    22722305                    } 
     
    26672700        if(isset(self::$table_info[$table_name])) { 
    26682701            $this->content_columns = self::$table_info[$table_name];   
    2669         } else { 
    2670             self::$db->loadModule('Reverse', null, true); 
    2671             $this->content_columns = self::$db->reverse->tableInfo($table_name); 
     2702        } else {        
     2703            $db =& $this->get_connection(true);                                       
     2704            $db->loadModule('Reverse', null, true); 
     2705            $this->content_columns = $db->reverse->tableInfo($table_name); 
    26722706            if($this->is_error($this->content_columns)) { 
    26732707                $this->raise($this->content_columns->getMessage());         
     
    26922726     */ 
    26932727    function get_insert_id() { 
    2694         // fetch the last inserted id via autoincrement or current value of a sequence 
    2695         if(self::$db->supports('auto_increment') === true) { 
    2696             $id = self::$db->lastInsertID("{$this->table_prefix}{$this->table_name}", $this->primary_keys[0]);    
     2728        // fetch the last inserted id via autoincrement or current value of a sequence  
     2729        $db =& $this->get_connection(); 
     2730        if($db->supports('auto_increment') === true) { 
     2731            $id = $db->lastInsertID("{$this->table_prefix}{$this->table_name}", $this->primary_keys[0]);    
    26972732            if($this->is_error($id)) { 
    26982733                $this->raise($id->getMessage()); 
     
    27282763     *  @throws {@link ActiveRecordError} 
    27292764     */ 
    2730     function establish_connection($connection_name = null, $read_only = false) { 
    2731         $connection_name = $this->get_connection_name($connection_name); 
     2765    function establish_connection($connection_name = null, $read_only = false) {  
     2766        #error_log("trying connection name:$connection_name"); 
     2767        $connection_name = $this->get_connection_name($connection_name);  
     2768        #error_log("got connection name:$connection_name read only:".($read_only ? 'true' : 'false'));  
    27322769        if($read_only) {  
    27332770            $connection =& self::$connection_pool_read_only[$connection_name]; 
     
    27732810                } 
    27742811            }  
    2775         } 
     2812        }  
    27762813        if(!$this->is_error($connection)) { 
    27772814            $connection->setFetchMode($this->fetch_mode); 
    27782815            if($read_only) { 
    2779                 self::$connection_pool_read_only[$connection_name] =& $connection; 
     2816                self::$connection_pool_read_only[$connection_name] =& $connection;  
     2817                $pool_size = count(self::$connection_pool_read_only); 
    27802818                $this->read_only_connection_name = $connection_name; 
    27812819            } else { 
    2782                 self::$connection_pool[$connection_name] =& $connection; 
     2820                self::$connection_pool[$connection_name] =& $connection;   
     2821                $pool_size = count(self::$connection_pool); 
    27832822                $this->connection_name = $connection_name; 
    2784             } 
    2785         } else { 
     2823            }   
     2824             
     2825            if($pool_size > 1 || $this->database_name != '') { 
     2826                $dsn = $connection->getDSN('array', true);      
     2827                #error_log("dsn:".print_r($dsn, true)); 
     2828                if($this->database_name != '') {  
     2829                    #$type = "database defined";  
     2830                    $database_name = $this->database_name;  
     2831                } elseif($dsn['database'] != '') {   
     2832                    #$type = "dsn"; 
     2833                    $database_name = $dsn['database']; 
     2834                } 
     2835                if($database_name) { 
     2836                    #error_log("connect $type switch database to {$database_name}"); 
     2837                    $connection->setDatabase($database_name);             
     2838                } 
     2839            }                                     
     2840        } else {    
    27862841            $this->raise($connection->getMessage()); 
    27872842        }       
     
    28062861     *  Gets the database connection whether its read only or read/write     
    28072862     */     
    2808     function get_connection($read_only = false) { 
     2863    function get_connection($read_only = false) {  
    28092864        if($read_only && $this->read_only_connection_name &&  
    28102865           array_key_exists($this->read_only_connection_name, self::$connection_pool_read_only)) { 
    2811             $db =& self::$connection_pool_read_only[$this->read_only_connection_name]; 
    2812             #error_log("get_connection($read_only) - using read only:".$this->read_only_connection_name); 
     2866            $db =& self::$connection_pool_read_only[$this->read_only_connection_name];  
     2867            $pool_size = count(self::$connection_pool_read_only); 
     2868            #error_log("get_connection pool:$pool_size - using read only:".$this->read_only_connection_name." dbname:".$db->database_name." table_name:".$this->table_name); 
    28132869        } elseif(array_key_exists($this->connection_name, self::$connection_pool)) { 
    2814             $db =& self::$connection_pool[$this->connection_name]; 
    2815             #error_log("get_connection($read_only) - using read/write from pool:".$this->connection_name); 
     2870            $db =& self::$connection_pool[$this->connection_name];    
     2871            $pool_size = count(self::$connection_pool); 
     2872            #error_log("get_connection pool:$pool_size - using read/write from pool:".$this->connection_name." dbname:".$db->database_name." table_name:".$this->table_name); 
    28162873        } else { 
    2817             $db =& self::$db; 
    2818             #error_log("get_connection($read_only) - using read/write default:".$this->connection_name); 
    2819         } 
     2874            $db =& self::$db;    
     2875            $pool_size = 1; 
     2876            #error_log("get_connection pool:1 - using read/write default:".$this->connection_name." dbname:".$db->database_name." table_name:".$this->table_name); 
     2877        }   
     2878             
     2879        if($pool_size > 1 || $this->database_name != '') { 
     2880            $dsn = $db->getDSN('array', true);      
     2881            #error_log("dsn:".print_r($dsn, true)); 
     2882            if($this->database_name != '') {  
     2883                $type = "database defined";  
     2884                $database_name = $this->database_name;  
     2885            } elseif($dsn['database'] != '') {   
     2886                $type = "dsn"; 
     2887                $database_name = $dsn['database']; 
     2888            } 
     2889            if($database_name) { 
     2890                #error_log("get_connection $type switch database to {$database_name} table:".$this->table_name); 
     2891                $db->setDatabase($database_name);             
     2892            } 
     2893        } 
     2894                     
    28202895        return $db; 
    2821     } 
     2896    }    
    28222897 
    28232898    /** 
     
    28262901    function clear_all_connections() { 
    28272902        self::$connection_pool = array(); 
    2828         self::$connection_pool_read_only = array(); 
     2903        self::$connection_pool_read_only = array();   
     2904    }   
     2905 
     2906    /** 
     2907     *  Select a different database    
     2908     * 
     2909     * @param   string  name of the database that should be selected 
     2910     * @return  string  name of the database previously connected to 
     2911     * @access  public      
     2912     */     
     2913    function set_database($database_name) {     
     2914        $db =& $this->get_connection(true); 
     2915        if($database_name && is_object($db)) { 
     2916            return $db->setDatabase($database_name); 
     2917        } 
    28292918    } 
    28302919 
     
    29052994            $this->after_validation(); 
    29062995            $this->validate_on_update(); 
    2907             $this->validate_on_update_builtin(); 
    29082996            $this->after_validation_on_update(); 
    29092997        } 
     
    34473535     */ 
    34483536    function raise($message) { 
    3449         $error_message  = "Model Class: ".$this->get_class_name()."<br>"; 
    3450         $error_message .= "Error Message: ".$message; 
     3537        $error_message  = "Model Class: ".$this->get_class_name()."<br>Error Message:"; 
     3538        if(is_object(self::$db)) {         
     3539            list(,$error_code_db, $error_msg_db) = self::$db->errorInfo();  
     3540            $error_message .= " ({$error_code_db}) {$error_msg_db}"; 
     3541        }    
     3542        if(!$error_code_db) { 
     3543            $error_message .= " ".$message; 
     3544        } 
    34513545        throw new ActiveRecordError($error_message, "ActiveRecord Error", "500"); 
    34523546    } 
     
    35293623     */ 
    35303624    function log_query($query) {     
    3531         if(self::$environment == 'development' && $query) { 
     3625        if((self::$environment != 'production' || self::$log_all) && $query) { 
    35323626            self::$query_log[] = $query;        
    35333627        }