呵呵,自己写着玩的一些东西,大家看了不要笑啊~

呵呵,自己写着玩的一些东西,大家看了不要笑啊~

数据类:

[复制到剪切板]
CODE:
<?php
/**
 * @name DbClass
 * @author gaoyuhong | qq 490997183 | email [email][email protected][/email]
 * @copyright [url]www.gg1st.cn[/url]
 * @example
                    $Db = DbClass::getInstance();
                    $Db->loadConfig('sqlite:a.db');
                    $Db->setTabel('admin');
                    $Db->setField(array('id','name','password'));
                    print_r($Db->doFecth());
                    $Db->setCondition(' and id=1');
                    print_r($Db->doFecthSigle());
                    echo $Db->doUpdate(array("name"=>"'NewAdminName'"));
                    echo $Db->doDelete();//注意,不带condition的delete将会清除数据表
                    echo $Db->doInsert(array("name"=>"'myadmin'","password"=>"'123456'"));
                    $Db->outDebug();

 */
        
    
class DbClass
        
{
            static 
$instance false;
            protected 
$connString;
            protected 
$Sql;
            protected 
$_tableName;
            protected 
$_ArrayFields;
            protected 
$Config;
            protected 
$_Condition;
            private function 
__construct(){
            }
            
/**
             * 生成DbClass单件
             *
             * @return DbClass
             */
            
public static function getInstance() {
                if (!
DbClass::$instance) {
                    
DbClass::$instance = &new DbClass();
                }
                return 
DbClass::$instance;
            }
            public function 
loadConfig($Config){
                if(isset(
$Config)){
                    
$this->Config $Config;
                }else{
                    
$this->Config null;
                }
            }
            
/**
            *设置连接字符串
            * @param string $ConnString
            * @example $Db->loadConfig('sqlite:a.sqlite');
            */
            
public function loadConnString($ConnString){
                if(isset(
$ConnString)){
                    
$this->_connString $ConnString;
                }else{
                    
$this->_connString null;
                }
            }
            
/**
            *获取连接字符串
            *@return string
            */
            
protected function getConnString(){
                return 
$this->_connString;
            }
            
            
/**
            *获取操作表名称
            *@return string
            */
            
protected function _getTable()    {
                    return 
$this->_tableName;
            }
            
/**
            *设置操作表名称
            *@param string $TableName
            */
            
public function setTable($TableName){
                if(isset(
$TableName))
                {
                    foreach(
$this->Config->dbConfig->tables->children() as $T){
                            if(
$T->getName()==$TableName){
                                
$this->_tableName=$T["name"];
                                break;
                            }
                    }
                }
                else
                {
                    
$this->_tableName null;
                }
            }
            private function 
ckTableExits(){

            }
            
/**
            *获取操作字段
            *@return array
            */
            
protected function _getFields(){
                return 
$this->_ArrayFields;
            }
            
/**
            *设置操作字段
            *@param array $arrayField
            *@example $Db->setField(array('a','b','c')); 查询时用
            *@example $Db->setField(array("a"=>"'这里是插入测试'","b"=>"'插入字段2'"); 插入、更新时用
            */
            
public function setField($arrayFields){
                if(isset(
$arrayFields) && is_array($arrayFields)){
                    
$this->_ArrayFields $arrayFields;
                }
                else{
                    
$this->_ArrayFields null;
                }
            }
            
/**
            *获取查询条件
            *@return string
            */
            
protected function _getCondition(){
                return  
$this->_Condition;
            }
            
/**
            *设置查询条件,不带where
            *@param string $Condition
            *@example $Db->setCondition(" and a=1 and b='cc' ");
            */
            
public function setCondition($Condition){
                if(isset(
$Condition)){
                    
$this->_Condition $this->Config->dbConfig->dbWhere."'1'='1'" $Condition ";";
                }else{
                    
$this->_Condition null;
                }
            }
            
/**
            *清除查询条件
            */
            
public function cleanCondition(){
                
$this->_Condition null;
            }
            
/**
            *获取要执行的sql语句
            *
            *@return string;
            */
            
protected function _getSql(){
                return 
$this->Sql.$this->_getCondition();
            }
            
/**
            *设置要执行的SQl语句,要附加查询条件,请使用setCondition方法
            *@param string $Sql
            *@example $Db->setSql('select * from aa');
                              $Db->setCondition(' and a=1 ');
            */
            
public function setSql($Sql){
                if (isset(
$Sql)) {
                    
$this->Sql $Sql;
                }else {
                    
$this->Sql null;
                }
            }
            
/**
            *调试,输出sql语句
            *@return string
            */
            
public function outDebug(){
                die(
$this->_getSql());
            }
            
/**
             * 数组转换为字符串
             *
             * @param array $array
             * @return String
             */
            
protected function _ArrayToString($array){
                if(
is_array($array)){
                    return 
implode($array,",");
                }
            }
            
/**
             * PDO连接对象
             *
             * @return PDO
             */
            
protected function _getConn(){
                
$conn null;
                try {
                    
$conn = new PDO($this->getConnString());
                }catch (
PDOException $e){
                    
$this->RecordeErrorLog($e->getMessage());
                }
                return 
$conn;
            }
            
/**
             * PDO记录集对象
             *
             * @return PDOStatement
             */
            
public function doQuery(){
                
$Query null;
                
$this->checkTable();
                
$temp $this->_ArrayToString($this->_getFields());
                
$this->setSql(str_replace(array("{f}","{t}"),array($temp,$this->_getTable()),$this->Config->dbConfig->selectSQL));
                try{
                    if(!
$Query  $this->_getConn()->query($this->_getSql())){
                        
$this->RecordErrorLog("Query SqlStatement Error:".$this->_getSql().date("Y-m-d H:i:s"));                
                    }
                }catch(
PDOException $e){
                    
$this->RecordeErrorLog($e->getMessage());
                }
                return 
$Query;
            }
            
/**
             * 记录集行数
             * @return int
             */
            
public function countRow(){
                try{
                    
$Count = &count($this->doQuery()->fetchAll());
                }catch(
PDOException $e){
                    
$this->RecordeErrorLog($e->getMessage());
                }
                return 
$Count;
            }
            
/**
             * 多条记录
             *
             * @return array
             */
            
public function doFetch(){
                
$Fetch null;
                try{
                    
$Fetch = &$this->doQuery()->fetchAll(PDO::FETCH_BOTH);
                }catch(
PDOException $e){
                    
$this->RecordeErrorLog($e->getMessage());
                }
                return 
$Fetch;
            }
            
/**
             * 单条记录
             *
             * @return array
             */
            
public function doFetchSigle(){
                
$Fetch null;

                try{
                    
$Fetch = &$this->doQuery()->fetch();
                }catch(
PDOException $e){
                    
$this->RecordeErrorLog($e->getMessage());
                }
                return 
$Fetch;
            }

