/** * Store an Aggregation in DB * * @param \Agg\Entity\Aggregation $aggregation */ public function storeAggregation($aggregation) { //Generate unique slug $slug = $this->getUniqueSlug($this->slugify($aggregation->getTitle())); $aggregation->setSlug($slug); $entity = new Entity(); $entity->setPartitionKey("talk"); $entity->setRowKey($slug); $entity->addProperty("slug", EdmType::STRING, $aggregation->getSlug()); $entity->addProperty("title", EdmType::STRING, $aggregation->getTitle()); $entity->addProperty("talks", EdmType::STRING, serialize($aggregation->getTalks())); $entity->addProperty("speakerName", EdmType::STRING, $aggregation->getSpeakerName()); $entity->addProperty("speakerUrl", EdmType::STRING, $aggregation->getSpeakerUrl()); try { $this->tableProxy->insertEntity(self::TABLE_AGGREGATION, $entity); } catch (ServiceException $e) { $code = $e->getCode(); $error_message = $e->getMessage(); echo $code . ": " . $error_message . "<br />"; } }
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."); } }
public static function getTestEntity($partitionKey, $rowKey) { $entity = new Entity(); $entity->setPartitionKey($partitionKey); $entity->setRowKey($rowKey); $entity->addProperty('CustomerId', EdmType::INT32, 890); $entity->addProperty('CustomerName', null, 'John'); $entity->addProperty('IsNew', EdmType::BOOLEAN, true); $entity->addProperty('JoinDate', EdmType::DATETIME, Utilities::convertToDateTime('2012-01-26T18:26:19.0000473Z')); return $entity; }
//rowkey를 위해 guid 생성 function getGUID() { if (function_exists('com_create_guid')) { return com_create_guid(); } else { mt_srand((double) microtime() * 10000); //optional for php 4.2.0 and up. $charid = strtoupper(md5(uniqid(rand(), true))); $hyphen = chr(45); // "-" $uuid = chr(123) . substr($charid, 0, 8) . $hyphen . substr($charid, 8, 4) . $hyphen . substr($charid, 12, 4) . $hyphen . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12) . chr(125); // "}" return $uuid; } } $dwguid = getGUID(); //guid 처리 $entity = new Entity(); $entity->setPartitionKey("VSTechUp"); $entity->setRowKey($dwguid); $entity->addProperty("Description", null, "VS가 쵝오에요~"); $entity->addProperty("DueDate", EdmType::DATETIME, new DateTime("2015-11-14T08:15:00-08:00")); $entity->addProperty("Location", EdmType::STRING, "광화문"); try { $tableRestProxy->insertEntity("phptable", $entity); //테이블명 } catch (ServiceException $e) { $code = $e->getCode(); $error_message = $e->getMessage(); }
/** * @covers WindowsAzure\Table\TableRestProxy::insertEntity */ public function testInsertStringWithAllAsciiCharacters() { $table = TableServiceFunctionalTestData::$TEST_TABLE_NAMES[0]; $e = new Entity(); $e->setPartitionKey('foo'); $e->setRowKey(TableServiceFunctionalTestData::getNewKey()); // ASCII code points in the following ranges are valid in XML 1.0 documents // - 0x09, 0x0A, 0x0D, 0x20-0x7F // Note: 0x0D gets mapped to 0x0A by the server. $k = ''; for ($b = 0x20; $b < 0x30; $b++) { $k .= chr($b); } $k .= chr(0x9); for ($b = 0x30; $b < 0x40; $b++) { $k .= chr($b); } $k .= chr(0xa); for ($b = 0x40; $b < 0x50; $b++) { $k .= chr($b); } $k .= chr(0xa); for ($b = 0x50; $b < 0x80; $b++) { $k .= chr($b); } $e->addProperty('foo', EdmType::STRING, $k); $ret = $this->restProxy->insertEntity($table, $e); $this->assertNotNull($ret, '$ret'); $this->assertNotNull($ret->getEntity(), '$ret->getEntity'); $l = $ret->getEntity()->getPropertyValue('foo'); $this->assertEquals($k, $l, '$ret->getEntity()->getPropertyValue(\'foo\')'); $this->clearTable($table); }
//rowkey를 위해 guid 생성 function getGUID() { if (function_exists('com_create_guid')) { return com_create_guid(); } else { mt_srand((double) microtime() * 10000); //optional for php 4.2.0 and up. $charid = strtoupper(md5(uniqid(rand(), true))); $hyphen = chr(45); // "-" $uuid = chr(123) . substr($charid, 0, 8) . $hyphen . substr($charid, 8, 4) . $hyphen . substr($charid, 12, 4) . $hyphen . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12) . chr(125); // "}" return $uuid; } } $dwguid = getGUID(); //guid 처리 $entity = new Entity(); $entity->setPartitionKey("KCD"); $entity->setRowKey($dwguid); $entity->addProperty("Description", null, "질문을 하시면 선물이!!!"); $entity->addProperty("DueDate", EdmType::DATETIME, new DateTime("2016-01-23T08:15:00-08:00")); $entity->addProperty("Location", EdmType::STRING, "광화문"); try { $tableRestProxy->insertEntity("phptable", $entity); //테이블명 } catch (ServiceException $e) { $code = $e->getCode(); $error_message = $e->getMessage(); }
/** * Callback function for session handler. It's invoked while the session data is being written. * * @param $sessionId The session ID. * @param $sessionData The data to be written in session. * * @return boolean If the write operation success. */ public function write($sessionId, $sessionData) { // serialize and encode the session data. $serializedData = base64_encode(serialize($sessionData)); try { // try to retrive the stored session entity and update it. $result = $this->_tableRestProxy->getEntity($this->_sessionContainer, $this->_sessionContainerPartition, $sessionId); $entity = $result->getEntity(); // update data and expiry time $entity->setPropertyValue('data', $serializedData); $entity->setPropertyValue('expires', time()); // update entity $this->_tableRestProxy->updateEntity($this->_sessionContainer, $entity); } catch (ServiceException $e) { // otherwise, create a new session entity to store the data. $entity = new Entity(); // set partition key and use session id as the row key. $entity->setPartitionKey($this->_sessionContainerPartition); $entity->setRowKey($sessionId); // set data and expiry time $entity->addProperty('data', EdmType::STRING, $serializedData); $entity->addProperty('expires', EdmType::INT32, time()); // insert the entity $this->_tableRestProxy->insertEntity($this->_sessionContainer, $entity); } return TRUE; }
/** * @covers WindowsAzure\Table\TableRestProxy::insertEntity * @covers WindowsAzure\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; }
public function testGc() { $client = $this->getMockClient(); $client->shouldReceive('queryEntities')->andThrow(new ServiceException('404')); $sessionHandler = new SessionHandler($client); $this->assertFalse($sessionHandler->gc(0)); $client = $this->getMockClient(); $queryEntitiesResult = new QueryEntitiesResult(); $entity = new Entity(); $entity->setRowKey('id'); $entity->addProperty('data', null, base64_encode('data')); $queryEntitiesResult->setEntities(array($entity)); $client->shouldReceive('queryEntities')->andReturn($queryEntitiesResult); $client->shouldReceive('deleteEntity'); $sessionHandler = new SessionHandler($client); $this->assertTrue($sessionHandler->gc(0)); }
/** * @param array $record * @param null|Entity $entity * @param array $exclude List of keys to exclude from adding to Entity * * @return Entity */ protected static function parseRecordToEntity($record = [], $entity = null, $exclude = []) { if (empty($entity)) { $entity = new Entity(); } foreach ($record as $key => $value) { if (false === array_search($key, $exclude)) { // valid types // const DATETIME = 'Edm.DateTime'; // const BINARY = 'Edm.Binary'; // const GUID = 'Edm.Guid'; $edmType = EdmType::STRING; switch (gettype($value)) { case 'boolean': $edmType = EdmType::BOOLEAN; break; case 'double': case 'float': $edmType = EdmType::DOUBLE; break; case 'integer': $edmType = $value > 2147483647 ? EdmType::INT64 : EdmType::INT32; break; } if ($entity->getProperty($key)) { $prop = new Property(); $prop->setEdmType($edmType); $prop->setValue($value); $entity->setProperty($key, $prop); } else { $entity->addProperty($key, $edmType, $value); } } } return $entity; }
/** * @covers WindowsAzure\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 \WindowsAzure\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) ? null : (string) $type, $isnull ? null : $value); } return $entity; }
/** * @covers WindowsAzure\Table\TableRestProxy::batch * @covers WindowsAzure\Table\TableRestProxy::insertEntity * @covers WindowsAzure\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'); }
/** * Create entity object with key and data values. * * @param array $key * @param array $data * @return \WindowsAzure\Table\Model\Entity */ private function createEntity(array $key, array $data) { list($partitonKey, $rowKey) = array_values($key); $entity = new Entity(); $entity->setPartitionKey((string) $partitonKey); $entity->setRowKey((string) $rowKey); foreach ($data as $variable => $value) { $type = $this->getPropertyType($value); $entity->addProperty($variable, $type, $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); }
/** * Create or update a session by ID. * * @param string $sessionId * @param string $sessionData */ public function write($sessionId, $sessionData) { $entity = new Entity(); $entity->setPartitionKey($this->partitionKey); $entity->setRowKey($sessionId); $entity->addProperty('last_accessed', EdmType::INT32, time()); $entity->addProperty('data', EdmType::STRING, base64_encode($sessionData)); try { $this->client->insertOrReplaceEntity($this->table, $entity); } catch (Exception $e) { return; } }