97 | | {{{#!div |
98 | | {{{#!sql |
99 | | }}} |
100 | | }}} |
| 97 | = Индексирање = |
| 98 | |
| 99 | ==== Индекси за {{{People}}} табелата ==== |
| 100 | {{{#!div |
| 101 | {{{#!sql |
| 102 | create index idx_people_embg on People(embg); |
| 103 | }}} |
| 104 | }}} |
| 105 | |
| 106 | {{{#!div |
| 107 | {{{#!sql |
| 108 | create index idx_people_names on People(first_name, last_name); |
| 109 | }}} |
| 110 | }}} |
| 111 | |
| 112 | {{{#!div |
| 113 | {{{#!sql |
| 114 | create index idx_people_nationality on People(nationality); |
| 115 | }}} |
| 116 | }}} |
| 117 | |
| 118 | ==== Индекси за {{{Crime_case}}} табелата ==== |
| 119 | |
| 120 | {{{#!div |
| 121 | {{{#!sql |
| 122 | create index idx_crime_case_status on Crime_case(c_status); |
| 123 | }}} |
| 124 | }}} |
| 125 | |
| 126 | {{{#!div |
| 127 | {{{#!sql |
| 128 | create index idx_crime_case_date on Crime_case(opening_date); |
| 129 | }}} |
| 130 | }}} |
| 131 | |
| 132 | ==== Индекси за {{{Statements}}} табелата ==== |
| 133 | |
| 134 | {{{#!div |
| 135 | {{{#!sql |
| 136 | create index idx_statements_date on Statements(statement_date); |
| 137 | }}} |
| 138 | }}} |
| 139 | |
| 140 | {{{#!div |
| 141 | {{{#!sql |
| 142 | create index idx_statements_incident on Statements(incident_timestamp); |
| 143 | }}} |
| 144 | }}} |
| 145 | |
| 146 | ==== Индекси за {{{Evidence}}} табелата ==== |
| 147 | |
| 148 | {{{#!div |
| 149 | {{{#!sql |
| 150 | create index idx_evidence_type on Evidence(e_type); |
| 151 | }}} |
| 152 | }}} |
| 153 | |
| 154 | {{{#!div |
| 155 | {{{#!sql |
| 156 | create index idx_evidence_found on Evidence(is_found); |
| 157 | }}} |
| 158 | }}} |
| 159 | |
| 160 | ==== Индекси за {{{Policeman}}} табелата ==== |
| 161 | |
| 162 | {{{#!div |
| 163 | {{{#!sql |
| 164 | create index idx_policeman_rank on Policeman(rank); |
| 165 | }}} |
| 166 | }}} |
| 167 | |
| 168 | = Функции и тригери = |
| 169 | === Траење на случај во денови === |
| 170 | {{{#!div |
| 171 | {{{#!sql |
| 172 | create or replace function calculate_case_duration(case_id bigint) |
| 173 | returns integer as $$ |
| 174 | declare |
| 175 | opening_date date; |
| 176 | closing_date date; |
| 177 | duration integer; |
| 178 | begin |
| 179 | select cc.opening_date into opening_date |
| 180 | from Crime_case as cc |
| 181 | where cc.c_id = case_id; |
| 182 | |
| 183 | select sc.date_of_closing into closing_date |
| 184 | from Solved_case as sc |
| 185 | where sc.c_id = case_id; |
| 186 | |
| 187 | if closing_date is not null then |
| 188 | duration := closing_date - opening_date; |
| 189 | else |
| 190 | duration := CURRENT_DATE - opening_date; |
| 191 | end if; |
| 192 | |
| 193 | return duration; |
| 194 | end; |
| 195 | $$ language plpgsql; |
| 196 | }}} |
| 197 | }}} |
| 198 | |
| 199 | === Промена на статус на случајот кога ќе биде завршен === |
| 200 | |
| 201 | {{{#!div |
| 202 | {{{#!sql |
| 203 | create or replace function update_case_status_on_solve() |
| 204 | returns trigger as $$ |
| 205 | begin |
| 206 | update Crime_case |
| 207 | set c_status = 'Z' |
| 208 | where c_id = NEW.c_id and c_status = 'A'; |
| 209 | |
| 210 | return new; |
| 211 | end; |
| 212 | $$ language plpgsql; |
| 213 | |
| 214 | create trigger trg_update_case_status |
| 215 | after insert on Solved_case |
| 216 | for each row |
| 217 | execute function update_case_status_on_solve(); |
| 218 | }}} |
| 219 | }}} |
| 220 | |
| 221 | = Процедури и трансакции = |
| 222 | === Креирање на нов случај со иницјална изјава === |
| 223 | {{{#!div |
| 224 | {{{#!sql |
| 225 | create or replace procedure create_crime_case( |
| 226 | in p_case_name varchar(255), |
| 227 | in p_police_station_id bigint, |
| 228 | in p_policeman_id bigint, |
| 229 | in p_victim_id bigint, |
| 230 | in p_witness_id bigint, |
| 231 | in p_statement_description varchar(10000), |
| 232 | in p_incident_timestamp timestamp, |
| 233 | in p_incident_place varchar(4000), |
| 234 | in p_crime_types bigint[] |
| 235 | ) |
| 236 | language plpgsql as $$ |
| 237 | declare |
| 238 | new_case_id bigint; |
| 239 | new_statement_id bigint; |
| 240 | crime_type_id bigint; |
| 241 | begin |
| 242 | select coalesce(MAX(c_id), 0) + 1 into new_case_id from Crime_case; |
| 243 | |
| 244 | insert into Crime_case (c_id, c_name, opening_date, c_status, p_id) |
| 245 | values (new_case_id, p_case_name, CURRENT_DATE, 'A', p_police_station_id); |
| 246 | |
| 247 | select coalesce(MAX(s_id), 0) + 1 INTO new_statement_id FROM Statements; |
| 248 | |
| 249 | insert into Statements (s_id, statement_date, description, incident_timestamp, incident_place, c_id, pe_id, victim_pe_id, witness_pe_id) |
| 250 | values (new_statement_id, CURRENT_DATE, p_statement_description, p_incident_timestamp, p_incident_place, new_case_id, p_policeman_id, p_victim_id, p_witness_id); |
| 251 | |
| 252 | foreach crime_type_id IN ARRAY p_crime_types |
| 253 | loop |
| 254 | insert into Case_belongs_to_type_of_crime (c_id, t_id) |
| 255 | values (new_case_id, crime_type_id); |
| 256 | end loop; |
| 257 | |
| 258 | raise notice 'Случајот % е успешно креиран со изјава %', new_case_id, new_statement_id; |
| 259 | end; |
| 260 | $$; |
| 261 | }}} |
| 262 | }}} |
| 263 | |
| 264 | === Трансфер на случај помеѓу полициски станици === |
| 265 | |
| 266 | {{{#!div |
| 267 | {{{#!sql |
| 268 | do $$ |
| 269 | declare |
| 270 | v_case_id bigint := 1; |
| 271 | v_old_station bigint; |
| 272 | v_new_station bigint := 2; |
| 273 | v_new_officer bigint; |
| 274 | begin |
| 275 | -- Get current station |
| 276 | select p_id into v_old_station from Crime_case where c_id = v_case_id; |
| 277 | |
| 278 | -- Get officer for new station |
| 279 | select pe_id into v_new_officer from Police_station where p_id = v_new_station; |
| 280 | |
| 281 | -- Transfer case |
| 282 | update Crime_case |
| 283 | set p_id = v_new_station |
| 284 | where c_id = v_case_id; |
| 285 | |
| 286 | -- Add transfer statement |
| 287 | insert into Statements (s_id, statement_date, description, incident_timestamp, |
| 288 | incident_place, c_id, pe_id, victim_pe_id, witness_pe_id) |
| 289 | select coalesce(MAX(s_id), 0) + 1, CURRENT_DATE, 'Случајот е префрлен од станица ' || v_old_station || ' во станица ' || v_new_station, CURRENT_TIMESTAMP, 'Префрлување на случај', v_case_id, (select pe_id from Policeman where p_id = v_new_station limit 1), 2, 3 |
| 290 | from Statements; |
| 291 | |
| 292 | raise notice 'Случајот со ID % е префрлен од полициска станица % во полициска станица %', v_case_id, v_old_station, v_new_station; |
| 293 | |
| 294 | exception |
| 295 | when others then |
| 296 | raise exception 'Transaction failed: %', SQLERRM; |
| 297 | end; |
| 298 | $$; |
| 299 | }}} |
| 300 | }}} |