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

Autoincrement via identity columns on Oracle #5443

Closed
wants to merge 1 commit into from

Conversation

morozov
Copy link
Member

@morozov morozov commented Jun 12, 2022

See #4744, #5396.

@morozov
Copy link
Member Author

morozov commented Jun 12, 2022

@greg0ire @derrabus if we decide to move forward with this, we need to consider changes in the IdentityGenerator API in the ORM since Oracle does support identity columns but its drivers don't support lastInsertId().

Oracle allows retrieval of the identity column value as follows:

$stmt = $connection->prepare('INSERT INTO AUTO_INCREMENT_TABLE VALUES(DEFAULT) RETURNING ID INTO :ID');
$stmt->bindParam('id', $id);
$stmt->executeStatement();
echo $id, PHP_EOL;
// 1

Despite it not being widespread, personally, I believe it's the most reasonable way of dealing with autogenerated values in SQL.

The current IdentityGenerator API design is quite unfortunate: it exposes the implementation details like sequence name and limits itself to being used before or after the insert (which is also an implementation detail). This limitation doesn't allow to implement the above approach. Instead, this API should do the insert. E.g.:

interface SomeNewName
{
    public function insert(EntityManagerInterface $em, $entity);
}

In addition to having autoincrement columns implemented in a more similar way across platforms, this may allow dropping the support of sequences at all, since they are used only as an implementation of autoincrement columns.

What do you think? Would you be interested in making the necessary API changes in the ORM? Otherwise, I don't think we can proceed with this change.

@derrabus
Copy link
Member

The new API you're suggesting makes perfect sense to me. However, I wonder if a piece of code that uses the DBAL for performing inserts should be aware of how to retrieve the generated ID for the current DBMS. Would it make sense to create an API for that in DBAL, something like a low-level version of Connection::insert() that returns the generated ID or writes it into an output variable?

@morozov
Copy link
Member Author

morozov commented Jun 13, 2022

I wonder if a piece of code that uses the DBAL for performing inserts should be aware of how to retrieve the generated ID for the current DBMS.

I tried but couldn't really come up with an abstract DBAL-level API for that. I propose that we implement a working code prototype first and then decide which library will own the new API. Personally, I believe that it should belong to the ORM because when it comes to building SQL, the DBAL isn't aware of any entity metadata while the ORM is.

@greg0ire
Copy link
Member

greg0ire commented Jun 13, 2022

@morozov
Copy link
Member Author

morozov commented Jun 13, 2022

The last one is about getting the entity state, and I'm not sure towards what we should migrate it, but maybe you do?

I'm not familiar with the ORM at all, but the last case looks like a hack to me — determining the state of the entity based on whether or not it has an identifier. Why cannot this state be explicitly updated after a successful attempt to persist the entity?

We can agree that the responsibilities of the new method will be (in no particular order):

  1. Do the insert.
  2. Generate the identifier.
  3. Set it on the entity.

If this operation succeeds, then something else can update the state of the entity to something appropriate.

@morozov
Copy link
Member Author

morozov commented Jun 30, 2022

Just to clarify, I'm not going to make the API changes in the ORM myself. Somebody else will have to do that. Do you @greg0ire or @derrabus want to give it a try or maybe someone from the community does?

@greg0ire
Copy link
Member

I don't feel up to that task, sorry 😞

@morozov
Copy link
Member Author

morozov commented Jul 3, 2022

Closing it for now.

@morozov morozov closed this Jul 3, 2022
@morozov morozov deleted the oracle-identity branch July 3, 2022 18:09
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants