Skip to content

Conversation

@rullzer
Copy link
Member

@rullzer rullzer commented Aug 30, 2019

Found when looking at nextcloud/mail#1969

Signed-off-by: Roeland Jago Douma [email protected]

@rullzer rullzer added bug 3. to review Waiting for reviews labels Aug 30, 2019
@rullzer rullzer added this to the Nextcloud 17 milestone Aug 30, 2019
@rullzer rullzer requested a review from ChristophWurst August 30, 2019 19:47
Found when looking at nextcloud/mail#1969

Signed-off-by: Roeland Jago Douma <[email protected]>
@kesselb
Copy link
Contributor

kesselb commented Aug 30, 2019

if($entity->id === null) {
$entity->setId((int)$qb->getLastInsertId());
}

If someone uses a string as id the value is already set before insert. So id is not null and getLastInsertId() is not called.

@rullzer
Copy link
Member Author

rullzer commented Aug 30, 2019

Ah mmm right... meh. let me think some moreabout it then.

@kesselb
Copy link
Contributor

kesselb commented Aug 30, 2019

Sorry 🙈 Let me rephrase it: I don't think we have to care about the case because its unlikely. If someone use a hash as id the hash should be set before the insert. We could throw a exception if the type is not integer and the id is null.


$qb->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
$qb->expr()->eq('id', $qb->createNamedParameter($id, $this->getParameterTypeForProperty($entity, 'id')))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was also my consideration. The only problem with this is the default type. It should be a new optional parameter with string as default and overwritten in this case

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, id has a default type of integer set. so this should actually work then

@ChristophWurst
Copy link
Member

ChristophWurst commented Sep 2, 2019

I don't think we have to care about the case because its unlikely. If someone use a hash as id the hash should be set before the insert. We could throw a exception if the type is not integer and the id is null.

See mail. This happens when you try to run an update on an existing row. Nextcloud will just take the id as int, even if it's a string. Then the generated query results in a SQL error.

inserts actually work as for every column the custom type is respected.

@kesselb
Copy link
Contributor

kesselb commented Sep 2, 2019

Sorry for the confusion 🙈

if($entity->id === null) {
$entity->setId((int)$qb->getLastInsertId());
}

For entities without id the lastInsertId is set as id. A entity with /** @var string $id **/ and no value will 💥 (if the setId method is properly typed of course).

I don't think we have to care about the case because its unlikely. If someone use a hash as id the hash should be set before the insert. We could throw a exception if the type is not integer and the id is null.

I was wondering if we need to care about this edge case. If someone complains we can still fix it later. I think for the mail app you usually try to insert entities with ids.

@ChristophWurst
Copy link
Member

Yep, lastInsertId is indeed a problem the way it's used here.

I was wondering if we need to care about this edge case. If someone complains we can still fix it later. I think for the mail app you usually try to insert entities with ids.

So the problem is actually inserting a row without an id value set and not having an auto increment id column. You can also have an int id without auto increment (for something where the id is provided and not auto assigned) and it will do 💥

@kesselb
Copy link
Contributor

kesselb commented Sep 2, 2019

So the problem is actually inserting a row without an id value set and not having an auto increment id column. You can also have an int id without auto increment (for something where the id is provided and not auto assigned) and it will do boom

You may interested in this former discussion: #13086

@ChristophWurst ChristophWurst added 4. to release Ready to be released and/or waiting for tests to finish and removed 3. to review Waiting for reviews labels Sep 3, 2019
@rullzer rullzer mentioned this pull request Sep 4, 2019
16 tasks
@rullzer
Copy link
Member Author

rullzer commented Sep 4, 2019

Ok maybe this isn't the most elegant way... let me consider a different approach later this evening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4. to release Ready to be released and/or waiting for tests to finish bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants