driftphp / reactphp-dbal

DBAL for ReactPHP on top of Doctrine
https://driftphp.io
41 stars 6 forks source link

Insert method suggestion #10

Closed enumag closed 4 years ago

enumag commented 4 years ago

While implementing something similar I added my own insert implementation because I wasn't satisfied with yours. The point is that it feels unnatural to pass $values and $parameters separately. I'm aware it is more flexible since you can use NOW(), subqueries and what not but in reality I wouldn't mind using the QueryBuilder directly for such cases. I see the insert method as just a simple helper so I wanted API like this:

$connection->insert($table, ['column' => 'value', ...]);

It can be implemented quite easily:

    public function insert(string $table, array $values): PromiseInterface
    {
        $queryBuilder = $this->createQueryBuilder();
        $queryBuilder->insert($table);

        foreach ($values as $column => $value) {
            $queryBuilder
                ->setValue($column, ':' . $column)
                ->setParameter($column, $value);
        }

        return $this->query($queryBuilder);
    }

It might be nice to add an update and delete methods with similar API:

    public function update(string $table, array $values, array $where): PromiseInterface;
    public function delete(string $table, array $where): PromiseInterface;

Note: I don't really care if you use this or not since I'm not using Drift directly. It's just a suggestion. Use it or close it as you see fit. ;-)

Btw. this is incorrect since it's a delete method.

mmoreram commented 4 years ago

I'm working on it right now.

Thanks for the suggestions.

enumag commented 4 years ago

Closed by #11