【源码】一个商业化的数据访问层的实现
时间:2006-03-14 来源:javalbt
自己做过几个PHP的私活,一直采用这个商业化的数据访问模块,效率非常可以。使用它后,我们不用考虑具体的数据访问层的实现了,只需要在自己的DAO实现层使用"$rs = $dataAccess->doSelect($con, $sql);"便可以执行任何数据库访问了。支持MySql, MS Sql server, Oracle, PostgreSQL等主流数据库,采用Pear DB连接。
好东西拿出来给大家一起共享!
记得,首先把这段代码加到自己的常量定义模块中:
define("COMMON_DB_USER", "JDWY"); //数据库用户
define("COMMON_DB_PASSWORD","JDWY"); //数据库密码
define("COMMON_DB_NAME", ""); //数据库名
define("COMMON_DB_TYPE", "oci8");//数据库类型: MySQL="mysql", PostgreSQL="pgsql", MS SQL-Server="mssql", ODBC="odbc"
define("COMMON_DB_HOST", ""); //主机
DBAccess.php实现:
require_once("DB.php");
class DataAccess {
/** DB User*/
var $dbUser = COMMON_DB_USER;
/** DB Password */
var $dbPass = COMMON_DB_PASSWORD;
/** DB Host */
var $dbHost = COMMON_DB_HOST;
/** DB Name */
var $dbName = COMMON_DB_NAME;
/** DB Type */
var $dbType = COMMON_DB_TYPE;
/** Log */
var $log;
/** Debug */
var $Debug=JDWY_ERROR_DEBUG;
var $CurrentErrorCode="";
var $CurrentErrorMsg ="";
var $CurrentSQL ="";
/**
* Construstor
*
* @return void
*/
function DataAccess() {
$this->log = new CustomLog(__CLASS__);
}
/**
* Show Debug Info
*
* @return void
*/
function debugErrorMsg($con,$sql_or_dsn) {
$errmsg="";
$this->CurrentSQL=$sql_or_dsn;
if(is_a ($con, "db_pgsql")) {
$nativeErrMsg = $con->errorNative();
$errcode = $con->errorCode($nativeErrMsg);
$errmsg.="Error Occured For Following SQL Operation:
";
$errmsg.="$sql_or_dsn
";
$errmsg.="Error Message=$nativeErrMsg
";
$errmsg.="Error Code =$errcode
";
$this->CurrentErrorCode=$this->parseError($nativeErrMsg);
$this->CurrentErrorMsg =$nativeErrMsg;
$this->log->error("\r\n".$sql_or_dsn."\r\n".$nativeErrMsg);
}elseif(is_a ($con, "db_error")){
$errmsg.="Error Occured While Connection To:$sql_or_dsn
";
$errmsg.="".ERR_COMMON_DBSERVER_CONNECT_FAILED."
";
$this->CurrentErrorCode='PBMS_ERR_DBSERVER_CONNECT_ERROR';
$this->CurrentErrorMsg ='Unable To Connect to DB Server';
$this->log->error("\r\n".ERR_COMMON_DBSERVER_CONNECT_FAILED.":$sql_or_dsn");
}else {
$errmsg.="Unknown Error Occured!
";
$this->CurrentErrorCode='EGSB_ERR_COMMON_UNKNOWN';
$this->CurrentErrorMsg ="Unknown Error";
$this->log->error("\r\n".$errmsg);
}
if($this->Debug) {
print("------------------------------------------------------------
");
print $errmsg;
print("------------------------------------------------------------
");
}
}
/**
* Parse Error Code
* @param String $error_msg
* @return Boolean $error_code
*/
function parseError($error_msg) {
$error_code="PBMS_ERR_COMMON_DATABASE";
if (preg_match ("/duplicate key violates unique constraint/i", $error_msg)) {
$error_code= "PBMS_ERR_DUPLICATE_KEY";
} elseif (preg_match ("/violates foreign key constraint/i", $error_msg)) {
$error_code= "PBMS_ERR_FOREIGN_KEY";
} elseif (preg_match ("/duplicate key violates unique constraint/i", $error_msg)) {
$error_code= "PBMS_ERR_UNIQUE_KEY";
}else {
}
return $error_code;
}
/**
*
* @return void
*/
function getConnection() {
$dsn = "$this->dbType://$this->dbUser:$this->dbPass@$this->dbHost/$this->dbName";
$con = DB::connect($dsn);
if (DB::isError($con)) {
$this->debugErrorMsg($con,$dsn);
exit;
}
return $con;
}
/**
*
* @return void
*/
function closeConnection($con) {
$con->disconnect();
}
/**
*
* @param String $sql
* @return Boolean $retval
*/
function doQuery($con, $sql) {
@$result = $con->query($sql); // select
if (DB::isError($result)) {
$this->debugErrorMsg($con,$sql);
return false;
}
return $result;
}
/**
*
* @param String $sql
* @return Boolean $retval
*/
function doTransaction($con, $sql) {
//$this->log->debug($sql);
@$result = $con->query($sql);
if (DB::isError($result)) {
$this->debugErrorMsg($con,$sql);
$this->execRollback($con);
return false;
}
return true;
}
/**
*
* @param String $sql
* @return Array DB
*/
function doSelect($con, $sql = "") {
if ($sql == "") {
if($this->Debug) {
print("SQL Is Empty
");
}
exit;
}
$rs = array();
$result = $this->doQuery($con, $sql);
if ($result){
$i = 0;
while ($row = $result->fetchRow(DB_FETCHMODE_ORDERED)) {
$rs[$i] = $row;
$i++;
}
$result->free();
}
return($rs);
}
/**
*
* @param String $sql
* @return String $str
*/
function addQ($con, $str) {
if ($str != "NULL"){
$str = (string)$str;
$str = trim($str);
$str = $con->quote($str);
}
return($str);
}
/**
*
* @return Boolean $retval
*/
function execBegin($con) {
$sql = $this->tranBEGIN();
if ($sql){
@$result = $con->query($sql);
if (DB::isError($result)) {
$this->debugErrorMsg($con,$sql);
return false;
}
}
return true;
}
/**
*
* @return Boolean $retval
*/
function execCommit($con) {
$sql = $this->tranCOMMIT();
if ($sql){
@$result = $con->query($sql);
if (DB::isError($result)) {
$this->debugErrorMsg($con,$sql);
return false;
}
}
return true;
}
/**
*
* @return Boolean $retval
*/
function execRollback($con) {
$sql = $this->tranROLLBACK();
if ($sql){
@$result = $con->query($sql);
if (DB::isError($result)) {
$this->debugErrorMsg($con,$sql);
return false;
}
}
return true;
}
/**
*
* @param String $target
* @param String $value
* @return String $yearmonth
*/
function truncDateSQL($target,$value) {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "DATE_TRUNC('month',$target)='$value'";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%Y%m')='".substr($value,0,6)."'";
}
}
/**
*
* @param String $target
* @param String $value
* @return String $year
*/
function truncMonthSQL($target,$value) {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "DATE_TRUNC('year',$target)='$value'";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%Y')='".substr($value,0,4)."'";
}
}
/**
*
* @param String $target
* @return String $yyyy/mm/dd
*/
function truncTime($target) {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "TO_CHAR(DATE_TRUNC('day',$target),'yyyy/mm/dd')";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%Y/%m/%d')";
}
}
/**
*
* @param String $target
* @return String $yyyy/mm
*/
function truncDate($target) {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "TO_CHAR(DATE_TRUNC('month',$target),'yyyy/mm')";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%Y/%m')";
}
}
/**
*
* @param String $target
* @param String $value1
* @param String $value2
* @return String $yearterm
*/
function betweenYearSQL($target, $value1, $value2) {
if ($value1> $value2){
$datemax = $value1;
$datemin = $value2;
} else {
$datemax = $value2;
$datemin = $value1;
}
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "DATE_TRUNC('year',$target) BETWEEN '" .$datemin. "/01/01' AND '" . $datemax. "/01/01'";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%Y') BETWEEN '" .$datemin. "' AND '" . $datemax. "'";
}
}
/**
*
* @param String $target
* @param String $value
* @return String $yearmonth
*/
function whereDateFormat($target, $value) {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "DATE_TRUNC('day',$target)='$value'";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%Y/%m/%d')='".substr($value,0,10)."'";
}
}
/**
*
* @param String $target
* @param String $value1
* @param String $value2
* @return String $yearmonth
*/
function whereBetweenDateFormat($target, $value1, $value2) {
if ($value1> $value2){
$datemax = $value1;
$datemin = $value2;
} else {
$datemax = $value2;
$datemin = $value1;
}
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "DATE_TRUNC('day',$target)='$datemin'";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%Y/%m/%d')='".substr($datemin,0,10)."'";
}
}
/**
*
* @param String $target
* @return String $hour
*/
function hourColFormat($target) {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "TO_CHAR(DATE_TRUNC('minute',$target),'HH24')";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%H')";
}
}
/**
*
* @param String $target
* @return String $hourminute
*/
function minuteColFormat($target) {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "TO_CHAR(DATE_TRUNC('minute',$target),'MI')";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%i')";
}
}
/**
*
* @param String $table
* @param String $column
* @return String $hourminute
*/
function insertID($table, $column) {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "CURRVAL('" .$table. "_" .$column. "_seq')";
}elseif (COMMON_DB_ID == COMMON_DB_ID_ORACLE){
return $table. "_" .$column. "_seq.CURRVAL";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "last_insert_id()";
}
}
/**
* SQL LIMIT
*
* @param String $start
* @param String $count
* @return String $ret SQL LIMIT
*/
function limit($start="", $count="1") {
$ret = "";
switch(COMMON_DB_ID) {
case COMMON_DB_ID_POSTGRESQL:
$limit = !empty($start) ? " OFFSET ".$start : "";
$ret = "LIMIT $count $limit;";
break;
case COMMON_DB_ID_MYSQL:
$limit = !empty($start) ? $start."," : "";
$ret = "LIMIT $limit $count;";
break;
case COMMON_DB_ID_ORACLE:
break;
default:
}
return $ret;
}
/**
* ORACLE LIMIT
*
*
* @param String $start
* @param String $count
* @param Recordset $rsObj
* @return void
*/
function limit2($start="1", $count="1", &$rsObj) {
switch(COMMON_DB_ID) {
case COMMON_DB_ID_ORACLE:
$rsbuf = array();
$i = 1;
foreach ($rsObj as $row){
if ($i >= $start)
$rsbuf[] = $row;
if ($i >= ($start +$count-1))
break;
$i++;
}
$rsObj = $rsbuf;
break;
default:
}
}
/**
*
* @param String $str
* @return String
*/
function blank2Null($str) {
if (!isset($str) || $str=="" || $str=="''" ||$str=='""') return "NULL";
return escapeshellcmd($str);
}
/**
*
* @return String $begin
*/
function tranBEGIN() {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "BEGIN;";
}elseif (COMMON_DB_ID == COMMON_DB_ID_ORACLE){
return "";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "BEGIN;";
}
}
/**
*
* @return String $begin
*/
function tranCOMMIT() {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "COMMIT;";
}elseif (COMMON_DB_ID == COMMON_DB_ID_ORACLE){
return "COMMIT;";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "COMMIT;";
}
}
/**
*
* @return String $begin
*/
function tranROLLBACK() {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "ROLLBACK;";
}elseif (COMMON_DB_ID == COMMON_DB_ID_ORACLE){
return "ROLLBACK;";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "ROLLBACK;";
}
}
function blank2Zone($str) {
if (!isset($str) || $str=="" || $str=="''" ||$str=='""') return "0";
return escapeshellcmd($str);
}
}
?>
好东西拿出来给大家一起共享!
记得,首先把这段代码加到自己的常量定义模块中:
define("COMMON_DB_USER", "JDWY"); //数据库用户
define("COMMON_DB_PASSWORD","JDWY"); //数据库密码
define("COMMON_DB_NAME", ""); //数据库名
define("COMMON_DB_TYPE", "oci8");//数据库类型: MySQL="mysql", PostgreSQL="pgsql", MS SQL-Server="mssql", ODBC="odbc"
define("COMMON_DB_HOST", ""); //主机
DBAccess.php实现:
require_once("DB.php");
class DataAccess {
/** DB User*/
var $dbUser = COMMON_DB_USER;
/** DB Password */
var $dbPass = COMMON_DB_PASSWORD;
/** DB Host */
var $dbHost = COMMON_DB_HOST;
/** DB Name */
var $dbName = COMMON_DB_NAME;
/** DB Type */
var $dbType = COMMON_DB_TYPE;
/** Log */
var $log;
/** Debug */
var $Debug=JDWY_ERROR_DEBUG;
var $CurrentErrorCode="";
var $CurrentErrorMsg ="";
var $CurrentSQL ="";
/**
* Construstor
*
* @return void
*/
function DataAccess() {
$this->log = new CustomLog(__CLASS__);
}
/**
* Show Debug Info
*
* @return void
*/
function debugErrorMsg($con,$sql_or_dsn) {
$errmsg="";
$this->CurrentSQL=$sql_or_dsn;
if(is_a ($con, "db_pgsql")) {
$nativeErrMsg = $con->errorNative();
$errcode = $con->errorCode($nativeErrMsg);
$errmsg.="Error Occured For Following SQL Operation:
";
$errmsg.="$sql_or_dsn
";
$errmsg.="Error Message=$nativeErrMsg
";
$errmsg.="Error Code =$errcode
";
$this->CurrentErrorCode=$this->parseError($nativeErrMsg);
$this->CurrentErrorMsg =$nativeErrMsg;
$this->log->error("\r\n".$sql_or_dsn."\r\n".$nativeErrMsg);
}elseif(is_a ($con, "db_error")){
$errmsg.="Error Occured While Connection To:$sql_or_dsn
";
$errmsg.="".ERR_COMMON_DBSERVER_CONNECT_FAILED."
";
$this->CurrentErrorCode='PBMS_ERR_DBSERVER_CONNECT_ERROR';
$this->CurrentErrorMsg ='Unable To Connect to DB Server';
$this->log->error("\r\n".ERR_COMMON_DBSERVER_CONNECT_FAILED.":$sql_or_dsn");
}else {
$errmsg.="Unknown Error Occured!
";
$this->CurrentErrorCode='EGSB_ERR_COMMON_UNKNOWN';
$this->CurrentErrorMsg ="Unknown Error";
$this->log->error("\r\n".$errmsg);
}
if($this->Debug) {
print("------------------------------------------------------------
");
print $errmsg;
print("------------------------------------------------------------
");
}
}
/**
* Parse Error Code
* @param String $error_msg
* @return Boolean $error_code
*/
function parseError($error_msg) {
$error_code="PBMS_ERR_COMMON_DATABASE";
if (preg_match ("/duplicate key violates unique constraint/i", $error_msg)) {
$error_code= "PBMS_ERR_DUPLICATE_KEY";
} elseif (preg_match ("/violates foreign key constraint/i", $error_msg)) {
$error_code= "PBMS_ERR_FOREIGN_KEY";
} elseif (preg_match ("/duplicate key violates unique constraint/i", $error_msg)) {
$error_code= "PBMS_ERR_UNIQUE_KEY";
}else {
}
return $error_code;
}
/**
*
* @return void
*/
function getConnection() {
$dsn = "$this->dbType://$this->dbUser:$this->dbPass@$this->dbHost/$this->dbName";
$con = DB::connect($dsn);
if (DB::isError($con)) {
$this->debugErrorMsg($con,$dsn);
exit;
}
return $con;
}
/**
*
* @return void
*/
function closeConnection($con) {
$con->disconnect();
}
/**
*
* @param String $sql
* @return Boolean $retval
*/
function doQuery($con, $sql) {
@$result = $con->query($sql); // select
if (DB::isError($result)) {
$this->debugErrorMsg($con,$sql);
return false;
}
return $result;
}
/**
*
* @param String $sql
* @return Boolean $retval
*/
function doTransaction($con, $sql) {
//$this->log->debug($sql);
@$result = $con->query($sql);
if (DB::isError($result)) {
$this->debugErrorMsg($con,$sql);
$this->execRollback($con);
return false;
}
return true;
}
/**
*
* @param String $sql
* @return Array DB
*/
function doSelect($con, $sql = "") {
if ($sql == "") {
if($this->Debug) {
print("SQL Is Empty
");
}
exit;
}
$rs = array();
$result = $this->doQuery($con, $sql);
if ($result){
$i = 0;
while ($row = $result->fetchRow(DB_FETCHMODE_ORDERED)) {
$rs[$i] = $row;
$i++;
}
$result->free();
}
return($rs);
}
/**
*
* @param String $sql
* @return String $str
*/
function addQ($con, $str) {
if ($str != "NULL"){
$str = (string)$str;
$str = trim($str);
$str = $con->quote($str);
}
return($str);
}
/**
*
* @return Boolean $retval
*/
function execBegin($con) {
$sql = $this->tranBEGIN();
if ($sql){
@$result = $con->query($sql);
if (DB::isError($result)) {
$this->debugErrorMsg($con,$sql);
return false;
}
}
return true;
}
/**
*
* @return Boolean $retval
*/
function execCommit($con) {
$sql = $this->tranCOMMIT();
if ($sql){
@$result = $con->query($sql);
if (DB::isError($result)) {
$this->debugErrorMsg($con,$sql);
return false;
}
}
return true;
}
/**
*
* @return Boolean $retval
*/
function execRollback($con) {
$sql = $this->tranROLLBACK();
if ($sql){
@$result = $con->query($sql);
if (DB::isError($result)) {
$this->debugErrorMsg($con,$sql);
return false;
}
}
return true;
}
/**
*
* @param String $target
* @param String $value
* @return String $yearmonth
*/
function truncDateSQL($target,$value) {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "DATE_TRUNC('month',$target)='$value'";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%Y%m')='".substr($value,0,6)."'";
}
}
/**
*
* @param String $target
* @param String $value
* @return String $year
*/
function truncMonthSQL($target,$value) {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "DATE_TRUNC('year',$target)='$value'";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%Y')='".substr($value,0,4)."'";
}
}
/**
*
* @param String $target
* @return String $yyyy/mm/dd
*/
function truncTime($target) {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "TO_CHAR(DATE_TRUNC('day',$target),'yyyy/mm/dd')";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%Y/%m/%d')";
}
}
/**
*
* @param String $target
* @return String $yyyy/mm
*/
function truncDate($target) {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "TO_CHAR(DATE_TRUNC('month',$target),'yyyy/mm')";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%Y/%m')";
}
}
/**
*
* @param String $target
* @param String $value1
* @param String $value2
* @return String $yearterm
*/
function betweenYearSQL($target, $value1, $value2) {
if ($value1> $value2){
$datemax = $value1;
$datemin = $value2;
} else {
$datemax = $value2;
$datemin = $value1;
}
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "DATE_TRUNC('year',$target) BETWEEN '" .$datemin. "/01/01' AND '" . $datemax. "/01/01'";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%Y') BETWEEN '" .$datemin. "' AND '" . $datemax. "'";
}
}
/**
*
* @param String $target
* @param String $value
* @return String $yearmonth
*/
function whereDateFormat($target, $value) {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "DATE_TRUNC('day',$target)='$value'";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%Y/%m/%d')='".substr($value,0,10)."'";
}
}
/**
*
* @param String $target
* @param String $value1
* @param String $value2
* @return String $yearmonth
*/
function whereBetweenDateFormat($target, $value1, $value2) {
if ($value1> $value2){
$datemax = $value1;
$datemin = $value2;
} else {
$datemax = $value2;
$datemin = $value1;
}
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "DATE_TRUNC('day',$target)='$datemin'";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%Y/%m/%d')='".substr($datemin,0,10)."'";
}
}
/**
*
* @param String $target
* @return String $hour
*/
function hourColFormat($target) {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "TO_CHAR(DATE_TRUNC('minute',$target),'HH24')";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%H')";
}
}
/**
*
* @param String $target
* @return String $hourminute
*/
function minuteColFormat($target) {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "TO_CHAR(DATE_TRUNC('minute',$target),'MI')";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "DATE_FORMAT($target,'%i')";
}
}
/**
*
* @param String $table
* @param String $column
* @return String $hourminute
*/
function insertID($table, $column) {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "CURRVAL('" .$table. "_" .$column. "_seq')";
}elseif (COMMON_DB_ID == COMMON_DB_ID_ORACLE){
return $table. "_" .$column. "_seq.CURRVAL";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "last_insert_id()";
}
}
/**
* SQL LIMIT
*
* @param String $start
* @param String $count
* @return String $ret SQL LIMIT
*/
function limit($start="", $count="1") {
$ret = "";
switch(COMMON_DB_ID) {
case COMMON_DB_ID_POSTGRESQL:
$limit = !empty($start) ? " OFFSET ".$start : "";
$ret = "LIMIT $count $limit;";
break;
case COMMON_DB_ID_MYSQL:
$limit = !empty($start) ? $start."," : "";
$ret = "LIMIT $limit $count;";
break;
case COMMON_DB_ID_ORACLE:
break;
default:
}
return $ret;
}
/**
* ORACLE LIMIT
*
*
* @param String $start
* @param String $count
* @param Recordset $rsObj
* @return void
*/
function limit2($start="1", $count="1", &$rsObj) {
switch(COMMON_DB_ID) {
case COMMON_DB_ID_ORACLE:
$rsbuf = array();
$i = 1;
foreach ($rsObj as $row){
if ($i >= $start)
$rsbuf[] = $row;
if ($i >= ($start +$count-1))
break;
$i++;
}
$rsObj = $rsbuf;
break;
default:
}
}
/**
*
* @param String $str
* @return String
*/
function blank2Null($str) {
if (!isset($str) || $str=="" || $str=="''" ||$str=='""') return "NULL";
return escapeshellcmd($str);
}
/**
*
* @return String $begin
*/
function tranBEGIN() {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "BEGIN;";
}elseif (COMMON_DB_ID == COMMON_DB_ID_ORACLE){
return "";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "BEGIN;";
}
}
/**
*
* @return String $begin
*/
function tranCOMMIT() {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "COMMIT;";
}elseif (COMMON_DB_ID == COMMON_DB_ID_ORACLE){
return "COMMIT;";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "COMMIT;";
}
}
/**
*
* @return String $begin
*/
function tranROLLBACK() {
if (COMMON_DB_ID == COMMON_DB_ID_POSTGRESQL){
return "ROLLBACK;";
}elseif (COMMON_DB_ID == COMMON_DB_ID_ORACLE){
return "ROLLBACK;";
}elseif (COMMON_DB_ID == COMMON_DB_ID_MYSQL){
return "ROLLBACK;";
}
}
function blank2Zone($str) {
if (!isset($str) || $str=="" || $str=="''" ||$str=='""') return "0";
return escapeshellcmd($str);
}
}
?>
相关阅读 更多 +