Changeset 250 for trunk/trax/vendor
- Timestamp:
- 08/25/06 09:56:48 (6 years ago)
- Location:
- trunk/trax/vendor/trax
- Files:
-
- 2 modified
-
action_view/helpers/active_record_helper.php (modified) (1 diff)
-
active_record.php (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/trax/vendor/trax/action_view/helpers/active_record_helper.php
r242 r250 466 466 * @uses attribute_name 467 467 * @uses object() 468 * @uses ActiveRecord::column_ for_attribute()468 * @uses ActiveRecord::column_type() 469 469 */ 470 470 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); 473 472 } 474 473 -
trunk/trax/vendor/trax/active_record.php
r249 r250 35 35 36 36 /** 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) 38 40 */ 39 require_once(' DB.php');41 require_once('MDB2.php'); 40 42 41 43 /** … … 66 68 * 67 69 * 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()} 69 71 * @var object DB 70 72 * 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} 72 74 */ 73 75 private static $db = null; … … 154 156 * @var integer 155 157 */ 156 public $fetch_mode = DB_FETCHMODE_ASSOC;158 public $fetch_mode = MDB2_FETCHMODE_ASSOC; 157 159 158 160 /** … … 951 953 return null; 952 954 } 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 } 953 968 954 969 /** … … 989 1004 $sql = "SELECT {$column} FROM {$this->table_name} WHERE {$conditions}"; 990 1005 $this->log_query($sql); 991 $result = self::$db-> getOne($sql);1006 $result = self::$db->queryOne($sql); 992 1007 if($this->is_error($result)) { 993 1008 $this->raise($result->getMessage()); … … 1033 1048 * 1034 1049 * @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} 1036 1051 * Result set from query 1037 1052 * @uses $db … … 1043 1058 # Run the query 1044 1059 $this->log_query($sql); 1045 $rs = self::$db->query($sql);1060 $rs =& self::$db->query($sql); 1046 1061 if ($this->is_error($rs)) { 1047 1062 if(self::$use_transactions && self::$begin_executed) { … … 1581 1596 */ 1582 1597 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 1583 1605 $attributes = $this->get_inserts(); 1584 1606 $fields = @implode(', ', array_keys($attributes)); 1585 1607 $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)"; 1587 1609 //echo "add_record: SQL: $sql<br>"; 1610 error_log("add_record: SQL: $sql"); 1588 1611 $result = $this->query($sql); 1612 1589 1613 if($this->is_error($result)) { 1590 1614 $this->raise($results->getMessage()); … … 1592 1616 $habtm_result = true; 1593 1617 $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; 1596 1624 if($primary_key_value != '') { 1597 1625 if($this->auto_save_habtm) { … … 1631 1659 $sql = "UPDATE {$this->table_name} SET {$updates} WHERE {$conditions}"; 1632 1660 //echo "update_record:$sql<br>"; 1633 //error_log("update_record: SQL: $sql<br>");1661 error_log("update_record: SQL: $sql"); 1634 1662 $result = $this->query($sql); 1635 1663 if($this->is_error($result)) { … … 1962 1990 return date($format); 1963 1991 } elseif($this->preserve_null_dates && is_null($value) && !stristr($field_info['flags'], "not_null")) { 1964 return 'NULL';1992 return null; 1965 1993 } 1966 1994 } elseif(!$this->new_record) { … … 1968 1996 return date($format); 1969 1997 } elseif($this->preserve_null_dates && is_null($value) && !stristr($field_info['flags'], "not_null")) { 1970 return 'NULL';1998 return null; 1971 1999 } 1972 2000 } … … 2114 2142 } 2115 2143 $return = array(); 2116 foreach ($attributes as $key => $value) {2144 foreach($attributes as $key => $value) { 2117 2145 $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); 2131 2148 } 2132 2149 return $return; … … 2255 2272 */ 2256 2273 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); 2258 2276 if($this->is_error($this->content_columns)) { 2259 2277 $this->raise($this->content_columns->getMessage()); … … 2276 2294 */ 2277 2295 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; 2283 2306 } 2284 2307 … … 2308 2331 */ 2309 2332 function establish_connection() { 2310 $connection = Trax::$active_record_connections[$this->connection_name];2333 $connection =& Trax::$active_record_connections[$this->connection_name]; 2311 2334 if(!is_object($connection) || $this->force_reconnect) { 2312 2335 if(array_key_exists($this->connection_name, Trax::$database_settings)) { … … 2334 2357 } 2335 2358 # 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); 2337 2361 } 2338 2362 if(!$this->is_error($connection)) { … … 2347 2371 2348 2372 /** 2349 * Test whether argument is a PEAR Error object or a DBError object.2373 * Test whether argument is a PEAR Error object or a MDB2 Error object. 2350 2374 * 2351 2375 * @param object $obj Object to test … … 2353 2377 */ 2354 2378 function is_error($obj) { 2355 if((PEAR::isError($obj)) || ( DB::isError($obj))) {2379 if((PEAR::isError($obj)) || (MDB2::isError($obj))) { 2356 2380 return true; 2357 2381 } else { … … 2431 2455 */ 2432 2456 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) { 2435 2459 case 'string': 2436 2460 case 'varchar': … … 2438 2462 case 'text': 2439 2463 case 'blob': 2464 case 'clob': 2440 2465 case 'date': 2466 case 'time': 2441 2467 case 'datetime': 2442 2468 case 'timestamp':
