Skip to content

Commit

Permalink
Support Query readPreference (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
dplewis authored and davimacedo committed Jul 12, 2019
1 parent 102dd2f commit c6a158f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/Parse/ParseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ class ParseQuery
*/
private $limit = -1;

/**
* The read preference for the main query.
*
* @var string
*/
private $readPreference;

/**
* The read preference for the queries to include pointers.
*
* @var string
*/
private $includeReadPreference;

/**
* The read preference for the sub queries.
*
* @var string
*/
private $subqueryReadPreference;

/**
* Create a Parse Query for a given Parse Class.
*
Expand Down Expand Up @@ -176,6 +197,18 @@ public function _setConditions($conditions)
$this->limit = $entry;
break;

case 'readPreference':
$this->readPreference = $entry;
break;

case 'includeReadPreference':
$this->includeReadPreference = $entry;
break;

case 'subqueryReadPreference':
$this->subqueryReadPreference = $entry;
break;

// skip
case 'skip':
$this->skip = $entry;
Expand Down Expand Up @@ -491,6 +524,15 @@ public function _getOptions()
if ($this->count) {
$opts['count'] = $this->count;
}
if ($this->readPreference) {
$opts['readPreference'] = $this->readPreference;
}
if ($this->includeReadPreference) {
$opts['includeReadPreference'] = $this->includeReadPreference;
}
if ($this->subqueryReadPreference) {
$opts['subqueryReadPreference'] = $this->subqueryReadPreference;
}

return $opts;
}
Expand Down Expand Up @@ -1375,4 +1417,22 @@ public function relatedTo($key, $value)

return $this;
}

/**
* Changes the read preference that the backend will use when performing the query to the database.
*
* @param string $readPreference The read preference for the main query.
* @param string $includeReadPreference The read preference for the queries to include pointers.
* @param string $subqueryReadPreference The read preference for the sub queries.
*
* @return ParseQuery Returns the query, so you can chain this call.
*/
public function readPreference($readPreference, $includeReadPreference = null, $subqueryReadPreference = null)
{
$this->readPreference = $readPreference;
$this->includeReadPreference = $includeReadPreference;
$this->subqueryReadPreference = $subqueryReadPreference;

return $this;
}
}
6 changes: 5 additions & 1 deletion tests/Parse/ParseQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,7 @@ public function testGetAndSetConditions()
$query->notEqualTo('key2', 'value2');
$query->includeKey(['include1','include2']);
$query->excludeKey(['excludeMe','excludeMeToo']);
$query->readPreference('PRIMARY', 'SECONDARY', 'SECONDARY_PREFERRED');
$query->contains('container', 'item');
$query->addDescending('desc');
$query->addAscending('asc');
Expand Down Expand Up @@ -2528,7 +2529,10 @@ public function testGetAndSetConditions()
'limit' => 42,
'skip' => 24,
'order' => '-desc,asc',
'count' => 1
'count' => 1,
'readPreference' => 'PRIMARY',
'includeReadPreference' => 'SECONDARY',
'subqueryReadPreference' => 'SECONDARY_PREFERRED',
], $conditions, 'Conditions were different than expected');

$query2 = new ParseQuery('TestObject');
Expand Down

0 comments on commit c6a158f

Please sign in to comment.