Ejemplo n.º 1
0
 public function getActionByUniquenessKey($user_id, $uniqueness_key)
 {
     $response = $this->client->query(['TableName' => 'tr_actions', 'IndexName' => 'uniqueness_key-index', 'KeyConditionExpression' => 'user_id = :user_id AND uniqueness_key = :u_key', 'ExpressionAttributeValues' => [':user_id' => ['S' => $user_id], ':u_key' => ['S' => $uniqueness_key]], 'Limit' => 1]);
     if (!empty($response['Items'])) {
         $action = new \App\Action();
         $action->fromArray($this->marshaler->unmarshalItem($response['Items'][0]));
         return $action;
     }
     return null;
 }
Ejemplo n.º 2
0
 /**
  * query
  *
  * @param  array $conditions
  * @param  array $options
  *
  * @return array
  *
  * @link http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.DynamoDb.DynamoDbClient.html#_query
  */
 public function query(array $conditions, array $options = array())
 {
     $args = array('TableName' => $this->_table_name, 'KeyConditions' => $conditions, 'ScanIndexForward' => true, 'Select' => 'ALL_ATTRIBUTES', 'ReturnConsumedCapacity' => 'TOTAL', 'ConsistentRead' => $this->_consistent_read);
     // Merge $options to $args
     $option_names = array('ScanIndexForward', 'QueryFilter');
     foreach ($option_names as $option_name) {
         if (isset($options[$option_name])) {
             $args[$option_name] = $options[$option_name];
         }
     }
     // if IndexName is specified
     if ($this->_query_index_name) {
         $args['IndexName'] = $this->_query_index_name;
     }
     if (intval($this->_limit) > 0) {
         // Has limit
         // if ExclusiveStartKey is set
         if ($this->_exclusive_start_key) {
             $exclusive_start_key = $this->_formatAttributes($this->_exclusive_start_key);
             $args['ExclusiveStartKey'] = $exclusive_start_key;
         }
         $args['Limit'] = intval($this->_limit);
         $result = self::$_client->query($args);
         self::_logQuery("query", $args, $result);
         // $result is "Guzzle\Service\Resource\Model"
         // and $result has next keys
         // - Count
         // - Items
         // - ScannedCount
         // - LastEvaluatedKey
         $items = $result['Items'];
         // Set LastEvaluatedKey
         $last_evaluated_key = null;
         if (isset($result['LastEvaluatedKey'])) {
             $last_evaluated_key = $this->_formatResult($result['LastEvaluatedKey']);
         }
         $this->_last_evaluated_key = $last_evaluated_key;
         // Set Count
         $result_count = null;
         if (isset($result['Count'])) {
             $result_count = $result['Count'];
         }
         $this->_result_count = $result_count;
     } else {
         // No limit (Use Iterator)
         $iterator = self::$_client->getIterator('Query', $args);
         self::_logQuery('getIterator', $args, $iterator);
         // $iterator is "Aws\Common\Iterator\AwsResourceIterator"
         $items = array();
         foreach ($iterator as $item) {
             $items[] = $item;
         }
         // Set Count
         $this->_result_count = count($items);
     }
     return $this->_formatResults($items);
 }
Ejemplo n.º 3
0
 /**
  * Get items via the query call
  * @param string $table The item table
  * @param mixed $hash The primary hash key
  * @param Context\Query|null $context The call context
  * @return Collection
  */
 public function query($table, $hash, Context\Query $context = null)
 {
     if (null !== $this->logger) {
         $this->log('Query on table ' . $table);
     }
     $hash = new Attribute($hash);
     $parameters = array('TableName' => $table, 'HashKeyValue' => $hash->getForDynamoDB());
     if (null !== $context) {
         $parameters += $context->getForDynamoDB();
     }
     if (null !== $this->logger) {
         $this->log('Query request paramaters : ' . print_r($parameters, true), Logger::DEBUG);
     }
     $response = $this->connector->query($parameters);
     if (null !== $this->logger) {
         $this->log('Query request response : ' . print_r($response, true), Logger::DEBUG);
     }
     $this->addConsumedReadUnits($table, floatval($response['ConsumedCapacityUnits']));
     if (isset($response['LastEvaluatedKey'])) {
         if (null === $context) {
             $nextContext = new Context\Query();
         } else {
             $nextContext = clone $context;
         }
         $nextContext->setExclusiveStartKey($response['LastEvaluatedKey']);
         if (null !== $this->logger) {
             $this->log('More Items to retrieve');
         }
     } else {
         $nextContext = null;
     }
     $items = new Collection($nextContext, $response['Count']);
     if (!empty($response['Items'])) {
         foreach ($response['Items'] as $responseItem) {
             $item = new Item($table);
             $item->populateFromDynamoDB($responseItem);
             $items->add($item);
         }
     }
     if (null !== $this->logger) {
         $this->log('Find ' . count($items) . ' Items');
     }
     return $items;
 }
Ejemplo n.º 4
0
 /**
  * Executes the Query operation.
  *
  * @param string $tableName The name of the table containing the requested items.
  * @param array  $args      Arguments of the query
  *
  * @return  Guzzle\Service\Resource\Model
  *
  * @see http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.DynamoDb.DynamoDbClient.html#_query
  */
 public function query($tableName, array $args)
 {
     $args['TableName'] = $tableName;
     return $this->client->query($args);
 }