2010年1月8日 星期五

CuRecord - Access Record

// Access Record

$record = new CuRecord($ado, $table_name, $key);

//
$record->SetField("field_name", $field_value );
$record->Save();

//
$record->Load("field_name", $field_value);
$value = $record->GetField("field_name");

//
$record->Load("field_name", $field_value);
$record->Destroy();


// CuRecord.php

class CuRecord  {

    var $ado;
    var $table_name; 
    
    var $key_name; 
    var $key_value; 
    
    var $database_fields;
    var $modified_fields;
    
    
    function CuRecord($ado, $table_name, $key_name = "sn" , $key_value = "") {
        
        $this->ado = &$ado;
        $this->table_name = $table_name;
        $this->key_name = $key_name;
        $this->key_value = $this->_check_input($key_value);
        
        $this->database_fields = array();
        $this->modified_fields = array();        
    }
    
    
    function SetField( $field, $value) {

        if( is_numeric($field) ) 
            return;
        
        $this->database_fields[$field] = $value;
        $this->modified_fields[$field] = true;

    }


    function GetField($field) {
        return $this->database_fields[$field]; 
    }   


    function Destroy() {
        
        if( !$this->key_value )
            return;
        return $this->ado->sql("DELETE FROM $this->table_name WHERE $this->key_name = '$this->key_value'");
        
    }   
    

    function Save()
    {
        
        if( $this->key_value )
        {
            $result = $this->_update();
        }
        else
        {
            $result = $this->_insert();
            $this->_load_key_value();
        }
        
        return $this->key_value;
    }
    

    function Load($bargain_field, $bargain_value)
    {
        
        if(!($bargain_field && $bargain_value ))
        {
            $bargain_field = $this->key_name;
            $bargain_value = $this->key_value;
        }
        
        if( !($bargain_field && $bargain_value ) )
            return;
        
        $value = $this->_check_input($bargain_value);
        $stmt = "`$bargain_field` = '$value'";
        $stmt = "SELECT * FROM $this->table_name WHERE $stmt";
         
        $result = $this->ado->sql($stmt);
        if( $result == "" )
            return ;
        
        $result_table = $this->ado->fetch_array();
        $this->database_fields = $result_table[0];
        
        if( sizeof($this->modified_fields) > 0 ) {
            foreach( $this->modified_fields as $key => $value ) {
                $this->modified_fields[$key] = false;
            }
        }       
        
        $this->key_value = $result_table[0][$this->key_name];
        
        return $this->key_value;
        
    }
    

    function _load_key_value()
    {
        $stmt = "SELECT MAX($this->key_name) AS $this->key_name FROM `$this->table_name` WHERE ";
        foreach( $this->database_fields as $key => $value ){
            if( !is_numeric($key) && $value ){
                if( $this->modified_fields[$key] == true ){
                    $value = $this->_check_input($value); 
                    $stmt .= "`$key` = '$value' AND ";
                }
            }
        } 
        
        $stmt = substr($stmt,0,strlen($stmt)-5);    
        $handle = $this->ado->sql($stmt);
        
        $result_table = $this->ado->fetch_array();
        $this->key_value = $result_table[0][$this->key_name];
        
        return $result_rows;
    }
    

    function _insert() {

        $stmt = "INSERT INTO `$this->table_name` ";
        
        foreach ($this->database_fields as $key => $value) {
            if( !is_numeric( $key )&& $value){
                $key = $this->_check_input($key);
                $key_stmt .= " `$key`,";
                
                $value = $this->_check_input($value);
                $value_stmt .= " '$value',";
                
            }
        }
        
        $key_stmt = substr($key_stmt,0,strlen($key_stmt)-1); 
        $value_stmt = substr($value_stmt,0,strlen($value_stmt)-1); 
        
        $stmt .= " (" . $key_stmt . ") VALUES (" . $value_stmt . ")" ;
        $handle = $this->ado->sql($stmt, "_insert");
        
        
        return $handle . " " . $stmt;
        
    }    
    
    
    function _update() {
        
        $field_value = $this->key_value;
        $field_name = $this->key_name;
        $table_name = $this->table_name;
        
        $stmt = "UPDATE `$table_name` SET ";
        foreach ($this->database_fields as $key => $value) {
            if( !is_numeric( $key ) ){
                if( $this->modified_fields[$key] == true ) {
                    $value = $this->_check_input($value);
                    if( $value == "" ){
                        $stmt .= "`$key` = 'NULL', ";
                    }
                    else {
                        $stmt .= "`$key` = '$value', ";
                    }
                }
            }
        }
        
        $stmt = substr($stmt, 0, strlen($stmt) - 2);
        $stmt .= " WHERE `$field_name` = '$field_value'"; 
        
        $handle = $this->ado->sql($stmt);
        return $handle . " " . $stmt;
        
    } 
    
    
    function _check_input($value)
    {
        if($value == "")
            return "";

        if (get_magic_quotes_gpc())
        {
            $value = stripslashes($value);
        }
        
        if (!is_numeric($value))
        {
            $value =  mysql_real_escape_string($value);
        }
        
        return $value;
    }
}

1 則留言:

  1. 對單筆資料做維護, 減少部份專案複雜度.那多筆資料維護呢?!可以使用Composite 做集合,但是會發生多次存取資料庫,必需想個機制優化, CuAdo 由外部傳入, 跟資料庫類型的偶合很容易的分離!

    回覆刪除