| 1 | = Import rejestrów = |
| 2 | |
| 3 | 1. tworzymy ręcznie tabelę (rejestr) w bazie w schemacie ''cregisters'' z dziedziczeniem po ''cregisters.register_entry''. '''Nazwa tabeli musi posiadać przedrostek "creg_"''' [[BR]] |
| 4 | przykład: |
| 5 | {{{ |
| 6 | CREATE TABLE cregisters.creg_przekaz ( |
| 7 | id INT NOT NULL , |
| 8 | nazwisko VARCHAR(50) NULL , |
| 9 | imie VARCHAR(50) NULL , |
| 10 | ulica VARCHAR(50) NULL , |
| 11 | nr_domu VARCHAR(50) NULL , |
| 12 | nr_mieszkania VARCHAR(50) NULL , |
| 13 | uwagi TEXT |
| 14 | ) |
| 15 | INHERITS (cregisters.register_entry) |
| 16 | WITH (OIDS=FALSE); |
| 17 | }}} |
| 18 | Dane (rekordy) możemy załadować w dowolnym momencie. |
| 19 | |
| 20 | 2. |
| 21 | |
| 22 | {{{ |
| 23 | |
| 24 | create or replace function my_exec1(text) returns void as $body$ |
| 25 | begin |
| 26 | execute $1; |
| 27 | end; |
| 28 | $body$ language plpgsql; |
| 29 | |
| 30 | |
| 31 | CREATE OR REPLACE FUNCTION creg_get_field_type(text) RETURNS text AS $$ |
| 32 | SELECT CASE $1 |
| 33 | WHEN 'bool' THEN 'bool' |
| 34 | WHEN 'timestamp' THEN 'datetime' |
| 35 | WHEN 'numeric' THEN 'text' |
| 36 | WHEN 'int2' THEN 'integer' |
| 37 | WHEN 'int4' THEN 'integer' |
| 38 | WHEN 'int8' THEN 'integer' |
| 39 | WHEN 'float' THEN 'float' |
| 40 | WHEN 'float8' THEN 'float' |
| 41 | WHEN 'varchar' THEN 'string' |
| 42 | WHEN 'timestamp' THEN 'datetime' |
| 43 | WHEN 'timestamptz' THEN 'datetime' |
| 44 | ELSE $1 |
| 45 | END; |
| 46 | $$ LANGUAGE 'sql' STRICT IMMUTABLE; |
| 47 | |
| 48 | |
| 49 | SELECT my_exec1('INSERT INTO cregisters.register (name__,tabnam,label_) VALUES ('''||replace(tablename,'creg_','')||''','''||tablename||''','''||replace(tablename,'creg_','')||''');') |
| 50 | FROM pg_tables |
| 51 | WHERE schemaname = 'cregisters' AND NOT EXISTS (SELECT 1 FROM cregisters.register WHERE tabnam = tablename) AND tablename ~ '^creg_'; |
| 52 | |
| 53 | |
| 54 | SELECT my_exec1('INSERT INTO cregisters.register_fields (cregid,name__,label1,label2,type__) VALUES ('||cregid||','''||name__||''','''||name__||''','''||name__||''','''||type__||''');') |
| 55 | FROM ( |
| 56 | SELECT reg.id____ as cregid, lower(a.attname::text) as name__, creg_get_field_type(typ.typname) as type__ |
| 57 | FROM pg_attribute a |
| 58 | LEFT JOIN pg_type typ ON typ.oid = a.atttypid |
| 59 | LEFT JOIN pg_index p ON p.indrelid = a.attrelid AND a.attnum = ANY(p.indkey) |
| 60 | LEFT JOIN pg_description d ON d.objoid = a.attrelid AND d.objsubid = a.attnum |
| 61 | LEFT JOIN pg_attrdef f ON f.adrelid = a.attrelid AND f.adnum = a.attnum |
| 62 | LEFT JOIN pg_inherits inh ON (inh.inhrelid = a.attrelid) |
| 63 | LEFT JOIN cregisters.register reg ON (('cregisters.' || reg.tabnam) = (inh.inhrelid::regclass)::text) |
| 64 | WHERE reg.id____ IS NOT NULL AND a.attnum > 0 AND NOT a.attisdropped AND inh.inhparent = 'cregisters.register_entry'::regclass AND attislocal IS TRUE |
| 65 | AND NOT EXISTS (SELECT 1 FROM cregisters.register_fields rf WHERE rf.cregid = reg.id____) |
| 66 | ORDER BY reg.id____, a.attnum |
| 67 | ) bb |
| 68 | ; |
| 69 | |
| 70 | SELECT my_exec1('GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE ' || (inhrelid::regclass)::text || ' TO http;') |
| 71 | FROM pg_inherits |
| 72 | WHERE inhparent = 'cregisters.register_entry'::regclass; |
| 73 | |
| 74 | |
| 75 | drop function my_exec1(text); |
| 76 | drop function creg_get_field_type(text); |
| 77 | |
| 78 | }}} |