/**
  * {@inheritdoc}
  */
 protected function write(array $record)
 {
     $filtered = json_decode($record['formatted'], true);
     $result = $this->flatten($filtered);
     $entity = new Entity();
     $entity->setPartitionKey($result['message'] . '-' . $_SERVER['SERVER_NAME']);
     $entity->setRowKey(strval(PHP_INT_MAX - (int) (microtime(true) * 10000)));
     foreach ($result as $k => $v) {
         $entity->addProperty(strval($k), null, strval($v));
     }
     $this->client->insertOrReplaceEntity($this->table, $entity);
 }
 private static function getNewEntity()
 {
     if (is_null(self::$curPartition) || self::$curPartition == count(self::$Partitions) - 1) {
         self::$curPartition = 0;
         self::$curRowKey = TableServiceFunctionalTestData::getNewKey();
     } else {
         self::$curPartition++;
     }
     $entity = new Entity();
     $entity->setPartitionKey(self::$Partitions[self::$curPartition]);
     $entity->setRowKey(self::$curRowKey);
     return $entity;
 }
 public static function setSettings($tenant, $settings)
 {
     $entity = new Entity();
     $entity->setPartitionKey($tenant);
     $entity->setRowKey("settings");
     foreach ($settings as $name => $value) {
         $entity->addProperty($name, null, $value);
     }
     try {
         self::$tableRestProxy->insertOrMergeEntity(self::$table, $entity);
     } catch (ServiceException $e) {
         $code = $e->getCode();
         $error_message = $e->getMessage();
         $app->error("Something went wrong when trying to save your configuration. Please try again later.");
     }
 }
function batchInsertEntitiesSample($tableClient)
{
    $batchOp = new BatchOperations();
    for ($i = 2; $i < 10; ++$i) {
        $entity = new Entity();
        $entity->setPartitionKey("pk");
        $entity->setRowKey('' . $i);
        $entity->addProperty("PropertyName", EdmType::STRING, "Sample" . $i);
        $batchOp->addInsertEntity("mytable", $entity);
    }
    try {
        $tableClient->batch($batchOp);
    } catch (ServiceException $e) {
        $code = $e->getCode();
        $error_message = $e->getMessage();
        echo $code . ": " . $error_message . PHP_EOL;
    }
}
 /**
  * @covers MicrosoftAzure\Storage\Table\TableRestProxy::deleteEntity
  * @covers MicrosoftAzure\Storage\Table\TableRestProxy::insertEntity
  */
 public function testDeleteEntityTroublesomeRowKey()
 {
     // The service does not allow the following common characters in keys:
     // 35 '#'
     // 47 '/'
     // 63 '?'
     // 92 '\'
     // In addition, the following values are not allowed, as they make the URL bad:
     // 0-31, 127-159
     // That still leaves several options for making troublesome keys
     // spaces
     // single quotes
     // Unicode
     // These need to be properly encoded when passed on the URL, else there will be trouble
     $table = TableServiceFunctionalTestData::$testTableNames[0];
     $e = new Entity();
     $e->setRowKey('row\'Key\'');
     $e->setPartitionKey('niceKey');
     $this->restProxy->insertEntity($table, $e);
     $this->restProxy->deleteEntity($table, $e->getPartitionKey(), $e->getRowKey());
     $qopts = new QueryEntitiesOptions();
     $qopts->setFilter(Filter::applyEq(Filter::applyPropertyName('RowKey'), Filter::applyConstant($e->getRowKey(), EdmType::STRING)));
     $queryres = $this->restProxy->queryEntities($table, $qopts);
     $this->assertEquals(0, count($queryres->getEntities()), 'entities returned');
     $e = new Entity();
     $e->setRowKey('row Key');
     $e->setPartitionKey('niceKey');
     $this->restProxy->insertEntity($table, $e);
     $this->restProxy->deleteEntity($table, $e->getPartitionKey(), $e->getRowKey());
     $qopts = new QueryEntitiesOptions();
     $qopts->setFilter(Filter::applyEq(Filter::applyPropertyName('RowKey'), Filter::applyConstant($e->getRowKey(), EdmType::STRING)));
     $queryres = $this->restProxy->queryEntities($table, $qopts);
     $this->assertEquals(0, count($queryres->getEntities()), 'entities returned');
     $e = new Entity();
     $e->setRowKey('row ' . TableServiceFunctionalTestData::getUnicodeString());
     $e->setPartitionKey('niceKey');
     $this->restProxy->insertEntity($table, $e);
     $this->restProxy->deleteEntity($table, $e->getPartitionKey(), $e->getRowKey());
     $qopts = new QueryEntitiesOptions();
     $qopts->setFilter(Filter::applyEq(Filter::applyPropertyName('RowKey'), Filter::applyConstant($e->getRowKey(), EdmType::STRING)));
     $queryres = $this->restProxy->queryEntities($table, $qopts);
     $this->assertEquals(0, count($queryres->getEntities()), 'entities returned');
     $this->clearTable($table);
 }
 static function cloneEntity($initialEnt)
 {
     $ret = new Entity();
     $initialProps = $initialEnt->getProperties();
     $retProps = array();
     foreach ($initialProps as $propName => $initialProp) {
         // Don't mess with the timestamp.
         if ($propName == 'Timestamp') {
             continue;
         }
         $retProp = new Property();
         $retProp->setEdmType($initialProp->getEdmType());
         $retProp->setValue($initialProp->getValue());
         $retProps[$propName] = $retProp;
     }
     $ret->setProperties($retProps);
     $ret->setETag($initialEnt->getETag());
     return $ret;
 }
 /**
  * @covers MicrosoftAzure\Storage\Table\TableRestProxy::batch
  * @covers MicrosoftAzure\Storage\Table\TableRestProxy::insertEntity
  * @covers MicrosoftAzure\Storage\Table\TableRestProxy::updateEntity
  */
 public function testBatchNegativeWorks()
 {
     // Arrange
     $table = self::$testTable8;
     $partitionKey = '001';
     // Insert an entity the modify it outside of the batch
     $entity1 = new Entity();
     $entity1->setPartitionKey($partitionKey);
     $entity1->setRowKey('batchNegativeWorks1');
     $entity1->addProperty('test', EdmType::INT32, 1);
     $entity2 = new Entity();
     $entity2->setPartitionKey($partitionKey);
     $entity2->setRowKey('batchNegativeWorks2');
     $entity2->addProperty('test', EdmType::INT32, 2);
     $entity3 = new Entity();
     $entity3->setPartitionKey($partitionKey);
     $entity3->setRowKey('batchNegativeWorks3');
     $entity3->addProperty('test', EdmType::INT32, 3);
     $entity1 = $this->restProxy->insertEntity($table, $entity1)->getEntity();
     $entity2 = $this->restProxy->insertEntity($table, $entity2)->getEntity();
     $entity2->addProperty('test', EdmType::INT32, -2);
     $this->restProxy->updateEntity($table, $entity2);
     // Act
     $batchOperations = new BatchOperations();
     // The $entity1 still has the original etag from the first submit,
     // so this update should fail, because another update was already made.
     $entity1->addProperty('test', EdmType::INT32, 3);
     $batchOperations->addDeleteEntity($table, $entity1->getPartitionKey(), $entity1->getRowKey(), $entity1->getETag());
     $batchOperations->addUpdateEntity($table, $entity2);
     $batchOperations->addInsertEntity($table, $entity3);
     $result = $this->restProxy->batch($batchOperations);
     // Assert
     $this->assertNotNull($result, '$result');
     $entries = $result->getEntries();
     $this->assertEquals(1, count($entries), 'count($result->getEntries())');
     $this->assertNotNull($entries[0], 'First $result should not be null');
     $this->assertTrue($entries[0] instanceof BatchError, 'First $result type');
     $error = $entries[0];
     $this->assertEquals(412, $error->getError()->getCode(), 'First $result status code');
 }
 /**
  * @covers MicrosoftAzure\Storage\Table\Models\Entity::isValid
  */
 public function testIsValidWithEmptyPartitionKey()
 {
     // Setup
     $entity = new Entity();
     $entity->addProperty('name', EdmType::STRING, 'string');
     // Assert
     $actual = $entity->isValid();
     // Assert
     $this->assertFalse($actual);
 }
 /**
  * Parses an entity entry from given SimpleXML object.
  * 
  * @param \SimpleXML $result The SimpleXML object representing the entity.
  * 
  * @return \MicrosoftAzure\Storage\Table\Models\Entity
  */
 private function _parseOneEntity($result)
 {
     $prefix = $this->_dataServicesMetadataPrefix;
     $prop = $result->content->xpath(".//{$prefix}:properties");
     $prop = $prop[0]->children($this->_dataServicesNamespaceName);
     $entity = new Entity();
     // Set ETag
     $etag = $result->attributes($this->_dataServicesMetadataNamespaceName);
     $etag = $etag[Resources::ETAG];
     $entity->setETag((string) $etag);
     foreach ($prop as $key => $value) {
         $attributes = $value->attributes($this->_dataServicesMetadataNamespaceName);
         $type = $attributes['type'];
         $isnull = $attributes['null'];
         $value = EdmType::unserializeQueryValue((string) $type, $value);
         $entity->addProperty((string) $key, is_null($type) ? EdmType::STRING : (string) $type, $isnull ? null : $value);
     }
     return $entity;
 }
 /**
  * Inserts new entity to the table.
  * 
  * @param string                     $entries The entries values.
  * @param Models\TableServiceOptions $options The optional parameters.
  * 
  * @return none
  * 
  * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179433.aspx
  */
 public function insertTypelessEntity($entries, $options = null)
 {
     $entity = new Entity();
     $entity->setPartitionKey($this->_defaultParitionKey);
     $entity->setRowKey(uniqid());
     foreach ($entries as $columnName => $value) {
         $entity->addProperty($columnName, null, $value);
     }
     $this->_proxy->insertEntity($this->_name, $entity, $options);
 }
 public static function getExpectedTestEntity($partitionKey, $rowKey)
 {
     $entity = new Entity();
     $entity->addProperty('PartitionKey', EdmType::STRING, $partitionKey);
     $entity->addProperty('RowKey', EdmType::STRING, $rowKey);
     $entity->addProperty('CustomerId', EdmType::INT32, 890);
     $entity->addProperty('CustomerName', EdmType::STRING, 'John');
     $entity->addProperty('IsNew', EdmType::BOOLEAN, true);
     $entity->addProperty('JoinDate', EdmType::DATETIME, Utilities::convertToDateTime('2012-01-26T18:26:19.0000473Z'));
     return $entity;
 }
 /**
  * @covers MicrosoftAzure\Storage\Table\TableRestProxy::insertEntity
  * @covers MicrosoftAzure\Storage\Table\TableRestProxy::queryEntities
  */
 public function testInsertEntityString()
 {
     foreach (TableServiceFunctionalTestData::getInterestingGoodStrings() as $o) {
         $ent = new Entity();
         $ent->setPartitionKey(TableServiceFunctionalTestData::getNewKey());
         $ent->setRowKey(TableServiceFunctionalTestData::getNewKey());
         $ent->addProperty('STRING', EdmType::STRING, $o);
         $this->insertEntityWorker($ent, true, null, $o);
     }
 }
 static function getSimpleEntities($count)
 {
     $ret = array();
     $e = new Entity();
     $e->setPartitionKey('singlePartition');
     $e->setRowKey(self::getNewKey());
     $e->addProperty('INT32', EdmType::INT32, 23);
     array_push($ret, $e);
     $booleans = self::getInterestingGoodBooleans();
     $dates = self::getInterestingGoodDates();
     $doubles = self::getInterestingGoodDoubles();
     $guids = self::getInterestingGoodGuids();
     $ints = self::getInterestingGoodInts();
     $longs = self::getInterestingGoodLongs();
     $binaries = self::getInterestingGoodBinaries();
     $strings = self::getInterestingGoodStrings();
     // The random here is not to generate random values, but to
     // get a good mix of values in the table entities.
     mt_srand(123);
     for ($i = 0; $i < $count - 1; $i++) {
         $e = new Entity();
         $e->setPartitionKey('singlePartition');
         $e->setRowKey(self::getNewKey());
         self::addProperty($e, 'BINARY', EdmType::BINARY, $binaries);
         self::addProperty($e, 'BOOLEAN', EdmType::BOOLEAN, $booleans);
         self::addProperty($e, 'DATETIME', EdmType::DATETIME, $dates);
         self::addProperty($e, 'DOUBLE', EdmType::DOUBLE, $doubles);
         self::addProperty($e, 'GUID', EdmType::GUID, $guids);
         self::addProperty($e, 'INT32', EdmType::INT32, $ints);
         self::addProperty($e, 'INT64', EdmType::INT64, $longs);
         self::addProperty($e, 'STRING', EdmType::STRING, $strings);
         array_push($ret, $e);
     }
     return $ret;
 }