Skip to content
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

Last inserted ID #14

Open
bartvanhoutte opened this issue Apr 3, 2020 · 11 comments
Open

Last inserted ID #14

bartvanhoutte opened this issue Apr 3, 2020 · 11 comments

Comments

@bartvanhoutte
Copy link
Contributor

How do you get the last inserted ID (auto increment) after inserting something into the DB?

@mmoreram
Copy link
Member

mmoreram commented Apr 3, 2020

We should check how all implementations return this value and return a structure. Good point.

@bartvanhoutte
Copy link
Contributor Author

In clue/reactphp-sqlite you can get the last inserted ID through $result->insertId in the resulting promise. I suggest to add a getLastInsertedId method to Result?

@mmoreram
Copy link
Member

mmoreram commented Apr 4, 2020

@bartvanhoutte thanks for the response!
I've implemented it on mysql and sqlite, but the problem we have is Postgresql.
AFAIK, postgres has some specific behavior on this, so I need to check a bit more how to do it.
Any clues?

-> Temporary PR - #16

@bartvanhoutte
Copy link
Contributor Author

@mmoreram I've just tested #16 with SQLite and it works, so looks good to me. I'm afraid I can't help you with pgsql.

Small question on the side. I see $connection->update does not return a Result but $result->fetchAllRows() instead. Is there any reason for this? This seems to be null when updating a record using SQLite.

@mmoreram
Copy link
Member

mmoreram commented Apr 6, 2020

@bartvanhoutte yes, you're right. We should change this behavior.

@developernaren
Copy link
Contributor

https://stackoverflow.com/a/2944481/2159370 there is an answer for postgresql here. Let me know if I can help with anything.

@developernaren
Copy link
Contributor

developernaren commented Apr 6, 2020

/**
     * @param string $table
     * @param array  $values
     *
     * @return PromiseInterface
     */
    public function insert(
        string $table,
        array $values
    ): PromiseInterface {

        $queryBuilder = $this
            ->createQueryBuilder()
            ->insert($table)
            ->values(array_combine(
                array_keys($values),
                array_fill(0, count($values), '?')
            ))
            ->setParameters(array_values($values));


        if($this->driver instanceof PostgreSQLDriver) {
            return $this->queryBySQL($queryBuilder->getSQL() . ' RETURNING id', $queryBuilder->getParameters());
        }

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

I tried this and this is returning correct result for insertId, I get an array with index id with correct inserted value.

@developernaren
Copy link
Contributor

 /**
     * @param string $table
     * @param array  $values
     *
     * @return PromiseInterface
     */
    public function insert(
        string $table,
        array $values
    ): PromiseInterface {

        $queryBuilder = $this
            ->createQueryBuilder()
            ->insert($table)
            ->values(array_combine(
                array_keys($values),
                array_fill(0, count($values), '?')
            ))
            ->setParameters(array_values($values));

        if($this->driver instanceof PostgreSQLDriver) {
            $query = sprintf("SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME = '%s'", $table);
            return $this->queryBySQL($query)->then(function (Result $response) use($queryBuilder){
                $allRows = $response->fetchAllRows();
                $fields = array_map(function ($item){
                    return $item['column_name'];
                }, $allRows);

                return $this->queryBySQL($queryBuilder->getSQL() . ' RETURNING ' . implode(',', $fields), $queryBuilder->getParameters());
            });
        }

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

this seems to work perfectly, fetches the column as well.

@alexmorbo
Copy link

Any news to merge? Seems working fine?

@mmoreram
Copy link
Member

@alexmorbo I added some extra changes on the PR - #19

TBH, the PostgreSQL last id implementation is not the best one, but right now, is the one I see.
If you want to review it, that would be great :)

@marinhekman
Copy link
Contributor

marinhekman commented Nov 23, 2022

Is this issue not resolved already?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants