Skip to content

Cannot set NULL values using query builder #19314

Closed

Description

@PVince81

Trying to set expiration to null for this PR #19132

        $builder = $this->connection->getQueryBuilder();
        $builder
            ->update('share')
            ->set('expiration', 'NULL')
            ->where($builder->expr()->isNotNull('expiration'))
            ->andWhere($builder->expr()->neq('share_type', $builder->expr()->literal(\OC\Share\Constants::SHARE_TYPE_LINK)));

I first searched for info about how to do it with Doctrine until I discovered that ownCloud has its own QuoteHelper which doesn't seem to support passing null values.

Neither of these work:

  • ->set('expiration', null) => creates invalid SQL query expiration = (no value)
  • ->set('expiration', 'null') => the value is quoted expiration = "null"
  • ->set('expiration', 'NULL') => the value is quoted expiration = "NULL"
  • ->set('expiration', $qb->literal(null)) => the value is an empty string expiration = ""

I asked on the #doctrine IRC and was told that the proper syntax should be:
->set('expiration', 'null')

So, the QuoteHelper needs to be fixed to match Doctrine's way of working to avoid having further developers getting lost searching the internet for mismatching information.

@nickvergessen @DeepDiver1975 any reason why we needed the QuoteHelper in the first place ? So Doctrine isn't able to handle this properly ?

Activity

  1. added this to the milestone on Sep 23, 2015
  2. PVince81 commented on Sep 23, 2015

    @PVince81
    ContributorAuthor

    You can try it out by checking out the branch "sharing-removebogusexpirationdate-querybuilder" and running ./autotest.sh sqlite tests/lib/repair/repairinvalidshares.php

  3. nickvergessen commented on Sep 24, 2015

    @nickvergessen
    Contributor

    The quote helper takes care of adding ` around column names, so you don't have to do that manually for oracle anymore.

    Your PR can also be easily fixed by using a parameter:

            $builder
                ->update('share')
                ->set('expiration', $builder->createParameter('expiration'))
                ->where($builder->expr()->isNotNull('expiration'))
                ->andWhere($builder->expr()->neq('share_type', $builder->expr()->literal(\OC\Share\Constants::SHARE_TYPE_LINK)))
                ->setParameter('expiration', null);

    But I will also fix the 'null' way. Let's hope no one ever tries to name a column null

  4. PVince81 commented on Sep 24, 2015

    @PVince81
    ContributorAuthor

    @nickvergessen thanks a lot!

  5. locked as resolved and limited conversation to collaborators on Aug 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions