<?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; }
<?php /** * Read data based on Ancestor Keys * * @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); // Load the parent (run 'ancestor_keys.php' if needed to create it) $obj_stored_parent = $obj_store->fetchOne("SELECT * FROM Person WHERE __key__ = KEY(Person, '*****@*****.**')"); // All "Person" entities in the group (INCLUDING the root) $arr = $obj_store->fetchAll("SELECT * FROM Person WHERE __key__ HAS ANCESTOR @person", ['person' => $obj_stored_parent]); print_r($arr); // Now just load one (which is a nested entity) print_r($obj_store->fetchAll("SELECT * FROM Person WHERE __key__ = @person", ['person' => $arr[1]]));
/** * Fetch data from 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_contact_schema, $obj_gateway); // A couple of tests show($obj_store->fetchAll("SELECT * FROM Contact_v1 WHERE tags = 'newsletter' AND tags = 'customer'")); show($obj_store->fetchAll("SELECT * FROM Contact_v1 WHERE tags = 'api'")); show($obj_store->fetchAll("SELECT * FROM Contact_v1 WHERE tags = 'newsletter'")); /** * Show result data * * @param $arr */ function show($arr) { echo PHP_EOL, "Query found ", count($arr), " records", PHP_EOL; foreach ($arr as $obj_model) { echo " Email: {$obj_model->getKeyName()}, Name: {$obj_model->first_name}", PHP_EOL; } }
$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"; } } else { echo ", which is not new this time round"; } echo PHP_EOL; }
/** * Name-spaced FETCH 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); // Fetch all (client 1) echo "From ns1", PHP_EOL; $arr_books1 = $obj_store_ns1->fetchAll("SELECT * FROM Book"); echo "Query client 1 found ", count($arr_books1), " records", PHP_EOL; foreach ($arr_books1 as $obj_book) { echo " Title: {$obj_book->title}, ISBN: {$obj_book->isbn}", PHP_EOL; } // Fetch all (client 2) echo "From ns2", PHP_EOL; $arr_books2 = $obj_store_ns2->fetchAll("SELECT * FROM Book"); echo "Query client 2 found ", count($arr_books2), " records", PHP_EOL; foreach ($arr_books2 as $obj_book) { echo " Title: {$obj_book->title}, ISBN: {$obj_book->isbn}", PHP_EOL; }
* * @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); echo "Found single result: [{$str_class}] {$mix_result->getKeyId()}, {$mix_result->title}, {$mix_result->due}", PHP_EOL; } elseif (is_array($mix_result)) { echo "Found ", count($mix_result), " results", PHP_EOL; if ($bol_recurse) {
public function testMixedOverlappingQuotedParamFallback() { $obj_deny_proxy = new DenyGQLProxyMock(); $obj_deny_proxy->init($this); $obj_request = new \google\appengine\datastore\v4\RunQueryRequest(); $obj_request->setSuggestedBatchSize(1000); $obj_request->mutableReadOptions(); $obj_partition = $obj_request->mutablePartitionId(); $obj_partition->setDatasetId('Dataset'); $obj_query = $obj_request->mutableQuery(); $obj_query->addKind()->setName('Book and "stuff"'); $obj_comp_filter = $obj_query->mutableFilter()->mutableCompositeFilter()->setOperator(\google\appengine\datastore\v4\CompositeFilter\Operator::AND_); $obj_prop_filter1 = $obj_comp_filter->addFilter()->mutablePropertyFilter()->setOperator(\google\appengine\datastore\v4\PropertyFilter\Operator::EQUAL); $obj_prop_filter1->mutableProperty()->setName('author'); $obj_prop_filter1->mutableValue()->setStringValue('William "Will" Shakespeare'); $obj_prop_filter2 = $obj_comp_filter->addFilter()->mutablePropertyFilter()->setOperator(\google\appengine\datastore\v4\PropertyFilter\Operator::EQUAL); $obj_prop_filter2->mutableProperty()->setName('isbn'); $obj_prop_filter2->mutableValue()->setStringValue("1234'5'6789"); $obj_deny_proxy->expectCall('datastore_v4', 'RunQuery', $obj_request, new \google\appengine\datastore\v4\RunQueryResponse()); $obj_gateway = new GDS\Gateway\ProtoBuf('Dataset'); $obj_store = new GDS\Store('Book', $obj_gateway); $obj_store->fetchAll('SELECT * FROM `Book and "stuff"` WHERE author = \'William "Will" Shakespeare\' AND isbn = "1234\'5\'6789"'); $obj_deny_proxy->verify(); }