程序源码:希望高手指点一下,还有哪里有改进的
[code]<?php
// +---------------------------------------------------------------
// | Aspire Mode Class
// +---------------------------------------------------------------
// | Copyright (c) 2009 http://www.bc263.com All rights reserved.
// +---------------------------------------------------------------
// | Author: streen003 <streen003gmail.com>
// +---------------------------------------------------------------
class Model {
//定义变量
private $table_info; //数据表信息
private $option; //SQL语句选项
protected $primary_key; //数据表主键
protected $table_name; //数据表名
protected $table_prefix; //数据表前缀
protected $class_name; //本类的名称
protected $db; //Mysql Server连接
private $params; //config数据库信息
protected $table_field;//数据表字段信息
protected $myrow; //返回数据
protected $order; //数据列表的排序,用于 ORDER BY
protected $quto_content;//函数quote_into()所用来暂存数据的
protected static $instance; //用于构建类的singleton模式参数
protected $cache_dir; //缓存文件目录
protected $cache; //缓存开启开关
//构造函数,用于初始化运行环境
public function __construct() {
global $config; //导入数据库连接信息
if (!$this->db) {
if (!$this->params) {
$this->params = $config;
}
$this->db = Db_Mysqli::getInstance($this->params);
}
$this->cache = 1;
return true;
}
// +---------------------------------------------------
// | 第一部分: 数据表信息处理
// +---------------------------------------------------
//清除变量,$string参数为$this->string对象型
protected function clear($string) {
if($string) {
unset($string);
}
}
//加载$this->table_name
protected function parse_table_name() {
if(!$this->table_name) {
$this->get_table_name();
}
return $this->table_name;
}
//加载$this->tabl_info
protected function parse_table_info() {
if(!$this->table_info) {
$this->get_table_info();
}
return $this->table_info;
}
//加载$this->primary_key
protected function parse_table_primarykey() {
if(!$this->primary_key) {
$this->get_table_primarykey();
}
return $this->primary_key;
}
//加载$this->table_field
protected function parse_table_field() {
if(!$this->table_field) {
$this->get_table_field();
}
return $this->table_field;
}
//获取类名
protected function get_class_name() {
if (!$this->class_name) {
$class_name = get_class($this);
$class_name= strtolower($class_name);
$this->class_name = $class_name;
}
return $this->class_name;
}
//获取数据表前缀
protected function get_table_prefix() {
if (!$this->table_prefix) {
$this->table_prefix = (!empty($this->params['prefix'])) ? $this->params['prefix'] : '';
}
return $this->table_prefix;
}
//获取数据表信息
protected function get_table_info() {
$this->parse_table_name();
$sql="SHOW FIELDS FROM {$this->table_name}";
$this->table_info = $this->db->get_array($sql);
return $this->table_info;
}
//获取数据表名
protected function get_table_name() {
$this->get_class_name();
$this->get_table_prefix();
$this->table_name = (!empty($this->table_prefix)) ? $this->table_prefix.substr($this->class_name,0,-5) : substr($this->class_name,0,-5);
return $this->table_name;
}
//cache_file文件生成
protected function parse_cache_file($name) {
if(!$this->cache_dir) {
$this->cache_dir = './Data/';
}
$this->parse_table_name();
$cache_file = $this->cache_dir.$this->table_name.'_'.$name.'.data.php';
return $cache_file;
}
//生成缓存文件
protected function create_cache($name, $data) {
$cache_file = $this->parse_cache_file($name);
$content = '<?php ';
$content .= '
.$name.'_cache = ';
$content .= var_export($data,true).';';
$content .= ' ?>';
//判断cache_dir是否存在,不存在则建立目录
if (!is_dir($this->cache_dir)) {
mkdir($this->cache_dir,'0777');
}
file_put_contents($cache_file,$content,LOCK_EX);
return true;
}
//加载缓存文件
protected function load_cache($name) {
$cache_file = $this->parse_cache_file($name);
include($cache_file);
return ${$name.'_cache'};
}
//获取数据表的主键
protected function get_table_primarykey() {
if ( $this->cache && file_exists( $this->parse_cache_file('primarykey') ) ) {
$this->primary_key = $this->load_cache('primarykey');
}
else {
$this->parse_table_info();
foreach ($this->table_info as $val) {
if ($val['Key']=='PRI') {
$this->primary_key = $val['Field'];
}
}
$this->create_cache('primarykey',$this->primary_key);
}
return $this->primary_key;
}
//获取数据表字段信息
public function get_table_field() {
if ( $this->cache && file_exists( $this->parse_cache_file('field') ) ) {
$this->table_field = $this->load_cache('field');
}
else {
$this->parse_table_info();
$fields = array();
foreach ($this->table_info as $val) {
$fields[] = $val['Field'];
}
$this->table_field = $fields;
$this->create_cache('field',$this->table_field);
}
return $this->table_field;
}
// +---------------------------------------------------
// | 第二部分: Select SQL 语句处理
// +---------------------------------------------------
//from('数据表','查询字段')用于处理 SELECT fields FROM table之类的SQL语句部分
public function from($name, $item=false) {
if (!empty($name)) {
$table_str = $this->parse_options($name);
$item_str = ($item==true) ? $this->parse_options($item) : '*';
$this->clear($this->option['from']);
$this->option['from'] = 'SELECT '.$item_str.' FROM '.$table_str;
return $this;
}
else {
return false;
}
}
//where('查询条件')用于处理 WHERE id=0531 诸如此类的SQL语句部分,注:当参数中含字符串时应先用quote_into()进行转义
public function where($string) {
if (!empty($string)) {
$where_str = $this->parse_options($string,true);
$this->option['where'] .= ($this->option['where']) ? ' AND '.$where_str : ' WHERE '.$where_str;
return $this;
}
else {
return false;
}
}
//or_where('查询条件')用于处理 OR WHERE id=0531 诸如此类的SQL语句部分,注:当参数中含字符串时应先用quote_into()进行转义
public function orwhere($string) {
if (!empty($string)) {
$or_where_str = $this->parse_options($string,true);
$this->option['or_where'] .= ($this->option['or_where']) ? ' AND '.$or_where_str : ' OR '.$or_where_str;
return $this;
}
else {
return false;
}
}
//order('排列条件')用于处理 ORDER BY post_id ASC 之类的SQL语句部分,注:当参数中含字符串时应先用quote_into()进行转义
public function order($string) {
if (!empty($string)) {
$order_str = $this->parse_options($string);
$this->option['order'] .= ($this->option['order']) ? ' AND '.$order_str : ' ORDER BY '.$order_str;
return $this;
}
else {
return false;
}
}
//limit(10,20)用于处理LIMIT 10, 20之类的SQL语句部分
public function limit($num1, $num2) {
if(is_int($num1)) {
$num1 = trim($num1);
$num2 = (is_int($num2)) ? trim($num2) : '';
$limit_str = $num2 ? $num1.', '.$num2 : $num1;
$this->clear($this->option['limit']);
$this->option['limit'] = ' LIMIT '.$limit_str;
return $this;
}
else {
return false;
}
}
//处理from(),where(),order()等函数参数,特别是对参数为数组的处理
protected function parse_options($string, $info=false) {
if (is_array($string)) {
$option_str = '';
if ($info==true) {
foreach ($string as $val) {
$option_str .= ' '.trim($val).' AND';
}
$option_str = substr($option_str,0,-3);
}
else {
foreach ($string as $val) {
$option_str .= ' '.trim($val).',';
}
$option_str = substr($option_str,0,-1);
}
}
else {
$option_str = trim($string);
}
return $option_str;
}
//引用参数,用来完成对某个参数加上单引号,方便SQL语句执行,用法:$this->quote_into('id=?', '5');
public function quote_into($string, $param) {
if (!empty($string) && !empty($param)) {
$string = str_replace('?', '\''.trim($param).'\'', $string);
$this->clear($this->quto_content);
$this->quto_content = $string;
return $this->quto_content;
}
else {
return false;
}
}
//组装SQL语句并完成查询,并返回查询结果,用法$this->select();
public function select() {
if ($this->option['from']) {
$sql=$this->option['from'];
if ($this->option['where']) {
$sql .= $this->option['where'];
if ($this->option['or_where']) {
$sql .= $this->option['or_where'];
unset($this->option['or_where']);
}
unset($this->option['where']);
}
if ($this->option['order']) {
$sql .=$this->option['order'];
unset($this->option['order']);
}
if ($this->option['limit']) {
$sql .= $this->option['limit'];
}
$this->clear($this->myrow);
$this->myrow = $this->db->get_array($sql);
return $this->myrow;
}
else {
return false;
}
}
// +---------------------------------------------------
// | 第三部分: Insert, Update, Delete, Find
// +---------------------------------------------------
//根据主键,获取某个主键的一行信息,主键可以类内设置
public function find($id) {
if (!empty($id)) {
$this->parse_table_primarykey();
$this->parse_table_name();
if (!$this->order) {
$this->order = 'DESC';
}
else {
$this->order = (in_array(strtoupper($this->order), array('ASC','DESC'))) ? strtoupper($this->order) : 'DESC';
}
$sql = 'SELECT * FROM '.$this->table_name.' WHERE '.$this->primary_key;
if (is_array($id)) {
$values = $this->parse_options($id);
$sql .= ' IN ('.$values.')';
}
else {
$sql .= ' = '.trim($id);
}
$sql .= ' ORDER BY '.$this->primary_key.' '.$this->order;
$this->clear($this->myrow);
$this->myrow = is_array($id) ? $this->db->get_array($sql) : $this->db->fetch_row($sql);
return $this->myrow;
}
else {
return false;
}
}
//根据主键信息,获取数据表全部信息
public function findAll() {
$this->parse_table_primarykey();
$this->parse_table_name();
if (!$this->order) {
$this->order = 'DESC';
}else {
$this->order = (in_array(strtoupper($this->order), array('ASC','DESC'))) ? strtoupper($this->order) : 'DESC';
}
$sql = 'SELECT * FROM '.$this->table_name.' ORDER BY '.$this->primary_key.' '.$this->order;
$this->clear($this->myrow);
$this->myrow = $this->db->get_array($sql);
return $this->myrow;
}
//根据某一条件,获取一行信息(字段型),注:只是一行信息
public function fetchRow($where) {
if (!empty($where)) {
$this->parse_table_name();
$sql = 'SELECT * FROM '.$this->table_name;
//处理where SQL语句
$this->clear($this->option['where']);
$this->where($where);
$sql .= $this->option['where'];
unset($this->option['where']);
$this->clear($this->myrow);
$this->myrow = $this->db->fetch_row($sql);
$this->myrow = (Object)$this->myrow;
//return $this->myrow;
return $this;
}
else {
return false;
}
}
//新建一行数据,对象型的
public function createRow() {
$this->clear($this->myrow);
$this->myrow = (object)$this->myrow;
return $this;
}
//向数据表写入一行信息
public function insert($content) {
if (is_array($content)) {
$this->parse_table_name();
$this->parse_table_field();
$field_str = '';
$content_str = '';
$key_arr = array_keys($content);
//处理所要写入内容的数组的 values 与数据表字段对应顺序
foreach ($this->table_field as $val) {
$field_str .= ' '.$val.',';
if (in_array($val, $key_arr)) {
$content_str .= ' \''.$content[$val].'\',';
}
else {
$content_str .=' NULL,';
}
}
$field_str = substr($field_str, 0, -1);
$content_str = substr($content_str, 0, -1);
$sql = 'INSERT INTO '.$this->table_name.'('.$field_str.' )'.' VALUES ('.$content_str.')';
$this->db->query($sql);
return true;
}
else {
return false;
}
}
//删除符合一定条件的行数据,注:如果$where中含有字符串,应用$this->qutote_into()进行转义
public function delete($where) {
$this->parse_table_name();
$this->clear($this->option['where']);
$this->where($where);
$sql = 'DELETE FROM '.$this->table_name.$this->option['where'];
$this->db->query($sql);
return true;
}
//更新一行信息
public function update($content,$where) {
if (is_array($content)) {
$this->parse_table_name();
$this->parse_table_field();
$content_str = '';
foreach ($content as $key=>$val) {
if (in_array($key, $this->table_field)) {
$content_str .= $key.' = \''.$val.'\',';
}
}
$content_str = substr($content_str, 0, -1);
$sql = 'UPDATE '.$this->table_name.' SET '.$content_str;
$this->clear($this->option['where']);
$this->where($where);
$sql .= $this->option['where'];
unset($this->option['where']);
$this->db->query($sql);
return true;
}
else {
return false;
}
}
//保存数据,实质函数为:update,没有ID时则insett,注:对数为对象型
public function save() {
if (is_object($this->myrow)) {
$myrow = (array)$this->myrow;
unset($this->myrow);
$key_arr = array_keys($myrow);
$this->parse_table_primarykey();
if(in_array($this->primary_key, $key_arr)) {
$where = $this->quote_into($this->primary_key.'=?',$myrow[$this->primary_key]);
unset($myrow[$this->primary_key]);
$this->update($myrow,$where);
}
else {
$this->insert($myrow);
}
return true;
}
else {
return false;
}
}
// +---------------------------------------------------
// | 第四部分: __SET(), __DESTRUCT(), __CALL()
// +---------------------------------------------------
//根据一个SQL语句获取执行后的全部数据库(字段型)
public function execute ($sql) {
if (!empty($sql)) {
$this->clear($this->myrow);
$this->myrow = $this->db->get_array($sql);
return $this->myrow;
}
else {
return false;
}
}
//对类内受保护对象进行赋值
public function __set($key, $val) {
if (is_object($this->myrow)) {
return $this->myrow->$key = $val;
}
else {
if(in_array($key, array('cache','order','table_name','primary_key','cache_dir'))) {
return $this->$key = $val;
}
else {
return false;
}
}
}
//获得类内一个受保护数据
public function __get($val) {
if ($val) {
return $this->$val;
}
else {
return false;
}
}
//析构函数,用于类内程序运行结束后,打扫战场
public function __destruct() {
$this->clear($this->params);
$this->clear($this->option['where']);
$this->clear($this->option['or_where']);
$this->clear($this->option['order']);
$this->clear($this->myrow);
}
//处理当类外调用不存在的方法
public function __call($method,array $args) {
echo 'Class Mode method '.$method.' is not exists!<br>The args is:<br>';
foreach ($args as $val) {
echo $val.'<br>';
}
}
//用于构建本类的singleton设计模式
public static function getinstance() {
if (self::$instance == null) {
self::$instance = new Model();
}
return self::$instance;
}
//直接调用函数,输出内容
public function __toString() {
if($this->option) {
$sql = $this->option['from'].$this->option['where'].$this->option['or_where'].$this->option['order'].$this->option['limit'];
return (string)$sql;
}
else {
return (string)'This is Model Class!';
}
}
}
?>[/code] |