1- alphaOnlyMatchRegex = r" ^[a-zA-Z0-9_]*$"
2-
31"""
42$(SIGNATURES)
53Returns the transaction for a given query.
64NOTE: Must commit(transaction) after you're done.
75"""
86function _queryNeo4j (neo4jInstance:: Neo4jInstance , query:: String ; currentTransaction:: Union{Nothing, Neo4j.Transaction} = nothing )
9- @debug " [Query] $(currentTransaction != nothing ? " [TRANSACTION]" : " " ) $query "
10- if currentTransaction == nothing
7+ @debug " [Query] $(currentTransaction != = nothing ? " [TRANSACTION]" : " " ) $query "
8+ if currentTransaction === nothing
119 loadtx = transaction (neo4jInstance. connection)
1210 loadtx (query)
1311 # Have to finish the transaction
@@ -27,7 +25,7 @@ Note: Using symbols so that the labels obey Neo4j requirements
2725function _createNode (neo4jInstance:: Neo4jInstance , labels:: Vector{String} , properties:: Dict{String, Any} , parentNode:: Union{Nothing, Neo4j.Node} , relationshipLabel:: Symbol = :NOTHING ):: Neo4j.Node
2826 createdNode = Neo4j. createnode (neo4jInstance. graph, properties)
2927 addnodelabels (createdNode, labels)
30- parentNode == nothing && return createdNode
28+ parentNode === nothing && return createdNode
3129 # Otherwise create the relationship
3230 createrel (parentNode, createdNode, String (relationshipLabel))
3331 return createdNode
@@ -56,6 +54,8 @@ $(SIGNATURES)
5654Get a node property - returns nothing if not found
5755"""
5856function _getNodeProperty (neo4jInstance:: Neo4jInstance , nodeLabels:: Vector{String} , property:: String ; currentTransaction:: Union{Nothing, Neo4j.Transaction} = nothing )
57+ # Note that properties should already by backticked, but double checking becase this is used in many places
58+ nodeLabels = [! startswith (a, " `" ) && ! endswith (a, " `" ) ? " `$(a) `" : a for a in nodeLabels]
5959 query = " match (n:$(join (nodeLabels, " :" )) ) return n.$property "
6060 result = _queryNeo4j (neo4jInstance, query, currentTransaction= currentTransaction)
6161 length (result. results[1 ][" data" ]) != 1 && error (" No data returned from the query." )
@@ -71,6 +71,9 @@ function _setNodeProperty(neo4jInstance::Neo4jInstance, nodeLabels::Vector{Strin
7171 if value isa String
7272 value = " \" " * replace (value, " \" " => " \\\" " )* " \" " # Escape strings
7373 end
74+ # Defensively wrap the node labels.
75+ nodeLabels = [! startswith (n, " `" ) && ! endswith (n, " `" ) ? " `$(n) `" : n for n in nodeLabels]
76+
7477 query = """
7578 match (n:$(join (nodeLabels, " :" )) )
7679 set n.$property = $value
@@ -87,14 +90,14 @@ $(SIGNATURES)
8790Get a node's tags
8891"""
8992function _getNodeTags (neo4jInstance:: Neo4jInstance , nodeLabels:: Vector{String} ):: Union{Nothing, Vector{String}}
90- query = " match (n:$(join (nodeLabels, " :" )) ) return labels(n)"
93+ query = " match (n:$(join (" ` " .* nodeLabels* " ` " , " :" )) ) return labels(n)"
9194 result = _queryNeo4j (neo4jInstance, query)
9295 length (result. results[1 ][" data" ]) != 1 && return nothing
9396 return result. results[1 ][" data" ][1 ][" row" ][1 ]
9497end
9598
9699function _getNodeCount (neo4jInstance:: Neo4jInstance , nodeLabels:: Vector{String} ; currentTransaction:: Union{Nothing, Neo4j.Transaction} = nothing ):: Int
97- query = " match (n:$(join (nodeLabels, " :" )) ) return count(n)"
100+ query = " match (n:$(join (" ` " .* nodeLabels.* " ` " , " :" )) ) return count(n)"
98101 result = _queryNeo4j (neo4jInstance, query, currentTransaction= currentTransaction)
99102 length (result. results[1 ][" data" ]) != 1 && return 0
100103 length (result. results[1 ][" data" ][1 ][" row" ]) != 1 && return 0
@@ -218,21 +221,21 @@ function _getLabelsForType(dfg::Neo4jDFG,
218221 isempty (dfg. sessionId) && error (" The DFG object's sessionId is empty, please specify a session ID." )
219222
220223 labels = []
221- type == User && (labels = [dfg. userId, " USER" ])
222- type == Robot && (labels = [dfg. userId, dfg. robotId, " ROBOT" ])
223- type == Session && (labels = [dfg. userId, dfg. robotId, dfg. sessionId, " SESSION" ])
224+ type == User && (labels = [" ` $( dfg. userId) ` " , " USER" ])
225+ type == Robot && (labels = [" ` $( dfg. userId) ` " , " ` $( dfg. robotId) ` " , " ROBOT" ])
226+ type == Session && (labels = [" ` $( dfg. userId) ` " , " ` $( dfg. robotId) ` " , " ` $( dfg. sessionId) ` " , " SESSION" ])
224227 type <: DFGVariable &&
225- (labels = [dfg. userId, dfg. robotId, dfg. sessionId, " VARIABLE" ])
228+ (labels = [" ` $( dfg. userId) ` " , " ` $( dfg. robotId) ` " , " ` $( dfg. sessionId) ` " , " VARIABLE" ])
226229 type <: DFGFactor &&
227- (labels = [dfg. userId, dfg. robotId, dfg. sessionId, " FACTOR" ])
230+ (labels = [" ` $( dfg. userId) ` " , " ` $( dfg. robotId) ` " , " ` $( dfg. sessionId) ` " , " FACTOR" ])
228231 type <: AbstractPointParametricEst &&
229- (labels = [dfg. userId, dfg. robotId, dfg. sessionId, " PPE" ])
232+ (labels = [" ` $( dfg. userId) ` " , " ` $( dfg. robotId) ` " , " ` $( dfg. sessionId) ` " , " PPE" ])
230233 type <: VariableNodeData &&
231- (labels = [dfg. userId, dfg. robotId, dfg. sessionId, " SOLVERDATA" ])
234+ (labels = [" ` $( dfg. userId) ` " , " ` $( dfg. robotId) ` " , " ` $( dfg. sessionId) ` " , " SOLVERDATA" ])
232235 type <: AbstractDataEntry &&
233- (labels = [dfg. userId, dfg. robotId, dfg. sessionId, " DATA" ])
236+ (labels = [" ` $( dfg. userId) ` " , " ` $( dfg. robotId) ` " , " ` $( dfg. sessionId) ` " , " DATA" ])
234237 # Some are children of nodes, so add that in if it's set.
235- parentKey != nothing && push! (labels, String (parentKey))
238+ parentKey != = nothing && push! (labels, String (parentKey))
236239 return labels
237240end
238241
@@ -260,7 +263,7 @@ function _listVarSubnodesForType(dfg::Neo4jDFG, variablekey::Symbol, dfgType::Ty
260263 query = " match (subnode:$(join (_getLabelsForType (dfg, dfgType, parentKey= variablekey),' :' )) ) return subnode.$keyToReturn "
261264 @debug " [Query] _listVarSubnodesForType query:\r\n $query "
262265 result = nothing
263- if currentTransaction != nothing
266+ if currentTransaction != = nothing
264267 result = currentTransaction (query; submit= true )
265268 else
266269 tx = transaction (dfg. neo4jInstance. connection)
@@ -276,7 +279,7 @@ function _getVarSubnodeProperties(dfg::Neo4jDFG, variablekey::Symbol, dfgType::T
276279 query = " match (subnode:$(join (_getLabelsForType (dfg, dfgType, parentKey= variablekey),' :' )) :$nodeKey ) return properties(subnode)"
277280 @debug " [Query] _getVarSubnodeProperties query:\r\n $query "
278281 result = nothing
279- if currentTransaction != nothing
282+ if currentTransaction != = nothing
280283 result = currentTransaction (query; submit= true )
281284 else
282285 tx = transaction (dfg. neo4jInstance. connection)
@@ -298,7 +301,9 @@ function _matchmergeVariableSubnode!(
298301 addProps:: Dict{String, String} = Dict {String, String} (),
299302 currentTransaction:: Union{Nothing, Neo4j.Transaction} = nothing ) where
300303 {N <: DFGNode , APPE <: AbstractPointParametricEst , ABDE <: AbstractDataEntry , PVND <: PackedVariableNodeData }
301-
304+
305+ # Defensively wrap the node labels.
306+ nodeLabels = [! startswith (n, " `" ) && ! endswith (n, " `" ) ? " `$(n) `" : n for n in nodeLabels]
302307 query = """
303308 MATCH (var:$variablekey :$(join (_getLabelsForType (dfg, DFGVariable, parentKey= variablekey),' :' )) )
304309 MERGE (var)-[:$relationshipKey ]->(subnode:$(join (nodeLabels,' :' )) )
@@ -307,7 +312,7 @@ function _matchmergeVariableSubnode!(
307312 RETURN properties(subnode)"""
308313 @debug " [Query] _matchmergeVariableSubnode! query:\r\n $query "
309314 result = nothing
310- if currentTransaction != nothing
315+ if currentTransaction != = nothing
311316 result = currentTransaction (query; submit= true ) # TODO : Maybe we should submit (; submit = true) for the results to fail early?
312317 else
313318 tx = transaction (dfg. neo4jInstance. connection)
@@ -327,6 +332,9 @@ function _deleteVarSubnode!(
327332 nodeLabels:: Vector{String} ,
328333 nodekey:: Symbol = :default ;
329334 currentTransaction:: Union{Nothing, Neo4j.Transaction} = nothing )
335+ # Defensively wrap the node labels.
336+ nodeLabels = [! startswith (n, " `" ) && ! endswith (n, " `" ) ? " `$(n) `" : n for n in nodeLabels]
337+
330338 query = """
331339 MATCH (node:$nodekey :$(join (nodeLabels,' :' )) )
332340 WITH node, properties(node) as props
@@ -335,7 +343,7 @@ function _deleteVarSubnode!(
335343 """
336344 @debug " [Query] _deleteVarSubnode delete query:\r\n $query "
337345 result = nothing
338- if currentTransaction != nothing
346+ if currentTransaction != = nothing
339347 result = currentTransaction (query; submit= true ) # TODO : Maybe we should submit (; submit = true) for the results to fail early?
340348 else
341349 tx = transaction (dfg. neo4jInstance. connection)
0 commit comments