コード例 #1
0
ファイル: Table.php プロジェクト: nanasess/ec-azure
 /**
  * Retrieve entities from table
  * 
  * @param string $tableName|Microsoft_WindowsAzure_Storage_TableEntityQuery    Table name -or- Microsoft_WindowsAzure_Storage_TableEntityQuery instance
  * @param string $filter                                                Filter condition (not applied when $tableName is a Microsoft_WindowsAzure_Storage_TableEntityQuery instance)
  * @param string $entityClass                                           Entity class name
  * @param string $nextPartitionKey                                      Next partition key, used for listing entities when total amount of entities is > 1000.
  * @param string $nextRowKey                                            Next row key, used for listing entities when total amount of entities is > 1000.
  * @return array Array of Microsoft_WindowsAzure_Storage_TableEntity
  * @throws Microsoft_WindowsAzure_Exception
  */
 public function retrieveEntities($tableName = '', $filter = '', $entityClass = 'Microsoft_WindowsAzure_Storage_DynamicTableEntity', $nextPartitionKey = null, $nextRowKey = null)
 {
     if ($tableName === '') {
         throw new Microsoft_WindowsAzure_Exception('Table name is not specified.');
     }
     if ($entityClass === '') {
         throw new Microsoft_WindowsAzure_Exception('Entity class is not specified.');
     }
     // Convenience...
     if (class_exists($filter)) {
         $entityClass = $filter;
         $filter = '';
     }
     // Query string
     $queryString = '';
     // Determine query
     if (is_string($tableName)) {
         // Option 1: $tableName is a string
         // Append parentheses
         if (strpos($tableName, '()') === false) {
             $tableName .= '()';
         }
         // Build query
         $query = array();
         // Filter?
         if ($filter !== '') {
             $query[] = '$filter=' . Microsoft_WindowsAzure_Storage_TableEntityQuery::encodeQuery($filter);
         }
         // Build queryString
         if (count($query) > 0) {
             $queryString = '?' . implode('&', $query);
         }
     } else {
         if (get_class($tableName) == 'Microsoft_WindowsAzure_Storage_TableEntityQuery') {
             // Option 2: $tableName is a Microsoft_WindowsAzure_Storage_TableEntityQuery instance
             // Build queryString
             $queryString = $tableName->assembleQueryString(true);
             // Change $tableName
             $tableName = $tableName->assembleFrom(true);
         } else {
             throw new Microsoft_WindowsAzure_Exception('Invalid argument: $tableName');
         }
     }
     // Add continuation querystring parameters?
     if (!is_null($nextPartitionKey) && !is_null($nextRowKey)) {
         if ($queryString !== '') {
             $queryString .= '&';
         } else {
             $queryString .= '?';
         }
         $queryString .= 'NextPartitionKey=' . rawurlencode($nextPartitionKey) . '&NextRowKey=' . rawurlencode($nextRowKey);
     }
     // Perform request
     $response = null;
     if ($this->isInBatch() && $this->getCurrentBatch()->getOperationCount() == 0) {
         $this->getCurrentBatch()->enlistOperation($tableName, $queryString, Microsoft_Http_Client::GET, array(), true, null);
         $response = $this->getCurrentBatch()->commit();
         // Get inner response (multipart)
         $innerResponse = $response->getBody();
         $innerResponse = substr($innerResponse, strpos($innerResponse, 'HTTP/1.1 200 OK'));
         $innerResponse = substr($innerResponse, 0, strpos($innerResponse, '--batchresponse'));
         $response = Microsoft_Http_Response::fromString($innerResponse);
     } else {
         $response = $this->_performRequest($tableName, $queryString, Microsoft_Http_Client::GET, array(), true, null);
     }
     if ($response->isSuccessful()) {
         // Parse result
         $result = $this->_parseResponse($response);
         if (!$result) {
             return array();
         }
         $entries = null;
         if ($result->entry) {
             if (count($result->entry) > 1) {
                 $entries = $result->entry;
             } else {
                 $entries = array($result->entry);
             }
         } else {
             // This one is tricky... If we have properties defined, we have an entity.
             $properties = $result->xpath('//m:properties');
             if ($properties) {
                 $entries = array($result);
             } else {
                 return array();
             }
         }
         // Create return value
         $returnValue = array();
         foreach ($entries as $entry) {
             // Parse properties
             $properties = $entry->xpath('.//m:properties');
             $properties = $properties[0]->children('http://schemas.microsoft.com/ado/2007/08/dataservices');
             // Create entity
             $entity = new $entityClass('', '');
             $entity->setAzureValues((array) $properties, $this->_throwExceptionOnMissingData);
             // If we have a Microsoft_WindowsAzure_Storage_DynamicTableEntity, make sure all property types are set
             if ($entity instanceof Microsoft_WindowsAzure_Storage_DynamicTableEntity) {
                 foreach ($properties as $key => $value) {
                     $attributes = $value->attributes('http://schemas.microsoft.com/ado/2007/08/dataservices/metadata');
                     $type = (string) $attributes['type'];
                     if ($type !== '') {
                         $entity->setAzureProperty($key, (string) $value, $type);
                     }
                 }
             }
             // Update etag
             $etag = $entry->attributes('http://schemas.microsoft.com/ado/2007/08/dataservices/metadata');
             $etag = (string) $etag['etag'];
             $entity->setEtag($etag);
             // Add to result
             $returnValue[] = $entity;
         }
         // More entities?
         if (!is_null($response->getHeader('x-ms-continuation-NextPartitionKey')) && !is_null($response->getHeader('x-ms-continuation-NextRowKey'))) {
             if (strpos($queryString, '$top') === false) {
                 $returnValue = array_merge($returnValue, $this->retrieveEntities($tableName, $filter, $entityClass, $response->getHeader('x-ms-continuation-NextPartitionKey'), $response->getHeader('x-ms-continuation-NextRowKey')));
             }
         }
         // Return
         return $returnValue;
     } else {
         throw new Microsoft_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
     }
 }
コード例 #2
0
 /**
  * Test where multiple query
  */
 public function testWhereMultipleQuery()
 {
     $target = new Microsoft_WindowsAzure_Storage_TableEntityQuery();
     $target->select()
            ->from('MyTable')
            ->where('Name eq ?', 'Maarten')
            ->andWhere('Visible eq true');
            
     $this->assertEquals('MyTable()?%24filter=Name%20eq%20%27Maarten%27%20and%20Visible%20eq%20true', $target->__toString());
 }