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 閱讀(702) 評論(0)  編輯  收藏 所屬分類: PHP
          主站蜘蛛池模板: 玉山县| 石门县| 额济纳旗| 漯河市| 阿尔山市| 通化县| 邵阳市| 昭平县| 百色市| 长白| 油尖旺区| 祁连县| 十堰市| 凌云县| 静海县| 大丰市| 淳安县| 昭苏县| 江永县| 蓝田县| 亳州市| 疏附县| 溧水县| 华池县| 池州市| 巩义市| 镇康县| 新巴尔虎右旗| 平陆县| 广宁县| 涪陵区| 兰州市| 遵义县| 旬阳县| 从江县| 栖霞市| 外汇| 灵台县| 兴安县| 孝感市| 辉南县|