if($queryResult=mysql_query($str,$this->conn))
            {
                
$resultTable = new DbTable();
                
$fields = array();
                
$rows = array();
                
while ($property = mysql_fetch_field($queryResult))
                {
                    
$fields[] = array($property->name,$property->type);
                }
                
while ($row = mysql_fetch_array($queryResult,MYSQL_NUM)) {
                    
foreach($row as $k=>$v)
                    
if(is_numeric($v))
                    
$row[$k]=floatval($v);
                    
$rows[] = $row;
                }
                
$resultTable->fields = $fields;
                
$resultTable->rows = $rows;
            }

class DbTable
{
    
var $rows;
    
var $fields;
    
var $tableName;

    
/**
     * {
     *     objectType:"DbTable",
     *  tableName:"tableName",
     *     fields:[
     *         [col1Name,col1Type],
     *         [col2Name,col2Tpye],
     *         [col3Name,col3Type]],
     *     rows:[
     *         [row1Col1,row1Col2,row1Col3],
     *         [row2Col1,row2Col2,row2Col3],
     *         [row3Col1,row3Col2,row3Col3]]
     * }
     
*/
    
function toString()
    {
        
$jsonFields = json_encode($this->fields);
        
$josnRows = json_encode($this->rows);
        
$result = "{objectType:\"DbTable\",tableName:".$this->tableName.",fields:".$jsonFields.",rows:".$josnRows."}";
        
return $result;
    }
}

class DataSet
{
    
var $tables;

    
/**
     * {
     *     objectType:"DataSet",
     *     tables:[
     *             {
     *                objectType:"DbTable",
     *                 tableName:"table1Name",
     *                 fields:[
     *                     [col1Name,col1Type],
     *                     [col2Name,col2Tpye],
     *                     [col3Name,col3Type]],
     *                 rows:[
     *                     [row1Col1,row1Col2,row1Col3],
     *                     [row2Col1,row2Col2,row2Col3],
     *                     [row3Col1,row3Col2,row3Col3]]
     *           },
     *           {
     *               objectType:"DbTable",
     *                 tableName:"table2Name",
     *                 fields:[
     *                     [col1Name,col1Type],
     *                     [col2Name,col2Tpye],
     *                     [col3Name,col3Type]],
     *                 rows:[
     *                     [row1Col1,row1Col2,row1Col3],
     *                     [row2Col1,row2Col2,row2Col3],
     *                     [row3Col1,row3Col2,row3Col3]]
     *             }
     *            ]
     * }
     
*/
    
/**
     * $resultDataSet = new DataSet();
     * $tables[] = DbTable1->toString();
     * $tables[] = DbTable2->toString();
     * $resultDataSet->tables = $tables;
     * $resultDataSet->toString();
     
*/
    
function toString()
    {
        
$result = "{objectType:\"DataSet\",tables:[";
        
foreach($this->tables as $k=>$v)
        {
            
$result.= "{".$this->tables[$k]."},";
        }
        
$result = substr($result,0,-1);
        
$result.="]}";
        
echo $result;
    }
}

    /**
     * insert into tableName (col1,col2,col3) values ('v1','v2','v3')
     * jsonStr:
     * {
     *         "rows" : [col1,col2,col3]
     * }
     * @param $jsonStr
     * @param $tableName
     
*/
    
function rowInsert($jsonStr,$tableName){
        
$jsonDecode = json_decode($jsonStr);
        
$result     = "insert into ".$tableName." values(";
        
foreach($jsonDecode ->rows as $k => $v){
            
$result .= "'".$v."',";
        }
        
$result  = substr($result,0,-1);
        
$result .= ")";
        
return $result;
    }

    
/**
     * UPDATE tableName SET col1 = col1Value, col2 = col2Value WHERE 1=1 AND primaryKey = key
     * @param $jsonStr
     * @param $tableName
     
*/
    
function rowUpdate($jsonStr,$tableName)
    {
        
$jsonDecode = json_decode($jsonStr);
        
$sql        = 'DESCRIBE `'.$tableName.'`;';
        
$descRs     = mysql_query($sql);
        
$setStr     = "UPDATE ".$tableName." SET";
        
$whereStr   = "  WHERE true ";
        
$i          = 0;
        
while ($row = mysql_fetch_array($descRs,MYSQL_NUM)) {
            
if($row[3== "PRI")
            
$whereStr.=" AND ".$row[0]." = '".$jsonDecode->rows[$i]."";
            
$setStr.= " ".$row[0]." = '".$jsonDecode->rows[$i]."',";
            
$i++;
        }
        
$setStr = substr($setStr,0,-1);
        
return $setStr.$whereStr;
    }

    
/**
     * DELETE tableName WHERE 1=1 AND primaryKey = key
     * @param $jsonStr
     * @param $tableName
     
*/
    
function rowDelete($jsonStr,$tableName)
    {
     
$jsonDecode = json_decode($jsonStr);
     
$sql        = 'DESCRIBE `'.$tableName.'`;';
     
$descRs     = mysql_query($sql);
     
$result     = " DELETE ".$tableName." WHERE true ";
     
while ($row = mysql_fetch_array($descRs,MYSQL_NUM)) {
         
$i=0;
         
if($row[3== "PRI")
         
$result.=" AND ".$row[0]." = '".$jsonDecode->rows[$i]."";
         
$i++;
     }
     
return $result;
    }




































posted on 2010-08-24 10:19 Ying-er 阅读(693) 评论(0)  编辑  收藏 所属分类: PHP

只有注册用户登录后才能发表评论。


网站导航: