Skip to content

Commit affbbb8

Browse files
author
twalder-docnet
committed
Begin reorganising example code
1 parent d9099b9 commit affbbb8

12 files changed

+246
-72
lines changed

examples/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
# Examples for php-gds #
22

3-
> @todo
3+
I've started re-organising the examples into sub-folders.
4+
5+
* /simple
6+
* /namespace
7+
8+
I'll continue doing this after the 2.0 release.
9+

examples/_includes.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Includes required for examples (no Composer needed!)
4+
*
5+
* @author Tom Walder <[email protected]>
6+
*/
7+
require_once('../src/GDS/Entity.php');
8+
require_once('../src/GDS/Schema.php');
9+
require_once('../src/GDS/Store.php');
10+
require_once('../src/GDS/Gateway.php');
11+
require_once('../src/GDS/Gateway/ProtoBuf.php');
12+
require_once('../src/GDS/Gateway/GoogleAPIClient.php');
13+
require_once('../src/GDS/Mapper.php');
14+
require_once('../src/GDS/Mapper/ProtoBuf.php');
15+
require_once('../src/GDS/Mapper/ProtoBufGQLParser.php');
16+
require_once('../src/GDS/Mapper/GoogleAPIClient.php');
17+
require_once('../src/GDS/Exception/GQL.php');

examples/boilerplate.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
$obj_gateway = new GDS\Gateway\GoogleAPIClient($obj_client, GDS_DATASET_ID);
1717

1818
// Define our Model Schema
19-
$obj_book_schema = (new GDS\Schema('Book'))->addString('title')->addString('author')->addString('isbn', TRUE);
19+
$obj_book_schema = (new GDS\Schema('Book'))
20+
->addString('title')
21+
->addString('author')
22+
->addString('isbn', TRUE)
23+
->addDatetime('published', FALSE)
24+
->addString('text', FALSE);
2025

2126
// Store requires a Gateway and Schema
2227
$obj_book_store = new GDS\Store($obj_book_schema, $obj_gateway);

examples/namespace/create.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Name-spaced CREATE examples for GDS
4+
*
5+
* @author Tom Walder <[email protected]>
6+
*/
7+
require_once('../_includes.php');
8+
9+
// Define our Book Schema
10+
$obj_schema = (new GDS\Schema('Book'))
11+
->addString('title', FALSE)
12+
->addString('author')
13+
->addString('isbn')
14+
->addDatetime('published', FALSE)
15+
->addInteger('pages', FALSE);
16+
17+
// This Store uses the default Protocol Buffer Gateway - for App Engine local development or live App Engine
18+
// BUT, this time With a namespace defined ("ns1")
19+
$obj_gateway_ns1 = new GDS\Gateway\ProtoBuf(null, 'ns1');
20+
$obj_store_ns1 = new \GDS\Store($obj_schema, $obj_gateway_ns1);
21+
22+
// This Store uses the default Protocol Buffer Gateway - for App Engine local development or live App Engine
23+
// BUT, this time With a namespace defined ("ns2")
24+
$obj_gateway_ns2 = new GDS\Gateway\ProtoBuf(null, 'ns2');
25+
$obj_store_ns2 = new \GDS\Store($obj_schema, $obj_gateway_ns2);
26+
27+
// Create a Book in the first namespace
28+
$obj_book1 = $obj_store_ns1->createEntity([
29+
'title' => 'Romeo and Juliet',
30+
'author' => 'William Shakespeare',
31+
'isbn' => '1840224339'
32+
]);
33+
$obj_store_ns1->upsert($obj_book1);
34+
35+
// Create a Book in the second namespace
36+
$obj_book2 = $obj_store_ns2->createEntity([
37+
'title' => 'Hamlet',
38+
'author' => 'William Shakespeare',
39+
'isbn' => '1853260096'
40+
]);
41+
$obj_store_ns2->upsert($obj_book2);

examples/namespace/fetch.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Name-spaced FETCH examples for GDS
4+
*
5+
* @author Tom Walder <[email protected]>
6+
*/
7+
require_once('../_includes.php');
8+
9+
// Define our Book Schema
10+
$obj_schema = (new GDS\Schema('Book'))
11+
->addString('title', FALSE)
12+
->addString('author')
13+
->addString('isbn')
14+
->addDatetime('published', FALSE)
15+
->addInteger('pages', FALSE);
16+
17+
// This Store uses the default Protocol Buffer Gateway - for App Engine local development or live App Engine
18+
// BUT, this time With a namespace defined ("ns1")
19+
$obj_gateway_ns1 = new GDS\Gateway\ProtoBuf(null, 'ns1');
20+
$obj_store_ns1 = new \GDS\Store($obj_schema, $obj_gateway_ns1);
21+
22+
// This Store uses the default Protocol Buffer Gateway - for App Engine local development or live App Engine
23+
// BUT, this time With a namespace defined ("ns2")
24+
$obj_gateway_ns2 = new GDS\Gateway\ProtoBuf(null, 'ns2');
25+
$obj_store_ns2 = new \GDS\Store($obj_schema, $obj_gateway_ns2);
26+
27+
// Fetch all (client 1)
28+
echo "From ns1", PHP_EOL;
29+
$arr_books1 = $obj_store_ns1->fetchAll("SELECT * FROM Book");
30+
echo "Query client 1 found ", count($arr_books1), " records", PHP_EOL;
31+
foreach($arr_books1 as $obj_book) {
32+
echo " Title: {$obj_book->title}, ISBN: {$obj_book->isbn}", PHP_EOL;
33+
}
34+
35+
// Fetch all (client 2)
36+
echo "From ns2", PHP_EOL;
37+
$arr_books2 = $obj_store_ns2->fetchAll("SELECT * FROM Book");
38+
echo "Query client 2 found ", count($arr_books2), " records", PHP_EOL;
39+
foreach($arr_books2 as $obj_book) {
40+
echo " Title: {$obj_book->title}, ISBN: {$obj_book->isbn}", PHP_EOL;
41+
}

examples/namespace/move.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Move data between name spaces
4+
*
5+
* @author Tom Walder <[email protected]>
6+
*/
7+
require_once('../_includes.php');
8+
9+
// Define our Book Schema
10+
$obj_schema = (new GDS\Schema('Book'))
11+
->addString('title', FALSE)
12+
->addString('author')
13+
->addString('isbn')
14+
->addDatetime('published', FALSE)
15+
->addInteger('pages', FALSE);
16+
17+
// This Store uses the default Protocol Buffer Gateway - for App Engine local development or live App Engine
18+
// BUT, this time With a namespace defined ("ns1")
19+
$obj_gateway_ns1 = new GDS\Gateway\ProtoBuf(null, 'ns1');
20+
$obj_store_ns1 = new \GDS\Store($obj_schema, $obj_gateway_ns1);
21+
22+
// This Store uses the default Protocol Buffer Gateway - for App Engine local development or live App Engine
23+
// BUT, this time With a namespace defined ("ns2")
24+
$obj_gateway_ns2 = new GDS\Gateway\ProtoBuf(null, 'ns2');
25+
$obj_store_ns2 = new \GDS\Store($obj_schema, $obj_gateway_ns2);
26+
27+
// Fetch a record from the first namespace
28+
$obj_book = $obj_store_ns1->fetchOne();
29+
30+
// =========
31+
// IMPORTANT
32+
// =========
33+
// The book inserted to ns2 here will have the SAME keyId or keyName
34+
// BUT will be in a different namespace, so will still be uniquely addressable
35+
// As the "fully qualified" key of an Entity in GDS includes the namespace
36+
37+
// Insert into the second namesapce
38+
$obj_store_ns2->upsert($obj_book);
39+

examples/namespace_boilerplate.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

examples/namespace_create.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

examples/namespace_fetch.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

