Skip to content

Instantly share code, notes, and snippets.

@laurieboyes laurieboyes/time-tree.cql Secret
Created Sep 28, 2017

What would you like to do?
MERGE (r:TimeTreeRoot)
WITH r, range(2010, 2020) AS years, range(1,12) as months
FOREACH(year IN years |
MERGE (y:Year {value: year})
MERGE (r)-[:CHILD]->(y)
FOREACH(month IN months |
CREATE (m:Month {value: month})
MERGE (y)-[:CHILD]->(m)
WHEN month IN [1,3,5,7,8,10,12] THEN range(1,31)
WHEN month = 2 THEN
WHEN year % 4 <> 0 THEN range(1,28)
WHEN year % 100 <> 0 THEN range(1,29)
WHEN year % 400 <> 0 THEN range(1,29)
ELSE range(1,28)
ELSE range(1,30)
END) |
CREATE (d:Day {value: day})
MERGE (m)-[:CHILD]->(d))))
//Create years linked list
MATCH (root:TimeTreeRoot)-->(year:Year)
WITH root, year ORDER BY year.value
WITH root, collect(year) as years
WITH root, years, years[0] AS first, years[size(years)-1] AS last
MERGE (root)-[:FIRST]->(first)
MERGE (root)-[:LAST]->(last)
FOREACH(i in RANGE(0, size(years)-2) |
FOREACH(year1 in [years[i]] |
FOREACH(year2 in [years[i+1]] |
MERGE (year1)-[:NEXT]->(year2))))
//Create months linked list
MATCH (year:Year)
WITH collect(year) as years
UNWIND years AS year
MATCH (year)--(first:Month {value: 1}), (year)--(last:Month {value: 12})
MERGE (year)-[:FIRST]->(first)
MERGE (year)-[:LAST]->(last)
MATCH (year:Year)-[:CHILD]->(month:Month)
WITH year, month
ORDER BY year.value, month.value
WITH collect(month) as months
FOREACH(i in RANGE(0, size(months)-2) |
FOREACH(month1 in [months[i]] |
FOREACH(month2 in [months[i+1]] |
MERGE (month1)-[:NEXT]->(month2))))
//Create days linked list
MATCH (month:Month)
WITH collect(month) as months
UNWIND months AS month
MATCH (month)-[:CHILD]->(day:Day)
WITH month, collect(day) AS days
WITH month, days[0] AS first, days[size(days)-1] AS last
MERGE (month)-[:FIRST]->(first)
MERGE (month)-[:LAST]->(last)
MATCH (year:Year)-[:CHILD]->(month:Month)-[:CHILD]->(day:Day)
WITH year,month,day
ORDER BY year.value, month.value, day.value
WITH collect(day) as days
FOREACH(i in RANGE(0, size(days)-2) |
FOREACH(day1 in [days[i]] |
FOREACH(day2 in [days[i+1]] |
MERGE (day1)-[:NEXT]->(day2))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.