<?php
/**
 * SQL命令執行器
 *
 * @version        $Id: sys_sql_query.php 1 22:28 2010年7月20日Z tianya $
 * @package        DedeCMS.Administrator
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
require(dirname(__FILE__)."/config.php");
CheckPurview('sys_Data');
if(empty($dopost)) $dopost = "";

//查看表結構
if($dopost=="viewinfo")
{
    if(empty($tablename))
    {
        echo "沒有指定表名！";
    }
    else
    {
        $dsql->SetQuery("SHOW CREATE TABLE ".$dsql->dbName.".".$tablename);
        $dsql->Execute('me');
        $row2 = $dsql->GetArray('me',MYSQL_BOTH);
        $ctinfo = $row2[1];
        echo "<xmp>".trim($ctinfo)."</xmp>";
    }
    exit();
}
//優化表
else if($dopost=="opimize")
{
    if(empty($tablename))
    {
        echo "沒有指定表名！";
    }
    else
    {
        $rs = $dsql->ExecuteNoneQuery("OPTIMIZE TABLE `$tablename` ");
        if($rs)  echo "執行優化表： $tablename  OK！";
        else echo "執行優化表： $tablename  失敗，原因是：".$dsql->GetError();
    }
    exit();
}
//優化全部表
else if($dopost=="opimizeAll")
{
    $dsql->SetQuery("SHOW TABLES");
    $dsql->Execute('t');
    while($row = $dsql->GetArray('t',MYSQL_BOTH))
    {
        $rs = $dsql->ExecuteNoneQuery("OPTIMIZE TABLE `{$row[0]}` ");
        if($rs) 
        {
            echo "優化表: {$row[0]} ok!<br />\r\n";
        } else {
            echo "優化表: {$row[0]} 失敗! 原因是: ".$dsql->GetError()."<br />\r\n";
        }
    }
    exit();
}
//修復表
else if($dopost=="repair")
{
    if(empty($tablename))
    {
        echo "沒有指定表名！";
    }
    else
    {
        $rs = $dsql->ExecuteNoneQuery("REPAIR TABLE `$tablename` ");
        if($rs) echo "修復表： $tablename  OK！";
        else echo "修復表： $tablename  失敗，原因是：".$dsql->GetError();
    }
    exit();
}
//修復全部表
else if($dopost=="repairAll")
{
    $dsql->SetQuery("Show Tables");
    $dsql->Execute('t');
    while($row = $dsql->GetArray('t',MYSQL_BOTH))
    {
        $rs = $dsql->ExecuteNoneQuery("REPAIR TABLE `{$row[0]}` ");
        if($rs) 
        {
            echo "修復表: {$row[0]} ok!<br />\r\n";
        } else {
            echo "修復表: {$row[0]} 失敗! 原因是: ".$dsql->GetError()."<br />\r\n";
        }
    }
    exit();
}
//執行SQL語句
else if($dopost=="query")
{
    $sqlquery = trim(stripslashes($sqlquery));
    if(preg_match("#drop(.*)table#i", $sqlquery) || preg_match("#drop(.*)database#", $sqlquery))
    {
        echo "<span style='font-size:10pt'>刪除'數據表'或'數據庫'的語句不允許在這裡執行。</span>";
        exit();
    }
    //運行查詢語句
    if(preg_match("#^select #i", $sqlquery))
    {
        $dsql->SetQuery($sqlquery);
        $dsql->Execute();
        if($dsql->GetTotalRow()<=0)
        {
            echo "運行SQL：{$sqlquery}，無返回記錄！";
        }
        else
        {
            echo "運行SQL：{$sqlquery}，共有".$dsql->GetTotalRow()."條記錄，最大返回100條！";
        }
        $j = 0;
        while($row = $dsql->GetArray())
        {
            $j++;
            if($j > 100)
            {
                break;
            }
            echo "<hr size=1 width='100%'/>";
            echo "記錄：$j";
            echo "<hr size=1 width='100%'/>";
            foreach($row as $k=>$v)
            {
                echo "<font color='red'>{$k}：</font>{$v}<br/>\r\n";
            }
        }
        exit();
    }
    if($querytype==2)
    {
        //普通的SQL語句
        $sqlquery = str_replace("\r","",$sqlquery);
        $sqls = preg_split("#;[ \t]{0,}\n#",$sqlquery);
        $nerrCode = ""; $i=0;
        foreach($sqls as $q)
        {
            $q = trim($q);
            if($q=="")
            {
                continue;
            }
            $dsql->ExecuteNoneQuery($q);
            $errCode = trim($dsql->GetError());
            if($errCode=="")
            {
                $i++;
            }
            else
            {
                $nerrCode .= "執行： <font color='blue'>$q</font> 出錯，錯誤提示：<font color='red'>".$errCode."</font><br>";
            }
        }
        echo "成功執行{$i}個SQL語句！<br><br>";
        echo $nerrCode;
    }
    else
    {
        $dsql->ExecuteNoneQuery($sqlquery);
        $nerrCode = trim($dsql->GetError());
        echo "成功執行1個SQL語句！<br><br>";
        echo $nerrCode;
    }
    exit();
}
include DedeInclude('templets/sys_sql_query.htm');