ClanCats / Hydrahon

🐉 Fast & standalone PHP MySQL Query Builder library.
https://clancats.io/hydrahon/master/
MIT License
278 stars 58 forks source link

Insert ID and Rows Affected #29

Open NickStallman opened 4 years ago

NickStallman commented 4 years ago

Currently when inserting, updating and deleting there is no return value at all.

This should be trivial to add, by supplementing the FetchableInterface with a InsertableInterface (could return an array containing both insert id and rows affected) and ModifyableInterface (returning the rows affected).

This change would be backwards compatible so if the feature isn't used then no changes to the fetcher function are required.

I could provide a PR for this feature if it would be helpful?

mario-deluna commented 4 years ago

Hi there I can't believe I never answered this issue. I could blame the pandemic but I honestly just oversaw it.

I would absolutely love such a PR.

What I currently do, is something like this:

$h = new \ClanCats\Hydrahon\Builder('mysql', function($query, $queryString, $queryParameters) use($connection)
{
    $statement = $connection->prepare($queryString);
    $statement->execute($queryParameters);

    if ($query instanceof \ClanCats\Hydrahon\Query\Sql\FetchableInterface)
    {
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
    }
    elseif ($query instanceof Insert)
    {
        return $connection->lastInsertId();
    }
    else {
        return $statement->rowCount();
    }
});

But having multiple interfaces clearly indicating what data can be retrieved would be way cleaner.

Matix-Media commented 4 years ago

I think a feature where you could store the last inserted id would be amazing! Like this:

$insertion = $h->table("libraries")->insert(["name" => "test"]);
echo "Last inserted ID: " . $insertion->lastInsertedId;