?? spdol.php
字號:
<?php/********************************************** Please do not remove this comment. Simple PHP Database Object Layer This is an attempt to detach database access operations from business logic code. The aim of design is simple and easy to use. Currently it worked with PHP version > 4.2. Created: Xiiin. 2006.09.16 Last modified: Xiiin. 2006.09.16 current version: 0.25beta**********************************************//*The abstract general database interface class.need to be inherited according to different DBMS.*/class dbi{ var $conn; var $dbhost; var $dbuser; var $dbpass; var $dbname; function dbi(){ //need to be implemented in child class. } function getconn(){ //need to be implemented in child class. } function query(){ //need to be implemented in child class. } function geterror(){ //need to be implemented in child class. } function getnumrows($rs){ //need to be implemented in child class. } function getrow($rs){ //need to be implemented in child class. } function close(){ //need to be implemented in child class. } function startta(){ //need to be implemented in child class. } function rollback(){ //need to be implemented in child class. } function commit(){ //need to be implemented in child class. }}/*The concrete dbi class that worked with MySQL version >= 4.1.*/class dbi_mysql extends dbi{ function dbi_mysql($host, $user, $pass, $db){ $this->conn = $this->getconn($host, $user, $pass, $db); if (!$this->conn){ die('can not establish db connection'); } } function getconn($host, $user, $pass, $db){ $conn = mysql_connect($host, $user, $pass); mysql_select_db($db, $conn); return $conn; } function query($sql){ $rs = mysql_query($sql, $this->conn); return $rs; } function geterror(){ return mysql_error($this->conn); } function getnumrows($rs){ return mysql_num_rows($rs); } function getrow($rs){ return mysql_fetch_array($rs); } function close(){ return mysql_close($this->conn); } function startta(){ return mysql_query('start transaction', $this->conn); } function rollback(){ return mysql_query('rollback', $this->conn); } function commit(){ return mysql_query('commit', $this->conn); }}/*The concrete dbi class that worked with PostgreSQL.ps: not completed yet.*/class dbi_postgresql extends dbi{ function dbi_postgresql(){ //TODO } function getconn(){ //TODO } function query(){ //TODO } function geterror(){ //TODO } function getnumrows($rs){ //TODO } function getrow($rs){ //TODO } function close(){ //TODO } function startta(){ //TODO } function rollback(){ //TODO } function commit(){ //TODO }}class table{ var $_tablename_; var $_dbi_; var $_defaultvalue_ = '!_have_no_value_!'; function table($tablename, &$dbi){ $fields = get_object_vars($this); foreach ($fields as $field => $value){ $this->$field = $this->_defaultvalue_; } $this->_tablename_ = $tablename; $this->_dbi_ = $dbi; } function getinstance(){ $classname = get_class($this); $instance = new $classname($this->_tablename_, $this->_dbi_); return $instance; } function getfieldsarray(){ $fields = get_object_vars($this); $array = array(); foreach ($fields as $field => $value){ if (!preg_match('/^!_.*_!$/', $field) && $value !== $this->_defaultvalue_){ $array[$field] = $value; } } return $array; } function cc($value){ //return iconv('utf-8', 'gb2312', $value); return $value; } function query($sql){ $rs = $this->_dbi_->query($sql); if (!$rs){ die('sql error: bad query: ' . $this->_dbi_->geterror()); } $ra = array(); while ($row = $this->_dbi_->getrow($rs)){ $o = $this->getinstance(); foreach ($row as $key => $value){ $key = strtolower($key); $o->$key = $value; } $ra[] = $o; } return $ra; } function select($orderby=-1, $ascdesc=-1, $startpos=-1, $length=-1, $gta=-1, $lta=-1, $neqa=-1){ $array = $this->getfieldsarray(); $sql = "select * from " . $this->_tablename_; if (count($array) != 0){ $sql .= ' where 1'; } foreach ($array as $key => $value){ $sql .= " and " . $key . "='" . $this->cc($value) . "'"; } if ($gta != -1){ foreach ($gta as $key => $value ){ $key = strtolower($key); if (!isset($this->$key)){ die('gt field does not exist in your class'); } $sql .= " and " .$key . ">'" . $value . "'"; } } if ($lta != -1){ foreach ($lta as $key => $value ){ $key = strtolower($key); if (!isset($this->$key)){ die('lt field does not exist in your class'); } $sql .= " and " .$key . "<'" . $value . "'"; } } if ($neqa != -1){ foreach ($neqa as $key => $value ){ $key = strtolower($key); if (!isset($this->$key)){ die('neq field does not exist in your class'); } $sql .= " and " .$key . "<>'" . $value . "'"; } } $orderby = strtolower($orderby); if ($orderby != -1){ if (!isset($this->$orderby)){ die('order by field does not exist in your class'); } $sql .= " order by " . $orderby; if ($ascdesc == 0){ $sql .= ' asc'; }else{ $sql .= ' desc'; } } if ($startpos != -1){ if (!is_a($this->_dbi_, 'dbi_mysql')){ die('your dbms does not support limited select operation.'); } $sql .= ' limit '.$startpos; if ($length != -1){ $sql .= ','.$length; } } $rs = $this->_dbi_->query($sql); if (!$rs){ die('sql error: bad select: ' . $this->_dbi_->geterror()); } $ra = array(); while ($row = $this->_dbi_->getrow($rs)){ $o = $this->getinstance(); foreach ($row as $key => $value){ $key = strtolower($key); $o->$key = $value; } $ra[] = $o; } return $ra; } function selectmax($field){ if (!isset($this->$field)){ die('max field does not exist in your class'); } $sql = "select max(" . $field . ") from " . $this->_tablename_ . " where 1"; $array = $this->getfieldsarray(); foreach ($array as $key => $value){ $sql .= " and " . $key . "='" . $this->cc($value) . "'"; } $rs = $this->_dbi_->query($sql); if (!$rs){ die('sql error: bad selectmax: ' . $this->_dbi_->geterror()); } $row = $this->_dbi_->getrow($rs); return $row[0]; } function selectmin($field){ if (!isset($this->$field)){ die('min field does not exist in your class'); } $sql = "select min(" . $field . ") from " . $this->_tablename_ . " where 1"; $array = $this->getfieldsarray(); foreach ($array as $key => $value){ $sql .= " and " . $key . "='" . $this->cc($value) . "'"; } $rs = $this->_dbi_->query($sql); if (!$rs){ die('sql error: bad selectmin: ' . $this->_dbi_->geterror()); } $row = $this->_dbi_->getrow($rs); return $row[0]; } function selectsum($field){ if (!isset($this->$field)){ die('sum field does not exist in your class'); } $sql = "select sum(" . $field . ") from " . $this->_tablename_ . " where 1"; $array = $this->getfieldsarray(); foreach ($array as $key => $value){ $sql .= " and " . $key . "='" . $this->cc($value) . "'"; } $rs = $this->_dbi_->query($sql); if (!$rs){ die('sql error: bad selectsum: ' . $this->_dbi_->geterror()); } $row = $this->_dbi_getrow($rs); return $row[0]; } function update($b){ if (count($b) == 0){ die('sql error: bad update parameter'); } $array = $this->getfieldsarray(); $sql = "update " . $this->_tablename_ . " set "; foreach ($b as $key => $value){ $sql .= strtolower($key) . "='" . $value . "',"; } $sql = substr($sql, 0, strlen($sql) - 1); $sql .= " where 1"; foreach ($array as $key => $value){ $sql .= " and " . $key . "='" . $this->cc($value) . "'"; } $rs = $this->_dbi_->query($sql); if (!$rs){ die('sql error: bad insert: '. $this->_dbi_->geterror()); } return $rs; } function delete(){ $array = $this->getfieldsarray(); $sql = "delete from " . $this->_tablename_ . " where 1"; foreach ($array as $key => $value){ $sql .= " and " . $key . "='" . $this->cc($value) . "'"; } $rs = $this->_dbi_->query($sql); if (!$rs){ die('sql error: bad delete: ' . $this->_dbi_->geterror()); } return $rs; } function insert(){ $array = $this->getfieldsarray(); if (count($array) == 0){ die("sql error: bad insert query"); } $tmpsql1 = "insert into " . $this->_tablename_ . "("; $tmpsql2 = '('; foreach ($array as $key => $value){ $tmpsql1 .= $key . ","; $tmpsql2 .= "'" . $value . "',"; } $tmpsql1 = substr($tmpsql1, 0, strlen($tmpsql1) - 1); $tmpsql1 .= ")values"; $tmpsql2 = substr($tmpsql2, 0, strlen($tmpsql2) - 1); $tmpsql2 .= ')'; $sql = $tmpsql1 . $tmpsql2; $rs = $this->_dbi_->query($sql); if (!$rs){ die('sql error: bad insert: ' . $this->_dbi_->geterror()); } return $rs; } function numrows($gta=-1, $lta=-1, $neqa=-1){ $array = $this->getfieldsarray(); $sql = "select count(*) from " . $this->_tablename_; if (count($array) != 0){ $sql .= ' where 1'; } foreach ($array as $key => $value){ $sql .= " and " . $key . "='" . $this->cc($value) . "'"; } if ($gta != -1){ foreach ($gta as $key => $value ){ $key = strtolower($key); if (!isset($this->$key)){ die('gt field does not exist in your class'); } $sql .= " and " .$key . ">'" . $value . "'"; } } if ($lta != -1){ foreach ($lta as $key => $value ){ $key = strtolower($key); if (!isset($this->$key)){ die('lt field does not exist in your class'); } $sql .= " and " .$key . "<'" . $value . "'"; } } if ($neqa != -1){ foreach ($neqa as $key => $value ){ $key = strtolower($key); if (!isset($this->$key)){ die('neq field does not exist in your class'); } $sql .= " and " .$key . "<>'" . $value . "'"; } } $rs = $this->_dbi_->query($sql); if (!$rs){ die('sql error: bad select: ' . $this->_dbi_->geterror()); } $row = $this->_dbi_->getrow($rs); return $row[0]; }}//table類加入numrows()方法,update()方法傳入?yún)?shù)數(shù)組鍵名轉(zhuǎn)換為小寫,強化select()功能,現(xiàn)在可以排序和限量查詢//table類增加selectmax(),selectmin()方法,selectnum()方法,參數(shù)為取max,min的字段名,修正查詢條件字段值不能為0的bug//table類的numrows()方法參數(shù)列表修改為三個數(shù)組,分別為大于,小于和不等于查詢,select()方法,修正selectmax()min()sum()的bug//dbi類加入startts(),rollback(),commit()方法,從而提供對事務(wù)的支持,table類加入query方法。?>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -