| | 170 | |
| | 171 | ==== PERSON избор |
| | 172 | {{{ |
| | 173 | SELECT r.report_id, r.created_at, r.person_id, r.report_type, r.summary |
| | 174 | FROM report r |
| | 175 | LEFT JOIN person p ON p.person_id = r.person_id |
| | 176 | WHERE 1=1 |
| | 177 | /* name contains */ |
| | 178 | AND (:namePattern IS NULL OR LOWER(p.name) LIKE :namePattern) |
| | 179 | /* surname contains */ |
| | 180 | AND (:surnamePattern IS NULL OR LOWER(p.surname) LIKE :surnamePattern) |
| | 181 | /* alive / not alive */ |
| | 182 | AND ( |
| | 183 | (:isAlive = TRUE AND p.date_of_death IS NULL) |
| | 184 | OR (:isAlive = FALSE AND p.date_of_death IS NOT NULL) |
| | 185 | ) |
| | 186 | /* exact age (years) using Postgres functionalities */ |
| | 187 | AND ( |
| | 188 | :correctAge IS NULL OR :correctAge = 0 OR |
| | 189 | date_part('year', age(current_date, p.date_of_birth)) = :correctAge |
| | 190 | ) |
| | 191 | /* age range expressed via date_of_birth bounds (computed in app, with function) */ |
| | 192 | AND (:dobMax IS NULL OR p.date_of_birth <= :dobMax) |
| | 193 | AND (:dobMin IS NULL OR p.date_of_birth >= :dobMin) |
| | 194 | /* gender equals */ |
| | 195 | AND (:gender IS NULL OR p.gender = :gender) |
| | 196 | /* address contains */ |
| | 197 | AND (:addrPattern IS NULL OR LOWER(p.address) LIKE :addrPattern); |
| | 198 | }}} |
| | 199 | |
| | 200 | ==== ACADEMIC избор |
| | 201 | |
| | 202 | {{{ |
| | 203 | SELECT r.report_id, r.created_at, r.person_id, r.report_type, r.summary |
| | 204 | FROM report r |
| | 205 | LEFT JOIN academicreport ar ON ar.report_id = r.report_id |
| | 206 | LEFT JOIN institution i ON i.institution_id = ar.institution_id |
| | 207 | WHERE 1=1 |
| | 208 | /* academic field contains */ |
| | 209 | AND (:fieldPattern IS NULL OR LOWER(ar.academic_field) LIKE :fieldPattern) |
| | 210 | /* institution type equals */ |
| | 211 | AND (:instType IS NULL OR i.type = :instType); |
| | 212 | }}} |
| | 213 | |
| | 214 | ==== CRIMINAL избор |
| | 215 | |
| | 216 | {{{ |
| | 217 | SELECT r.report_id, r.created_at, r.person_id, r.report_type, r.summary |
| | 218 | FROM report r |
| | 219 | LEFT JOIN criminalreport cr ON cr.report_id = r.report_id |
| | 220 | LEFT JOIN crimetype ct ON ct.crime_type_id = cr.crime_type_id |
| | 221 | JOIN punishment p ON p.report_id = cr.report_id |
| | 222 | WHERE 1=1 |
| | 223 | /* severity level equals */ |
| | 224 | AND (:severityLevel IS NULL OR ct.severity_level = :severityLevel) |
| | 225 | /* resolved flag equals */ |
| | 226 | AND (:isResolved IS NULL OR cr.resolved = :isResolved) |
| | 227 | /* fine (applies when punishment_type = FINE) */ |
| | 228 | AND (:punType <> 'FINE' OR :fineAmount IS NULL OR p.fine_to_pay = :fineAmount) |
| | 229 | /* prison years via years_total(created_at, release_date) (applies when punishment_type = PRISON) */ |
| | 230 | AND (:punType <> 'PRISON' OR :prisonYears IS NULL OR :prisonYears <= 0 |
| | 231 | OR years_total(r.created_at, p.release_date) = :prisonYears) |
| | 232 | /* crime type label contains */ |
| | 233 | AND (:crimeLabelPattern IS NULL OR LOWER(ct.label) LIKE :crimeLabelPattern); |
| | 234 | }}} |
| | 235 | |
| | 236 | ==== MEDICAL избор |
| | 237 | |
| | 238 | {{{ |
| | 239 | SELECT r.report_id, r.created_at, r.person_id, r.report_type, r.summary |
| | 240 | FROM report r |
| | 241 | LEFT JOIN medicalreport mr ON mr.report_id = r.report_id |
| | 242 | LEFT JOIN doctor d ON d.doctor_id = mr.doctor_id |
| | 243 | LEFT JOIN medicalreport_diagnosis mrd ON mrd.report_id = mr.report_id |
| | 244 | LEFT JOIN diagnosis diag ON diag.diagnosis_id = mrd.diagnosis_id |
| | 245 | WHERE 1=1 |
| | 246 | /* next control presence (choose exactly one branch in code) */ |
| | 247 | AND ( |
| | 248 | (:hasNextControl = TRUE AND mr.next_control_date IS NOT NULL) OR |
| | 249 | (:hasNextControl = FALSE AND mr.next_control_date IS NULL) OR |
| | 250 | (:hasNextControl IS NULL) |
| | 251 | ) |
| | 252 | /* doctor name/surname contains */ |
| | 253 | AND (:docNamePattern IS NULL OR LOWER(d.name) LIKE :docNamePattern) |
| | 254 | AND (:docSurnamePattern IS NULL OR LOWER(d.surname) LIKE :docSurnamePattern) |
| | 255 | /* specialization equals */ |
| | 256 | AND (:specialization IS NULL OR d.specialization = :specialization) |
| | 257 | /* chronic flag equals */ |
| | 258 | AND (:isChronic IS NULL OR diag.is_chronic = :isChronic); |
| | 259 | }}} |
| | 260 | |
| | 261 | ==== EMPLOYMENT избор |
| | 262 | |
| | 263 | {{{ |
| | 264 | SELECT r.report_id, r.created_at, r.person_id, r.report_type, r.summary |
| | 265 | FROM report r |
| | 266 | LEFT JOIN employmentreport e ON e.report_id = r.report_id |
| | 267 | WHERE 1=1 |
| | 268 | /* income comparison: more/equal/less (include only one branch) */ |
| | 269 | AND ( |
| | 270 | (:incomeCmp = 'more' AND e.income_per_month > :incomeAmount) OR |
| | 271 | (:incomeCmp = 'equal' AND e.income_per_month = :incomeAmount) OR |
| | 272 | (:incomeCmp = 'less' AND e.income_per_month < :incomeAmount) OR |
| | 273 | (:incomeCmp IS NULL) |
| | 274 | ) |
| | 275 | /* years_total(start_date,end_date) custom DB function */ |
| | 276 | AND ( |
| | 277 | :yearsCmp IS NULL OR :yearsValue IS NULL OR :yearsValue <= 0 OR |
| | 278 | ( |
| | 279 | (:yearsCmp = 'more' AND years_total(e.start_date, e.end_date) > :yearsValue) OR |
| | 280 | (:yearsCmp = 'equal' AND years_total(e.start_date, e.end_date) = :yearsValue) OR |
| | 281 | (:yearsCmp = 'less' AND years_total(e.start_date, e.end_date) < :yearsValue) |
| | 282 | ) |
| | 283 | ); |
| | 284 | }}} |