# Delete all "person" vertices with location equal to "us"
CREATE QUERY deleteEx() FOR GRAPH workNet {
S = {person.*};
DELETE s FROM S:s
WHERE s.locationId == "us";
}
DELETE 语句示例2
# Delete all "worksFor" edges where the person's location is "us"
CREATE QUERY deleteEx2() FOR GRAPH workNet {
S = {person.*};
DELETE e FROM S:s -(worksFor:e)-> company:t
WHERE s.locationId == "us";
}
CREATE QUERY countAtLocation(STRING loc) FOR GRAPH workNet {
SetAccum<EDGE> @@selEdge;
Start = {person.*};
SV = SELECT s FROM Start:s
WHERE s.locationId == loc;
PRINT SV.size() AS numVertices;
SE = SELECT s FROM Start:s -(worksFor:e)-> company:t
WHERE s.locationId == loc
ACCUM @@selEdge += e;
PRINT @@selEdge.size() AS numEdges;
}
例如,假设按照countAtLocation,deleteEx2和deleteEx的顺序执行查询
deleteEx.run
RUN QUERY countAtLocation("us")
RUN QUERY deleteEx2()
RUN QUERY countAtLocation("us")
RUN QUERY deleteEx()
RUN QUERY countAtLocation("us")
# Remove any post vertices posted by the given user
CREATE QUERY deletePosts(vertex<person> seed) FOR GRAPH socialNet {
start = {seed};
# Best practice is to delete a vertex in a POST-ACCUM, which only
# occurs once for each vertex v, guaranteeing that a vertex is not
# deleted more than once
postAccumDeletedPosts = SELECT v FROM start -(posted:e)-> post:v
POST-ACCUM DELETE (v);
# Possible, but not recommended as the DML-sub DELETE statement occurs
# once for each edge of the vertex v
accumDeletedPosts = SELECT v FROM start -(posted:e)-> post:v
ACCUM DELETE (v);
}
# Need a separate query to display the results, because deletions don't take effect during the query.
CREATE QUERY selectUserPosts(vertex<person> seed) FOR GRAPH socialNet {
start = {seed};
userPosts = SELECT v FROM start -(posted:e)-> post:v;
PRINT userPosts;
}
例如,如果顺序执行以下的selectUserPosts和deletePosts查询
deletePosts.run
RUN QUERY selectUserPosts("person3")
RUN QUERY deletePosts("person3")
RUN QUERY selectUserPosts("person3")
会得到以下结果:
DeletePosts 示例的结果
# Before the deletion
{
"error": false,
"message": "",
"version": {
"schema": 0,
"api": "v2"
},
"results": [{"selectedPosts": [{
"v_id": "2",
"attributes": {
"postTime": "2011-02-03 01:02:42",
"subject": "query languages"
},
"v_type": "post"
}]}]
}
# Deletion; no output results requested at this point
{
"error": false,
"message": "",
"version": {
"schema": 0,
"api": "v2"
},
"results": []
}
# After the deletion
{
"error": false,
"message": "",
"version": {
"schema": 0,
"api": "v2"
},
"results": [{"selectedPosts": []}]
}
CREATE QUERY insertEx(STRING name, STRING name2, STRING name3, STRING comp) FOR GRAPH workNet {
# Vertex insertion
# Adds 2 'company' vertices. One is located in the USA, and a sister company in Japan.
INSERT INTO company VALUES ( comp, comp, "us" );
INSERT INTO company (PRIMARY_ID, country) VALUES ( comp + "_jp", "jp" );
# Edge insertion
# Adds a 'worksFor' edge from person 'name' to the company 'comp', filling in default
# values for startYear (0), startMonth (0), and fullTime (false).
INSERT INTO worksFor VALUES (name person, comp company, _, _, _);
# Adds a 'worksFor' edge from person 'name2' to the company 'comp', filling in default
# values for startMonth (0), but specifying values for startYear and fullTime.
INSERT INTO worksFor (FROM, TO, startYear, fullTime) VALUES (name2 person, comp company, 2017, true);
# Adds a 'worksFor' edge from person 'name3' to the company 'comp', filling in default
# values for startMonth (0), and fullTime (false) but specifying a value for startYear (2017).
INSERT INTO worksFor (FROM, TO, startYear) VALUES (name3 person, comp company, 2000 + 17);
}
# Add a child company of a given company name. The new child company is in japan
CREATE QUERY addNewChildCompany(STRING name) FOR GRAPH workNet {
allCompanies = {company.*};
X = SELECT s
FROM allCompanies:s
WHERE s.id == name
ACCUM INSERT INTO company VALUES ( name + "_jp", name + "_jp", "jp" );
}
# Add separate query to list the companies, before and after the insertion
CREATE QUERY listCompanyNames(STRING countryFilter) FOR GRAPH workNet {
allCompanies = {company.*};
C = SELECT s
FROM allCompanies:s
WHERE s.country == countryFilter;
PRINT C.size() AS numCompanies;
PRINT C;
}
# Change all "person" vertices with location equal to "us" to "USA"
CREATE QUERY updateEx() FOR GRAPH workNet {
S = {person.*};
UPDATE s FROM S:s
SET s.locationId = "USA", # simple base type attribute
s.skillList = [1,2,3] # collection-type attribute
WHERE s.locationId == "us";
# The update cannot become effective within this query, so PRINT S still show "us".
PRINT S;
}
# The second example is equivalent to the above updateEx
CREATE QUERY updateEx2() FOR GRAPH workNet {
S = {person.*};
X = SELECT s
FROM S:s
WHERE S.locationId == "USA"
POST-ACCUM S.locationId = "us",
S.skillList = [3,2,1];
PRINT S;
}
# change the given person's new location
CREATE QUERY updateByAssignment(VERTEX<person> v, STRING newLocation) FOR GRAPH workNet{
v.locationId = newLocation;
}