/** * Test GQL Fetch With Namespace */ public function testGqlFetchWithNamespace() { $str_ns = 'Namespacey'; $str_id = '1263751723'; $str_id_parent = '1263751724'; $str_id_grandparent = '1263751725'; $obj_http = $this->initTestHttpClient('https://datastore.googleapis.com/v1/projects/DatasetTest:runQuery', ['json' => (object) ['gqlQuery' => (object) ['allowLiterals' => true, 'queryString' => 'SELECT * FROM Test LIMIT 1'], 'partitionId' => (object) ['projectId' => self::TEST_PROJECT, 'namespaceId' => $str_ns]]], ['batch' => (object) ['entityResultType' => 'FULL', 'entityResults' => [(object) ['entity' => (object) ['key' => (object) ['path' => [(object) ['kind' => 'GrandParent', 'id' => $str_id_grandparent], (object) ['kind' => 'Parent', 'id' => $str_id_parent], (object) ['kind' => 'Test', 'id' => $str_id]]], 'properties' => (object) ['name' => (object) ['excludeFromIndexes' => false, 'stringValue' => 'Tom'], 'age' => (object) ['excludeFromIndexes' => false, 'integerValue' => 37]]], 'version' => '123', 'cursor' => 'gfuh37f86gyu23']]]]); $obj_gateway = $this->initTestGateway($str_ns)->setHttpClient($obj_http); $obj_store = new \GDS\Store('Test', $obj_gateway); $obj_entity = $obj_store->fetchOne("SELECT * FROM Test"); $this->assertInstanceOf('\\GDS\\Entity', $obj_entity); $this->assertEquals($str_id, $obj_entity->getKeyId()); $this->assertEquals('Tom', $obj_entity->name); $this->assertEquals(37, $obj_entity->age); // Do we have ancestry? $this->assertTrue(is_array($obj_entity->getAncestry())); $this->assertEquals(2, count($obj_entity->getAncestry())); // Extract the ancestry $arr_ancestry = $obj_entity->getAncestry(); $arr_grandparent = $arr_ancestry[0]; $arr_parent = $arr_ancestry[1]; // Grandparent tests $this->assertArrayHasKey('kind', $arr_grandparent); $this->assertEquals('GrandParent', $arr_grandparent['kind']); $this->assertArrayHasKey('id', $arr_grandparent); $this->assertEquals($str_id_grandparent, $arr_grandparent['id']); // Parent test $this->assertArrayHasKey('kind', $arr_parent); $this->assertEquals('Parent', $arr_parent['kind']); $this->assertArrayHasKey('id', $arr_parent); $this->assertEquals($str_id_parent, $arr_parent['id']); $this->validateHttpClient($obj_http); }
<?php /** * Create a single record in GDS * * @author Tom Walder <*****@*****.**> */ require_once 'boilerplate.php'; $obj_person_schema = (new GDS\Schema('Person'))->addString('name')->addString('description'); $obj_store = new GDS\Store($obj_person_schema, $obj_gateway); // Create the parent $obj_john = new \GDS\Entity(); $obj_john->name = 'John Smiths'; $obj_john->description = 'A parent'; $obj_john->setKeyName('*****@*****.**'); $obj_store->upsert($obj_john); $obj_stored_parent = $obj_store->fetchOne("SELECT * FROM Person WHERE __key__ = KEY(Person, '*****@*****.**')"); // Create a child $obj_jane = new \GDS\Entity(); $obj_jane->name = 'Jane Smiths'; $obj_jane->description = 'A child'; $obj_jane->setKeyName('*****@*****.**'); $obj_jane->setAncestry($obj_stored_parent); $obj_store->upsert($obj_jane);
<?php /** * Simplest GDS example - no schema, Kind only * * @author Tom Walder <*****@*****.**> */ require_once '../vendor/autoload.php'; require_once 'config/setup.php'; // We'll need a Google_Client, use our convenience method $obj_client = GDS\Gateway\GoogleAPIClient::createGoogleClient(GDS_APP_NAME, GDS_SERVICE_ACCOUNT_NAME, GDS_KEY_FILE_PATH); // Gateway requires a Google_Client and Dataset ID $obj_gateway = new GDS\Gateway\GoogleAPIClient($obj_client, GDS_DATASET_ID); // Store requires a Gateway and Kind $obj_book_store = new GDS\Store($obj_gateway, 'Book'); // Fetch a record $obj_book = $obj_book_store->fetchOne(); // Dump the result print_r($obj_book);
/** * Test transaction Request */ public function testBeginAndUseTransactionRequest() { $obj_gateway = new GDS\Gateway\GoogleAPIClient($this->setupTestClient(), 'Dataset'); $obj_store = new \GDS\Store('Book', $obj_gateway); $this->expectRequest('https://www.googleapis.com/datastore/v1beta2/datasets/Dataset/beginTransaction', '{}', '{"transaction":"EeDoHGJsLR4eGjkABRmGMYV-Vj6Gtwn3ayLOvPX8ccUzuR4NZG0MMhmD28O-3gTTwdIUINZeJBk22kubBQPd0-Nz1sY="}'); $obj_store->beginTransaction(); // And a second test to show we have correctly extracted the Transaction ID $this->expectRequest('https://www.googleapis.com/datastore/v1beta2/datasets/Dataset/runQuery', '{"readOptions":{"transaction":"EeDoHGJsLR4eGjkABRmGMYV-Vj6Gtwn3ayLOvPX8ccUzuR4NZG0MMhmD28O-3gTTwdIUINZeJBk22kubBQPd0-Nz1sY="},"gqlQuery":{"allowLiteral":true,"queryString":"SELECT * FROM `Book` ORDER BY __key__ ASC LIMIT 1"}}'); $obj_store->fetchOne(); }
<?php /** * Move data between name spaces * * @author Tom Walder <*****@*****.**> */ require_once '../_includes.php'; // Define our Book Schema $obj_schema = (new GDS\Schema('Book'))->addString('title', FALSE)->addString('author')->addString('isbn')->addDatetime('published', FALSE)->addInteger('pages', FALSE); // This Store uses the default Protocol Buffer Gateway - for App Engine local development or live App Engine // BUT, this time With a namespace defined ("ns1") $obj_gateway_ns1 = new GDS\Gateway\ProtoBuf(null, 'ns1'); $obj_store_ns1 = new \GDS\Store($obj_schema, $obj_gateway_ns1); // This Store uses the default Protocol Buffer Gateway - for App Engine local development or live App Engine // BUT, this time With a namespace defined ("ns2") $obj_gateway_ns2 = new GDS\Gateway\ProtoBuf(null, 'ns2'); $obj_store_ns2 = new \GDS\Store($obj_schema, $obj_gateway_ns2); // Fetch a record from the first namespace $obj_book = $obj_store_ns1->fetchOne(); // ========= // IMPORTANT // ========= // The book inserted to ns2 here will have the SAME keyId or keyName // BUT will be in a different namespace, so will still be uniquely addressable // As the "fully qualified" key of an Entity in GDS includes the namespace // Insert into the second namesapce $obj_store_ns2->upsert($obj_book);
/** * Run a complex GQL Query */ public function testGqlQueryParams() { $obj_http = $this->initTestHttpClient('https://datastore.googleapis.com/v1/projects/DatasetTest:runQuery', ['json' => (object) ['gqlQuery' => (object) ['allowLiterals' => true, 'queryString' => 'SELECT * FROM Test WHERE booly = @booly AND stringy = @stringy AND inty = @inty AND floaty = @floaty AND datey = @datey AND somekey = @somekey LIMIT 1', 'namedBindings' => (object) ['booly' => (object) ['value' => (object) ['booleanValue' => true]], 'stringy' => (object) ['value' => (object) ['stringValue' => 'test']], 'inty' => (object) ['value' => (object) ['integerValue' => 123]], 'floaty' => (object) ['value' => (object) ['doubleValue' => 4.56]], 'datey' => (object) ['value' => (object) ['timestampValue' => '1955-11-10T01:02:03.000000Z']], 'somekey' => (object) ['value' => (object) ['keyValue' => (object) ['path' => [(object) ['kind' => 'Test', 'name' => 'my-first-key-name']], 'partitionId' => (object) ['projectId' => self::TEST_PROJECT]]]]]], 'partitionId' => (object) ['projectId' => self::TEST_PROJECT]]], ['batch' => (object) ['entityResultType' => 'FULL', 'entityResults' => []]]); $obj_gateway = $this->initTestGateway()->setHttpClient($obj_http); $obj_store = new \GDS\Store('Test', $obj_gateway); $obj_key_entity = $obj_store->createEntity()->setKeyName('my-first-key-name'); $obj_store->fetchOne("SELECT * FROM Test WHERE booly = @booly AND stringy = @stringy AND inty = @inty AND floaty = @floaty AND datey = @datey AND somekey = @somekey", ['booly' => true, 'stringy' => 'test', 'inty' => 123, 'floaty' => 4.56, 'datey' => new DateTime('1955-11-10 01:02:03'), 'somekey' => $obj_key_entity]); $this->validateHttpClient($obj_http); }