Skip to content

Commit 5525e78

Browse files
author
twalder-docnet
committed
Add named parameter support to methods proviging GQL access
1 parent e216917 commit 5525e78

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

examples/query_params.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Query parameter examples
4+
*
5+
* @author Tom Walder <[email protected]>
6+
*/
7+
require_once('boilerplate.php');
8+
9+
// A single named parameter, "isbn"
10+
$obj_book = $obj_book_store->fetchOne("SELECT * FROM Book WHERE isbn = @isbn", ['isbn' => '1840224339']);
11+
describeResult($obj_book);
12+
13+
// Fetch n results
14+
$arr_books = $obj_book_store->fetchAll("SELECT * FROM Book WHERE isbn < @isbn", ['isbn' => '1840224339']);
15+
describeResult($arr_books);
16+
17+
// Query & pagination
18+
$arr_books = $obj_book_store->query("SELECT * FROM Book WHERE isbn > @isbn", ['isbn' => '1840224339']);
19+
while($arr_page = $obj_book_store->fetchPage(5)) {
20+
describeResult($arr_page);
21+
}
22+
23+
24+
25+
26+
/**
27+
* Helper function to simplify results display
28+
*
29+
* @param $mix_result
30+
* @param bool $bol_recurse
31+
*/
32+
function describeResult($mix_result, $bol_recurse = FALSE)
33+
{
34+
if($mix_result instanceof GDS\Entity) {
35+
$str_class = get_class($mix_result);
36+
echo "Found single result: [{$str_class}] {$mix_result->getKeyId()}, {$mix_result->title}, {$mix_result->isbn}, {$mix_result->author}", PHP_EOL;
37+
} elseif (is_array($mix_result)) {
38+
echo "Found ", count($mix_result), " results", PHP_EOL;
39+
if($bol_recurse) {
40+
foreach($mix_result as $mix_row) {
41+
describeResult($mix_row);
42+
}
43+
}
44+
} else {
45+
echo "No result(s) found", PHP_EOL;
46+
}
47+
}

src/GDS/Store.php

+22-8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ class Store
5353
*/
5454
private $str_last_query = NULL;
5555

56+
/**
57+
* Named parameters for the last query
58+
*
59+
* @var array|null
60+
*/
61+
private $arr_last_params = NULL;
62+
5663
/**
5764
* The last result cursor
5865
*
@@ -182,11 +189,13 @@ public function fetchByName($str_name)
182189
* Fetch Entities based on a GQL query
183190
*
184191
* @param $str_query
192+
* @param array|null $arr_params
185193
* @return Entity[]
186194
*/
187-
public function query($str_query)
195+
public function query($str_query, $arr_params = NULL)
188196
{
189197
$this->str_last_query = $str_query;
198+
$this->arr_last_params = $arr_params;
190199
$this->str_last_cursor = NULL;
191200
return $this;
192201
}
@@ -195,33 +204,35 @@ public function query($str_query)
195204
* Fetch ONE Entity based on a GQL query
196205
*
197206
* @param $str_query
207+
* @param array|null $arr_params
198208
* @return Entity
199209
*/
200-
public function fetchOne($str_query = NULL)
210+
public function fetchOne($str_query = NULL, $arr_params = NULL)
201211
{
202212
if(NULL !== $str_query) {
203-
$this->query($str_query);
213+
$this->query($str_query, $arr_params);
204214
}
205215
$arr_results = $this->obj_gateway
206216
->withTransaction($this->str_transaction_id)
207-
->gql($this->str_last_query . ' LIMIT 1');
217+
->gql($this->str_last_query . ' LIMIT 1', $this->arr_last_params);
208218
return $this->mapOneFromResults($arr_results);
209219
}
210220

211221
/**
212222
* Fetch Entities (optionally based on a GQL query)
213223
*
214224
* @param $str_query
225+
* @param array|null $arr_params
215226
* @return Entity[]
216227
*/
217-
public function fetchAll($str_query = NULL)
228+
public function fetchAll($str_query = NULL, $arr_params = NULL)
218229
{
219230
if(NULL !== $str_query) {
220-
$this->query($str_query);
231+
$this->query($str_query, $arr_params);
221232
}
222233
$arr_results = $this->obj_gateway
223234
->withTransaction($this->str_transaction_id)
224-
->gql($this->str_last_query);
235+
->gql($this->str_last_query, $this->arr_last_params);
225236
return $this->mapFromResults($arr_results);
226237
}
227238

@@ -235,7 +246,7 @@ public function fetchAll($str_query = NULL)
235246
public function fetchPage($int_page_size, $mix_offset = NULL)
236247
{
237248
$str_offset = '';
238-
$arr_params = [];
249+
$arr_params = (array)$this->arr_last_params;
239250
if(NULL !== $mix_offset) {
240251
if(is_int($mix_offset)) {
241252
$str_offset = 'OFFSET @intOffset';
@@ -248,6 +259,9 @@ public function fetchPage($int_page_size, $mix_offset = NULL)
248259
$str_offset = 'OFFSET @startCursor';
249260
$arr_params['startCursor'] = $this->str_last_cursor;
250261
}
262+
if(empty($arr_params)) {
263+
$arr_params = NULL;
264+
}
251265
$arr_results = $this->obj_gateway
252266
->withTransaction($this->str_transaction_id)
253267
->gql($this->str_last_query . " LIMIT {$int_page_size} {$str_offset}", $arr_params);

0 commit comments

Comments
 (0)