DAO操作MYSQL

来源:互联网 发布:女人不孕不育网络咨询 编辑:程序博客网 时间:2024/06/01 23:04

原文地址


要用PHP操作一个数据库很简单,相应入门后的PHPER都可以做到,但是在应对大量的表操作时,我们为许多的MySQL语句感到厌烦,所以我们急切想要把大量数据库操作封装起来。所以就出现了数据库对象映射。

首先我们新建一个接口。

singleton.class.php

[php] view plain copy
 print?
  1. <?php  
  2. /** 
  3.  * @author tomyjohn 
  4.  * @link 
  5.  * @license 
  6.  * @version 1.0 
  7.  * @copyright Copyright 2010 tomyjohn - tomyjohn.gicp.net 
  8.  * @package singleton 
  9.  */  
  10. /** 
  11.  * 数据库对象 
  12.  */  
  13. interface singleton{  
  14.       
  15.     /** 
  16.      * 生成数据库对象 
  17.      * @returns object data object; 
  18.      * @access public  
  19.      */  
  20.     public static function getInstance();  
  21. }  

再新建一个抽象类,这个抽象类把所有的数据库都简明地用5个方法来抽象。

db.class.php

[php] view plain copy
 print?
  1. <?php  
  2. /** 
  3.  * @author tomyjohn 
  4.  * @link 
  5.  * @license 
  6.  * @version 1.0 
  7.  * @copyright Copyright 2010 tomyjohn - tomyjohn.gicp.net 
  8.  * @package db 
  9.  */  
  10. /** 
  11.  *抽象DB类 
  12.  */  
  13. abstract class db{  
  14.       
  15.     /** 
  16.      * 工厂模式 
  17.      * @param string $type sql type  
  18.      * @returns object  
  19.      * @access public  
  20.      */  
  21.     public static function factory($type){  
  22.         return call_user_func(array($type,'getInstance'));  
  23.     }  
  24.     /** 
  25.      * 执行SQL语句 
  26.      * @param string $query sql 语句 
  27.      * @return object resource or false; 
  28.      * @access public 
  29.      */  
  30.     abstract public function execute($query);  
  31.     /** 
  32.      * 获取SQL语句返回的数组 
  33.      * @param string $query sql 语句 
  34.      * @return object resource or false; 
  35.      * @access public 
  36.      */  
  37.     abstract public function get_array($query);  
  38.     /** 
  39.      * 获取上一条语句的执行ID 
  40.      * @param string $query sql 语句 
  41.      * @return integer number or false; 
  42.      * @access public 
  43.      */  
  44.     abstract public function insert_get_id($query);  
  45.     /** 
  46.      * 转化特殊字符 
  47.      * @param string $string  
  48.      * @return string 处理后的字符串 
  49.      * @access public 
  50.      */  
  51.     abstract public function clean($string);  
  52. }  

相信看到这里,都会想到那个call_user_func方法该如何使用,别急,往下看

mysql.class.php

