Skip to content

Commit c6a158f

Browse files
dplewisdavimacedo
authored andcommitted
Support Query readPreference (#446)
1 parent 102dd2f commit c6a158f

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/Parse/ParseQuery.php

+60
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,27 @@ class ParseQuery
7878
*/
7979
private $limit = -1;
8080

81+
/**
82+
* The read preference for the main query.
83+
*
84+
* @var string
85+
*/
86+
private $readPreference;
87+
88+
/**
89+
* The read preference for the queries to include pointers.
90+
*
91+
* @var string
92+
*/
93+
private $includeReadPreference;
94+
95+
/**
96+
* The read preference for the sub queries.
97+
*
98+
* @var string
99+
*/
100+
private $subqueryReadPreference;
101+
81102
/**
82103
* Create a Parse Query for a given Parse Class.
83104
*
@@ -176,6 +197,18 @@ public function _setConditions($conditions)
176197
$this->limit = $entry;
177198
break;
178199

200+
case 'readPreference':
201+
$this->readPreference = $entry;
202+
break;
203+
204+
case 'includeReadPreference':
205+
$this->includeReadPreference = $entry;
206+
break;
207+
208+
case 'subqueryReadPreference':
209+
$this->subqueryReadPreference = $entry;
210+
break;
211+
179212
// skip
180213
case 'skip':
181214
$this->skip = $entry;
@@ -491,6 +524,15 @@ public function _getOptions()
491524
if ($this->count) {
492525
$opts['count'] = $this->count;
493526
}
527+
if ($this->readPreference) {
528+
$opts['readPreference'] = $this->readPreference;
529+
}
530+
if ($this->includeReadPreference) {
531+
$opts['includeReadPreference'] = $this->includeReadPreference;
532+
}
533+
if ($this->subqueryReadPreference) {
534+
$opts['subqueryReadPreference'] = $this->subqueryReadPreference;
535+
}
494536

495537
return $opts;
496538
}
@@ -1375,4 +1417,22 @@ public function relatedTo($key, $value)
13751417

13761418
return $this;
13771419
}
1420+
1421+
/**
1422+
* Changes the read preference that the backend will use when performing the query to the database.
1423+
*
1424+
* @param string $readPreference The read preference for the main query.
1425+
* @param string $includeReadPreference The read preference for the queries to include pointers.
1426+
* @param string $subqueryReadPreference The read preference for the sub queries.
1427+
*
1428+
* @return ParseQuery Returns the query, so you can chain this call.
1429+
*/
1430+
public function readPreference($readPreference, $includeReadPreference = null, $subqueryReadPreference = null)
1431+
{
1432+
$this->readPreference = $readPreference;
1433+
$this->includeReadPreference = $includeReadPreference;
1434+
$this->subqueryReadPreference = $subqueryReadPreference;
1435+
1436+
return $this;
1437+
}
13781438
}

tests/Parse/ParseQueryTest.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,7 @@ public function testGetAndSetConditions()
24992499
$query->notEqualTo('key2', 'value2');
25002500
$query->includeKey(['include1','include2']);
25012501
$query->excludeKey(['excludeMe','excludeMeToo']);
2502+
$query->readPreference('PRIMARY', 'SECONDARY', 'SECONDARY_PREFERRED');
25022503
$query->contains('container', 'item');
25032504
$query->addDescending('desc');
25042505
$query->addAscending('asc');
@@ -2528,7 +2529,10 @@ public function testGetAndSetConditions()
25282529
'limit' => 42,
25292530
'skip' => 24,
25302531
'order' => '-desc,asc',
2531-
'count' => 1
2532+
'count' => 1,
2533+
'readPreference' => 'PRIMARY',
2534+
'includeReadPreference' => 'SECONDARY',
2535+
'subqueryReadPreference' => 'SECONDARY_PREFERRED',
25322536
], $conditions, 'Conditions were different than expected');
25332537

25342538
$query2 = new ParseQuery('TestObject');

0 commit comments

Comments
 (0)