Skip to content

Commit 7a8e0e1

Browse files
Fixed errors in file and updated criteria to lowercase
1 parent 78beac0 commit 7a8e0e1

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

docs/utility-guides/SubjectSelectionQueryBuilder.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ The `SubjectSelectionQueryBuilder` is a flexible utility for constructing SQL qu
1212
- Lynch pathway logic and Notify message status
1313

1414
For example:
15+
1516
- NHS number
1617
- Subject age
1718
- Hub code
1819
- Screening centre code
1920
- GP practice linkage
2021
- Screening status
21-
- and many more, (including date-based and status-based filters).
22+
- and many more, (including date-based and status-based filters).
2223

2324
It also handles special cases such as `unchanged` values and supports modifiers like `NOT:` for negation.
2425

@@ -44,10 +45,11 @@ Using `build_subject_selection_query`:
4445
query, bind_vars = builder.build_subject_selection_query(
4546
criteria=criteria_dict, # Dict[str, str] of selection criteria
4647
user=test_user, # User object
47-
subject=test_subject, # Subject object (can be None)
48+
subject=test_subject, # Subject object
4849
subjects_to_retrieve=100 # Optional limit
4950
)
5051
```
52+
5153
When you call `build_subject_selection_query(...)`, it returns a tuple containing:
5254

5355
`query` — a complete SQL string with placeholders like :nhs_number, ready to be run against the database.
@@ -62,11 +64,11 @@ This approach ensures injection-safe execution (defending against SQL injection
6264

6365
```python
6466
criteria = {
65-
"NHS_NUMBER": "1234567890",
66-
"SCREENING_STATUS": "invited"
67+
"nhs_number": "1234567890",
68+
"screening_status": "invited"
6769
}
6870

69-
user = User(user_id=42, organisation=None) # Simulated user
71+
user = User(user_id=42, organisation=None) # Optional; used for 'unchanged' logic
7072
subject = Subject() # Optional; used for 'unchanged' logic
7173

7274
builder = SubjectSelectionQueryBuilder()
@@ -86,6 +88,7 @@ WHERE 1=1
8688
AND ss.screening_status_id = 1001
8789
FETCH FIRST 1 ROWS ONLY
8890
```
91+
8992
(Note: 1001 would be the resolved ID for "invited" in ScreeningStatusType.)
9093

9194
#### bind_vars
@@ -101,7 +104,8 @@ FETCH FIRST 1 ROWS ONLY
101104
You can pass both values directly into your DB layer or test stub:
102105

103106
```python
104-
cursor.execute(query, bind_vars)
107+
from utils.oracle.oracle import OracleDB
108+
df = OracleDB().execute_query(query, bind_vars)
105109
```
106110

107111
## Supported Inputs
@@ -118,11 +122,12 @@ Example:
118122

119123
```python
120124
{
121-
"SUBJECT_HAS_EVENT_STATUS": "ES01",
122-
"SUBJECT_AGE": "> 60",
123-
"DATE_OF_DEATH": "null"
125+
"subject_has_event_status": "ES01",
126+
"subject_age": "> 60",
127+
"date_of_death": "null"
124128
}
125129
```
130+
126131
Each of those triggers a different clause in the generated SQL.
127132

128133
### 2. user (User)
@@ -136,16 +141,19 @@ Example:
136141
```python
137142
"SUBJECT_HUB_CODE": "USER_HUB"
138143
```
144+
139145
This means “filter by the hub assigned to this user’s organisation,” not a fixed hub like ABC.
140146

141147
### 3. subject (Subject)
148+
142149
This is used when a filter wants to compare the current value in the database to an existing value on file—often represented by the "UNCHANGED" keyword.
143150

144151
Example:
145152

146153
```python
147154
"SCREENING_STATUS": "unchanged"
148155
```
156+
149157
That’s saying: “Only return subjects whose screening status has not changed compared to what’s currently recorded on the subject object.”
150158

151159
Without a subject, "unchanged" logic isn’t possible and will raise a validation error.
@@ -164,11 +172,10 @@ Joins to related datasets are added dynamically only when required (e.g. latest
164172

165173
All dates are handled via Oracle `TRUNC(SYSDATE)` and `TO_DATE()` expressions to ensure consistent date logic.
166174

167-
168175
## Reference
169176

170177
For a full list of supported `SubjectSelectionCriteriaKey` values and expected inputs, refer to the enumeration in:
171178

172179
`classes/subject_selection_criteria_key.py`
173180

174-
Or explore the full `SubjectSelectionQueryBuilder._dispatch_criteria_key()` method to review how each key is implemented.
181+
Or explore the full `SubjectSelectionQueryBuilder._dispatch_criteria_key()` method to review how each key is implemented.

0 commit comments

Comments
 (0)