-
Notifications
You must be signed in to change notification settings - Fork 140
Review databases "compiled to" from CAP Java #2329
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
base: main
Are you sure you want to change the base?
Changes from all commits
459586d
10986c8
2de4a7a
e5e563f
ec4b149
0cfc89d
8245fd0
c2117ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,7 +13,24 @@ Databases are deployed based on the entity definitions in your CDS models. This | |||||||||
|
|
||||||||||
| ## Using `cds compile`, ... | ||||||||||
|
|
||||||||||
| CDS compilation to database-specific DDLs is handled by the `cds compile` command, which is part of the [`cds` CLI](../../tools/cds-cli). When you run `cds deploy` or `cds watch`, this command is invoked automatically to generate the necessary DDL statements for your target database. | ||||||||||
| CDS compilation to database-specific DDLs is handled by the [`cds compile`](../../tools/cds-cli#cds-compile) command, which is part of the [`cds` CLI](../../tools/cds-cli). When you run `cds deploy` or `cds watch`, this command is invoked automatically to generate the necessary DDL statements for your target database. | ||||||||||
|
|
||||||||||
| In CAP Java, the DDL is generated by the [Maven build](../../java/developing-applications/building#maven-build-options) which uses the [CDS Maven Plugin](../../java/developing-applications/building#cds-maven-plugin)'s [cds](../../java/assets/cds-maven-plugin-site/cds-mojo.html) goal to invoke `cds deploy --dry`. | ||||||||||
|
|
||||||||||
| :::details | ||||||||||
| A build configuration to create the DDL specific for H2: | ||||||||||
|
|
||||||||||
| ```xml | ||||||||||
| <execution> | ||||||||||
| <goals><goal>cds</goal></goals> | ||||||||||
| <configuration> | ||||||||||
| <commands> | ||||||||||
| <command>deploy --to h2 --dry --out "${project.basedir}/src/main/resources/schema-h2.sql"</command> | ||||||||||
| </commands> | ||||||||||
| </configuration> | ||||||||||
| </execution> | ||||||||||
| ``` | ||||||||||
| ::: | ||||||||||
|
|
||||||||||
| You can also run the command manually to see the generated DDL for your models. For example, to inspect what the SQL DDL for your entire model would look like, simply run: | ||||||||||
|
|
||||||||||
|
|
@@ -60,32 +77,9 @@ code --diff _out/c/sqlite.sql _out/c/h2.sql | |||||||||
| > [!tip] CDS models are database-agnostic | ||||||||||
| > CDS models are designed to be database-agnostic, allowing you to switch between different databases with minimal changes. The `--dialect` option helps you see how your models translate to different database-specific DDLs. \ | ||||||||||
|
|
||||||||||
|
|
||||||||||
| ### Dialects by `cds env` Profiles | ||||||||||
|
|
||||||||||
| The dialect is automatically inferred from your project configuration, and the current profile, so you typically don't need to specify it explicitly. For example, if your project is configured to use SAP HANA in production and SQLite in development, the respective dialects will be applied automatically. | ||||||||||
| Try this out using the `--profile` option: | ||||||||||
|
|
||||||||||
| ```shell | ||||||||||
| cds compile \* --to sql --profile development | ||||||||||
| cds compile \* --to sql --profile production | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| ::: details Use `cds env` to check your effective configurations: | ||||||||||
| ```shell | ||||||||||
| cds env requires.db --profile development | ||||||||||
| cds env requires.db --profile production | ||||||||||
| ``` | ||||||||||
| ::: | ||||||||||
|
|
||||||||||
| > [!tip] Dialects are inferred from profiles automatically | ||||||||||
| > You typically don't need to specify the `--dialect` option manually, as it is derived from your project configuration and the active profile. | ||||||||||
|
|
||||||||||
|
|
||||||||||
|
|
||||||||||
| ### Using `cds deploy` | ||||||||||
|
|
||||||||||
| We can use `cds deploy` to inspect the generated DDL without actually deploying it, by using the `--dry` option. This will print the ultimate DDL statements to the console instead of executing them against the database, for example: | ||||||||||
| We can use `cds deploy` to inspect the generated DDL without actually deploying it, by using the `--dry` option. This will print the DDL statements to the console instead of executing them against the database, for example: | ||||||||||
|
|
||||||||||
| ```shell | ||||||||||
| cds deploy --dry | ||||||||||
|
|
@@ -134,6 +128,24 @@ Essentially, `cds deploy` calls `cds compile --to sql` under the hood, but goe | |||||||||
| > [!note] Ad-hoc Deployments | ||||||||||
| > Without the `--dry` option, `cds deploy` would not only compile your CDS models to DDL, but would also do an ad-hoc deployment to the target database, if available. How that works is explained in more detail in the database-specific guides for [_SAP HANA_](hana), [_SQLite_](sqlite), and [_PostgreSQL_](postgres). | ||||||||||
|
|
||||||||||
| ### Dialects by `cds env` Profiles for CAP Node.js | ||||||||||
|
|
||||||||||
| The dialect is automatically inferred from your project configuration, and the current profile, so you typically don't need to specify it explicitly. For example, if your project is configured to use SAP HANA in production and SQLite in development, the respective dialects will be applied automatically. Try this out using the `--profile` option: | ||||||||||
|
|
||||||||||
| ```shell | ||||||||||
| cds deploy --dry --profile development | ||||||||||
| cds deploy --dry --profile production | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| ::: details Use `cds env` to check your effective configurations: | ||||||||||
| ```shell | ||||||||||
| cds env requires.db --profile development | ||||||||||
| cds env requires.db --profile production | ||||||||||
| ``` | ||||||||||
| ::: | ||||||||||
|
|
||||||||||
| > [!tip] Dialects are inferred from profiles automatically | ||||||||||
| > You typically don't need to specify the `--dialect` option manually, as it is derived from your project configuration and the active profile. | ||||||||||
|
|
||||||||||
|
|
||||||||||
| ## CDL ⇒ DDL Translation | ||||||||||
|
|
@@ -181,13 +193,16 @@ CREATE TABLE sap_capire_bookshop_Books_Details ( ... ); | |||||||||
| ``` | ||||||||||
| ::: | ||||||||||
|
|
||||||||||
| > [!tip] Guaranteed & Stable Slugification | ||||||||||
| > The slugification effects are guaranteed and stable, which means that you can rely on it and use the slugified names in native SQL queries. For example, both of the following CQL queries are equivalent and will work as expected: | ||||||||||
| > [!tip] You may use slugified names in CAP Node.js | ||||||||||
| > In CAP Node.js, the slugification effects are guaranteed and stable, which means that you can rely on it and use the slugified names in _native SQL_ queries. For example, both of the following CQL queries are equivalent and will work as expected: | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Or maybe one of these: ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Or maybe one of these: slugged, slugify, sluiced, slugabed? |
||||||||||
| > | ||||||||||
| > ```js | ||||||||||
| > await cds.run `SELECT from sap.capire.bookshop.Books` | ||||||||||
| > await cds.run `SELECT from sap_capire_bookshop_Books` | ||||||||||
| > ``` | ||||||||||
|
|
||||||||||
| ```js | ||||||||||
| await cds.run `SELECT from sap.capire.bookshop.Books` | ||||||||||
| await cds.run `SELECT from sap_capire_bookshop_Books` | ||||||||||
| ``` | ||||||||||
| > [!warning] Don't use slugified names in CAP Java | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Or maybe one of these: slugged, slugify, sluiced, slugabed? |
||||||||||
| In CAP Java, you can use fully qualified entity names with dots. The slugified names can't be used in CQL queries. | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Or maybe one of these: slugged, slugify, sluiced, slugabed? |
||||||||||
|
|
||||||||||
| > [!tip] | ||||||||||
| > Prefer entity names like `Books.Details` over _CamelCase_ variants like `BooksDetails`. While both work equally, they show up differently in native tools of databases that don't preserve case, for example in SAP HANA: The former will show up as `BOOKS_DETAILS`, while the latter shows up as `BOOKSDETAILS`, which is harder to read. | ||||||||||
|
|
@@ -276,14 +291,23 @@ CREATE TABLE Books ( | |||||||||
| ``` | ||||||||||
| ::: | ||||||||||
|
|
||||||||||
| > [!tip] Guaranteed & Stable Flattening | ||||||||||
| > The flattening effects are guaranteed and stable, which means that you can rely on it and use the flattened elements in native SQL queries. For example, both of the following CQL queries are equivalent and would work as expected: | ||||||||||
| The flattening effects for structured elements are guaranteed and stable. If a CDS model is compiled for usage with OData only the flattened elements are preserved in the CDS model. | ||||||||||
|
|
||||||||||
| ```js | ||||||||||
| await cds.run `SELECT price.amount from Books` | ||||||||||
| await cds.run `SELECT price_amount from Books` | ||||||||||
| ``` | ||||||||||
| > [!tip] Use flattening in runtime queries in CAP Node.js | ||||||||||
| > The flattening effects can also be used at runtime with CAP Node.js. For example, both of the following CQL queries are equivalent and would work as expected: | ||||||||||
| > | ||||||||||
| > ```js | ||||||||||
| > await cds.run `SELECT price.amount from Books` | ||||||||||
| > await cds.run `SELECT price_amount from Books` | ||||||||||
| > ``` | ||||||||||
|
|
||||||||||
| > [!warning] No flattening in runtime queries in CAP Java | ||||||||||
| > CAP Java does not perform an automatic flattening of path expressions: | ||||||||||
| > | ||||||||||
| > ```java | ||||||||||
| > Select.from("Books").columns("price_amount"); // valid | ||||||||||
| > Select.from("Books").columns("price.amount"); // invalid | ||||||||||
| > ``` | ||||||||||
|
|
||||||||||
| ### Associations ⇒ JOINs | ||||||||||
|
|
||||||||||
|
|
@@ -467,13 +491,18 @@ However, even though CAP allows this, and handles all accesses correctly, it is | |||||||||
| > [!warning] DON'T use Database-Invalid Names! | ||||||||||
| > It's **strongly discouraged** to use names that contain non-ASCII characters, or conflict with database reserved words. Even more avoid [delimited names](../../cds/cdl#keywords-identifiers) in CDS models in the first place, as that impacts readability of your models. | ||||||||||
|
|
||||||||||
| > [!warning] Avoid using reserved Java keywords | ||||||||||
| > A CAP Java project usually uses generated [accessor interfaces](../../java/cds-data#generated-accessor-interfaces). Avoid using reserved Java keywords as identifiers in a CDS model as using Java keywords might cause generated interfaces that don't compile. You may use the `@cds.java.name` annotation to mitigate such conflicts. | ||||||||||
|
|
||||||||||
|
|
||||||||||
| ###### reserved-words | ||||||||||
| > [!tip] Lists of Reserved Words | ||||||||||
| > Check out the reserved words for the databases you are targeting: \ | ||||||||||
| > [_SAP HANA_](https://help.sap.com/docs/HANA_CLOUD_DATABASE/c1d3f60099654ecfb3fe36ac93c121bb/28bcd6af3eb6437892719f7c27a8a285.html) | ||||||||||
| > , [_SQLite_](https://www.sqlite.org/lang_keywords.html) | ||||||||||
| > , [_H2_](https://www.h2database.com/html/advanced.html#keywords) | ||||||||||
| > , [_PostgreSQL_](https://www.postgresql.org/docs/current/sql-keywords-appendix.html) | ||||||||||
| > , [_Java_](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html) | ||||||||||
|
|
||||||||||
|
|
||||||||||
|
|
||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe one of these: slugged, slugify, sluiced, slugabed?