ClanCats/Hydrahon

Insert ID and Rows Affected

Opened this issue · 2 comments

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?

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.

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;