forked from eldersantos/community
/
StartClause.scala
175 lines (132 loc) · 6.85 KB
/
StartClause.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Copyright (c) 2002-2012 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.cypher.internal.parser.v1_8
import org.neo4j.cypher.internal.commands._
import org.neo4j.graphdb.Direction
import org.neo4j.helpers.ThisShouldNotHappenError
trait StartClause extends Base with Expressions with CreateUnique {
def start: Parser[(Seq[StartItem], Seq[NamedPath])] = createStart | readStart | failure("expected START or CREATE")
def readStart: Parser[(Seq[StartItem], Seq[NamedPath])] = ignoreCase("start") ~> commaList(startBit) ^^ (x => (x, Seq()))
def createStart: Parser[(Seq[StartItem], Seq[NamedPath])] = relate|create
def create = ignoreCase("create") ~> commaList(usePattern(translate)) ^^ {
case matching =>
val pathsAndItems = matching.flatten.filter(_.isInstanceOf[NamedPathWStartItems]).map(_.asInstanceOf[NamedPathWStartItems])
val startItems = matching.flatten.filter(_.isInstanceOf[StartItem]).map(_.asInstanceOf[StartItem])
val namedPaths = pathsAndItems.map(_.path)
val pathItems = pathsAndItems.flatMap(_.items)
((startItems ++ pathItems), namedPaths)
}
case class NamedPathWStartItems(path:NamedPath, items:Seq[StartItem])
private def removeProperties(in:AbstractPattern):AbstractPattern = in match {
case ParsedNamedPath(name, patterns) => throw new ThisShouldNotHappenError("Andres", "We don't support paths in paths, and so should never see this")
case rel: ParsedRelation => rel.copy(
props = Map(),
start = rel.start.copy(props = Map()),
end = rel.end.copy(props = Map())
)
case n:ParsedEntity => n.copy(props = Map())
}
private def translate(abstractPattern: AbstractPattern): Maybe[Any] = abstractPattern match {
case ParsedNamedPath(name, patterns) =>
val namedPathPatterns: Maybe[Any] = patterns.
map(removeProperties).
map(matchTranslator).
reduce(_ ++ _)
val startItems = patterns.map(p => translate(p.makeOutgoing)).reduce(_ ++ _)
startItems match {
case No(msg) => No(msg)
case Yes(stuff) => namedPathPatterns.seqMap(p => {
val namedPath: NamedPath = NamedPath(name, p.map(_.asInstanceOf[Pattern]): _*)
Seq(NamedPathWStartItems(namedPath, stuff.map(_.asInstanceOf[StartItem])))
})
}
case ParsedRelation(name, props, ParsedEntity(a, startProps, True()), ParsedEntity(b, endProps, True()), relType, dir, map, True()) if relType.size == 1 =>
val (from, to) = if (dir != Direction.INCOMING)
(a, b)
else
(b, a)
Yes(Seq(CreateRelationshipStartItem(name, (from, startProps), (to, endProps), relType.head, props)))
case ParsedEntity(Entity(name), props, True()) =>
Yes(Seq(CreateNodeStartItem(name, props)))
case ParsedEntity(p, _, True()) if p.isInstanceOf[ParameterExpression] =>
Yes(Seq(CreateNodeStartItem(namer.name(None), Map[String, Expression]("*" -> p))))
case _ => No(Seq(""))
}
def startBit =
(identity ~ "=" ~ lookup ^^ {
case id ~ "=" ~ l => l(id)
}
| identity ~> "=" ~> opt("(") ~> failure("expected either node or relationship here")
| identity ~> failure("expected identifier assignment"))
def nodes = ignoreCase("node")
def rels = (ignoreCase("relationship") | ignoreCase("rel")) ^^^ "rel"
def typ = nodes | rels | failure("expected either node or relationship here")
def lookup: Parser[String => StartItem] =
nodes ~> parens(parameter) ^^ (p => (column: String) => NodeById(column, p)) |
nodes ~> ids ^^ (p => (column: String) => NodeById(column, p)) |
nodes ~> idxLookup ^^ nodeIndexLookup |
nodes ~> idxString ^^ nodeIndexString |
nodes ~> parens("*") ^^ (x => (column: String) => AllNodes(column)) |
rels ~> parens(parameter) ^^ (p => (column: String) => RelationshipById(column, p)) |
rels ~> ids ^^ (p => (column: String) => RelationshipById(column, p)) |
rels ~> idxLookup ^^ relationshipIndexLookup |
rels ~> idxString ^^ relationshipIndexString |
rels ~> parens("*") ^^ (x => (column: String) => AllRelationships(column)) |
nodes ~> opt("(") ~> failure("expected node id, or *") |
rels ~> opt("(") ~> failure("expected relationship id, or *")
def relationshipIndexString: ((String, Expression)) => (String) => RelationshipByIndexQuery = {
case (idxName, query) => (column: String) => RelationshipByIndexQuery(column, idxName, query)
}
def nodeIndexString: ((String, Expression)) => (String) => NodeByIndexQuery = {
case (idxName, query) => (column: String) => NodeByIndexQuery(column, idxName, query)
}
def nodeIndexLookup: ((String, Expression, Expression)) => (String) => NodeByIndex = {
case (idxName, key, value) => (column: String) => NodeByIndex(column, idxName, key, value)
}
def relationshipIndexLookup: ((String, Expression, Expression)) => (String) => RelationshipByIndex = {
case (idxName, key, value) => (column: String) => RelationshipByIndex(column, idxName, key, value)
}
def ids =
(parens(commaList(wholeNumber)) ^^ (x => Literal(x.map(_.toLong)))
| parens(commaList(wholeNumber) ~ opt(",")) ~> failure("trailing coma")
| "(" ~> failure("expected graph entity id"))
def idxString: Parser[(String, Expression)] = ":" ~> identity ~ parens(parameter | stringLit) ^^ {
case id ~ valu => (id, valu)
}
def idxLookup: Parser[(String, Expression, Expression)] =
":" ~> identity ~ parens(idxQueries) ^^ {
case a ~ b => (a, b._1, b._2)
} |
":" ~> identity ~> "(" ~> (id | parameter) ~> failure("`=` expected")
def idxQueries: Parser[(Expression, Expression)] = idxQuery
def indexValue = parameter | stringLit | failure("string literal or parameter expected")
def idxQuery: Parser[(Expression, Expression)] =
((id | parameter) ~ "=" ~ indexValue ^^ {
case k ~ "=" ~ v => (k, v)
}
| "=" ~> failure("Need index key"))
def id: Parser[Expression] = identity ^^ (x => Literal(x))
def andQuery: Parser[String] = idxQuery ~ ignoreCase("and") ~ idxQueries ^^ {
case q ~ and ~ qs => q + " AND " + qs
}
def orQuery: Parser[String] = idxQuery ~ ignoreCase("or") ~ idxQueries ^^ {
case q ~ or ~ qs => q + " OR " + qs
}
}