From a3153c99aa22482c1e770d0618418ae3721753af Mon Sep 17 00:00:00 2001 From: Daria Date: Fri, 6 Jul 2018 21:41:07 +0300 Subject: [PATCH 1/5] init I've made DZ, D.Drayko --- tasks/lec_1/DomZ1.sql | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tasks/lec_1/DomZ1.sql diff --git a/tasks/lec_1/DomZ1.sql b/tasks/lec_1/DomZ1.sql new file mode 100644 index 0000000..3edef7a --- /dev/null +++ b/tasks/lec_1/DomZ1.sql @@ -0,0 +1,67 @@ +--1. +select count(*) as Fall from fw_process_log +where N_STATUS = 500 +and instr( V_MESSAGE, 'Заказ 2520123')<>0; + +--2. +select '2520123' as OrderNum, to_char(max(DT_TIMESTAMP),'dd.mon.yyyy') as OrderDate from fw_process_log +where N_STATUS = 500 +and instr( V_MESSAGE, 'Заказ 2520123')<>0; + +--3. +select distinct(ltrim(V_MESSAGE,'Загрузка порции заказов начиная с ')) as ORDER_NUM from fw_process_log +where N_ACTION=12 +and V_MESSAGE<>'Загрузка порции заказов начиная с -1'; + +--4. +select COUNT(distinct(ltrim(V_MESSAGE,'Загрузка порции заказов начиная с '))) from fw_process_log +where N_ACTION=12; + +--5. +select sum(cast (substr(V_MESSAGE, (length(V_MESSAGE)-8)) as numeric)) as DURATIO from fw_process_log +where N_ACTION=11; + +--6. +select count(*) as CLOSED from fw_process_log +where N_ACTION = 11 +and DT_TIMESTAMP >=TO_DATE('1, 3, 2018', 'dd.mm.yyyy') +and DT_TIMESTAMP <=TO_DATE('31, 3, 2018', 'dd.mm.yyyy'); + +--7. +select count(*) as REPEA from (select SID, count(ID_LOG) as Qu from fw_process_log +group by SID) X +where Qu>1; + +--8. +select DT_TIMESTAMP, substr(V_MESSAGE,13,instr(V_MESSAGE, '@')-13) as LOGIN from fw_process_log +where ID_USER=11136 +and DT_TIMESTAMP=(select max(DT_TIMESTAMP) from fw_process_log +where ID_USER=11136); + +--9. +select to_char(trunc(DT_TIMESTAMP, 'MM'), 'Month') as Mont, count(*) as Quantity from fw_process_log +group by trunc(DT_TIMESTAMP, 'MM') ; + +--10. +select count(*) as Quer, count(distinct(V_MESSAGE)) as Uniq from fw_process_log +where N_STATUS=500 +and ID_PROCESS=5 +and DT_TIMESTAMP > to_date('22.02.2018', 'dd.mm.yyyy') +and DT_TIMESTAMP < to_date('02.03.2018', 'dd.mm.yyyy'); + +--11. +select min(N_SUM) from fw_transfers +where DT_INCOMING <= to_date('14.02.2017 12:00:00', 'dd.mm.yyyy hh24:mi:ss') +and DT_INCOMING >= to_date('14.02.2017 10:00:00', 'dd.mm.yyyy hh24:mi:ss') +and ID_CONTRACT_FROM <> ID_CONTRACT_TO; + +--12. +select ID_CONTRACT_TO, DT_REAL, length(V_DESCRIPTION) as V_LEN from fw_transfers +where length(V_DESCRIPTION)>22 +order by V_LEN desc; + +--13. +select to_char(DT_INCOMING, 'dd.mm.yyyy') as CH_DATE, count(*) as CONTRACTS from (select * from fw_transfers +where ID_CONTRACT_FROM=ID_CONTRACT_TO) X +group by to_char(DT_INCOMING, 'dd.mm.yyyy'); + From 4fe6579de8d8605297d24073cdfd7099f516efd8 Mon Sep 17 00:00:00 2001 From: Daria Date: Wed, 11 Jul 2018 10:52:40 +0300 Subject: [PATCH 2/5] I tried not to fail, but I did. DZ2, D.Drayko --- tasks/lec_2/dz2.sql | 147 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 tasks/lec_2/dz2.sql diff --git a/tasks/lec_2/dz2.sql b/tasks/lec_2/dz2.sql new file mode 100644 index 0000000..f4e040b --- /dev/null +++ b/tasks/lec_2/dz2.sql @@ -0,0 +1,147 @@ +--1. +select t.f_sum, t.dt_event from fw_contracts f + join TRANS_EXTERNAL t + on t.id_contract = f.id_contract_inst + and t.v_status >= 'A' + and t.dt_event= (select max(t.dt_event) + from fw_contracts f + join TRANS_EXTERNAL t + on t.id_contract = f.id_contract_inst + and t.v_status >= 'A' + + where f.dt_start <= current_timestamp + and f.dt_stop > current_timestamp + and f.v_ext_ident = '0102100000088207_MG1') + + where f.dt_start <= current_timestamp + and f.dt_stop > current_timestamp + and f.v_ext_ident = '0102100000088207_MG1'; + +--2. +select f.v_ext_ident, f.dt_reg_event, d.V_NAME from fw_contracts f +left join fw_departments d +on d.id_department=f.id_department +and d.b_deleted=0 +where f.dt_start<=current_timestamp +and f.dt_stop>current_timestamp; + +--3. +select V_NAME from (select count(*) as Quant_Contr, V_NAME from fw_contracts c + join fw_departments d + on d.id_department=c.id_department + where c.dt_start<=current_timestamp + and c.dt_stop>current_timestamp + group by d.V_NAME) +where Quant_contr<2; + + +--4. +select d.V_NAME, All_sum, Q_Sum, Q_contr from +(select d.id_department as ID_D, nvl2(sum(t.f_sum), sum(t.f_sum), 0) as All_sum, count(t.f_sum) as Q_sum, count(c.id_contract_inst) as Q_Contr from fw_departments d +left join fw_contracts c +on c.id_department=d.id_department +and c.dt_start <= current_timestamp + and c.dt_stop > current_timestamp +left join trans_external t +on t.id_contract = c.id_contract_inst + and t.v_status = 'A' +and t.dt_event> (select max(t.dt_event) +from trans_external t) - interval '1' month +where b_deleted=0 +group by d.id_department) X +join fw_departments d +on d.id_department=X.ID_D + +--5. +select f.v_ext_ident, f.v_status, (select count(1) + from TRANS_EXTERNAL t + where t.id_contract = f.id_contract_inst + and t.DT_EVENT=to_date('01.01.2017', 'dd.mm.yyyy') + and t.v_status = 'A') as Quant + + from fw_contracts f + where f.dt_start <= current_timestamp + and f.dt_stop > current_timestamp + and 3 <= (select count(1) + from TRANS_EXTERNAL t + where t.id_contract = f.id_contract_inst + and t.DT_EVENT=to_date('01.01.2017', 'dd.mm.yyyy') + and t.v_status = 'A') ; + +--6. +select distinct c.V_EXT_IDENT, c.V_STATUS, d.V_NAME +from fw_contracts c +join trans_external t +on t.id_contract = c.id_contract_inst +and t.v_status>='A' +and exists (select * from trans_external + where t.dt_event=to_date('01.01.2017', 'dd.mm.yyyy')) +left join fw_departments d +on d.id_department = c.id_department + and d.b_deleted = 0 + +where c.dt_start <= current_timestamp + and c.dt_stop > current_timestamp; + +--7. +select distinct d.V_NAME + from (select * from fw_contracts c + where c.dt_start <= current_timestamp + and c.dt_stop > current_timestamp) z + right join fw_departments d + on d.id_department=z.id_department + + and b_deleted =0 + where z.id_department is null; + +--8. + +select distinct Quant, Last_trans, c.v_ext_ident, u.v_username from +(select c.id_contract_inst as ID_contr, count(id_contract) as Quant, max(t.dt_event) as Last_trans, max(t.id_source) as SOURC + from fw_contracts c + left join TRANS_EXTERNAL t + on t.id_contract = c.id_contract_inst + and t.v_status >= 'A' + + where c.dt_start <= current_timestamp + and c.dt_stop > current_timestamp + group by c.id_contract_inst) X + join fw_contracts c + on c.id_contract_inst=X.ID_contr + left join ci_users u + on u.id_user=X.SOURC + and u.v_status='a' + ; + + + +--9. +select f.V_Ext_ident + from fw_contracts f + join TRANS_EXTERNAL t + on t.id_contract = f.id_contract_inst + and t.id_trans=6397542 + where f.dt_start <= to_date('01.01.2016','dd.mm.yyyy') + and f.dt_stop >= to_date('01.01.2016','dd.mm.yyyy'); + +--10. +select c.ID_CONTRACT_INST, c.V_EXT_IDENT, c.V_STATUS, cur.V_NAME from fw_contracts c +join FW_CURRENCY cur +on cur.id_currency=c.ID_CURRENCY +and cur.b_deleted=0 + +where c.ID_CONTRACT_INST in (select ID_CONTRACT_INST from (select ID_CONTRACT_INST,count(distinct ID_CURRENCY) as Qu from fw_contracts c + group by ID_CONTRACT_INST) + where Qu>1) +and c.dt_start <= current_timestamp + and c.dt_stop > current_timestamp; + +--11. +select ID_CONTRACT_INST from (select ID_CONTRACT_INST, count(*) as Qu from (select * from fw_contracts c +where c.V_STATUS='C') + +group by ID_CONTRACT_INST) +where Qu>1 From 741a8a7207dde511346cc8cd5e9cbe75f8eff94d Mon Sep 17 00:00:00 2001 From: Daria Date: Sat, 14 Jul 2018 21:29:28 +0300 Subject: [PATCH 3/5] Prac from Drayko D. --- tasks/prac_1/prac.sql | 146 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 tasks/prac_1/prac.sql diff --git a/tasks/prac_1/prac.sql b/tasks/prac_1/prac.sql new file mode 100644 index 0000000..539fca8 --- /dev/null +++ b/tasks/prac_1/prac.sql @@ -0,0 +1,146 @@ +--4. +select Serv_Tab.v_name, dep_name, sum(ap) from (select * from fw_services sc +join fw_service se +on se.id_service=sc.id_service +and se.b_deleted=0 +where sc.v_status='A' + and sc.dt_start <= current_timestamp + and sc.dt_stop > current_timestamp + and sc.b_deleted=0) Serv_Tab +join (select ID_C, AP, Y.DEP, DEP_NAME + from (select f.id_contract_inst as ID_C, + sum(s.N_cost_period) as AP, + d.id_department as DEP + + from fw_contracts f + join fw_services_cost s + on s.id_contract_inst = f.id_contract_inst + and s.dt_start <= current_timestamp + and s.dt_stop > current_timestamp + left join fw_departments d + on d.id_department = f.id_department + and b_deleted = 0 + + + where f.dt_start <= current_timestamp + and f.dt_stop > current_timestamp + group by f.id_contract_inst, d.id_department) AV + Join (select X.DEP as DEP, X.DEP_NAME as DEP_NAME, avg(X.SUMM) AS AVG_SUMM + from (select f.id_contract_inst as ID_C, + sum(s.N_cost_period) as SUMM, + d.id_department as DEP, + d.V_NAME as DEP_NAME + from fw_contracts f + join fw_services_cost s + on s.id_contract_inst = f.id_contract_inst + and s.dt_start <= current_timestamp + and s.dt_stop > current_timestamp + left join fw_departments d + on d.id_department = f.id_department + and b_deleted = 0 + + where f.dt_start <= current_timestamp + and f.dt_stop > current_timestamp + group by f.id_contract_inst, d.id_department, d.V_NAME) X + group by X.DEP, X.DEP_NAME) Y + on Y.Dep=Av.dep + where av.ap>y.avg_summ) Z3 + on Z3.ID_C=Serv_Tab.id_contract_inst + + group by Serv_Tab.v_name, Dep_name; + +--5. +select * from (select count(distinct fsc.n_discount_period) as CHANGES, fc.v_ext_ident as ID_C from fw_contracts fc +join fw_services_cost fsc +on fsc.id_contract_inst=fc.id_contract_inst +and fsc.dt_start>to_date('31.10.2017', 'dd.mm.yyyy') +and fsc.dt_startcurrent_timestamp +and fc.dt_start=2; + +--6. +select Y.DEP, Y.TP, Y.AP from (select tp.v_name as TP, d.v_name as DEP, sum(s.n_cost_period) as AP +from fw_contracts f + join fw_services_cost s + on s.id_contract_inst = f.id_contract_inst + and s.dt_start <= current_timestamp + and s.dt_stop > current_timestamp + + left join fw_departments d + on d.id_department = f.id_department + and b_deleted = 0 + + join fw_services cs + on cs.id_service_inst=s.id_service_inst + and cs.b_deleted=0 + and cs.dt_start <= current_timestamp + and cs.dt_stop > current_timestamp + and cs.v_status='A' + + join fw_service fws + on fws.id_service=cs.id_service + and fws.b_deleted=0 + and fws.b_add_service=1 + + join fw_tariff_plan tp + on tp.id_tariff_plan=cs.id_tariff_plan + and tp.dt_start <= current_timestamp + and tp.dt_stop > current_timestamp + and tp.b_deleted=0 + + + where f.dt_start <= current_timestamp + and f.dt_stop > current_timestamp + and f.v_status='A' + +group by tp.v_name, d.v_name) Y + +cross join + +(select X.DEP, max(X.AP) as M_AP from (select tp.v_name as TP, d.v_name as DEP, sum(s.n_cost_period) as AP +from fw_contracts f + join fw_services_cost s + on s.id_contract_inst = f.id_contract_inst + and s.dt_start <= current_timestamp + and s.dt_stop > current_timestamp + + left join fw_departments d + on d.id_department = f.id_department + and b_deleted = 0 + + join fw_services cs + on cs.id_service_inst=s.id_service_inst + and cs.b_deleted=0 + and cs.dt_start <= current_timestamp + and cs.dt_stop > current_timestamp + and cs.v_status='A' + + join fw_service fws + on fws.id_service=cs.id_service + and fws.b_deleted=0 + and fws.b_add_service=1 + + join fw_tariff_plan tp + on tp.id_tariff_plan=cs.id_tariff_plan + and tp.dt_start <= current_timestamp + and tp.dt_stop > current_timestamp + and tp.b_deleted=0 + + + where f.dt_start <= current_timestamp + and f.dt_stop > current_timestamp + and f.v_status='A' + +group by tp.v_name, d.v_name) X +group by X.DEP) Z + +where Z.DEP=Y.DEP +and Y.AP=Z.M_AP + + + From 0761a53ce1bab2dc566209999a578dd084710d02 Mon Sep 17 00:00:00 2001 From: Daria Date: Tue, 17 Jul 2018 22:50:23 +0300 Subject: [PATCH 4/5] Dz 3 from D. Drayko --- tasks/lec_3/dz3.sql | 160 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 tasks/lec_3/dz3.sql diff --git a/tasks/lec_3/dz3.sql b/tasks/lec_3/dz3.sql new file mode 100644 index 0000000..3c6aee5 --- /dev/null +++ b/tasks/lec_3/dz3.sql @@ -0,0 +1,160 @@ +--1. +CREATE OR REPLACE PROCEDURE saveSIGNERS(pV_FIO IN scd_signers.v_fio%TYPE, + pID_MANAGER IN scd_signers.id_manager%TYPE, + pACTION IN NUMBER) IS + pid_USER NUMBER; +BEGIN + SELECT ci.id_user + INTO pid_USER + FROM ci_users ci + WHERE ci.id_user = pID_MANAGER; + + CASE + WHEN pACTION = 3 THEN + DELETE FROM scd_signers ss WHERE id_manager = pID_MANAGER; + WHEN pACTION = 1 THEN + INSERT INTO scd_signers + (v_fio, id_manager) + VALUES + (pV_FIO, pID_MANAGER); + WHEN pACTION = 2 THEN + UPDATE scd_signers + SET v_fio = pv_fio + WHERE id_manager= pID_MANAGER; + END CASE; +EXCEPTION + WHEN no_data_found THEN + raise_application_error (-20020,'Пользователь не найден'); + WHEN OTHERS THEN + raise_application_error(-20020, + 'Существующий пользователь'); +END; +/ +--2. +create or replace function getDecoder(id_eq in scd_equip_kits.id_equip_kits_inst%type) +return varchar2 IS + +v_ext varchar2(255); +v_cas varchar2(255); +v_ret varchar2(255); +agency number; + BEGIN + + + + SELECT c.b_agency INTO agency + FROM scd_equip_kits ek JOIN scd_contracts c ON c.id_contract_inst = ek.id_contract_inst + WHERE ek.id_equip_kits_inst=id_eq and ek.dt_start <= current_timestamp AND ek.dt_stop > current_timestamp; + + CASE + WHEN agency = 1 THEN + SELECT ek.V_CAS_ID INTO v_ret + FROM SCD_EQUIP_KITS ek WHERE ek.id_equip_kits_inst=id_eq and ek.dt_start <= current_timestamp AND ek.dt_stop > current_timestamp; + WHEN agency <> 1 THEN + SELECT ek.V_EXT_IDENT INTO v_ret + FROM SCD_EQUIP_KITS ek WHERE ek.id_equip_kits_inst=id_eq and ek.dt_start <= current_timestamp AND ek.dt_stop > current_timestamp; + + END CASE; +return v_ret; +EXCEPTION + WHEN no_data_found THEN + raise_application_error (-20020,'Оборудование не найдено'); + WHEN OTHERS THEN + raise_application_error(-20020, 'Какая-то ошибка'); + +END; +/ +--3. +create or replace PROCEDURE getEquip(dwr OUT sys_refcursor, + pID_EQUIP_KITS_INST in number default null) IS + BEGIN + + case + when pID_EQUIP_KITS_INST is null + then + + OPEN dwr FOR + select cl.V_LONG_TITLE, u.V_USERNAME, fc.V_EXT_IDENT, ekt.v_name, getDecoder(ek.id_equip_kits_inst) from scd_equip_kits ek + join fw_contracts fc + on fc.id_contract_inst=ek.id_contract_inst + and fc.dt_start <= current_timestamp AND fc.dt_stop > current_timestamp + and fc.v_status='A' + join fw_clients cl + on cl.ID_CLIENT_INST=fc.ID_CLIENT_INST + and cl.dt_start <= current_timestamp AND cl.dt_stop > current_timestamp + join ci_users u + on u.ID_CLIENT_INST=fc.ID_CLIENT_INST + and u.v_status='A' + join scd_equipment_kits_type ekt + on ekt.id_equip_kits_type=ek.id_equip_kits_type + and ekt.dt_start <= current_timestamp AND ekt.dt_stop > current_timestamp + where ek.dt_start <= current_timestamp AND ek.dt_stop > current_timestamp + ; + when pID_EQUIP_KITS_INST is not null + then + OPEN dwr FOR + select cl.V_LONG_TITLE, u.V_USERNAME, fc.V_EXT_IDENT, ekt.v_name, getDecoder(ek.id_equip_kits_inst) from scd_equip_kits ek + join fw_contracts fc + on fc.id_contract_inst=ek.id_contract_inst + and fc.dt_start <= current_timestamp AND fc.dt_stop > current_timestamp + and fc.v_status='A' + join fw_clients cl + on cl.ID_CLIENT_INST=fc.ID_CLIENT_INST + and cl.dt_start <= current_timestamp AND cl.dt_stop > current_timestamp + join ci_users u + on u.ID_CLIENT_INST=fc.ID_CLIENT_INST + and u.v_status='A' + join scd_equipment_kits_type ekt + on ekt.id_equip_kits_type=ek.id_equip_kits_type + and ekt.dt_start <= current_timestamp AND ekt.dt_stop > current_timestamp + where ek.dt_start <= current_timestamp AND ek.dt_stop > current_timestamp and ek.ID_EQUIP_KITS_INST=pID_EQUIP_KITS_INST + ; + + end case; + EXCEPTION + WHEN no_data_found THEN + raise_application_error (-20020,'Нету'); + WHEN OTHERS THEN + raise_application_error(-20020, + 'Другая ошибка'); + + + END; + / +--4. + CREATE OR REPLACE PROCEDURE checkstatus IS + +BEGIN + FOR i IN (select distinct ek.id_equip_kits_inst, cl.V_LONG_TITLE, c.b_agency, fc.v_ext_ident from scd_equip_kits ek + JOIN scd_equipment_status ses + ON ses.id_equipment_status = ek.id_status + AND ses.b_deleted = 0 + and ses.v_name <>'Продано' + join fw_contracts fc + on fc.id_contract_inst=ek.id_contract_inst + and fc.dt_start <= current_timestamp AND fc.dt_stop > current_timestamp + and fc.v_status='A' + join fw_clients cl + on cl.ID_CLIENT_INST=fc.ID_CLIENT_INST + and cl.dt_start <= current_timestamp AND cl.dt_stop > current_timestamp + JOIN scd_contracts c ON c.id_contract_inst = ek.id_contract_inst + join scd_equipment_kits_type ekt + on ekt.id_equip_kits_type=ek.id_equip_kits_type + and ekt.dt_start <= current_timestamp AND ekt.dt_stop > current_timestamp + where ek.dt_start <= current_timestamp AND ek.dt_stop > current_timestamp) LOOP + + + update scd_equipment_status + set v_name='Продано'; + + if i.b_agency=1 + + then + dbms_output.put_line ('Для оборудования '||i.id_equip_kits_inst||' дилера '||i.V_LONG_TITLE||' с контрактом '||i.v_ext_ident||',являющегося агентской сетью был проставлен статус Продано.'); + else + dbms_output.put_line ('Для оборудования '||i.id_equip_kits_inst||' дилера '||i.V_LONG_TITLE||' с контрактом '||i.v_ext_ident||',не являющегося агентской сетью был проставлен статус Продано.'); + end if; + END LOOP; + end; + + From 9c0bcf6b02ef9097c86c69c047f004296614ea13 Mon Sep 17 00:00:00 2001 From: Daria Date: Sun, 22 Jul 2018 23:27:48 +0300 Subject: [PATCH 5/5] Dz 4 from D. Drayko --- tasks/lec_4/dz4.sql | 261 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 tasks/lec_4/dz4.sql diff --git a/tasks/lec_4/dz4.sql b/tasks/lec_4/dz4.sql new file mode 100644 index 0000000..087288e --- /dev/null +++ b/tasks/lec_4/dz4.sql @@ -0,0 +1,261 @@ +--1. +create or replace function Valid_IP (IP_ADDRESS in varchar2) + return number + IS +fst number; +sec number; +thd number; +fth number; + BEGIN + + + if (length(IP_ADDRESS) - length(replace(IP_ADDRESS,'.')))!=3 + then return 0; + else + fst:= to_number(substr(IP_ADDRESS, 1, instr(IP_ADDRESS,'.')-1)); + sec:= to_number(substr(IP_ADDRESS, instr(IP_ADDRESS,'.')+1, instr(IP_ADDRESS,'.',1,2)-instr(IP_ADDRESS,'.'))); + thd:= to_number(substr(IP_ADDRESS, instr(IP_ADDRESS,'.',1,2)+1, instr(IP_ADDRESS,'.',1,3)-instr(IP_ADDRESS,'.',1,2))); + fth:= to_number(substr(IP_ADDRESS, instr(IP_ADDRESS,'.',1,3)+1)); if fst>255 or sec>255 or thd>255 or fth>255 + then return 0; + else return 1; + end if; + end if; +exception +when others then +raise_application_error (-20026, 'INVALID IP ADRESS' ); + + END; + / + +create or replace PROCEDURE saveCOMMUTATOR( + pID_COMMUTATOR in incb_commutator.id_commutator%type, + pIP_ADDRESS IN incb_commutator.IP_ADDRESS%TYPE, + pID_COMMUTATOR_TYPE IN incb_commutator.ID_COMMUTATOR_TYPE%TYPE default null, + pV_DESCRIPTION IN incb_commutator.V_DESCRIPTION%TYPE default null, + pB_DELETED IN incb_commutator.B_DELETED%TYPE default null, + pV_MAC_ADDRESS IN incb_commutator.V_MAC_ADDRESS%TYPE, + pV_COMMUNITY_READ IN incb_commutator.V_COMMUNITY_READ%TYPE, + pV_COMMUNITY_WRITE IN incb_commutator.V_COMMUNITY_WRITE%TYPE, + pREMOTE_ID IN incb_commutator.REMOTE_ID%TYPE, + pB_NEED_CONVERT_HEX IN incb_commutator.B_NEED_CONVERT_HEX%TYPE default 0, + pREMOTE_ID_HEX IN incb_commutator.REMOTE_ID_HEX%TYPE default null, + pACTION in number) IS +ip_ad NUMBER; +V_MAC NUMBER; +existing_adress exception; +input_hex exception; +bad_ip exception; +begin + +if pB_NEED_CONVERT_HEX=1 and pREMOTE_ID_HEX is null and pACTION!=1 +then raise input_hex; +elsif Valid_IP(pIP_ADDRESS) = 0 and pACTION!=1 +then raise bad_ip; +end if; -- hex + + +select count(1) +into ip_ad +from incb_commutator C +WHERE B_DELETED=0 +AND C.IP_ADDRESS=pIP_ADDRESS; -- ip + +select count(1) +into V_MAC +from incb_commutator C +WHERE B_DELETED=0 +AND C.V_MAC_ADDRESS=pV_MAC_ADDRESS; -- mac + + CASE + WHEN pACTION = 1 THEN + DELETE FROM incb_commutator WHERE IP_ADDRESS = pIP_ADDRESS and V_MAC_ADDRESS=pV_MAC_ADDRESS; + WHEN pACTION = 2 AND IP_AD=0 AND V_MAC=0 THEN + + INSERT INTO incb_commutator + (ID_COMMUTATOR, IP_ADDRESS, ID_COMMUTATOR_TYPE, V_DESCRIPTION, B_DELETED, V_MAC_ADDRESS, V_COMMUNITY_READ, V_COMMUNITY_WRITE, REMOTE_ID, B_NEED_CONVERT_HEX, REMOTE_ID_HEX) + VALUES + (s_incb_commutator.nextval, pIP_ADDRESS, pID_COMMUTATOR_TYPE, pV_DESCRIPTION, pB_DELETED, pV_MAC_ADDRESS, pV_COMMUNITY_READ, pV_COMMUNITY_WRITE, pREMOTE_ID, pB_NEED_CONVERT_HEX, pREMOTE_ID_HEX); + WHEN pACTION = 2 AND (IP_AD!=0 OR V_MAC!=0) THEN + raise existing_adress; + WHEN pACTION = 3 THEN + UPDATE incb_commutator + SET V_COMMUNITY_READ=pV_COMMUNITY_READ, V_COMMUNITY_WRITE = pV_COMMUNITY_WRITE, REMOTE_ID=pREMOTE_ID, + ID_COMMUTATOR_TYPE=pID_COMMUTATOR_TYPE, V_DESCRIPTION=pV_DESCRIPTION, B_DELETED=pB_DELETED + WHERE IP_ADDRESS = pIP_ADDRESS and V_MAC_ADDRESS=pV_MAC_ADDRESS; + END CASE; +EXCEPTION + WHEN no_data_found THEN + raise_application_error (-20022,'UNKNOWN ADRESS'); + WHEN existing_adress THEN + raise_application_error(-20020, 'EXISTING ADRESS' ); + WHEN input_hex THEN + raise_application_error (-20021, 'input REMOTE_ID_HEX' ); + WHEN bad_ip THEN + raise_application_error (-20026, 'INVALID IP ADRESS' ); + + WHEN OTHERS THEN + raise_application_error(-20023, + 'OTHERS'); +END; +/ + +create or replace PROCEDURE getCOMMUTATOR(dwr OUT sys_refcursor, + pID_COMMUTATOR in incb_commutator.id_commutator%type default null) IS + BEGIN + + OPEN dwr FOR + select ic.IP_ADDRESS, ic.V_MAC_ADDRESS, ict.V_VENDOR, ict.V_MODEL, + case + when B_NEED_CONVERT_HEX=1 + then ic.REMOTE_ID_HEX + else ic.REMOTE_ID + end + from incb_commutator ic + join incb_commutator_type ict + on ict.ID_COMMUTATOR_TYPE=ic.ID_COMMUTATOR_TYPE + and ict.b_deleted=0 + + where ic.b_deleted=0 and ( pID_COMMUTATOR is null or ic.ID_COMMUTATOR=pID_COMMUTATOR ); + + + END; + / + declare + u sys_refcursor; + begin +saveCOMMUTATOR(1,'128.118.5.56',2001,'description',0,'51:96:4','1','1','32',1,'yuegf', 2); +saveCOMMUTATOR(1,'128.178.5.56',2002,'description',0,'61:91:4','1','1','fg',0,null, 2); +saveCOMMUTATOR(1,'128.188.5.56',2003,'description',0,'561:46:4','1','1','45f',1,0, 2); +saveCOMMUTATOR(1,'128.118.5.56',2001,'nodescriptionatall',0,'51:96:4','1','1','44f',1,'yuegf', 3); +saveCOMMUTATOR(1,'128.188.5.56',2003,'description',0,'561:96:4','1','1','45f',1,0, 1); +getCOMMUTATOR(u,2001); +end; +/ +select * from incb_commutator +/ + +--2. +create or replace function check_access_comm(pIP_ADDRESS IN incb_commutator.IP_ADDRESS%TYPE, + V_COMMUNITY IN incb_commutator.V_COMMUNITY_WRITE%TYPE, + B_MODE_WRITE in number) +return number IS + + +ACCESSS varchar2(255); + BEGIN + + select case B_MODE_WRITE + when 1 + then ic.V_COMMUNITY_WRITE + when 0 + then ic.V_COMMUNITY_read + end + into ACCESSS + from incb_commutator ic + where b_deleted=0 + and ic.ip_address=pIP_ADDRESS; + + + + if ACCESSS=V_COMMUNITY + then return 1; + else return 0; + end if; + + EXCEPTION + WHEN no_data_found THEN + raise_application_error (-20025, 'NO SUCH AN ADRESS' ); + WHEN OTHERS THEN + raise_application_error(-20020, '- '); + end; + / + --3. + create or replace function get_remote_id(pID_COMMUTATOR in incb_commutator.id_commutator%type) +return number IS + +needed number; +hexx varchar2(255); +emptyy exception; +remote_id1 varchar2(255); + BEGIN + select REMOTE_ID_HEX, B_NEED_CONVERT_HEX, REMOTE_ID into hexx, needed, remote_id1 from incb_commutator + where ID_COMMUTATOR=pID_COMMUTATOR + and b_deleted=0; + + case + when needed=1 and hexx is null + then raise emptyy; + when needed=1 and hexx is not null + then return hexx; + when needed=0 + then return remote_id1; + + end case; + + EXCEPTION + WHEN no_data_found THEN + raise_application_error (-20025, 'NO SUCH AN ID' ); + WHEN emptyy THEN + raise_application_error(-20026, 'HEX NOT FOUND'); + end; + +--4. , , . :( +TYPE listed IS TABLE OF number; +create or replace PROCEDURE check_and_del_data (B_FORCE_DELETE in number, coll out listed) +is + + + COMM_rec incb_commutator%ROWTYPE; + +coll1 listed; +coll2 listed; +coll3 listed; +coll4 listed; +begin + + select distinct ID_COMMUTATOR bulk collect + into coll1 + from incb_commutator ic +cross join (select V_MAC_ADDRESS, count(*) as Qu from incb_commutator + where b_deleted=0 + group by V_MAC_ADDRESS) U + where U.V_MAC_ADDRESS=ic.V_MAC_ADDRESS + and U.Qu>1; + +select distinct ID_COMMUTATOR bulk collect + into coll1 + from incb_commutator ic +cross join (select IP_ADDRESS, count(*) as Qu from incb_commutator + where b_deleted=0 + group by IP_ADDRESS) U + where U.IP_ADDRESS=ic.IP_ADDRESS + and U.Qu>1; + +select distinct ID_COMMUTATOR bulk collect + into coll3 + from incb_commutator + where b_deleted=0 + and B_NEED_CONVERT_HEX=1 + and REMOTE_ID_HEX is null; + +select distinct ID_COMMUTATOR bulk collect + into coll4 + from incb_commutator + where Valid_IP(IP_ADDRESS) = 0 + and b_deleted=0; + coll:=coll1 MULTISET UNION DISTINCT coll2 MULTISET UNION DISTINCT coll3 MULTISET UNION DISTINCT coll4; + +if B_FORCE_DELETE = 1 +then +for i in coll.first .. coll.last loop +SELECT * INTO COMM_rec FROM +incb_commutator +WHERE B_DELETED=0 +AND ID_COMMUTATOR=COLL(i); + saveCOMMUTATOR(comm_rec.ID_COMMUTATOR, comm_rec.IP_ADDRESS, comm_rec.ID_COMMUTATOR_TYPE, comm_rec.V_DESCRIPTION, + comm_rec.B_DELETED, comm_rec.V_MAC_ADDRESS, comm_rec.V_COMMUNITY_READ, comm_rec.V_COMMUNITY_WRITE, + comm_rec.REMOTE_ID, comm_rec.B_NEED_CONVERT_HEX, comm_rec.REMOTE_ID_HEX, 1); + end loop; +end if; + end; +