[html] view plain copy
 print?
  1. <?php  
  2. /**  
  3.  * @author tomyjohn  
  4.  * @link  
  5.  * @license  
  6.  * @version 1.0  
  7.  * @copyright Copyright 2010 tomyjohn - tomyjohn.gicp.net  
  8.  * @package db  
  9.  */  
  10. /**  
  11.  *MYSQL数据库对象  
  12.  */  
  13. class mysql extends db implements singleton{  
  14.       
  15.     /**  
  16.      * @var $instance object   
  17.      * @access current class  
  18.      */  
  19.     protected static $instance = null;  
  20.     /**  
  21.      * @var $link resource  
  22.      * @access current class  
  23.      */  
  24.     protected $link;  
  25.       
  26.     /**  
  27.      * 数据库实例  
  28.      * @return  $self::instance object  
  29.      */  
  30.     public static function getInstance(){  
  31.         if(is_null(self::$instance)){  
  32.             self::$instance = new self();  
  33.         }  
  34.         return self::$instance;  
  35.     }  
  36.       
  37.     /**  
  38.      * 构造器  
  39.      */  
  40.     protected function __construct(){  
  41.         global $current_conf;  
  42.         $user = $current_conf['DBUSER'];  
  43.         $pass = $current_conf['DBPWD'];  
  44.         $host = $current_conf['DBHOST'];  
  45.         $db   = $current_conf['DBNAME'];  
  46.           
  47.         $this->link = mysql_connect($host,$user,$pass);  
  48.           
  49.         mysql_set_charset($current_conf['DBCHARSET'] , $this->link);  
  50.         mysql_select_db($db);  
  51.     }  
  52.       
  53.     /**  
  54.      * 转化特殊字符  
  55.      * @param string $string   
  56.      * @return string 处理后的字符串  
  57.      * @access public  
  58.      */  
  59.     public function clean($string){  
  60.         return mysql_real_escape_string($string,$this->link);  
  61.     }  
  62.       
  63.     /**  
  64.      * 执行SQL语句  
  65.      * @param string $query sql 语句  
  66.      * @return object resource or false;  
  67.      * @access public  
  68.      */  
  69.     public function execute($query){  
  70.         return mysql_query($query,$this->link);  
  71.     }  
  72.     /**  
  73.      * 获取上一条语句的执行ID  
  74.      * @param string $query sql 语句  
  75.      * @return integer number or false;  
  76.      * @access public  
  77.      */  
  78.     public function insert_get_id($query){  
  79.         $this->execute($query);  
  80.         return mysql_insert_id($this->link);  
  81.     }  
  82.     /**  
  83.      * 获取SQL语句返回的数组  
  84.      * @param string $query sql 语句  
  85.      * @return object resource or false;  
  86.      * @access public  
  87.      */  
  88.     public function get_array($query){  
  89.         $result = $this->execute($query);  
  90.         $return = array();  
  91.         if($result){  
  92.             while($row = mysql_fetch_array($result , MYSQL_ASSOC)){  
  93.                 $return[] =$row;  
  94.             }  
  95.         }  
  96.         return $return;  
  97.     }  
  98. }  

current_conf 这个数组是我项目里的,其实也可以用你数据库的用户名和密码代替,看完这个我想你们也应该清楚了,继承DB然后实现singleton接口后的这个类,其实也可用到MSSQL,ORACL,以及其他数据库,但是光有这个,我们只能使操作数据库变成这样

[html] view plain copy
 print?
  1. $connection = db::factory('mysql');  
  2. $sql = "SELECT * FROM table";  
  3. $value_array = $connection->get_array($sql);  
[html] view plain copy
 print?
  1. print_r($value_array);  
这样虽然解决了扩展,解决了一些重复的操作,但是还不是很方便,我们应该更进一步,使数据库表用对象表示,即数据库映射

dao.class.php

