Ba é an t-alt a spreag an sceitse seo a scríobh
Dhealraigh sé suimiúil go raibh an smaoineamh céanna - "loighic gnó a chur i bhfeidhm sa bhunachar sonraí".
Ní mise amháin a tháinig chun cuimhne.
Chomh maith leis sin, don todhchaí, bhí mé ag iarraidh, ar an gcéad dul síos, dom féin, forbairtí suimiúla a tháinig chun cinn le linn an fhorfheidhmithe a chaomhnú. Go háirithe ag cur san áireamh go ndearnadh cinneadh straitéiseach le déanaí an ailtireacht a athrú agus loighic an ghnó a aistriú go dtí an leibhéal inneall. Ionas nach mbeidh gach rud atá forbartha go luath chun aon úsáid ag aon duine agus ní bheidh aon suim ag éinne.
Níl na modhanna a gcuirtear síos orthu de chineál éigin fionnachtana nó eisceachtúil. tá a fhios agam conas, tá gach rud clasaiceach agus tá sé curtha i bhfeidhm arís agus arís eile (mar shampla, úsáid mé cur chuige den chineál céanna 20 bliain ó shin ar Oracle) Chinn mé gach rud a bhailiú in aon áit amháin. Ar eagla go mbeidh sé áisiúil do dhuine éigin. Mar is léir ón gcleachtas, is minic a thagann an smaoineamh céanna chuig daoine éagsúla go neamhspleách. Agus tá sé úsáideach é a choinneáil duit féin mar cuimhneachán.
Ar ndóigh, níl aon rud sa saol seo foirfe, is féidir botúin agus typos ar an drochuair. Fáiltítear go láidir roimh cháineadh agus tuairimí agus táthar ag súil leo, agus mionsonra amháin eile - fágtar sonraí cur chun feidhme ar leith ar lár. Fós féin, tá gach rud fós in úsáid i dtionscadal oibre fíor. Mar sin, níl san alt ach sceitse agus cur síos ar an gcoincheap ginearálta, rud ar bith níos mó. Tá súil agam go bhfuil go leor sonraí ann chun an pictiúr iomlán a thuiscint.
Is é an smaoineamh ginearálta ná “roinn agus ceannas, folaigh agus úinéireacht”
Is é an smaoineamh clasaiceach - scéimre ar leith le haghaidh táblaí, scéimre ar leith le haghaidh feidhmeanna stóráilte.
Níl rochtain ag an gcliant ar na sonraí go díreach. Is féidir leis an gcliant go léir a dhéanamh ná an fheidhm atá stóráilte a ghlaoch agus an freagra a fhaightear a phróiseáil.
Rólanna
CREATE ROLE store;
CREATE ROLE sys_functions;
CREATE ROLE loc_audit_functions;
CREATE ROLE service_functions;
CREATE ROLE business_functions;
Scéimeanna
Scéim stórála táblaí
Táblaí sprice a chuireann aonáin ábhair i bhfeidhm.
CREATE SCHEMA store AUTHORIZATION store ;
Léaráid fheidhm an chórais
Feidhmeanna córais, go háirithe le haghaidh athruithe tábla logáil.
CREATE SCHEMA sys_functions AUTHORIZATION sys_functions ;
Scéim iniúchta áitiúil
Feidhmeanna agus táblaí chun iniúchadh áitiúil a dhéanamh ar fhorghníomhú feidhmeanna stóráilte agus athruithe ar sprioctháblaí.
CREATE SCHEMA loc_audit_functions AUTHORIZATION loc_audit_functions;
Léaráid feidhm seirbhíse
Feidhmeanna le haghaidh feidhmeanna seirbhíse agus DML.
CREATE SCHEMA service_functions AUTHORIZATION service_functions;
Léaráid feidhm ghnó
Feidhmeanna le haghaidh feidhmeanna gnó deiridh a ghlaonn an cliant orthu.
CREATE SCHEMA business_functions AUTHORIZATION business_functions;
Cearta rochtana
Ról - DBA tá rochtain iomlán aige ar gach scéimre (scartha ó ról Úinéir SS).
CREATE ROLE dba_role;
GRANT store TO dba_role;
GRANT sys_functions TO dba_role;
GRANT loc_audit_functions TO dba_role;
GRANT service_functions TO dba_role;
GRANT business_functions TO dba_role;
Ról - USER Tá an phribhléid MÍNIÚ sa léaráid feidhmeanna_gnó.
CREATE ROLE user_role;
Pribhléidí idir scéimeanna
DEONTAS
Ós rud é go gcruthaítear na feidhmeanna go léir leis an tréith SAINMHÍNteoir SLÁNDÁLA treoracha ag teastáil FORGHNÍOMHÚ MAIDIR LE GACH FEIDHM A CHÚghairm… Ón bpobal;
REVOKE EXECUTE ON ALL FUNCTION IN SCHEMA sys_functions FROM public ;
REVOKE EXECUTE ON ALL FUNCTION IN SCHEMA loc_audit_functions FROM public ;
REVOKE EXECUTE ON ALL FUNCTION IN SCHEMA service_functions FROM public ;
REVOKE EXECUTE ON ALL FUNCTION IN SCHEMA business_functions FROM public ;
GRANT USAGE ON SCHEMA sys_functions TO dba_role ;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA sys_functions TO dba_role ;
GRANT USAGE ON SCHEMA loc_audit_functions TO dba_role ;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA loc_audit_functions TO dba_role ;
GRANT USAGE ON SCHEMA service_functions TO dba_role ;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA service_functions TO dba_role ;
GRANT USAGE ON SCHEMA business_functions TO dba_role ;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA business_functions TO dba_role ;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA business_functions TO user_role ;
GRANT ALL PRIVILEGES ON SCHEMA store TO GROUP business_functions ;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA store TO business_functions ;
GRANT USAGE ON ALL SEQUENCES IN SCHEMA store TO business_functions ;
Mar sin tá scéimre an bhunachair shonraí réidh. Is féidir leat tosú ag líonadh na sonraí.
Táblaí sprice
Is fánach táblaí a chruthú. Gan aon ghnéithe speisialta, ach amháin gur cinneadh gan úsáid a bhaint as SRAITH agus seichimh a ghiniúint go follasach. Plus, ar ndóigh, úsáid uasta na treoracha
COMMENT ON ...
Tuairimí le haghaidh Gach réada, gan eisceacht.
Iniúchadh áitiúil
Chun feidhmiú na bhfeidhmeanna stóráilte agus athruithe ar sprioctháblaí a logáil, úsáidtear tábla iniúchta áitiúil, a chuimsíonn, i measc rudaí eile, sonraí faoin nasc cliant, lipéad an mhodúil ar a dtugtar, agus luachanna iarbhír an ionchuir agus paraiméadair aschuir i bhfoirm JSON.
Feidhmeanna córais
Deartha chun athruithe a logáil isteach sna táblaí sprice. Is feidhmeanna truicear iad.
Teimpléad - feidhm an chórais
---------------------------------------------------------
-- INSERT
CREATE OR REPLACE FUNCTION sys_functions.table_insert_log ()
RETURNS TRIGGER AS $$
BEGIN
PERFORM loc_audit_functions.make_log( ' '||'table' , 'insert' , json_build_object('id', NEW.id) );
RETURN NULL ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE TRIGGER table_after_insert AFTER INSERT ON storage.table FOR EACH ROW EXECUTE PROCEDURE sys_functions.table_insert_log();
---------------------------------------------------------
-- UPDATE
CREATE OR REPLACE FUNCTION sys_functions.table_update_log ()
RETURNS TRIGGER AS $$
BEGIN
IF OLD.column != NEW.column
THEN
PERFORM loc_audit_functions.make_log( ' '||'table' , 'update' , json_build_object('OLD.column', OLD.column , 'NEW.column' , NEW.column ) );
END IF ;
RETURN NULL ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE TRIGGER table_after_update AFTER UPDATE ON storage.table FOR EACH ROW EXECUTE PROCEDURE sys_functions.table_update_log ();
---------------------------------------------------------
-- DELETE
CREATE OR REPLACE FUNCTION sys_functions.table_delete_log ()
RETURNS TRIGGER AS $$
BEGIN
PERFORM loc_audit_functions.make_log( ' '||'table' , 'delete' , json_build_object('id', OLD.id ) );
RETURN NULL ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE TRIGGER table_after_delete AFTER DELETE ON storage.table FOR EACH ROW EXECUTE PROCEDURE sys_functions.table_delete_log ();
Feidhmeanna seirbhíse
Deartha chun oibríochtaí seirbhíse agus DML a chur i bhfeidhm ar na táblaí sprice.
Teimpléad - feidhm seirbhíse
--INSERT
--RETURN id OF NEW ROW
CREATE OR REPLACE FUNCTION service_functions.table_insert ( new_column store.table.column%TYPE )
RETURNS integer AS $$
DECLARE
new_id integer ;
BEGIN
-- Generate new id
new_id = nextval('store.table.seq');
-- Insert into table
INSERT INTO store.table
(
id ,
column
)
VALUES
(
new_id ,
new_column
);
RETURN new_id ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;
--DELETE
--RETURN ROW NUMBERS DELETED
CREATE OR REPLACE FUNCTION service_functions.table_delete ( current_id integer )
RETURNS integer AS $$
DECLARE
rows_count integer ;
BEGIN
DELETE FROM store.table WHERE id = current_id;
GET DIAGNOSTICS rows_count = ROW_COUNT;
RETURN rows_count ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- UPDATE DETAILS
-- RETURN ROW NUMBERS UPDATED
CREATE OR REPLACE FUNCTION service_functions.table_update_column
(
current_id integer
,new_column store.table.column%TYPE
)
RETURNS integer AS $$
DECLARE
rows_count integer ;
BEGIN
UPDATE store.table
SET
column = new_column
WHERE id = current_id;
GET DIAGNOSTICS rows_count = ROW_COUNT;
RETURN rows_count ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;
Feidhmeanna gnó
Deartha le haghaidh feidhmeanna gnó deiridh ar a dtugtar an cliant. Filleann siad i gcónaí - JSON. Chun earráidí forghníomhaithe a thascradh agus a logáil, bain úsáid as an mbloc EISCEACHT.
Teimpléad - feidhm ghnó
CREATE OR REPLACE FUNCTION business_functions.business_function_template(
--Input parameters
)
RETURNS JSON AS $$
DECLARE
------------------------
--for exception catching
error_message text ;
error_json json ;
result json ;
------------------------
BEGIN
--LOGGING
PERFORM loc_audit_functions.make_log
(
'business_function_template',
'STARTED',
json_build_object
(
--IN Parameters
)
);
PERFORM business_functions.notice('business_function_template');
--START BUSINESS PART
--END BUSINESS PART
-- SUCCESFULLY RESULT
PERFORM business_functions.notice('result');
PERFORM business_functions.notice(result);
PERFORM loc_audit_functions.make_log
(
'business_function_template',
'FINISHED',
json_build_object( 'result',result )
);
RETURN result ;
----------------------------------------------------------------------------------------------------------
-- EXCEPTION CATCHING
EXCEPTION
WHEN OTHERS THEN
PERFORM loc_audit_functions.make_log
(
'business_function_template',
'STARTED',
json_build_object
(
--IN Parameters
) , TRUE );
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR',
json_build_object('SQLSTATE',SQLSTATE ), TRUE
);
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR',
json_build_object('SQLERRM',SQLERRM ), TRUE
);
GET STACKED DIAGNOSTICS error_message = RETURNED_SQLSTATE ;
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR-RETURNED_SQLSTATE',json_build_object('RETURNED_SQLSTATE',error_message ), TRUE );
GET STACKED DIAGNOSTICS error_message = COLUMN_NAME ;
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR-COLUMN_NAME',
json_build_object('COLUMN_NAME',error_message ), TRUE );
GET STACKED DIAGNOSTICS error_message = CONSTRAINT_NAME ;
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR-CONSTRAINT_NAME',
json_build_object('CONSTRAINT_NAME',error_message ), TRUE );
GET STACKED DIAGNOSTICS error_message = PG_DATATYPE_NAME ;
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR-PG_DATATYPE_NAME',
json_build_object('PG_DATATYPE_NAME',error_message ), TRUE );
GET STACKED DIAGNOSTICS error_message = MESSAGE_TEXT ;
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR-MESSAGE_TEXT',json_build_object('MESSAGE_TEXT',error_message ), TRUE );
GET STACKED DIAGNOSTICS error_message = SCHEMA_NAME ;
PERFORM loc_audit_functions.make_log
(s
'business_function_template',
' ERROR-SCHEMA_NAME',json_build_object('SCHEMA_NAME',error_message ), TRUE );
GET STACKED DIAGNOSTICS error_message = PG_EXCEPTION_DETAIL ;
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR-PG_EXCEPTION_DETAIL',
json_build_object('PG_EXCEPTION_DETAIL',error_message ), TRUE );
GET STACKED DIAGNOSTICS error_message = PG_EXCEPTION_HINT ;
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR-PG_EXCEPTION_HINT',json_build_object('PG_EXCEPTION_HINT',error_message ), TRUE );
GET STACKED DIAGNOSTICS error_message = PG_EXCEPTION_CONTEXT ;
PERFORM loc_audit_functions.make_log
(
'business_function_template',
' ERROR-PG_EXCEPTION_CONTEXT',json_build_object('PG_EXCEPTION_CONTEXT',error_message ), TRUE );
RAISE WARNING 'ALARM: %' , SQLERRM ;
SELECT json_build_object
(
'isError' , TRUE ,
'errorMsg' , SQLERRM
) INTO error_json ;
RETURN error_json ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;
Iomlán na
Chun cur síos a dhéanamh ar an bpictiúr ginearálta, sílim gur leor é. Má tá suim ag éinne sna sonraí agus sna torthaí, scríobh tuairimí, beidh mé sásta baint bhreise a chur leis an bpictiúr.
PS
Earráid shimplí a logáil - cineál paraiméadar ionchuir
-[ RECORD 1 ]- date_trunc | 2020-08-19 13:15:46 id | 1072 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | STARTED jsonb_pretty | { | "dko": { | "id": 4, | "type": "Type1", | "title": "CREATED BY addKD", | "Weight": 10, | "Tr": "300", | "reduction": 10, | "isTrud": "TRUE", | "description": "decription", | "lowerTr": "100", | "measurement": "measurement1", | "methodology": "m1", | "passportUrl": "files", | "upperTr": "200", | "weightingFactor": 100.123, | "actualTrValue": null, | "upperTrCalcNumber": "120" | }, | "CardId": 3 | } -[ RECORD 2 ]- date_trunc | 2020-08-19 13:15:46 id | 1073 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR jsonb_pretty | { | "SQLSTATE": "22P02" | } -[ RECORD 3 ]- date_trunc | 2020-08-19 13:15:46 id | 1074 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR jsonb_pretty | { | "SQLERRM": "invalid input syntax for type numeric: "null"" | } -[ RECORD 4 ]- date_trunc | 2020-08-19 13:15:46 id | 1075 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-RETURNED_SQLSTATE jsonb_pretty | { | "RETURNED_SQLSTATE": "22P02" | } -[ RECORD 5 ]- date_trunc | 2020-08-19 13:15:46 id | 1076 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-COLUMN_NAME jsonb_pretty | { | "COLUMN_NAME": "" | } -[ RECORD 6 ]- date_trunc | 2020-08-19 13:15:46 id | 1077 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-CONSTRAINT_NAME jsonb_pretty | { | "CONSTRAINT_NAME": "" | } -[ RECORD 7 ]- date_trunc | 2020-08-19 13:15:46 id | 1078 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-PG_DATATYPE_NAME jsonb_pretty | { | "PG_DATATYPE_NAME": "" | } -[ RECORD 8 ]- date_trunc | 2020-08-19 13:15:46 id | 1079 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-MESSAGE_TEXT jsonb_pretty | { | "MESSAGE_TEXT": "invalid input syntax for type numeric: "null"" | } -[ RECORD 9 ]- date_trunc | 2020-08-19 13:15:46 id | 1080 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-SCHEMA_NAME jsonb_pretty | { | "SCHEMA_NAME": "" | } -[ RECORD 10 ]- date_trunc | 2020-08-19 13:15:46 id | 1081 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-PG_EXCEPTION_DETAIL jsonb_pretty | { | "PG_EXCEPTION_DETAIL": "" | } -[ RECORD 11 ]- date_trunc | 2020-08-19 13:15:46 id | 1082 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-PG_EXCEPTION_HINT jsonb_pretty | { | "PG_EXCEPTION_HINT": "" | } -[ RECORD 12 ]- date_trunc | 2020-08-19 13:15:46 id | 1083 usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-PG_EXCEPTION_CONTEXT jsonb_pretty | { usename | emp1 log_module | addKD log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a status | ERROR-MESSAGE_TEXT jsonb_pretty | { | "MESSAGE_TEXT": "invalid input syntax for type numeric: "null"" | }
Foinse: will.com