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

fix: dont use virtual key for UPDATE … where (<key>) in <subquery> #800

Merged
merged 10 commits into from
Sep 12, 2024

Conversation

patricebender
Copy link
Member

@patricebender patricebender commented Sep 7, 2024

For a draft enabled model:

namespace my;

entity Books {
  key ID : Integer;
  title  : String;
  stock  : Integer;
  author : Association to Authors;
}

entity Authors {
  key ID : Integer;
  name   : String;
  alive  : Boolean;
}

service CatalogService {
    @odata.draft.enabled
    @readonly entity Books as projection on my.Books;

    @readonly entity Authors as projection on my.Authors;
}

my.Books will have a field:

        "IsActiveEntity": {
          "type": "cds.Boolean",
          "key": true,
          "default": {
            "val": true
          },
          "@UI.Hidden": true
        },

which is marked as virtual by the lean draft implementation. This virtual key must not be part of the primary key matching which is done for a path expression in a UPDATE.where:

it('Update with path expressions in where is handled', () => {
const { UPDATE } = cds.ql
let u = UPDATE.entity({ ref: ['bookshop.Books'] }).where(
`author.name LIKE '%Bron%' or ( author.name LIKE '%King' and title = 'The Dark Tower') and stock >= 15`,
)
let expected = UPDATE.entity({ ref: ['bookshop.Books'] })
expected.UPDATE.where = [
{ list: [{ ref: ['Books2', 'ID'] }] },
'in',
CQL`
(SELECT Books.ID from bookshop.Books as Books
left join bookshop.Authors as author on author.ID = Books.author_ID
where author.name LIKE '%Bron%' or ( author.name LIKE '%King' and Books.title = 'The Dark Tower') and Books.stock >= 15
)
`,
]
expected.UPDATE.entity = {
as: 'Books2',
ref: ['bookshop.Books'],
}
let res = cqn4sql(u, model)
expect(JSON.parse(JSON.stringify(res))).to.deep.equal(JSON.parse(JSON.stringify(expected)))
})

TODO:

  • test this

For a draft enabled model:

```
namespace my;

entity Books {
  key ID : Integer;
  title  : String;
  stock  : Integer;
  author : Association to Authors;
}

entity Authors {
  key ID : Integer;
  name   : String;
  alive  : Boolean;
}

service CatalogService {
    @odata.draft.enabled
    @readonly entity Books as projection on my.Books;

    @readonly entity Authors as projection on my.Authors;
}
```

`my.Books` will have a field:
```
        "IsActiveEntity": {
          "type": "cds.Boolean",
          "key": true,
          "default": {
            "val": true
          },
          "@UI.Hidden": true
        },
```

which is marked as virtual by the lean draft implementation.
This virtual key must not be part of the primary key matching
which is done for a path expression in a `UPDATE.where`:

https://github.com/cap-js/cds-dbs/blob/982b8b796da4b577c5641039d2036816209c0437/db-service/test/cqn4sql/UPDATE.test.js#L73-L97

TODO:

- [] test this
@patricebender patricebender changed the title fix: dont use virtual key elements for UPDATE where condition fix: dont use virtual key for UPDATE … where (<key>) in <subquery> Sep 10, 2024
@patricebender
Copy link
Member Author

also check if UPDATEs on tables w/o primary keys cause issues

Copy link
Contributor

@David-Kunz David-Kunz left a comment

Choose a reason for hiding this comment

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

lgtm 🎸

@patricebender patricebender enabled auto-merge (squash) September 12, 2024 07:09
@patricebender patricebender merged commit d25af70 into main Sep 12, 2024
4 checks passed
@patricebender patricebender deleted the patrice/draft branch September 12, 2024 07:24
@cap-bots cap-bots mentioned this pull request Sep 12, 2024
johannes-vogel pushed a commit that referenced this pull request Oct 1, 2024
🤖 I have created a release *beep* *boop*
---


<details><summary>db-service: 1.13.0</summary>

##
[1.13.0](db-service-v1.12.1...db-service-v1.13.0)
(2024-10-01)


### Added

* Add quoted mode support
([#681](#681))
([43c7a6c](43c7a6c))


### Fixed

* **deep-queries:** properly return insert result
([#803](#803))
([8d800e2](8d800e2))
* dont use virtual key for `UPDATE … where (&lt;key&gt;) in <subquery>`
([#800](#800))
([d25af70](d25af70))
* reject all path expressions w/o foreign keys
([#806](#806))
([cd271a8](cd271a8))
</details>

<details><summary>hana: 1.3.0</summary>

##
[1.3.0](hana-v1.2.0...hana-v1.3.0)
(2024-10-01)


### Added

* Add quoted mode support
([#681](#681))
([43c7a6c](43c7a6c))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
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

Successfully merging this pull request may close these issues.

2 participants