sql-cast-suggestion
Rule Details
With compiler v2, appending a type declaration to a column expression in a view's query doesn't generate a cast in SQL anymore, as that created conflicts with various database-specific behaviors. This rule ensures that such casts are added explicitly by suggesting possible missing SQL casts.
Examples
✅ Correct example
In the following example, the entity ListOfBooks
contains explicit casts for elements name2
and name3
, so the rule will not be triggered for these elements:
ts
namespace sap.capire.bookshop;
entity Books {
key ID : Integer;
@mandatory title : localized String(111);
@mandatory name : localized String(111);
}
entity ListOfBooks as SELECT from Books {
*, ID,
title || name as name1 : String,
cast (title || name as String) as name2,
cast (title || name as String) as name3 : String,
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
❌ Incorrect example
In the next example, the rule will be triggered for elements name1
and name2
because they require explicit casts:
ts
namespace sap.capire.bookshop;
entity Books {
key ID : Integer;
@mandatory title : localized String(111);
@mandatory name : localized String(111);
}
// Potential issue - Missing SQL cast for column expression?
// Potential issue - Missing SQL cast for column expression?
entity ListOfBooks as (
SELECT from Books {
title || name as name1 : String,
}
) UNION (
SELECT from Books {
title || name as name2 : String,
}
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Version
This rule was introduced in @sap/eslint-plugin-cds 1.0.8
.