Feature Request: SQLSCRIPT - Dynamic Type Naming
There are scenarios where being able to set the Type name in a query dynamically would be useful. For example:
LET sources = SELECT FROM Sources WHERE id='abc';
IF($sources.size() > 0){
LET source = $sources[0];
LET type = $source.type;
LET target = SELECT FROM $type WHERE id='abc';
CREATE EDGE IS_SOURCED_FROM FROM $target TO $source IF NOT EXISTS;
};
Currently, this isn't supported and would lead to the error on line 5:
Type with name '$type' was not found
A related missing feature in the above code is also that CREATE EDGE $edgetype FROM $source TO $ target does not allow variable (string) edge-type names.
Actual supported variables:
BEGIN;
LET sources = SELECT FROM V1 WHERE id = '1';
LET source = $sources[0];
LET type = $source.vType;
LET target = SELECT FROM $type WHERE id = '9';
LET edgeType = 'HasSource';
LET e = CREATE EDGE $edgeType FROM $target TO $source IF NOT EXISTS ;
COMMIT;
RETURN $e;
more tests needed to discover corners cases to be supported
some notes on how to write scripts with dynamic type definitions: this works:
LET docType = 'TheDoc';
LET d =INSERT INTO $docType SET id = 1;
this doesn't work:
LET docType = 'TheDoc';
INSERT INTO $docType SET id = 1;
This is due to how the plan is create and executed. In the second example the INSERT... is executed immediately , not waiting for the execution of the LET statement before.
new level unlocked:
BEGIN;
LET vTypes = ['V1', 'V2', 'V3'];
FOREACH ($vType IN $vTypes) {
CREATE VERTEX TYPE $vType EXTENDS V;
}
LET eTypes = ['E1', 'E2', 'E3'];
FOREACH ($eType IN $eTypes) {
CREATE EDGE TYPE $eType EXTENDS E;
}
LET dTypes = ['D1', 'D2', 'D3'];
FOREACH ($dType IN $dTypes) {
CREATE DOCUMENT TYPE $dType ;
}
LET types = ['V1', 'V2', 'V3','E1', 'E2', 'E3','D1', 'D2', 'D3'];
FOREACH ($type IN $types) {
CREATE PROPERTY $type.id STRING;
CREATE INDEX ON $type (id) UNIQUE NULL_STRATEGY SKIP;
}
COMMIT;