Changeset 315 for trunk/trax/vendor
- Timestamp:
- 07/08/09 01:40:57 (3 years ago)
- Files:
-
- 1 modified
-
trunk/trax/vendor/trax/active_record.php (modified) (30 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/trax/vendor/trax/active_record.php
r313 r315 177 177 * What environment to run in. 178 178 */ 179 public static $environment = ' development';179 public static $environment = 'production'; 180 180 181 181 /** … … 502 502 */ 503 503 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; 504 509 505 510 /** … … 771 776 if(@array_key_exists("join_table", $parameters)) { 772 777 $join_table = $parameters['join_table']; 773 } 778 } 779 if(@array_key_exists("index_on", $parameters)) { 780 $index_on = $parameters['index_on']; 781 } 774 782 if(@array_key_exists("foreign_key", $parameters)) { 775 783 $this_foreign_key = $parameters['foreign_key']; … … 792 800 # Instantiate an object to access find_all 793 801 $other_class_object = new $other_class_name(); 802 if(!is_null($index_on)) { 803 $other_class_object->index_on = $index_on; 804 } 794 805 795 806 # If finder_sql is specified just use it instead of determining the joins/sql … … 876 887 if(@array_key_exists("per_page", $parameters)) { 877 888 $options['per_page'] = $parameters['per_page']; 878 } 889 } 890 if(@array_key_exists("index_on", $parameters)) { 891 $index_on = $parameters['index_on']; 892 } 879 893 if(@array_key_exists("foreign_key", $parameters)) { 880 894 $foreign_key = $parameters['foreign_key']; … … 898 912 899 913 # 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 } 901 918 902 919 # If finder_sql is specified just use it instead of determining the association … … 1086 1103 #($parameters[0]) ? $field = $parameters[0] : $field = "*"; 1087 1104 $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} "; 1089 1106 # Use any passed-in parameters 1090 1107 if(is_array($parameters[1])) { … … 1095 1112 } 1096 1113 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>"; 1099 1119 //print_r($parameters[0]); 1100 1120 //echo $sql; … … 1213 1233 */ 1214 1234 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(); 1218 1239 if($this->is_error($rs)) { 1219 1240 $this->raise($rs->getMessage()); … … 1230 1251 */ 1231 1252 function save_point($save_point) { 1232 if(!is_null($save_point)) { 1253 if(!is_null($save_point)) { 1254 $db =& $this->get_connection(); 1233 1255 # check if transaction are supported by this driver 1234 if( self::$db->supports('transactions')) {1256 if($db->supports('transactions')) { 1235 1257 # 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')) { 1237 1259 # Set a savepoint 1238 $rs = self::$db->beginTransaction($save_point);1260 $rs = $db->beginTransaction($save_point); 1239 1261 if($this->is_error($rs)) { 1240 1262 $this->raise($rs->getMessage()); … … 1251 1273 * @todo Document this API 1252 1274 */ 1253 function commit() { 1275 function commit() { 1276 $db =& $this->get_connection(); 1254 1277 # check if transaction are supported by this driver 1255 if( self::$db->supports('transactions')) {1278 if($db->supports('transactions')) { 1256 1279 # 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(); 1259 1282 if($this->is_error($rs)) { 1260 1283 $this->raise($rs->getMessage()); … … 1271 1294 * @todo Document this API 1272 1295 */ 1273 function rollback() { 1296 function rollback() { 1297 $db =& $this->get_connection(true); 1274 1298 # 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(); 1277 1301 if($this->is_error($rs)) { 1278 1302 $this->raise($rs->getMessage()); … … 1298 1322 $db =& $this->get_connection($read_only); 1299 1323 $rs =& $db->query($sql); 1300 if ($this->is_error($rs)) {1324 if($this->is_error($rs)) { 1301 1325 if(self::$auto_rollback && self::$in_transaction) { 1302 1326 $this->rollback(); … … 1345 1369 $conditions .= " AND "; 1346 1370 $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") ; 1351 1377 $create_fields[$part] = $parameters[$param_index]; 1352 1378 $conditions .= "{$part} = {$value}"; … … 1409 1435 $per_page = null; 1410 1436 $select = null; 1411 $paginate = false; 1437 $paginate = false; 1438 $group = null; 1439 $having = null; 1412 1440 1413 1441 # this is if they passed in an associative array to emulate … … 1426 1454 } 1427 1455 1428 # Test source of SQL for query 1429 if(stristr($conditions, "SELECT ")) {1456 # Test source of SQL for query 1457 if(stristr($conditions, "SELECT ")) { 1430 1458 # SQL completely specified in argument so use it as is 1431 1459 $sql = $conditions; 1432 } else { 1460 } 1461 else { 1433 1462 1434 1463 # If select fields not specified just do a SELECT * … … 1654 1683 $find_all = false; 1655 1684 if(is_array($id)) { 1656 if( $id[0]) {1685 if(isset($id[0])) { 1657 1686 # passed in array of numbers array(1,2,4,23) 1658 1687 $primary_key = $this->primary_keys[0]; … … 1903 1932 * @throws {@link ActiveRecordError} 1904 1933 */ 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); 1907 1937 # $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]); 1909 1939 if($this->is_error($primary_key_value)) { 1910 1940 $this->raise($primary_key_value->getMessage()); … … 1920 1950 $habtm_result = true; 1921 1951 $primary_key = $this->primary_keys[0]; 1922 # $ idis now equivalent to the value in the id field that was inserted1923 $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]); 1924 1954 if($this->is_error($primary_key_value)) { 1925 1955 $this->raise($primary_key_value->getMessage()); … … 2189 2219 * @throws {@link ActiveRecordError} 2190 2220 */ 2191 function delete_all($conditions = null ) {2221 function delete_all($conditions = null, $limit = null) { 2192 2222 if(is_null($conditions)) { 2193 2223 $this->add_error("No conditions specified to delete on."); 2194 2224 return false; 2225 } 2226 if(!is_null($limit)) { 2227 $limit = "LIMIT {$limit}"; 2195 2228 } 2196 2229 # 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}"); 2198 2231 # reset this to a new record 2199 2232 $this->new_record = true; … … 2268 2301 $values = @implode(', ', array_values($attributes)); 2269 2302 $sql = "INSERT INTO $table_name ($fields) VALUES ($values)"; 2270 error_log("add_habtm_records: SQL: $sql");2303 #error_log("add_habtm_records: SQL: $sql"); 2271 2304 $this->query($sql); 2272 2305 } … … 2667 2700 if(isset(self::$table_info[$table_name])) { 2668 2701 $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); 2672 2706 if($this->is_error($this->content_columns)) { 2673 2707 $this->raise($this->content_columns->getMessage()); … … 2692 2726 */ 2693 2727 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]); 2697 2732 if($this->is_error($id)) { 2698 2733 $this->raise($id->getMessage()); … … 2728 2763 * @throws {@link ActiveRecordError} 2729 2764 */ 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')); 2732 2769 if($read_only) { 2733 2770 $connection =& self::$connection_pool_read_only[$connection_name]; … … 2773 2810 } 2774 2811 } 2775 } 2812 } 2776 2813 if(!$this->is_error($connection)) { 2777 2814 $connection->setFetchMode($this->fetch_mode); 2778 2815 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); 2780 2818 $this->read_only_connection_name = $connection_name; 2781 2819 } else { 2782 self::$connection_pool[$connection_name] =& $connection; 2820 self::$connection_pool[$connection_name] =& $connection; 2821 $pool_size = count(self::$connection_pool); 2783 2822 $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 { 2786 2841 $this->raise($connection->getMessage()); 2787 2842 } … … 2806 2861 * Gets the database connection whether its read only or read/write 2807 2862 */ 2808 function get_connection($read_only = false) { 2863 function get_connection($read_only = false) { 2809 2864 if($read_only && $this->read_only_connection_name && 2810 2865 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); 2813 2869 } 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); 2816 2873 } 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 2820 2895 return $db; 2821 } 2896 } 2822 2897 2823 2898 /** … … 2826 2901 function clear_all_connections() { 2827 2902 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 } 2829 2918 } 2830 2919 … … 2905 2994 $this->after_validation(); 2906 2995 $this->validate_on_update(); 2907 $this->validate_on_update_builtin();2908 2996 $this->after_validation_on_update(); 2909 2997 } … … 3447 3535 */ 3448 3536 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 } 3451 3545 throw new ActiveRecordError($error_message, "ActiveRecord Error", "500"); 3452 3546 } … … 3529 3623 */ 3530 3624 function log_query($query) { 3531 if( self::$environment == 'development'&& $query) {3625 if((self::$environment != 'production' || self::$log_all) && $query) { 3532 3626 self::$query_log[] = $query; 3533 3627 }
