Development

Changeset 5600

You must first sign up to be able to contribute.

Changeset 5600

Show
Ignore:
Timestamp:
10/19/07 18:22:47 (6 years ago)
Author:
Jonathan.Wage
Message:

Fixes to allow batches of queries in one request.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfMDB2RestPlugin/trunk/lib/MDB2RestClient.class.php

    r5587 r5600  
    1010class MDB2RestClient 
    1111{ 
    12   protected $serverUrl  = null, 
    13             $username   = null, 
    14             $password   = null; 
     12  protected $serverUrl      = null, 
     13            $username       = null, 
     14            $password       = null, 
     15            $batch          = false, 
     16            $batchRequests  = array(); 
    1517   
    1618  /** 
     
    2325  { 
    2426    $this->serverUrl = $serverUrl; 
     27  } 
     28   
     29  /** 
     30   * startBatch 
     31   * 
     32   * @return void 
     33   */ 
     34  public function startBatch() 
     35  { 
     36    $this->batch = true; 
     37  } 
     38 
     39  /** 
     40   * endBatch 
     41   * 
     42   * @return void 
     43   */ 
     44  public function endBatch() 
     45  { 
     46    $this->batch = false; 
     47     
     48    return $this->retrieveBatchResults(); 
     49  } 
     50   
     51  /** 
     52   * retrieveBatchResults 
     53   *  
     54   * @return void 
     55   */ 
     56  protected function retrieveBatchResults() 
     57  { 
     58    $request = array('method' => 'batch', 'arguments' => $this->batchRequests); 
     59 
     60    $results = $this->request($request); 
     61 
     62    return $this->processRequestResults($results); 
    2563  } 
    2664   
     
    67105    $results = ob_get_contents(); 
    68106    ob_end_clean(); 
    69      
     107 
    70108    return unserialize($results); 
    71109  } 
     
    162200    $request = array('method' => $method, 'arguments' => $arguments); 
    163201     
    164     $results = $this->request($request); 
    165      
    166     if (isset($results['error'])) { 
    167       throw new Exception($results['error']); 
     202    if ($this->batch) { 
     203      $this->batchRequests[] = $request; 
    168204    } else { 
    169       return $results; 
     205      $results = $this->request($request); 
     206       
     207      return $this->processRequestResults($results); 
    170208    } 
    171209  } 
     210   
     211  /** 
     212   * processRequestResults 
     213   *  
     214   * @param array $results 
     215   */ 
     216  protected function processRequestResults($results) 
     217  { 
     218      if (isset($results['error'])) { 
     219        throw new Exception($results['error']); 
     220      } else { 
     221        return $results; 
     222      } 
     223  } 
    172224} 
  • plugins/sfMDB2RestPlugin/trunk/lib/MDB2RestServer.class.php

    r5586 r5600  
    9393    $request = unserialize($this->request['data']); 
    9494     
     95    $results = $this->executeRequest($request); 
     96     
     97    echo serialize($results); 
     98  } 
     99   
     100  /** 
     101   * executeRequest 
     102   * 
     103   * @param array $request 
     104   */ 
     105  protected function executeRequest($request) 
     106  { 
    95107    $method = $request['method']; 
    96108    $arguments = $request['arguments']; 
    97109     
    98     $results = $this->$method($arguments); 
    99      
    100     if ($results instanceof MDB2_error) { 
    101       $results = array('error' => $results->getMessage()); 
    102     } 
    103      
    104     echo serialize($results); 
     110    try { 
     111      $results = $this->$method($arguments); 
     112     
     113      if ($results instanceof MDB2_error) { 
     114        $results = array('error' => $results->getMessage()); 
     115      } 
     116    } catch (Exception $e) { 
     117      $results = array('error' => $e->getMessage()); 
     118    } 
     119 
     120    return $results; 
     121  } 
     122   
     123  /** 
     124   * batch 
     125   * 
     126   * @param array $arguments 
     127   */ 
     128  public function batch($arguments) 
     129  { 
     130    $results = array(); 
     131    foreach ($arguments as $request) { 
     132      $results[] = $this->executeRequest($request); 
     133    } 
     134 
     135    return $results; 
    105136  } 
    106137