<?php
/**
+------------------------------------------------------------------------------
* Framk PHP框架
+------------------------------------------------------------------------------
* @package Framk
* @author shawn fon <shawn.fon@gmail.com>
+------------------------------------------------------------------------------
*/
class Database {
private $db;
private $transTimes;
public
function __construct() {
$host = $GLOBALS[ 'DB' ][ 'DBhost' ];
$name = $GLOBALS[ 'DB' ][ 'DBname' ];
$port = $GLOBALS[ 'DB' ][ 'DBport' ];
$user = $GLOBALS[ 'DB' ][ 'DBuser' ];
$pwd = $GLOBALS[ 'DB' ][ 'DBpsw' ];
$this->db = new PDO( "mysql:host={$host};dbname={$name}", "{$user}", "{$pwd}" );
$this->db->query( "SET NAMES 'UTF8'" );
$this->db->query( "SET TIME_ZONE = '+8:00'" );
}
public
function findAll( $sql ) {
$result = $this->db->query( $sql );
if ( $result ) {
$data = $result->fetchAll( PDO::FETCH_ASSOC );
} else {
_error( 'queryError', '数据表不存在 或SQL语法错误:' . $sql, true );
}
return $data;
}
public
function findOne( $sql ) {
$result = $this->db->query( $sql );
if ( $result ) {
$data = $result->fetch();
} else {
_error( 'queryError', '数据表不存在 或SQL语法错误:' . $sql, true );
}
return $data;
}
public
function countRecords( $sql ) {
$result = $this->db->query( $sql );
if ( $result ) {
return $result->rowCount();
} else {
_error( 'queryError', '数据表不存在 或SQL语法错误:' . $sql, true );
}
}
public
function updt( $sql ) {
$result = $this->db->exec( $sql );
if ( $result ) { $sql = trim( $sql );
$method = strtolower( substr( $sql, 0, 6 ) );
if ( $method == 'insert' || $method == 'replace' ) {
return $this->db->lastInsertId(); } else {
return $result; }
} else {
return false;
}
}
public
function update( $table, $arrayDataValue, $where = '', $debug = false ) {
$this->checkFields( $table, $arrayDataValue );
if ( $where ) {
$strSql = '';
foreach ( $arrayDataValue as $key => $value ) {
$strSql .= ", `$key`='$value'";
}
$strSql = substr( $strSql, 1 );
$strSql = "UPDATE `$table` SET $strSql WHERE $where";
} else {
$strSql = "REPLACE INTO `$table` (`" . implode( '`,`', array_keys( $arrayDataValue ) ) . "`) VALUES ('" . implode( "','", $arrayDataValue ) . "')";
}
if ( $debug === true )$this->debug( $strSql );
$result = $this->db->exec( $strSql );
$this->getPDOError();
return $result;
}
public
function insert( $table, $arrayDataValue, $debug = false ) {
$this->checkFields( $table, $arrayDataValue );
$strSql = "INSERT INTO `$table` (`" . implode( '`,`', array_keys( $arrayDataValue ) ) . "`) VALUES ('" . implode( "','", $arrayDataValue ) . "')";
if ( $debug === true )$this->debug( $strSql );
$result = $this->db->exec( $strSql );
$this->getPDOError();
return $result;
}
public
function replace( $table, $arrayDataValue, $debug = false ) {
$this->checkFields( $table, $arrayDataValue );
$strSql = "REPLACE INTO `$table`(`" . implode( '`,`', array_keys( $arrayDataValue ) ) . "`) VALUES ('" . implode( "','", $arrayDataValue ) . "')";
if ( $debug === true )$this->debug( $strSql );
$result = $this->db->exec( $strSql );
$this->getPDOError();
return $result;
}
public
function delete( $table, $where = '', $debug = false ) {
if ( $where == '' ) {
$this->outputError( "'WHERE' is Null" );
} else {
$strSql = "DELETE FROM `$table` WHERE $where";
if ( $debug === true )$this->debug( $strSql );
$result = $this->db->exec( $strSql );
$this->getPDOError();
return $result;
}
}
public
function execSql( $strSql, $debug = false ) {
if ( $debug === true )$this->debug( $strSql );
$result = $this->db->exec( $strSql );
$this->getPDOError();
return $result;
}
public
function getMaxValue( $table, $field_name, $where = '', $debug = false ) {
$strSql = "SELECT MAX(" . $field_name . ") AS MAX_VALUE FROM $table";
if ( $where != '' )$strSql .= " WHERE $where";
if ( $debug === true )$this->debug( $strSql );
$arrTemp = $this->query( $strSql, 'Row' );
$maxValue = $arrTemp[ "MAX_VALUE" ];
if ( $maxValue == "" || $maxValue == null ) {
$maxValue = 0;
}
return $maxValue;
}
public
function getCount( $table, $field_name, $where = '', $debug = false ) {
$strSql = "SELECT COUNT($field_name) AS NUM FROM $table";
if ( $where != '' )$strSql .= " WHERE $where";
if ( $debug === true )$this->debug( $strSql );
$arrTemp = $this->query( $strSql, 'Row' );
return $arrTemp[ 'NUM' ];
}
public
function getTableEngine( $dbName, $tableName ) {
$strSql = "SHOW TABLE STATUS FROM $dbName WHERE Name='" . $tableName . "'";
$arrayTableInfo = $this->query( $strSql );
$this->getPDOError();
return $arrayTableInfo[ 0 ][ 'Engine' ];
}
public
function prepareSql( $sql = '' ) {
return $this->db->prepare( $sql );
}
public
function execute( $presql ) {
return $this->db->execute( $presql );
}
public
function setAttribute( $p, $d ) {
$this->db->setAttribute( $p, $d );
}
public
function begintrans() {
$this->db->beginTransaction();
}
public
function commit() {
$this->db->commit();
}
public
function rollback() {
$this->db->rollback();
}
public
function execTransaction( $arraySql ) {
$retval = 1;
$this->beginTransaction();
foreach ( $arraySql as $strSql ) {
if ( $this->execSql( $strSql ) == 0 )$retval = 0;
}
if ( $retval == 0 ) {
$this->rollback();
return false;
} else {
$this->commit();
return true;
}
}
private
function checkFields( $table, $arrayFields ) {
$fields = $this->getFields( $table );
foreach ( $arrayFields as $key => $value ) {
if ( !in_array( $key, $fields ) ) {
$this->outputError( "Unknown column `$key` in field list." );
}
}
}
private
function getFields( $table ) {
$fields = array();
$recordset = $this->db->query( "SHOW COLUMNS FROM $table" );
$this->getPDOError();
$recordset->setFetchMode( PDO::FETCH_ASSOC );
$result = $recordset->fetchAll();
foreach ( $result as $rows ) {
$fields[] = $rows[ 'Field' ];
}
return $fields;
}
private
function getPDOError() {
if ( $this->db->errorCode() != '00000' ) {
$arrayError = $this->db->errorInfo();
$this->outputError( $arrayError[ 2 ] );
}
}
private
function debug( $debuginfo ) {
var_dump( $debuginfo );
exit();
}
private
function outputError( $strErrMsg ) {
throw new Exception( 'MySQL Error: ' . $strErrMsg );
}
public
function destruct() {
$this->db = null;
}
public
function version() {
$result = $this->db->query( 'select version()' );
$version = $result->fetch();
return $version;
}
public
function __destruct() {
$this->db = null;
}
}
?>