Skip to content

Use promises for query() method #61

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
Jun 25, 2018

Conversation

clue
Copy link
Contributor

@clue clue commented Jun 25, 2018

This PR updates the query() method to use promises instead of accepting an optional callback function. The updated query() method also accepts an array of query parameters for consistency with the queryStream() method (#57):

// old
$connection->query('CREATE TABLE test');
$connection->query('DELETE FROM user WHERE id < ?', $id);
$connection->query('SELECT * FROM user', function (QueryCommand $command) {
    if ($command->hasError()) {
        echo 'Error: ' . $command->getError()->getMessage() . PHP_EOL;
    } elseif (isset($command->resultRows)) {
        var_dump($command->resultRows);
    }
});

// new
$connection->query('CREATE TABLE test');
$connection->query('DELETE FROM user WHERE id < ?', [$id]);
$connection->query('SELECT * FROM user')->then(function (QueryCommand $command) {
    if (isset($command->resultRows)) {
        var_dump($command->resultRows);
    }
}, function (Exception $error) {
    echo 'Error: ' . $error->getMessage() . PHP_EOL;
});

This PR also includes relevant documentation and tests. The old method definition was somewhat inconsistent and under-documented. For instance, it had some limited streaming capability by not passing a callback. You should use the new queryStream() method instead.

Refs #57
Refs #4

@clue clue added this to the v0.4.0 milestone Jun 25, 2018
@clue clue requested a review from jsor June 25, 2018 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants