[Apocalypse.cow] PL/SQL voorbeelden

De Blende, Christophe christophe.deblende op vlaanderen.be
Wo Jul 4 14:17:00 CEST 2018


Co’s,

hier een paar voorbeelden van stored procedures in postgres.


-- functie die in verschillende tabellen zoekt of een bepaalde text voorkomt en logt dit in de console
CREATE OR REPLACE FUNCTION bsos.searchReferences(searchTerm TEXT)
  RETURNS VOID AS $$
DECLARE
  _events domainevententry%ROWTYPE;
  _sagas  sagaentry%ROWTYPE;
  _jobs   qrtz_job_details%ROWTYPE;
BEGIN
  RAISE NOTICE '====================================================================================';
  RAISE NOTICE 'Searching "%" in domain events', searchTerm;
  FOR _events IN SELECT *
                 FROM domainevententry
                 WHERE encode(payload, 'escape') ~* searchTerm
                 ORDER BY timestamp, sequencenumber
  LOOP
    RAISE NOTICE '------------------------------------------------------------------------------------';
    RAISE NOTICE ' -> found a match for event with id "%" - type "%" - timestamp "%"', _events.eventidentifier, _events.type, _events.timestamp;
    RAISE NOTICE ' -> "%"', encode(_events.payload, 'escape');
  END LOOP;
  RAISE NOTICE '------------------------------------------------------------------------------------';

  RAISE NOTICE '====================================================================================';
  RAISE NOTICE 'Searching "%" in sagas', searchTerm;
  FOR _sagas IN SELECT se.*
                FROM sagaentry se
                WHERE encode(se.serializedsaga, 'escape') ~* searchTerm
                      OR se.sagaid IN (SELECT ave.sagaid
                                       FROM associationvalueentry ave
                                       WHERE ave.associationvalue = searchTerm)
  LOOP
    RAISE NOTICE '------------------------------------------------------------------------------------';
    RAISE NOTICE ' -> found a match for saga with id "%" and type "%"', _sagas.sagaid, _sagas.sagatype;
    RAISE NOTICE ' -> "%"', encode(_sagas.serializedsaga, 'escape');
  END LOOP;
  RAISE NOTICE '------------------------------------------------------------------------------------';

  RAISE NOTICE '====================================================================================';
  RAISE NOTICE 'Searching "%" in quartz jobs', searchTerm;
  FOR _jobs IN SELECT *
               FROM qrtz_job_details
               WHERE encode(job_data, 'escape') ~* searchTerm
  LOOP
    RAISE NOTICE '------------------------------------------------------------------------------------';
    RAISE NOTICE ' -> found a match';
    RAISE NOTICE ' -> "%"', encode(_jobs.job_data, 'escape');
  END LOOP;
  RAISE NOTICE '------------------------------------------------------------------------------------';

END;
$$ LANGUAGE plpgsql


-- index op json array moet mogelijk gemaakt worden; kan enkel via IMMUTABLE function
-- http://stackoverflow.com/questions/18404055/index-for-finding-an-element-in-a-json-array
CREATE OR REPLACE FUNCTION json_val_arr(_j JSON, _key TEXT)
  RETURNS TEXT [] AS
  $$
SELECT array_agg(elem->>_key)
FROM   json_array_elements(_j) AS x(elem)
$$
LANGUAGE SQL IMMUTABLE


CREATE OR REPLACE FUNCTION trfn_log_cud() RETURNS trigger AS $$
  BEGIN
    IF (TG_OP = 'DELETE')  THEN
       RAISE WARNING '% (%): %', TG_TABLE_NAME, TG_OP, OLD;
       RETURN OLD;
    ELSIF (TG_OP = 'INSERT')  THEN
       RAISE WARNING '% (%): % ', TG_TABLE_NAME, TG_OP, NEW;
       RETURN NEW;
    ELSIF (TG_OP = 'UPDATE')  THEN
       RAISE WARNING '% (%): % -> %' , TG_TABLE_NAME, TG_OP, OLD, NEW;
       RETURN NEW;
    END IF;
  END;
$$ LANGUAGE plpgsql

DISCLAIMER:

Deze werden gebruikt in BSOS om dingen te debuggen of om indexen te maken. Het is niet de bedoeling om business logica in een stored procedure te steken.

Met vriendelijke groeten,

Christophe De Blende
Ontwikkelaar

DEPARTEMENT OMGEVING
Afdeling Data- en Informatiebeheer, en Digitale Maatschappij
M 0498 16 13 87
Koning Albert II-laan 20 bus 8, 1000 Brussel
www.omgevingvlaanderen.be<http://www.omgevingvlaanderen.be/>


[cid:image001.png op 01D2C81C.23755FE0]<https://twitter.com/omgevingVL> <https://twitter.com/omgevingVL> [cid:image002.png op 01D2C81C.23755FE0] <https://www.facebook.com/Departementomgeving> <https://www.facebook.com/Departementomgeving>  <https://www.facebook.com/Departementomgeving> [cid:image003.png op 01D2C81C.23755FE0] <https://www.linkedin.com/company/departement-omgeving> <https://www.linkedin.com/company/departement-omgeving>






[cid:image004.png op 01D2C81C.23755FE0]

------------- volgend deel ------------
Een HTML-bijlage is gescrubt...
URL: <http://list.milieuinfo.be/pipermail/apocalypse.cow/attachments/20180704/a62cd32d/attachment-0001.html>
------------- volgend deel ------------
Een niet-tekst bijlage is gescrubt...
Naam: image001.png
Type: image/png
Grootte: 1820 bytes
Omschrijving: image001.png
URL : <http://list.milieuinfo.be/pipermail/apocalypse.cow/attachments/20180704/a62cd32d/attachment-0004.png>
------------- volgend deel ------------
Een niet-tekst bijlage is gescrubt...
Naam: image002.png
Type: image/png
Grootte: 1850 bytes
Omschrijving: image002.png
URL : <http://list.milieuinfo.be/pipermail/apocalypse.cow/attachments/20180704/a62cd32d/attachment-0005.png>
------------- volgend deel ------------
Een niet-tekst bijlage is gescrubt...
Naam: image003.png
Type: image/png
Grootte: 1874 bytes
Omschrijving: image003.png
URL : <http://list.milieuinfo.be/pipermail/apocalypse.cow/attachments/20180704/a62cd32d/attachment-0006.png>
------------- volgend deel ------------
Een niet-tekst bijlage is gescrubt...
Naam: image004.png
Type: image/png
Grootte: 3641 bytes
Omschrijving: image004.png
URL : <http://list.milieuinfo.be/pipermail/apocalypse.cow/attachments/20180704/a62cd32d/attachment-0007.png>


More information about the Apocalypse.cow mailing list