1
- from dataclasses import dataclass
2
- from typing import Any , Dict , List
1
+ from dataclasses import dataclass , field
2
+ from typing import Any
3
+
4
+
5
+ @dataclass
6
+ class ApocUpdateStatistics :
7
+ """Type definition for APOC update statistics."""
8
+
9
+ relationshipsDeleted : int = 0 # Number of relationships deleted
10
+ relationshipsCreated : int = 0 # Number of relationships created
11
+ nodesDeleted : int = 0 # Number of nodes deleted
12
+ nodesCreated : int = 0 # Number of nodes created
13
+ labelsRemoved : int = 0 # Number of labels removed
14
+ labelsAdded : int = 0 # Number of labels added
15
+ propertiesSet : int = 0 # Number of properties set
16
+
17
+
18
+ @dataclass
19
+ class ApocBatchResponse :
20
+ """Type definition for APOC periodic.iterate response fields."""
21
+
22
+ batches : int = 0 # Number of batches processed
23
+ total : int = 0 # Total number of operations
24
+ timeTaken : int = 0 # Time taken in milliseconds
25
+ committedOperations : int = 0 # Number of operations committed
26
+ failedOperations : int = 0 # Number of operations that failed
27
+ failedBatches : int = 0 # Number of batches that failed
28
+ retries : int = 0 # Number of retries performed
29
+ errorMessages : dict [str , Any ] = field (default_factory = dict ) # Map of error messages
30
+ wasTerminated : bool = False # Whether the operation was terminated
31
+ updateStatistics : ApocUpdateStatistics = field (default_factory = ApocUpdateStatistics )
32
+
33
+
34
+ # Get fields from ApocBatchResponse dataclass
35
+ APOC_BATCH_QUERY_RESPONSE_FIELDS = [
36
+ field .name for field in ApocBatchResponse .__dataclass_fields__ .values ()
37
+ ]
3
38
4
39
UNWIND_QUERY = "UNWIND $batched_parameter_sets as params RETURN params"
5
- COMMIT_QUERY = """
40
+
41
+ # Build the YIELD and RETURN clauses using the standardized fields
42
+ YIELD_CLAUSE = f"YIELD { ', ' .join (APOC_BATCH_QUERY_RESPONSE_FIELDS )} "
43
+ RETURN_CLAUSE = f"RETURN { ', ' .join (APOC_BATCH_QUERY_RESPONSE_FIELDS )} "
44
+
45
+ COMMIT_QUERY = f"""
6
46
CALL apoc.periodic.iterate(
7
47
$iterable_query,
8
48
$batched_query,
9
- {batchSize: $chunk_size, parallel: $execute_chunks_in_parallel, retries: $retries_per_chunk, params: $iterate_params}
49
+ {{ batchSize: $chunk_size, parallel: $execute_chunks_in_parallel, retries: $retries_per_chunk, params: $iterate_params} }
10
50
)
11
- YIELD batches, committedOperations, failedOperations, errorMessages
12
- RETURN batches, committedOperations, failedOperations, errorMessages
51
+ { YIELD_CLAUSE }
52
+ { RETURN_CLAUSE }
13
53
"""
14
54
15
- NON_APOCH_COMMIT_QUERY = """
55
+ NON_APOC_COMMIT_QUERY = """
16
56
UNWIND $iterate_params.batched_parameter_sets AS param
17
57
CALL apoc.cypher.doIt($batched_query, {params: param})
18
58
YIELD value
23
63
@dataclass (slots = True , frozen = True )
24
64
class Query :
25
65
query_statement : str
26
- parameters : Dict [str , Any ]
66
+ parameters : dict [str , Any ]
67
+ is_apoc : bool = False # Indicates if this is an APOC query
27
68
28
69
@classmethod
29
70
def from_statement (cls , query_statement : str , ** parameters : Any ) -> "Query" :
@@ -47,13 +88,15 @@ def feed_batched_query(
47
88
"chunk_size" : chunk_size ,
48
89
"retries_per_chunk" : retries_per_chunk ,
49
90
},
91
+ is_apoc = True , # This is an APOC query
50
92
)
51
93
52
94
53
95
@dataclass (slots = True , frozen = True )
54
96
class QueryBatch :
55
97
query_statement : str
56
- batched_parameter_sets : List [Dict [str , Any ]]
98
+ batched_parameter_sets : list [dict [str , Any ]]
99
+ is_apoc : bool = False # Indicates if this is an APOC query
57
100
58
101
def as_query (
59
102
self ,
@@ -63,7 +106,7 @@ def as_query(
63
106
retries_per_chunk : int = 3 ,
64
107
) -> Query :
65
108
return Query (
66
- {True : COMMIT_QUERY , False : NON_APOCH_COMMIT_QUERY }[apoc_iterate ],
109
+ {True : COMMIT_QUERY , False : NON_APOC_COMMIT_QUERY }[apoc_iterate ],
67
110
{
68
111
"iterate_params" : {
69
112
"batched_parameter_sets" : self .batched_parameter_sets
@@ -74,4 +117,5 @@ def as_query(
74
117
"chunk_size" : chunk_size ,
75
118
"retries_per_chunk" : retries_per_chunk ,
76
119
},
120
+ is_apoc = apoc_iterate , # Set is_apoc based on apoc_iterate parameter
77
121
)
0 commit comments