-
Notifications
You must be signed in to change notification settings - Fork 139
cds 9: modified input validations #1809
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
Changes from all commits
f54ba1b
5d77383
6be63b5
81c1dda
bef4b49
40d9039
4b864aa
e66a912
9183de4
44b4197
a063a68
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 |
|---|---|---|
|
|
@@ -784,6 +784,46 @@ Find here a collection of resources on selected databases and their reference do | |
|
|
||
| ## Database Constraints | ||
|
|
||
| ### Not Null | ||
|
|
||
| You can specify that a column's value must not be `NULL` by adding the [`not null` constraint](../cds/cdl#null-values) to the element, for example: | ||
|
|
||
| ```cds | ||
| entity Books { | ||
| key ID: Integer; | ||
| title: String not null; | ||
| } | ||
| ``` | ||
|
|
||
|
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. Should we mention that key elements are implicitly If an element is defined in a view as Moreover
Contributor
Author
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.
How does one achieve this? Is that even possible?
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. I checked and have the impression that it's not possible. But then, the question arises: How do I express that a virtual/calculated/@CoreComputed element is guaranteed to not be null?
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. I have double-checked with @stewsk - you can actually define a virtual element, which is type NotNullableString : String not null;
view V as select from E { *, 'x' as nns : NotNullableString };this is not possible, however: view V as select from E { *, 'x' as nns : String not null }; |
||
|
|
||
| ### Unique | ||
|
|
||
| Annotate an entity with `@assert.unique.<constraintName>`, specifying one or more element combinations to enforce uniqueness checks on all CREATE and UPDATE operations. For example: | ||
|
|
||
| ```cds | ||
| @assert.unique: { | ||
| locale: [ parent, locale ], | ||
| timeslice: [ parent, validFrom ], | ||
| } | ||
| entity LocalizedTemporalData { | ||
| key record_ID : UUID; // technical primary key | ||
| parent : Association to Data; | ||
| locale : String; | ||
| validFrom : Date; | ||
| validTo : Date; | ||
| } | ||
| ``` | ||
| {.indent} | ||
|
|
||
| The value of the annotation is an array of paths referring to elements in the entity. These elements may be of a scalar type, structs, or managed associations. Individual foreign keys or unmanaged associations are not supported. | ||
|
|
||
| If structured elements are specified, the unique constraint will contain all columns stemming from it. If the path points to a managed association, the unique constraint will contain all foreign key columns stemming from it. | ||
| ::: tip | ||
| You don't need to specify `@assert.unique` constraints for the primary key elements of an entity as these are automatically secured by a SQL `PRIMARY KEY` constraint. | ||
| ::: | ||
|
|
||
| ### Foreign Keys | ||
|
|
||
| The information about foreign key relations contained in the associations of CDS models can be used to generate foreign key constraints on the database tables. Within CAP, referential consistency is established only at commit. The ["deferred" concept for foreign key constraints](https://www.sqlite.org/foreignkeys.html) in SQL databases allows the constraints to be checked and enforced at the time of the [COMMIT statement within a transaction](https://www.sqlite.org/lang_transaction.html) rather than immediately when the data is modified, providing more flexibility in maintaining data integrity. | ||
|
|
||
| Enable generation of foreign key constraints on the database with: | ||
|
|
@@ -906,7 +946,7 @@ TODO: remove the above with cds9 | |
| The `@sap/cds-compiler` and all CAP Node.js database services come with out of the box support for common OData functions. | ||
|
|
||
| ::: warning Case Sensitivity | ||
| The OData function mappings are case-sensitive and must be written as in the list below. | ||
| The OData function mappings are case-sensitive and must be written as in the list below. | ||
| ::: | ||
|
|
||
| Assuming you have the following entity definition: | ||
|
|
@@ -940,58 +980,58 @@ For example, `startsWith` instead of `startswith` will be passed as-is to the da | |
|
|
||
| #### String Functions | ||
|
|
||
| - `concat(x, y, ...)` | ||
| - `concat(x, y, ...)` | ||
| Concatenates the given strings or numbers. | ||
|
|
||
| - `trim(x)` | ||
| - `trim(x)` | ||
| Removes leading and trailing whitespaces. | ||
|
|
||
| - `contains(x, y)` | ||
| - `contains(x, y)` | ||
| Checks whether `y` is contained in `x` (case-sensitive). | ||
|
|
||
| - `startswith(x, y)` | ||
| - `startswith(x, y)` | ||
| Checks whether `y` starts with `x` (case-sensitive). | ||
|
|
||
| - `endswith(x, y)` | ||
| - `endswith(x, y)` | ||
| Checks whether `y` ends with `x` (case-sensitive). | ||
|
|
||
| - `matchespattern(x, y)` | ||
| - `matchespattern(x, y)` | ||
| Checks whether `x` matches the regular expression `y`. | ||
|
|
||
| - `indexof(x, y)` <sup>1</sup> | ||
| - `indexof(x, y)` <sup>1</sup> | ||
| Returns the index of the first occurrence of `y` in `x` (case-sensitive). | ||
|
|
||
| - `substring(x, i, n?)` <sup>1</sup> | ||
| - `substring(x, i, n?)` <sup>1</sup> | ||
| Extracts a substring from `x` starting at index `i` (0-based) with an optional length `n`. | ||
|
|
||
| | Parameter | Positive | Negative | Omitted | ||
| | --- | --- | --- | -- | | ||
| | `i` | starts at index `i` | starts `i` positions before the end | | ||
| | `n` | extracts `n` characters | invalid | extracts until the end of the string | ||
|
|
||
| - `length(x)` | ||
| - `length(x)` | ||
| Returns the length of the string `x`. | ||
|
|
||
| - `tolower(x)` | ||
| - `tolower(x)` | ||
| Converts all characters in `x` to lowercase. | ||
|
|
||
| - `toupper(x)` | ||
| - `toupper(x)` | ||
| Converts all characters in `x` to uppercase. | ||
|
|
||
| > <sup>1</sup> These functions work zero-based. For example, `substring('abcdef', 1, 3)` returns 'bcd' | ||
|
|
||
| #### Numeric Functions | ||
|
|
||
| - `ceiling(x)` | ||
| - `ceiling(x)` | ||
| Rounds the numeric parameter up to the nearest integer. | ||
|
|
||
| - `floor(x)` | ||
| - `floor(x)` | ||
| Rounds the numeric parameter down to the nearest integer. | ||
|
|
||
| - `round(x)` | ||
| Rounds the numeric parameter to the nearest integer. | ||
| - `round(x)` | ||
| Rounds the numeric parameter to the nearest integer. | ||
| The midpoint between two integers is rounded away from zero (e.g., `0.5` → `1` and `-0.5` → `-1`). | ||
|
|
||
| ::: warning `round` function with more than one argument | ||
| Note that most databases support `round` functions with multiple arguments, | ||
| the second parameter being the precision. SAP HANA even has a third argument which is the rounding mode. | ||
|
|
@@ -1000,24 +1040,24 @@ For example, `startsWith` instead of `startswith` will be passed as-is to the da | |
|
|
||
| #### Date and Time Functions | ||
|
|
||
| - `year(x)`, `month(x)`, `day(x)`, `hour(x)`, `minute(x)`, `second(x)` | ||
| - `year(x)`, `month(x)`, `day(x)`, `hour(x)`, `minute(x)`, `second(x)` | ||
| Extracts and returns specific date / time parts as integer value from a given `cds.DateTime`, `cds.Date`, or `cds.Time`. | ||
|
|
||
| - `time(x)`, `date(x)` | ||
| - `time(x)`, `date(x)` | ||
| Extracts and returns a time or date from a given `cds.DateTime`, `cds.Date`, or `cds.Time`. | ||
|
|
||
| - `fractionalseconds(x)` | ||
| - `fractionalseconds(x)` | ||
| Returns a `Decimal` representing the fractional seconds for a given `cds.Timestamp`. | ||
|
|
||
| - `maxdatetime()` | ||
| - `maxdatetime()` | ||
| Returns the latest possible point in time: `'9999-12-31T23:59:59.999Z'`. | ||
|
|
||
| - `mindatetime()` | ||
| - `mindatetime()` | ||
| Returns the earliest possible point in time: `'0001-01-01T00:00:00.000Z'`. | ||
|
|
||
| #### Aggregate Functions | ||
|
|
||
| - `min(x)`, `max(x)`, `sum(x)`, `average(x)`, `count(x)`, `countdistinct(x)` | ||
| - `min(x)`, `max(x)`, `sum(x)`, `average(x)`, `count(x)`, `countdistinct(x)` | ||
| Standard aggregate functions used to calculate minimum, maximum, sum, average, count, and distinct count of values. | ||
|
|
||
|
|
||
|
|
@@ -1030,30 +1070,30 @@ out of the box support for some common SAP HANA functions, to further increase t | |
| For the SAP HANA functions, both usages are allowed: all-lowercase as given above, as well as all-uppercase. | ||
| ::: | ||
|
|
||
| - `years_between` | ||
| - `years_between` | ||
| Computes the number of years between two specified dates. ([link](https://help.sap.com/docs/hana-cloud-database/sap-hana-cloud-sap-hana-database-sql-reference-guide/years-between-function-datetime?locale=en-US)) | ||
| - `months_between` | ||
| - `months_between` | ||
| Computes the number of months between two specified dates. ([link](https://help.sap.com/docs/hana-cloud-database/sap-hana-cloud-sap-hana-database-sql-reference-guide/months-between-function-datetime?locale=en-US)) | ||
| - `days_between` | ||
| - `days_between` | ||
| Computes the number of days between two specified dates. ([link](https://help.sap.com/docs/hana-cloud-database/sap-hana-cloud-sap-hana-database-sql-reference-guide/days-between-function-datetime?locale=en-US)) | ||
| - `seconds_between` | ||
| - `seconds_between` | ||
| Computes the number of seconds between two specified dates. ([link](https://help.sap.com/docs/hana-cloud-database/sap-hana-cloud-sap-hana-database-sql-reference-guide/seconds-between-function-datetime?locale=en-US)) | ||
| - `nano100_between` | ||
| - `nano100_between` | ||
| Computes the time difference between two dates to the precision of 0.1 microseconds. ([link](https://help.sap.com/docs/hana-cloud-database/sap-hana-cloud-sap-hana-database-sql-reference-guide/nano100-between-function-datetime?locale=en-US)) | ||
|
|
||
| ### Special Runtime Functions | ||
|
|
||
| In addition to the OData and SAP HANA standard functions, the **CAP runtimes** provides special functions that are only available for runtime queries: | ||
|
|
||
| - `search(x, y)` | ||
| - `search(x, y)` | ||
| Checks whether `y` is contained in any element of `x` (fuzzy matching may apply). | ||
| See [Searching Data](../guides/providing-services#searching-data) for more details. | ||
|
|
||
| - `session_context(<var>)` | ||
| - `session_context(<var>)` | ||
| Utilizes standard variable names to maintain session context. | ||
| Refer to [Session Variables](#session-variables) for additional information. | ||
|
|
||
| - `now()` | ||
| - `now()` | ||
| Returns the current timestamp. | ||
|
|
||
| ## Using Native Features { #native-db-functions} | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.