ClanCats/Hydrahon

Possible to generate query without db connection?

Closed this issue · 2 comments

Hello. I'm wondering if it's possible to use Hydrahon to output a query without having to supply an extant db connection.

Thank you.

Yes this is possible, you could simply return the query string and parameters in your execution function:

$h = new \ClanCats\Hydrahon\Builder('mysql', function($query, $queryString, $queryParameters)
{
    return [$queryString, $queryParameters];
});

list($queryString, $queryParameters) = $h
    ->table('cars')
    ->select()
    ->where('fuel', 'gasoline')
    ->orderBy('popularity', 'desc')
    ->limit(10)
    ->execute();

var_dump($queryString);
var_dump($queryParameters);

Output:

string(76) "select * from `cars` where `fuel` = ? order by `popularity` desc limit 0, 10"
array(1) {
  [0]=>
  string(8) "gasoline"
}

@mario-deluna , thanks man. I really appreciate this example.