            protected function 
doExec(){
                        
$this->checkTable();
                        return 
$this->_getConn()->exec($this->_getSql()  ) ;
            }
            
/**
            *表名检测
            */
            
private function checkTable()
            {
                    
$temp = array();
                    
$TableList $this->Config->dbConfig->tables[0];
                    foreach(
$TableList->children() as $R)
                    {
                        
$temp[] = $R["name"];
                    }
                    if(!
in_array($this->_getTable(),$temp)){
                        
$this->RecordErrorLog("No Such Table:".$this->_getTable());
                    }
            }
            public function 
getLastId(){
                return 
$this->_getConn()->lastInsertId();
            }
            
/**
            *错误处理
            */
            
private function RecordErrorLog($ErrorInfo)
            {
                
ErrorModel::WriteLog($this->Config->Error->FileName,$ErrorInfo);
            }
            
/**
             * 插入操作
             *
             * @param array $arrayData
             */
            
public function doInsert($arrayData){
                
$this->cleanCondition();
                foreach(
$arrayData as $a=>$v)
                {
                    
$temp[]= $a;
                    
$tempv[] = $v;
                }
                
$Fields $this->_ArrayToString($temp);
                
$Values $this->_ArrayToString($tempv);
                
$Sql str_replace(array("{t}","{f}","{v}"),array($this->_getTable(),$Fields,$Values),$this->Config->dbConfig->inertSQL);
                
$this->setSql($Sql);
                
$this->doExec();
                return 
$this->_getConn()->lastInsertId();
            }
            
/**
             * 更新操作,返回被印象行数
             *
             * @param Array $ArrayData
             * @return int
             */
            
public function doUpdate($ArrayData){
                    foreach(
$ArrayData as $a=>$v)
                    {
                        
$temp[]="$a=$v";
                    }
                    
$tmp=$this->_ArrayToString($temp);
                    
$Sql str_replace(array("{t}","{f}"),array($this->_getTable(),$tmp),$this->Config->dbConfig->updateSQL);
                    
$this->setSql($Sql);
                    return 
$this->doExec();
            }
            
/**
             * 删除操作,返回被印象行数
             *    @return integer
             */
            
public function doDelete(){

                
$this->setSql(str_replace("{t}",$this->_getTable(),$this->Config->dbConfig->deleteSQL).$this->_getCondition().";");
                return 
$this->doExec();
            }
            
/**
            *检查是否为同一条记录
            *
            *@return int
            */
            
public function checkSameRecord(){
                if(
$this->countRow()>1)
                {
                    return 
false;
                }
                else
                {
                    return 
$this->countRow();
                }
            }
        }
?> ;


------------------------------------------------------------------------------------------------------------------------------------------------
配置文件:

[复制到剪切板]
CODE:
<?xml version="1.0" encoding="utf-8"?>
<
webconfig>
    <
dbConfig>
        <
adapter>sqlite</adapter>
        <
dbname value="dbFile/center.sqlite"/>
        <
dbPrefix value="gg1st_"></dbPrefix>
        <
dbSelect>SELECT </dbSelect>
        <
dbFromFROM </dbFrom>
        <
dbInsertINSERT INTO </dbInsert>
        <
dbDeleteDELETE FROM </dbDelete>
        <
dbUpdateUPDATE </dbUpdate>
        <
dbWhereWHERE </dbWhere>
        <
insertSQLINSERT INTO {t}{fVALUES ({v}) </insertSQL>
        <
deleteSQLDELETE FROM {t} </deleteSQL>
        <
selectSQLSELECT {fFROM {t} </selectSQL>
        <
joinSQLJOIN {jtON {f} </joinSQL>
        <
leftjoinSQLLEFT JOIN {jtON {f} </leftjoinSQL>
        <
updateSQLUPDATE {tSET {f} </updateSQL>
        <
tables>
            <
blogtable name="gg1st_articles" primarykey="article_id"></blogtable>
            <
downloadtable name="gg1st_download" primarykey="download_id"></downloadtable>
            <
blogview name="view_article_class" primarykey="article_id"></blogview>
            <
downloadview name="view_download_class" primarykey="download_id"></downloadview>
            <
classtable name="gg1st_class" primarykey="class_id"></classtable>
            <
tagstable name="gg1st_tags" primarykey="tag_id"></tagstable>
            <
commenttable name="gg1st_comment" primarykey="comment_id"></commenttable>
            <
gbooktable name="gg1st_guestbook" primarykey="guestbook_id"></gbooktable>
            <
membertable name="gg1st_member" primarykey="member_id"></membertable>
            <
programtable name="gg1st_program" primarykey="id"></programtable>
            <
systemtable name="gg1st_system" primarykey="id"></systemtable>
            <
taggedtable name="gg1st_tagged" primarykey="id"></taggedtable>
            <
topictable name="gg1st_topic" primarykey="id"></topictable>        
        </
tables>
    </
dbConfig>
    <
Error>
        <
FileName>log/error.log</FileName>
    </
Error>
    <
folderConfig>
        <
uploadDir value="public/upload/">
            <
image vaue="images/"/>
            <
compress value="compress/"/>
            <
program value="program/"/>
            <
music value="music/"/>
            <
vedio value="vedio/"/>
        </
uploadDir>
        <
staticDir value="public/html/">
            <
blog value="artilces/"/>
            <
download value="downloads/"/>
            <
album value="album/"/>
        </
staticDir>
    </
folderConfig>
    <
templateConfig>
        <
back>
            <
templateDir value="view/templates/backtpl/"/>
            <
compileDir value="view/templates_c/back/"/>
            <
cacheDir value="view/cache/back/"/>
        </
back>
        <
front>
            <
templateDir value="view/templates/default/"/>
            <
compileDir value="view/templates_c/"/>
            <
cacheDir value="view/cache/"/>
        </
front>
        <
left value="{"/>
        <
right value="}"/>
        <
caching>true</caching>
    </
templateConfig>
    <
siteConfig>
        <
siteName></siteName>
        <
siteMeta></siteMeta>
        <
attrName value="value"/>
        <!--
错误调试模式-->
        <
DisplayError>true</DisplayError>
        <!--
禁语-->
        <
siteDeneyWords>
            <
paramter></paramter>
            <
paramter>妈逼</paramter>
            <
paramter>妈b</paramter>
            <
paramter></paramter>
            <
paramter>营销</paramter>
            <
paramter>传销</paramter>
            <
paramter>枪支</paramter>
            <
paramter>弹药</paramter>
            <
paramter>小姐</paramter>
        </
siteDeneyWords>
    </
siteConfig>
</
webconfig> ;

----------------------------------------------------------------------------------------------------------------------
配置文件:

[复制到剪切板]
CODE:
<?php        
        define
('DS',DIRECTORY_SEPARATOR);        
        include_once(
ROOTDIR.'library'.DS.'Smarty'.DS.'Smarty.class.php');
        include_once(
ROOTDIR.'include'.DS.'function.php');
        function 
__autoload($className){
            include_once(
ROOTDIR."include/".$className.'.php');
        }
        
$Config simplexml_load_file(ROOTDIR."config/web.config");
        
$arrtName $Config->siteConfig->attrName["value"];
        
define("DISPLAYERROR",$Config->siteConfig->DisplayError);
        
$smarty = new Smarty();
        if(!
ISBACK)
        {
            
$smarty->template_dir ROOTDIR.$Config->templateConfig->front->templateDir[$arrtName];
            
$smarty->compile_dir ROOTDIR.$Config->templateConfig->front->compileDir[$arrtName];
            
$smarty->cache_dir ROOTDIR.$Config->templateConfig->front->cacheDir[$arrtName];
        }else{
            
$smarty->template_dir ROOTDIR.$Config->templateConfig->back->templateDir[$arrtName];
            
$smarty->compile_dir ROOTDIR.$Config->templateConfig->back->compileDir[$arrtName];
            
$smarty->cache_dir ROOTDIR.$Config->templateConfig->back->cacheDir[$arrtName];
        }
        
$smarty->left_delimiter $Config->templateConfig->left[$arrtName];
        
$smarty->right_delimiter $Config->templateConfig->right[$arrtName];
        
$Db DbClass::getInstance();
        
$connString $Config->dbConfig->adapter.":".ROOTDIR.$Config->dbConfig->dbname[$arrtName];
        
$Db->loadConnString($connString);
        
$Db->loadConfig($Config);
?> ;


=======================================================================
问题:left 和right join要是数据库不支持咋办?比如:sqlite不支持right join的
请教

忘记错误处理了,补上:

[复制到剪切板]
CODE:
<?php
    
class ErrorModel
    
{
        public static function 
WriteLog($FilePath,$ErrorInfo){

            
$fp fopen(ROOTDIR.$FilePath,"a+");
            
$Error = new Exception($ErrorInfo);
            
fwrite($fp"[Error]".$Error->getMessage()."\r\n");
            
fwrite($fp"       [Time]".date("Y-m-d H:i:s")."\r\n");
            
fwrite($fp"       [Trace]\r\n");
            
fwrite($fp"           ".$Error->getTraceAsString()."\r\n");
            
fwrite($fp"***************************************************************\r\n");
            
fclose($fp);
            if(
DISPLAYERROR=="true"){
                throw(
$Error);
            }
                        die(
"Page Runtime Error");
        }
    }
?> ;