/** * Set the Book custom entity class */ public function testCreateEntity() { $obj_gateway = new \GDS\Gateway\ProtoBuf('Dataset'); $obj_store = new \GDS\Store('Book', $obj_gateway); $obj_store->setEntityClass('Book'); $obj_book = $obj_store->createEntity(['title' => 'Discworld']); $this->assertInstanceOf('\\Book', $obj_book); }
/** * Insert One WITH result */ public function testUpsertOneAutoIdWithResult() { $obj_request = new \google\appengine\datastore\v4\CommitRequest(); $obj_request->setMode(\google\appengine\datastore\v4\CommitRequest\Mode::NON_TRANSACTIONAL); $obj_mutation = $obj_request->mutableDeprecatedMutation(); $obj_entity = $obj_mutation->addInsertAutoId(); $obj_key = $obj_entity->mutableKey(); $obj_partition = $obj_key->mutablePartitionId(); $obj_partition->setDatasetId('Dataset'); $obj_kpe = $obj_key->addPathElement(); $obj_kpe->setKind('Film'); $obj_property = $obj_entity->addProperty(); $obj_property->setName('title'); $obj_val = $obj_property->mutableValue(); $obj_val->setIndexed(TRUE); $obj_val->setStringValue('Back to the Future'); $obj_response = new \google\appengine\datastore\v4\CommitResponse(); $obj_mutation_result = $obj_response->mutableDeprecatedMutationResult(); $obj_ai_key = $obj_mutation_result->addInsertAutoIdKey(); $obj_ai_kpe = $obj_ai_key->addPathElement(); $obj_ai_kpe->setKind('Film')->setId(499190400); $this->apiProxyMock->expectCall('datastore_v4', 'Commit', $obj_request, $obj_response); $obj_gateway = new GDS\Gateway\ProtoBuf('Dataset'); $obj_store = new GDS\Store('Film', $obj_gateway); $obj_book = $obj_store->createEntity(['title' => 'Back to the Future']); $obj_store->upsert($obj_book); $this->assertEquals(499190400, $obj_book->getKeyId()); $this->apiProxyMock->verify(); }
<?php /** * Name-spaced CREATE examples for GDS * * @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); // Create a Book in the first namespace $obj_book1 = $obj_store_ns1->createEntity(['title' => 'Romeo and Juliet', 'author' => 'William Shakespeare', 'isbn' => '1840224339']); $obj_store_ns1->upsert($obj_book1); // Create a Book in the second namespace $obj_book2 = $obj_store_ns2->createEntity(['title' => 'Hamlet', 'author' => 'William Shakespeare', 'isbn' => '1853260096']); $obj_store_ns2->upsert($obj_book2);
<?php /** * Create a hierarchy of records 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 = $obj_store->createEntity(); $obj_john->name = 'John Smiths'; $obj_john->description = 'A parent'; $obj_store->upsert($obj_john); // Create a child $obj_jane = $obj_store->createEntity(); $obj_jane->name = 'Jane Smiths'; $obj_jane->description = 'A child'; $obj_jane->setAncestry($obj_john); $obj_store->upsert($obj_jane); // Create a grand child $obj_jo = $obj_store->createEntity(); $obj_jo->name = 'Jo Smiths'; $obj_jo->description = 'A child'; $obj_jo->setAncestry($obj_jane); $obj_store->upsert($obj_jo); // Now fetch and display the Entity Group print_r($obj_store->fetchEntityGroup($obj_john));
<?php /** * Create or some records in GDS * with an indexed string field * * * @author Tom Walder <*****@*****.**> */ require_once '../vendor/autoload.php'; require_once 'config/setup.php'; // We'll need a Google_Client, use our convenience method $obj_google_client = GDS\Gateway\GoogleAPIClient::createGoogleClient(GDS_APP_NAME, GDS_SERVICE_ACCOUNT_NAME, GDS_KEY_FILE_PATH); $obj_gateway = new GDS\Gateway\GoogleAPIClient($obj_google_client, GDS_DATASET_ID); // Optionally, namespace // Define the model on-the-fly $obj_contact_schema = (new GDS\Schema('Contact'))->addString('first_name')->addString('last_name')->addStringList('tags', TRUE); // Configure the Store $obj_store = new GDS\Store($obj_gateway, $obj_contact_schema); // Create 1 $obj_contact1 = $obj_store->createEntity(['first_name' => 'Tom', 'last_name' => 'Walder', 'tags' => ["customer", "newsletter"]]); $obj_contact1->setKeyName('*****@*****.**'); // Create 2 $obj_contact2 = $obj_store->createEntity(['first_name' => 'Thomas', 'last_name' => 'Walder', 'tags' => ["newsletter", "api"]]); $obj_contact2->setKeyName('*****@*****.**'); // Upsert $obj_store->upsert([$obj_contact1, $obj_contact2]);
/** * Test upsert in transaction */ public function testUpsertWithKeyNameInTransactionRequest() { $obj_gateway = new GDS\Gateway\GoogleAPIClient($this->setupTestClient(), 'Dataset'); $obj_store = new \GDS\Store('Film', $obj_gateway); $this->expectRequest('https://www.googleapis.com/datastore/v1beta2/datasets/Dataset/beginTransaction', '{}', '{"transaction":"EeDoHGJsLR4eGjkABRmGMYV-Vj6Gtwn3ayLOvPX8ccUzuR4NZG0MMhmD28O-3gTTwdIUINZeJBk22kubBQPd0-Nz1sY="}'); $obj_store->beginTransaction(); $this->expectRequest('https://www.googleapis.com/datastore/v1beta2/datasets/Dataset/commit', '{"mode":"TRANSACTIONAL","transaction":"EeDoHGJsLR4eGjkABRmGMYV-Vj6Gtwn3ayLOvPX8ccUzuR4NZG0MMhmD28O-3gTTwdIUINZeJBk22kubBQPd0-Nz1sY=","mutation":{"upsert":[{"key":{"path":[{"kind":"Film","name":"B2TF"}]},"properties":{"title":{"indexed":true,"stringValue":"Back to the Future"}}}]}}'); $obj_film = $obj_store->createEntity(['title' => 'Back to the Future']); $obj_film->setKeyName('B2TF'); $obj_store->upsert($obj_film); }
<?php /** * Create, update and retrieve a series of Entities to test retrieval and mapping of Auto-insert IDs * * @author Tom Walder <*****@*****.**> */ require_once 'boilerplate.php'; $obj_store = new GDS\Store('Temperatures', $obj_gateway); // Delete ALL // $obj_store->delete($obj_store->fetchAll()); // Create some new records with random temperatures $arr_new_records = []; $int_new_records = mt_rand(10, 20); for ($int = 1; $int <= $int_new_records; $int++) { $obj_new_entity = $obj_store->createEntity(['temp' => mt_rand(1, 9999)]); $arr_new_records[] = $obj_new_entity; } $obj_store->upsert($arr_new_records); // Now keep a record of the mapping (POST upsert) $arr_temp_id_map = []; foreach ($arr_new_records as $obj_temp) { $arr_temp_id_map[$obj_temp->getKeyId()] = $obj_temp->temp; } // Get all records from the Datastore and compare $arr_all_temps = $obj_store->fetchAll(); foreach ($arr_all_temps as $obj_stored_temp) { echo $obj_stored_temp->getKeyID() . " has temp " . $obj_stored_temp->temp; if (isset($arr_temp_id_map[$obj_stored_temp->getKeyID()])) { if ($arr_temp_id_map[$obj_stored_temp->getKeyID()] == $obj_stored_temp->temp) { echo ", match OK";
<?php /** * Query parameter examples - datetime bindings * * @author Tom Walder <*****@*****.**> */ require_once 'boilerplate.php'; // Schema with datetime $obj_task_schema = (new GDS\Schema('Task'))->addString('title')->addDatetime('due', TRUE); // Store requires a Gateway and Schema $obj_task_store = new GDS\Store($obj_gateway, $obj_task_schema); // Insert some data, with datetime binding $obj_task_1 = $obj_task_store->createEntity(['title' => 'My first task', 'due' => new DateTime('+1 day')]); $obj_task_store->upsert($obj_task_1); // Insert some data, with "normal" string format $obj_task_2 = $obj_task_store->createEntity(['title' => 'My first task', 'due' => date('Y-m-d H:10:00')]); $obj_task_store->upsert($obj_task_2); // Fetch with datetime binding $arr_results = $obj_task_store->fetchAll("SELECT * FROM Task WHERE due < @dtm", ['dtm' => new DateTime('+6 hours')]); describeResult($arr_results, TRUE); /** * Helper function to simplify results display * * @param $mix_result * @param bool $bol_recurse */ function describeResult($mix_result, $bol_recurse = FALSE) { if ($mix_result instanceof GDS\Entity) { $str_class = get_class($mix_result);
<?php /** * GDS Example - Create one record (using the array syntax), with no Schema * * @author Tom Walder <*****@*****.**> */ require_once '../_includes.php'; // This Store uses the default Protocol Buffer Gateway - for App Engine local development or live App Engine $obj_store = new \GDS\Store('Book'); // Alternative Gateway (remote JSON API) // Download your service JSON file from the Google Developer Console // $obj_client = \GDS\Gateway\GoogleAPIClient::createClientFromJson('/path/to/your/service.json'); // $obj_gateway = new \GDS\Gateway\GoogleAPIClient($obj_client, 'your-app-id'); // $obj_store = new \GDS\Store('Book', $obj_gateway); // Create a simple Entity object $obj_book = $obj_store->createEntity(['title' => 'Romeo and Juliet', 'author' => 'William Shakespeare', 'isbn' => '1840224339']); // Insert into the Datastore $obj_store->upsert($obj_book); // Show the key echo "Created: ", $obj_book->getKeyId(), PHP_EOL;
<?php /** * GDS Example - Create several records in one upsert, with no Schema * * @author Tom Walder <*****@*****.**> */ require_once '../_includes.php'; // This Store uses the default Protocol Buffer Gateway - for App Engine local development or live App Engine $obj_store = new \GDS\Store('Book'); // Alternative Gateway (remote JSON API) // Download your service JSON file from the Google Developer Console // $obj_gateway = new \GDS\Gateway\RESTv1('your-app-id'); // $obj_store = new \GDS\Store('Book', $obj_gateway); // Create some Entity objects $obj_romeo = $obj_store->createEntity(['title' => 'Romeo and Juliet', 'author' => 'William Shakespeare', 'isbn' => '1840224339']); $obj_midsummer = $obj_store->createEntity(['title' => "A Midsummer Night's Dream", 'author' => 'William Shakespeare', 'isbn' => '1853260304']); // Insert multiple into the Datastore $arr_books = [$obj_romeo, $obj_midsummer]; $obj_store->upsert($arr_books); // Show their keys foreach ($arr_books as $obj_book) { echo "Created: ", $obj_book->getKeyId(), PHP_EOL; }
/** * 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); }