Development

Changeset 6818

You must first sign up to be able to contribute.

Changeset 6818

Show
Ignore:
Timestamp:
12/29/07 22:44:41 (6 months ago)
Author:
nicolas
Message:

sfAmazonSimpleDBPlugin:

  • Added pagination capabilities
  • Removed unused files provided with the official Amazon SimpleDB PHP library
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfAmazonSimpleDBPlugin/trunk/README

    r6815 r6818  
    6060=== Listing domains === 
    6161 
    62 To list available domains for yourt accounts
     62To list available domains for your AWS account
    6363 
    6464{{{ 
    65 $domains = $service->listDomains(); 
    66 echo implode(', ', $domains); 
     65$domainsQuery = $service->listDomains(); 
     66echo implode(', ', $domainsQuery['results']); 
     67}}} 
     68 
     69You can even limit the number of results, eg. for pagination purpose: 
     70 
     71{{{ 
     72$domainsQuery = $service->listDomains(5); 
     73echo '5 first domains : '.implode(', ', $domainsQuery['results']); 
     74 
     75if (isset($domainsQuery['next_token'])) 
     76
     77  $nextDomainsQuery = $service->listDomains(5, $domainsQuery['next_token']); 
     78  echo 'Next 5 domains : '.implode(', ', $nextDomainsQuery['results']); 
     79
    6780}}} 
    6881 
     
    105118 
    106119{{{ 
    107 $products = $service->query("['Color' = 'Red'] intersection ['Size' = 'Small']"); 
    108 echo 'Red small products are '.implode(', ', $results); 
     120$resultsQuery = $service->query("['Color' = 'Red'] intersection ['Size' = 'Small']"); 
     121echo 'Red small products are '.implode(', ', $resultsQuery['results']); 
     122}}} 
     123 
     124[http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_Query.html#SDB_API_Query_QueryExpressionSyntax More on query syntax and available operators]. 
     125 
     126You can even limit the number of results, eg. for pagination purpose: 
     127 
     128{{{ 
     129$resultsQuery = $service->query("['Color' = 'Red']", 5) 
     130echo '5 first results : '.implode(', ', $resultsQuery['results']); 
     131 
     132if (isset($resultsQuery['next_token'])) 
     133
     134  $nextResultsQuery = $service->query("['Color' = 'Red']", 5, $resultsQuery['next_token']); 
     135  echo 'Next 5 results : '.implode(', ', $nextResultsQuery['results']); 
     136
    109137}}} 
    110138 
  • plugins/sfAmazonSimpleDBPlugin/trunk/lib/sfAmazonSimpleDBClient.class.php

    r6815 r6818  
    147147   * Lists available domains 
    148148   * 
     149   * @param int  $max     Max entries to retrieve (optional) 
     150   * @param int  $offset  Offset to start with (optional) 
    149151   * @return array of strings 
    150152   * @throws sfAmazonSimpleDBException 
    151153   */ 
    152   public function listDomains(
     154  public function listDomains($max = null, $offset = null
    153155  { 
    154156    try 
     
    156158      $results = array(); 
    157159      $action = new Amazon_SimpleDB_Model_ListDomains(); 
     160      if (!is_null($max) && $max > 0) 
     161      { 
     162        $action->setMaxNumberOfDomains($max); 
     163      } 
     164      if (!is_null($offset)) 
     165      { 
     166        $action->setNextToken($offset); 
     167      } 
     168      $results = array('results' => array()); 
    158169      sfLogger::getInstance()->info('Listing existing domains'); 
    159170      $response = self::getClient()->listDomains($action); 
     
    164175        foreach ($domainNameList as $domainName)  
    165176        {  
    166           $results[] = $domainName;   
    167         }  
     177          $results['results'][] = $domainName;   
     178        } 
     179        if ($listDomainsResult->isSetNextToken())  
     180        { 
     181          $results['next_token'] = $listDomainsResult->getNextToken(); 
     182        } 
     183      } 
     184      if ($response->isSetResponseMetadata())  
     185      {  
     186        $responseMetadata = $response->getResponseMetadata(); 
     187        if ($responseMetadata->isSetRequestId())  
     188        { 
     189          $results['request_id'] = $responseMetadata->getRequestId(); 
     190        } 
     191        if ($responseMetadata->isSetBoxUsage())  
     192        { 
     193          $results['box_usage'] = $responseMetadata->getBoxUsage(); 
     194        } 
    168195      } 
    169196      return $results; 
     
    303330   * Query database with given expression 
    304331   * 
    305    * @param  string  $expression 
    306    * @param  string  $domain 
     332   * @param  string  $expression  Query expression (optional) 
     333   * @param  int     $max         Max entries to retrieve (optional) 
     334   * @param  int     $offset      Offset to start with (optional) 
     335   * @param  string  $domain      Domain to query (optional) 
    307336   * @return array 
    308    */ 
    309   public function query($expression = null, $domain = null) 
     337   * @link   http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_Query.html#SDB_API_Query_QueryExpressionSyntax 
     338   */ 
     339  public function query($expression = null, $max = null, $offset = null, $domain = null) 
    310340  { 
    311341    if (is_null($domain)) 
     
    317347      throw new sfAmazonSimpleDBException('query(): No domain has been selected nor specified'); 
    318348    } 
    319     $results = array(); 
     349    $results = array('results' => array()); 
    320350    $action = new Amazon_SimpleDB_Model_Query(); 
    321351    $action->setDomainName($domain); 
     
    324354      $action->setQueryExpression($expression); 
    325355    } 
     356    if (!is_null($max) && $max > 0) 
     357    { 
     358      $action->setMaxNumberOfItems($max); 
     359    } 
     360    if (!is_null($offset)) 
     361    { 
     362      $action->setNextToken($offset); 
     363    } 
    326364    sfLogger::getInstance()->info(sprintf('Querying "%s" in domain "%s"', $expression, $domain)); 
    327365    $response = self::getClient()->query($action);  
     
    332370      foreach ($itemNameList as $itemName) 
    333371      { 
    334         $results[] = $itemName; 
     372        $results['results'][] = $itemName; 
     373      } 
     374      if ($queryResult->isSetNextToken())  
     375      { 
     376        $results['next_token'] = $queryResult->getNextToken(); 
     377      } 
     378    } 
     379    if ($response->isSetResponseMetadata())  
     380    {  
     381      $responseMetadata = $response->getResponseMetadata(); 
     382      if ($responseMetadata->isSetRequestId())  
     383      { 
     384        $results['request_id'] = $responseMetadata->getRequestId(); 
     385      } 
     386      if ($responseMetadata->isSetBoxUsage())  
     387      { 
     388        $results['box_usage'] = $responseMetadata->getBoxUsage(); 
    335389      } 
    336390    } 
  • plugins/sfAmazonSimpleDBPlugin/trunk/test/unit/sfAmazonSimpleDBTest.php

    r6815 r6818  
    66require_once($sf_root.'/lib/symfony/vendor/lime/lime.php'); 
    77 
    8 $t = new lime_test(44, new lime_output_color()); 
     8$t = new lime_test(54, new lime_output_color()); 
    99 
    1010$service = sfAmazonSimpleDBClient::getInstance(); 
     
    1414try 
    1515{ 
    16   $domainCreation = $service->createDomain('sfAmazonSimpleDBPluginTestDomain'); 
    17   $t->ok($domainCreation, 'createDomain() confirms domain has been created'); 
     16  $domainCreation1 = $service->createDomain('sfAmazonSimpleDBPluginTestDomain'); 
     17  $domainCreation2 = $service->createDomain('sfAmazonSimpleDBPluginTestDomain2'); 
     18  $domainCreation3 = $service->createDomain('sfAmazonSimpleDBPluginTestDomain3'); 
     19  $t->ok($domainCreation1 && $domainCreation2 && $domainCreation3, 'createDomain() confirms domain has been created'); 
    1820  $t->pass('createDomain() does not throw an exception'); 
    1921} 
     
    2628try 
    2729{ 
    28   $domainsList = $service->listDomains(); 
     30  $domainsResults = $service->listDomains(); 
     31  $domainsList = $domainsResults['results']; 
    2932  $t->isa_ok($domainsList, 'array', 'listDomains() returns an array'); 
    3033  $t->ok(in_array('sfAmazonSimpleDBPluginTestDomain', $domainsList), 'listDomains() lists new created domain'); 
     34  $t->ok(in_array('sfAmazonSimpleDBPluginTestDomain2', $domainsList), 'listDomains() lists new created domain'); 
     35  $t->ok(in_array('sfAmazonSimpleDBPluginTestDomain3', $domainsList), 'listDomains() lists new created domain'); 
     36   
     37  $t->diag('Testing limit and offseting for domains list'); 
     38  $domainsResults = $service->listDomains(2); 
     39  $domainsList = $domainsResults['results']; 
     40  $t->is(count($domainsList), 2, 'listDomains() retrieves the correct limited number of results'); 
     41  $t->ok(isset($domainsResults['next_token']), 'listDomains() returns a next token'); 
     42   
     43  $domainsResultsNext = $service->listDomains(2, $domainsResults['next_token']); 
     44  $domainsResultsListNext = $domainsResultsNext['results']; 
     45  $t->is(count($domainsResultsListNext), 1, 'listDomains() retrieves the correct limited number of results with an offset'); 
     46   
    3147  $t->pass('listDomains() does not throw an exception'); 
    3248} 
     
    8399{ 
    84100  $t->diag(' 1. Getting all data'); 
    85   $query = $service->query(); 
    86   $t->isa_ok($query, 'array', 'query() retrieves results as an array'); 
    87   $t->is(count($query), 3, 'query() retrieves the correct number of results'); 
     101  $queryResults = $service->query(); 
     102  $queryResultsList = $queryResults['results']; 
     103  $t->isa_ok($queryResultsList, 'array', 'query() retrieves results as an array'); 
     104  $t->is(count($queryResultsList), 3, 'query() retrieves the correct number of results'); 
    88105   
    89106  $t->diag(' 2. Filtering one attribute'); 
    90   $query = $service->query("['Color' = 'Red']"); 
    91   $t->isa_ok($query, 'array', 'query() retrieves results as an array'); 
    92   $t->is(count($query), 1, 'query() retrieves the correct number of results'); 
    93   $t->is($query[0], 'Entry #2', 'query() retrieves the correct result'); 
     107  $queryResults = $service->query("['Color' = 'Red']"); 
     108  $queryResultsList = $queryResults['results']; 
     109  $t->isa_ok($queryResultsList, 'array', 'query() retrieves results as an array'); 
     110  $t->is(count($queryResultsList), 1, 'query() retrieves the correct number of results'); 
     111  $t->is($queryResultsList[0], 'Entry #2', 'query() retrieves the correct result'); 
    94112   
    95113  $t->diag(' 3. Filtering multiple attributes'); 
    96   $query = $service->query("['Color' = 'Green'] intersection ['Size' = 'Small']"); 
    97   $t->is(count($query), 1, 'query() retrieves the correct number of results'); 
    98   $t->is($query[0], 'Entry #3', 'query() retrieves the correct result'); 
     114  $queryResults = $service->query("['Color' = 'Green'] intersection ['Size' = 'Small']"); 
     115  $queryResultsList = $queryResults['results']; 
     116  $t->is(count($queryResultsList), 1, 'query() retrieves the correct number of results'); 
     117  $t->is($queryResultsList[0], 'Entry #3', 'query() retrieves the correct result'); 
     118   
     119  $t->diag(' 4. Testing limit and offseting for results list'); 
     120  $queryResults = $service->query(null, 2); 
     121  $queryResultsList = $queryResults['results']; 
     122  $t->is(count($queryResultsList), 2, 'query() retrieves the correct limited number of results'); 
     123  $t->ok(isset($queryResults['next_token']), 'query() retrieves a next token attribute'); 
     124     
     125  $queryResultsNext = $service->query(null, 2, $queryResults['next_token']); 
     126  $queryResultsListNext = $queryResultsNext['results']; 
     127  $t->is(count($queryResultsListNext), 1, 'query() retrieves the correct limited number of results with an offset'); 
     128   
    99129  $t->pass('query() does not throw an exception'); 
    100130} 
     
    161191try 
    162192{ 
    163   $domainDeletion = $service->deleteDomain('sfAmazonSimpleDBPluginTestDomain'); 
    164   $t->ok($domainDeletion, 'deleteDomain() confirms domain has been deleted'); 
     193  $domainDeletion1 = $service->deleteDomain('sfAmazonSimpleDBPluginTestDomain'); 
     194  $domainDeletion2 = $service->deleteDomain('sfAmazonSimpleDBPluginTestDomain2'); 
     195  $domainDeletion3 = $service->deleteDomain('sfAmazonSimpleDBPluginTestDomain3'); 
     196  $t->ok($domainDeletion1 && $domainDeletion2 && $domainDeletion3, 'deleteDomain() confirms domain has been deleted'); 
    165197  $t->pass('deleteDomain() does not throw an exception'); 
    166198} 
     
    170202} 
    171203 
    172 $domainsList = $service->listDomains(); 
     204$domainsResults = $service->listDomains(); 
     205$domainsList = $domainsResults['results']; 
    173206$t->ok(!in_array('sfAmazonSimpleDBPluginTestDomain', $domainsList), 'listDomains() no more lists deleted domain'); 
     207$t->ok(!in_array('sfAmazonSimpleDBPluginTestDomain2', $domainsList), 'listDomains() no more lists deleted domain'); 
     208$t->ok(!in_array('sfAmazonSimpleDBPluginTestDomain3', $domainsList), 'listDomains() no more lists deleted domain'); 
    174209$t->is($service->getSelectedDomain(), null, 'getSelectedDomain() no more returns a deleted domain as selected');