3.class.sql 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. -- Функция формата даты
  2. create or replace function approx_report_date(t_date timestamptz)
  3. returns varchar(5)
  4. language plpgsql
  5. as
  6. $$
  7. begin
  8. return lpad(extract(day from t_date)::text, 2, '0') || lpad(extract(hour from t_date)::text, 2, '0') || floor((extract(minute from t_date) / 10))::varchar;
  9. end;
  10. $$;
  11. -- Функция формата высоты
  12. create or replace function approx_report_height(height numeric)
  13. returns varchar(4)
  14. language plpgsql
  15. as
  16. $$
  17. begin
  18. return lpad(height::text, 4, '0');
  19. end;
  20. $$;
  21. -- Функция формата отклонения по давлению и температуре
  22. create or replace function approx_report_deviation(pressure numeric, temperature numeric)
  23. returns varchar(5)
  24. language plpgsql
  25. as
  26. $$
  27. declare
  28. pressure_dev numeric;
  29. temperature_dev numeric;
  30. res varchar(5);
  31. begin
  32. pressure_dev = (pressure - (select value from numeric_constants where key = 'rel_pressure'));
  33. temperature_dev = (temperature - (select value from numeric_constants where key = 'rel_temperature'));
  34. temperature_dev := round(temperature_dev + calc_adjustment(calc_interpolation(temperature)));
  35. res = case when pressure_dev >= 0 then pressure_dev::text else ('5' || lpad((pressure_dev*-1)::text, 2, '0')) end;
  36. res = concat(res, lpad(temperature_dev::text, 2, '0'));
  37. return res;
  38. end;
  39. $$;
  40. -- Тип приблизительного отчета
  41. CREATE TYPE approx_report AS
  42. (
  43. date character varying(5),
  44. height character varying(4),
  45. deviation character varying(5)
  46. );
  47. -- Функция получения приблизительного отчета
  48. create or replace function approx_report(date timestamptz, height numeric, pressure numeric, temperature numeric)
  49. returns approx_report
  50. language plpgsql
  51. as
  52. $$
  53. begin
  54. return (approx_report_date(date)::varchar(5), approx_report_height(height)::varchar(4), approx_report_deviation(pressure, temperature)::varchar(5)) as approx_report;
  55. end;
  56. $$;