@@ -241,6 +241,22 @@ def get_neighbors(self, node_id: int, rel: Optional[str] = None, lbl: Optional[s
241
241
return {'nodes' : [], 'edges' : []}
242
242
243
243
244
+ def _class_from_node (self , n : Node ) -> Class :
245
+ """
246
+ Create a Class from a graph node
247
+ """
248
+
249
+ doc = n .properties .get ('doc' )
250
+ name = n .properties .get ('name' )
251
+ path = n .properties .get ('path' )
252
+ src_end = n .properties .get ('src_end' )
253
+ src_start = n .properties .get ('src_start' )
254
+
255
+ c = Class (path , name , doc , src_start , src_end )
256
+ c .id = n .id
257
+
258
+ return c
259
+
244
260
def add_class (self , c : Class ) -> None :
245
261
"""
246
262
Adds a class node to the graph database.
@@ -252,7 +268,7 @@ def add_class(self, c: Class) -> None:
252
268
q = """MERGE (c:Class:Searchable {name: $name, path: $path, src_start: $src_start,
253
269
src_end: $src_end})
254
270
SET c.doc = $doc
255
- RETURN ID(c) """
271
+ RETURN c """
256
272
257
273
params = {
258
274
'doc' : c .doc ,
@@ -262,24 +278,9 @@ def add_class(self, c: Class) -> None:
262
278
'src_end' : c .src_end ,
263
279
}
264
280
265
- res = self ._query (q , params )
266
- c .id = res .result_set [0 ][0 ]
267
-
268
- def _class_from_node (self , n : Node ) -> Class :
269
- """
270
- Create a Class from a graph node
271
- """
272
-
273
- doc = n .properties .get ('doc' )
274
- name = n .properties .get ('name' )
275
- path = n .properties .get ('path' )
276
- src_end = n .properties .get ('src_end' )
277
- src_start = n .properties .get ('src_start' )
278
-
279
- c = Class (path , name , doc , src_start , src_end )
280
- c .id = n .id
281
-
282
- return c
281
+ res = self ._query (q , params )
282
+ node = res .result_set [0 ][0 ]
283
+ c .id = node .id
283
284
284
285
def get_class_by_name (self , class_name : str ) -> Optional [Class ]:
285
286
q = "MATCH (c:Class) WHERE c.name = $name RETURN c LIMIT 1"
@@ -303,6 +304,30 @@ def get_class(self, class_id: int) -> Optional[Class]:
303
304
c = res .result_set [0 ][0 ]
304
305
return self ._class_from_node (c )
305
306
307
+ def _function_from_node (self , n : Node ) -> Function :
308
+ """
309
+ Create a Function from a graph node
310
+ """
311
+
312
+ src = n .properties .get ('src' )
313
+ doc = n .properties .get ('doc' )
314
+ path = n .properties .get ('path' )
315
+ name = n .properties .get ('name' )
316
+ args = n .properties .get ('args' )
317
+ src_end = n .properties .get ('src_end' )
318
+ ret_type = n .properties .get ('ret_type' )
319
+ src_start = n .properties .get ('src_start' )
320
+
321
+ f = Function (path , name , doc , ret_type , src , src_start , src_end )
322
+ for arg in args :
323
+ name = arg [0 ]
324
+ type_ = arg [1 ]
325
+ f .add_argument (name , type_ )
326
+
327
+ f .id = n .id
328
+
329
+ return f
330
+
306
331
def add_function (self , func : Function ) -> None :
307
332
"""
308
333
Adds a function node to the graph database.
@@ -314,7 +339,7 @@ def add_function(self, func: Function) -> None:
314
339
q = """MERGE (f:Function:Searchable {path: $path, name: $name,
315
340
src_start: $src_start, src_end: $src_end})
316
341
SET f.args = $args, f.ret_type = $ret_type, f.src = $src, f.doc = $doc
317
- RETURN ID(f) """
342
+ RETURN f """
318
343
319
344
# Prepare arguments in a more straightforward manner
320
345
args = [[arg .name , arg .type ] for arg in func .args ]
@@ -329,34 +354,10 @@ def add_function(self, func: Function) -> None:
329
354
'ret_type' : func .ret_type
330
355
}
331
356
332
- res = self ._query (q , params )
333
- func .id = res .result_set [0 ][0 ]
334
-
335
- def _function_from_node (self , n : Node ) -> Function :
336
- """
337
- Create a Function from a graph node
338
- """
339
-
340
- src = n .properties .get ('src' )
341
- doc = n .properties .get ('doc' )
342
- path = n .properties .get ('path' )
343
- name = n .properties .get ('name' )
344
- args = n .properties .get ('args' )
345
- src_end = n .properties .get ('src_end' )
346
- ret_type = n .properties .get ('ret_type' )
347
- src_start = n .properties .get ('src_start' )
348
-
349
- f = Function (path , name , doc , ret_type , src , src_start , src_end )
350
- for arg in args :
351
- name = arg [0 ]
352
- type_ = arg [1 ]
353
- f .add_argument (name , type_ )
354
-
355
- f .id = n .id
357
+ res = self ._query (q , params )
358
+ node = res .result_set [0 ][0 ]
359
+ func .id = node .id
356
360
357
- return f
358
-
359
-
360
361
# set functions metadata
361
362
def set_functions_metadata (self , ids : List [int ], metadata : List [dict ]) -> None :
362
363
assert (len (ids ) == len (metadata ))
@@ -366,7 +367,8 @@ def set_functions_metadata(self, ids: List[int], metadata: List[dict]) -> None:
366
367
WITH $ids[i] AS id, $values[i] AS v
367
368
MATCH (f)
368
369
WHERE ID(f) = id
369
- SET f += v"""
370
+ SET f += v
371
+ RETURN f"""
370
372
371
373
params = {'ids' : ids , 'values' : metadata }
372
374
@@ -483,11 +485,12 @@ def add_file(self, file: File) -> None:
483
485
"""
484
486
485
487
q = """MERGE (f:File:Searchable {path: $path, name: $name, ext: $ext})
486
- RETURN ID(f) """
488
+ RETURN f """
487
489
params = {'path' : file .path , 'name' : file .name , 'ext' : file .ext }
488
490
489
- res = self ._query (q , params )
490
- file .id = res .result_set [0 ][0 ]
491
+ res = self ._query (q , params )
492
+ node = res .result_set [0 ][0 ]
493
+ file .id = node .id
491
494
492
495
def delete_files (self , files : List [dict ]) -> tuple [str , dict , List [int ]]:
493
496
"""
@@ -580,7 +583,8 @@ def connect_entities(self, relation: str, src_id: int, dest_id: int) -> None:
580
583
581
584
q = f"""MATCH (src), (dest)
582
585
WHERE ID(src) = $src_id AND ID(dest) = $dest_id
583
- MERGE (src)-[:{ relation } ]->(dest)"""
586
+ MERGE (src)-[e:{ relation } ]->(dest)
587
+ RETURN e"""
584
588
585
589
params = {'src_id' : src_id , 'dest_id' : dest_id }
586
590
self ._query (q , params )
@@ -597,36 +601,12 @@ def function_calls_function(self, caller_id: int, callee_id: int, pos: int) -> N
597
601
598
602
q = """MATCH (caller:Function), (callee:Function)
599
603
WHERE ID(caller) = $caller_id AND ID(callee) = $callee_id
600
- MERGE (caller)-[e:CALLS {pos:$pos}]->(callee)"""
604
+ MERGE (caller)-[e:CALLS {pos:$pos}]->(callee)
605
+ RETURN e"""
601
606
602
607
params = {'caller_id' : caller_id , 'callee_id' : callee_id , 'pos' : pos }
603
608
self ._query (q , params )
604
609
605
- def add_struct (self , s : Struct ) -> None :
606
- """
607
- Adds a struct node to the graph database.
608
-
609
- Args:
610
- s (Struct): The Struct object to be added.
611
- """
612
-
613
- q = """MERGE (s:Struct:Searchable {name: $name, path: $path, src_start: $src_start,
614
- src_end: $src_end})
615
- SET s.doc = $doc, s.fields = $fields
616
- RETURN ID(s)"""
617
-
618
- params = {
619
- 'doc' : s .doc ,
620
- 'name' : s .name ,
621
- 'path' : s .path ,
622
- 'src_start' : s .src_start ,
623
- 'src_end' : s .src_end ,
624
- 'fields' : s .fields
625
- }
626
-
627
- res = self ._query (q , params )
628
- s .id = res .result_set [0 ][0 ]
629
-
630
610
def _struct_from_node (self , n : Node ) -> Struct :
631
611
"""
632
612
Create a Struct from a graph node
@@ -652,6 +632,32 @@ def _struct_from_node(self, n: Node) -> Struct:
652
632
653
633
return s
654
634
635
+ def add_struct (self , s : Struct ) -> None :
636
+ """
637
+ Adds a struct node to the graph database.
638
+
639
+ Args:
640
+ s (Struct): The Struct object to be added.
641
+ """
642
+
643
+ q = """MERGE (s:Struct:Searchable {name: $name, path: $path, src_start: $src_start,
644
+ src_end: $src_end})
645
+ SET s.doc = $doc, s.fields = $fields
646
+ RETURN s"""
647
+
648
+ params = {
649
+ 'doc' : s .doc ,
650
+ 'name' : s .name ,
651
+ 'path' : s .path ,
652
+ 'src_start' : s .src_start ,
653
+ 'src_end' : s .src_end ,
654
+ 'fields' : s .fields
655
+ }
656
+
657
+ res = self ._query (q , params )
658
+ node = res .result_set [0 ][0 ]
659
+ s .id = node .id
660
+
655
661
def get_struct_by_name (self , struct_name : str ) -> Optional [Struct ]:
656
662
q = "MATCH (s:Struct) WHERE s.name = $name RETURN s LIMIT 1"
657
663
res = self ._query (q , {'name' : struct_name }).result_set
0 commit comments