Exemplo n.º 1
1
 /**
  * Test upsert with namespace
  */
 public function testUpsertWithNamespace()
 {
     $str_ns = 'SomeNamepsace';
     $obj_http = $this->initTestHttpClient('https://datastore.googleapis.com/v1/projects/DatasetTest:commit', ['json' => (object) ['mode' => 'NON_TRANSACTIONAL', 'mutations' => [(object) ['upsert' => (object) ['key' => (object) ['path' => [(object) ['kind' => 'Test', 'id' => '123456789']], 'partitionId' => (object) ['projectId' => self::TEST_PROJECT, 'namespaceId' => $str_ns]], 'properties' => (object) ['name' => (object) ['excludeFromIndexes' => false, 'stringValue' => 'Tom']]]]]]]);
     $obj_gateway = $this->initTestGateway($str_ns)->setHttpClient($obj_http);
     $obj_store = new \GDS\Store('Test', $obj_gateway);
     $obj_entity = new GDS\Entity();
     $obj_entity->setKeyId('123456789');
     $obj_entity->name = 'Tom';
     $obj_store->upsert($obj_entity);
     $this->validateHttpClient($obj_http);
 }
Exemplo n.º 2
0
<?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);
 /**
  * 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();
 }
Exemplo n.º 4
0
<?php

/**
 * GDS Example - Create one record, 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 a simple Entity object
$obj_book = new GDS\Entity();
$obj_book->title = 'Romeo and Juliet';
$obj_book->author = 'William Shakespeare';
$obj_book->isbn = '1840224339';
// Insert into the Datastore
$obj_store->upsert($obj_book);
// Show the key
echo "Created: ", $obj_book->getKeyId(), PHP_EOL;
Exemplo n.º 5
0
<?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 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]);
Exemplo n.º 7
0
 /**
  * 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 a single record in GDS
 *
 * @author Tom Walder <*****@*****.**>
 */
require_once 'boilerplate.php';
$obj_store = new GDS\Store($obj_gateway, 'Friend');
// So now create a simple Model object
$obj_charlie = new GDS\Entity();
$obj_charlie->name = 'Charlie';
$obj_charlie->age = 26;
$obj_charlie->height = 120;
$obj_store->upsert($obj_charlie);
echo "Created: ", $obj_charlie->getKeyId(), PHP_EOL;
// So now create a simple Model object
$obj_max = new GDS\Entity();
$obj_max->name = 'Max';
$obj_max->age = 26;
$obj_max->height = 122;
$obj_store->upsert($obj_max);
echo "Created: ", $obj_max->getKeyId(), PHP_EOL;
echo "Query 1:", PHP_EOL;
foreach ($obj_store->fetchAll("SELECT * FROM Friend WHERE age = 26") as $obj_result) {
    echo "Got: ", $obj_result->getKeyId(), ' ', $obj_result->name, PHP_EOL;
}
echo "Query 2:", PHP_EOL;
foreach ($obj_store->fetchAll("SELECT * FROM Friend WHERE age = 26 AND height = 122") as $obj_result) {
    echo "Got: ", $obj_result->getKeyId(), ' ', $obj_result->name, PHP_EOL;
}
Exemplo n.º 9
0
 * 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";
        } else {
            echo ", Error: NO MATCH";
        }
<?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);
Exemplo n.º 11
0
<?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;
}
Exemplo n.º 12
0
 /**
  * Test basic entity insert
  */
 public function testBasicInsert()
 {
     $int_new_id = mt_rand(100000, 999999);
     $obj_http = $this->initTestHttpClient('https://datastore.googleapis.com/v1/projects/DatasetTest:commit', ['json' => (object) ['mode' => 'NON_TRANSACTIONAL', 'mutations' => [(object) ['insert' => (object) ['key' => (object) ['path' => [(object) ['kind' => 'Test']], 'partitionId' => (object) ['projectId' => self::TEST_PROJECT]], 'properties' => (object) ['name' => (object) ['excludeFromIndexes' => false, 'stringValue' => 'Tom']]]]]]], ['mutationResults' => [(object) ['key' => (object) ['path' => [(object) ['kind' => 'Test', 'id' => $int_new_id]], 'partitionId' => (object) ['projectId' => self::TEST_PROJECT]], 'version' => '123']]]);
     $obj_gateway = $this->initTestGateway()->setHttpClient($obj_http);
     $obj_store = new \GDS\Store('Test', $obj_gateway);
     $obj_entity = new GDS\Entity();
     $obj_entity->name = 'Tom';
     $obj_store->upsert($obj_entity);
     $this->validateHttpClient($obj_http);
     $this->assertEquals($int_new_id, $obj_entity->getKeyId());
 }