Skip to content

Support Query readPreference #446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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