[html] view plain copy
 print?
  1. <?php  
  2. class dao{  
  3.     /**  
  4.      * @var $values array 存放数据库对象   
  5.      * @access current class  
  6.      */  
  7.     protected $values = array();  
  8.       
  9.     /**  
  10.      * @var $suffix array 存放数据库对象   
  11.      * @access public   
  12.      */  
  13.     public $suffix = '';  
  14.       
  15.     /**  
  16.      * 构造器  
  17.      */  
  18.     public function __construct($qualifier = null){  
  19.         global $current_conf;  
  20.         $this->suffix = $current_conf['DBSUFFIX'];  
  21.           
  22.         if(!is_null($qualifier)){  
  23.             $conditional = array();  
  24.               
  25.             if(is_numeric($qualifier)){  
  26.                 $conditional = array('id'=>$qualifier);  
  27.             }  
  28.             else if(is_array($qualifier)){  
  29.                 $conditional = $qualifier;  
  30.             }  
  31.             else{  
  32.                 throw new Exception('Invalid type of qualifier given!');  
  33.             }  
  34.               
  35.             $this->populate($conditional);  
  36.         }         
  37.     }  
  38.       
  39.     public function __set($name , $value){  
  40.         $this->values[$name] = $value;  
  41.     }  
  42.       
  43.     public function __get($name){  
  44.         if(isset($this->values[$name])){  
  45.             return $this->values[$name];  
  46.         }  
  47.         else{  
  48.             return null;  
  49.         }  
  50.     }  
  51.       
  52.     /**  
  53.      * 解析实例的参数   
  54.      * @param $conditional obj   
  55.      */  
  56.     protected function populate($conditional){  
  57.         $connection = db::factory('mysql');  
  58.           
  59.         $sql = "SELECT * FROM {$this->suffix}{$this->table} WHERE ";  
  60.         $qualifier = '';  
  61.           
  62.         foreach($conditional as $column => $value){  
  63.             if(!empty($qualifier)){  
  64.                 $qualifier .' AND ';  
  65.             }  
  66.             $qualifier ."`{$column}`='" . $connection->clean($value) . "' ";  
  67.         }  
  68.           
  69.         $sql .= $qualifier;  
  70.         $value_array = $connection->get_array($sql);  
  71.           
  72.         if(!isset($value_array[0])){  
  73.             $value_array[0] = array();  
  74.         }  
  75.           
  76.         foreach($value_array[0] as $key => $value){  
  77.             $this->values[$key] = $value;  
  78.         }  
  79.     }  
  80.       
  81.     /**  
  82.      * 保存数据  
  83.      */  
  84.     public function save(){  
  85.         if(!$this->id){  
  86.             $this->create();  
  87.         }  
  88.         else{  
  89.             return $this->update();  
  90.         }  
  91.     }  
  92.       
  93.     /**  
  94.      * 添加数据  
  95.      */  
  96.     public function create(){  
  97.         $connection = db::factory('mysql');  
  98.           
  99.         $sql = "INSERT INTO {$this->suffix}{$this->table}(`";  
  100.         $sql .implode('`, `' , array_keys($this->values));  
  101.         $sql .="`) VALUES('";  
  102.           
  103.         $clean = array();  
  104.         foreach($this->values as $value){  
  105.             $clean[] = $connection->clean($value);  
  106.         }  
  107.           
  108.         $sql .implode("', '" , $clean);  
  109.         $sql .="')";  
  110.           
  111.         $this->id = $connection->insert_get_id($sql);  
  112.     }  
  113.       
  114.     /**  
  115.      * 更新数据  
  116.      * @return 返回执行操作的结果  
  117.      */  
  118.     public function update(){  
  119.         $connection = db::factory('mysql');  
  120.           
  121.         $sql = "UPDATE {$this->suffix}{$this->table} set ";  
  122.           
  123.         $updates = array();  
  124.         foreach($this->values as $key=>$value){  
  125.             if($key!='id'){  
  126.                 $updates[] = "`{$key}`='" . $connection->clean($value) . "'";  
  127.             }  
  128.         }  
  129.           
  130.         $sql .implode(',' , $updates);  
  131.         $sql ." WHERE id={$this->id}";  
  132.           
  133.         return $connection->execute($sql);  
  134.     }  
  135.     /**  
  136.      * 删除数据  
  137.      * @return 返回执行操作的结果  
  138.      */  
  139.     public function delete(){  
  140.         $connection = db::factory('mysql');  
  141.           
  142.         $sql = "DELETE FROM {$this->suffix}{$this->table} WHERE ";  
  143.         $qualifier = 'id='.$this->id;  
  144.   
  145.         $sql .= $qualifier;  
  146.         return $connection->execute($sql);  
  147.     }  
  148.       
  149.     /**  
  150.      * 对象转成数组  
  151.      * @return array  
  152.      */  
  153.     public function object_to_array(){  
  154.         return $this->values;  
  155.     }  
  156.       
  157. }  

如果看到这里,我相信大家都会想去继承这个类。 是的,如果继承了这个类,那么每条记录就可以变成对象,就可以用面向对象的方式去处理了。

我写一个

news.dao.class

[php] view plain copy
 print?
  1. <?php  
  2. class news extends dao{  
  3.     protected $table = __CLASS__;  
  4.       
  5. }  

0 0
原创粉丝点击