OSDN Git Service

modified: DBRecord.class.php 高速化
authorepgrec@park.mda.or.jp <yoneda@localhost.localdomain>
Sun, 15 Apr 2012 11:56:53 +0000 (20:56 +0900)
committerepgrec@park.mda.or.jp <yoneda@localhost.localdomain>
Sun, 15 Apr 2012 11:56:53 +0000 (20:56 +0900)
DBRecord.class.php

index e094891..883cb1e 100755 (executable)
@@ -7,9 +7,15 @@ class DBRecord {
        protected $settings;
        
        protected $dbh;
+       protected $record_data;
+       protected $f_dirty;
+       
        public $id;
        
     function __construct( $table, $property = null, $value = null ) {
+               $this->f_dirty = false;
+               $this->record_data = false;
+               
                $this->settings = Settings::factory();
                
                $this->table = $this->settings->tbl_prefix.$table;
@@ -33,10 +39,10 @@ class DBRecord {
                                      "='".mysql_real_escape_string( $value )."'";
                        
                        $res = $this->__query( $sqlstr );
-                       $arr = mysql_fetch_array( $res , MYSQL_ASSOC );
-                       if( $arr === FALSE ) throw new exception( "construct:".$table."に".$property."=".$value."はありません" );
+                       $this->record_data = mysql_fetch_array( $res , MYSQL_ASSOC );
+                       if( $this->record_data === false ) throw new exception( "construct:".$table."に".$property."=".$value."はありません" );
                        // 最初にヒットした行のidを使用する
-                       $this->id = $arr['id'];
+                       $this->id = $this->record_data['id'];
                }
                
                return;
@@ -76,30 +82,38 @@ class DBRecord {
        }
        
        function __set( $property, $value ) {
-               if( $property == "id" ) throw new exception( "set:idの変更は不可" );
+               if( $property === "id" ) throw new exception( "set:idの変更は不可" );
                // id = 0なら空の新規レコード作成
                if( $this->id == 0 ) {
                        $sqlstr = "INSERT INTO ".$this->table." VALUES ( )";
                        $res = $this->__query( $sqlstr );
                        $this->id = mysql_insert_id();
+                       
+                       // $this->record_data読み出し 
+                       $sqlstr = "SELECT * FROM ".$this->table.
+                                   " WHERE id = '".$this->id."'";
+                       
+                       $res = $this->__query( $sqlstr );
+                       $this->record_data = mysql_fetch_array( $res , MYSQL_ASSOC );
+               }
+               if( $this->record_data === false ) throw new exception("set: DBの異常?" );
+               
+               if( array_key_exists( $property, $this->record_data ) ) {
+                       $this->record_data[$property] = mysql_real_escape_string($value);
+                       $this->f_dirty = true;
+               }
+               else {
+                       throw new exception("set:$property はありません" );
                }
-               $sqlstr = "UPDATE ".$this->table." SET ".
-                            mysql_real_escape_string($property)."='".
-                            mysql_real_escape_string($value)."' WHERE id='".$this->id."'";
-               $res = $this->__query( $sqlstr );
-               if( $res == FALSE )  throw new exception("set:セット失敗" );
        }
        
        function __get( $property ) {
                if( $this->id == 0 ) throw new exception( "get:無効なid" );
-               if( $property == "id" ) return $this->id;
-               
-               $sqlstr = "SELECT ".mysql_real_escape_string($property)." FROM ".$this->table." WHERE id='".$this->id."'";
-               $res = $this->__query($sqlstr);
-               $arr = mysql_fetch_row( $res );
-               if( $arr === FALSE ) throw new exception( "get:".$property."は存在しない" );
+               if( $property === "id" ) return $this->id;
+               if( $this->record_data === false ) throw new exception( "get: 無効なレコード" );
+               if( ! array_key_exists( $property, $this->record_data ) ) throw new exception( "get: $propertyは存在しません" );
                
-               return stripslashes($arr[0]);
+               return stripslashes($this->record_data[$property]);
        }
        
        function delete() {
@@ -108,6 +122,27 @@ class DBRecord {
                $sqlstr = "DELETE FROM ".$this->table." WHERE id='".$this->id."'";
                $this->__query( $sqlstr );
                $this->id = 0;
+               $this->record_data = false;
+               $this->f_dirty = false;
+       }
+       
+       function close() {
+               if( $this->id != 0 ) { 
+                       if( $this->f_dirty ) {
+                               $sqlstr = "UPDATE ".$this->table." SET";
+                               foreach( $this->record_data as $property => $value ) {
+                                       if( $property === "id" ) continue;
+                                       $sqlstr .= " ".$property." = '".$value."',";
+                               }
+                               $sqlstr = rtrim($sqlstr, "," );
+                               $sqlstr .= " WHERE id = '".$this->id."'";
+                               $res = $this->__query($sqlstr);
+                               if( $res === false ) throw new exception( "close: アップデート失敗" );
+                       }
+                       $this->id = 0;
+                       $this->f_dirty = false;
+                       $this->record_data = false;
+               }
        }
        
        // countを実行する
@@ -145,7 +180,10 @@ class DBRecord {
        }
        
        function __destruct() {
-               $this->id = 0;
+               // 呼び忘れに対応
+               if( $this->id != 0 ) {
+                       $this->close();
+               }
        }
 }
 ?>