examples/simple/create_many.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* GDS Example - Create several records in one upsert, with no Schema
4+
*
5+
* @author Tom Walder <[email protected]>
6+
*/
7+
require_once('../_includes.php');
8+
9+
// This Store uses the default Protocol Buffer Gateway - for App Engine local development or live App Engine
10+
$obj_store = new \GDS\Store('Book');
11+
12+
// Alternative Gateway (remote JSON API)
13+
// Download your service JSON file from the Google Developer Console
14+
// $obj_client = \GDS\Gateway\GoogleAPIClient::createClientFromJson('/path/to/your/service.json');
15+
// $obj_gateway = new \GDS\Gateway\GoogleAPIClient($obj_client, 'your-app-id');
16+
// $obj_store = new \GDS\Store('Book', $obj_gateway);
17+
18+
// Create some Entity objects
19+
$obj_romeo = $obj_store->createEntity([
20+
'title' => 'Romeo and Juliet',
21+
'author' => 'William Shakespeare',
22+
'isbn' => '1840224339'
23+
]);
24+
$obj_midsummer = $obj_store->createEntity([
25+
'title' => "A Midsummer Night's Dream",
26+
'author' => 'William Shakespeare',
27+
'isbn' => '1853260304'
28+
]);
29+
30+
// Insert multiple into the Datastore
31+
$arr_books = [$obj_romeo, $obj_midsummer];
32+
$obj_store->upsert($arr_books);
33+
34+
// Show their keys
35+
foreach ($arr_books as $obj_book) {
36+
echo "Created: ", $obj_book->getKeyId(), PHP_EOL;
37+
}
38+

examples/simple/create_one.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* GDS Example - Create one record, with no Schema
4+
*
5+
* @author Tom Walder <[email protected]>
6+
*/
7+
require_once('../_includes.php');
8+
9+
// This Store uses the default Protocol Buffer Gateway - for App Engine local development or live App Engine
10+
$obj_store = new \GDS\Store('Book');
11+
12+
// Alternative Gateway (remote JSON API)
13+
// Download your service JSON file from the Google Developer Console
14+
// $obj_client = \GDS\Gateway\GoogleAPIClient::createClientFromJson('/path/to/your/service.json');
15+
// $obj_gateway = new \GDS\Gateway\GoogleAPIClient($obj_client, 'your-app-id');
16+
// $obj_store = new \GDS\Store('Book', $obj_gateway);
17+
18+
// Create a simple Entity object
19+
$obj_book = new GDS\Entity();
20+
$obj_book->title = 'Romeo and Juliet';
21+
$obj_book->author = 'William Shakespeare';
22+
$obj_book->isbn = '1840224339';
23+
24+
// Insert into the Datastore
25+
$obj_store->upsert($obj_book);
26+
27+
// Show the key
28+
echo "Created: ", $obj_book->getKeyId(), PHP_EOL;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* GDS Example - Create one record (using the array syntax), with no Schema
4+
*
5+
* @author Tom Walder <[email protected]>
6+
*/
7+
require_once('../_includes.php');
8+
9+
// This Store uses the default Protocol Buffer Gateway - for App Engine local development or live App Engine
10+
$obj_store = new \GDS\Store('Book');
11+
12+
// Alternative Gateway (remote JSON API)
13+
// Download your service JSON file from the Google Developer Console
14+
// $obj_client = \GDS\Gateway\GoogleAPIClient::createClientFromJson('/path/to/your/service.json');
15+
// $obj_gateway = new \GDS\Gateway\GoogleAPIClient($obj_client, 'your-app-id');
16+
// $obj_store = new \GDS\Store('Book', $obj_gateway);
17+
18+
// Create a simple Entity object
19+
$obj_book = $obj_store->createEntity([
20+
'title' => 'Romeo and Juliet',
21+
'author' => 'William Shakespeare',
22+
'isbn' => '1840224339'
23+
]);
24+
25+
// Insert into the Datastore
26+
$obj_store->upsert($obj_book);
27+
28+
// Show the key
29+
echo "Created: ", $obj_book->getKeyId(), PHP_EOL;

0 commit comments

Comments
 (0)