implemented core of catalog-entry-item schema based on PostgreSQL General Bits
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 23 Jul 2005 15:46:24 +0000 (15:46 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 23 Jul 2005 15:46:24 +0000 (15:46 +0000)
issue 110, Accessing Inherited Table Data

git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@24 07558da8-63fa-0310-ba24-9fe276d99e06

sql/Makefile [new file with mode: 0644]
sql/data.sql [new file with mode: 0644]
sql/mkindex.pl [new file with mode: 0755]
sql/schema.sql [new file with mode: 0644]

diff --git a/sql/Makefile b/sql/Makefile
new file mode 100644 (file)
index 0000000..9a1e2a8
--- /dev/null
@@ -0,0 +1,14 @@
+db=webpac2
+
+init:
+       dropdb $(db) || true
+       createdb $(db)
+       psql $(db) < schema.sql
+       ./mkindex.pl schema.sql | psql $(db)
+       test -f data.sql && psql $(db) < data.sql
+
+save:
+       pg_dump -c $(db) > $(db).sql
+
+load:
+       psql $(db) < $(db).sql
diff --git a/sql/data.sql b/sql/data.sql
new file mode 100644 (file)
index 0000000..a42a6a3
--- /dev/null
@@ -0,0 +1,7 @@
+-- some test data
+
+insert into catalog_webarchive (path,title,uri) values (
+       '/rest/references/PgGeneratBits/bits',
+       'PostgreSQL General Bits',
+       'http://www.varlena.com/varlena/GeneralBits/archive.php'
+);
diff --git a/sql/mkindex.pl b/sql/mkindex.pl
new file mode 100755 (executable)
index 0000000..8872492
--- /dev/null
@@ -0,0 +1,52 @@
+#!/usr/bin/perl -w
+
+# Helper script to produce alter tables for inherited tables and
+# indexes from source shema
+
+use strict;
+#use Data::Dumper;
+
+my $out;
+
+my ($table, $inherit);
+
+while (<>) {
+       chomp;
+
+       if (/create\s+table\s+(\S+)/i) {
+               $table = $1;
+       }
+
+       next unless ($table);
+
+       if (/primary\s+key\s*\(\s*(\S+)\s*\)/i ) {
+               $out->{table_pk}->{$table} = $1;
+       }
+       if (/^\s*(\S+)\s*.+?--\s*((?:unique\s+)*index)/i) {
+               $out->{index}->{$2}->{$table} = $1;
+       }
+
+       if (/\s*inherits\s*\(\s*(\S+)\s*\)/) {
+               $out->{inherits}->{$table} = $1;
+       }
+
+}
+
+#print STDERR Dumper($out);
+
+foreach my $table (keys %{ $out->{inherits} }) {
+       my $parent = $out->{inherits}->{$table} || die;
+       my $pk = $out->{table_pk}->{$parent} || die;
+       my $seq = $parent . '_' . $pk . '_seq';
+       print qq{alter table $table alter column $pk set default nextval('$seq');\n};
+}
+
+foreach my $type (keys %{ $out->{index} }) {
+       foreach my $table (keys %{ $out->{index}->{$type} }) {
+               my $f = $out->{index}->{$type}->{$table} || die;
+               my $i = $table . '_' . $f . '_ind';
+               print qq{create $type $i on $table($f);\n};
+       }
+}
+
+       
diff --git a/sql/schema.sql b/sql/schema.sql
new file mode 100644 (file)
index 0000000..2678879
--- /dev/null
@@ -0,0 +1,70 @@
+-- Catalogs
+create table catalogs (
+       id      serial,
+       title   text not null,
+       path    text,
+       date    timestamp not null default now(),
+       primary key(id)
+);
+
+create table catalog_webarchive (
+       uri     text not null,                  -- unique index
+       last_crawled timestamp
+) inherits (catalogs) ;
+
+-- Entries in Catalog
+create table entries (
+       id      serial,
+       title   text,
+       path    text,
+       date    timestamp not null default now(),
+       primary key(id)
+);
+
+create table catalog_entry (
+       catalog_id int references catalogs(id),
+       entry_id int references entries(id),
+       e_type text not null,                   -- index
+       primary key (catalog_id, entry_id)
+);
+
+-- Pg General Bits entries
+create table entries_pgbits (
+       issue   int not null                    -- unique index
+) inherits (entries) ;
+
+-- Items in Entries
+create table items (
+       id      serial,
+       title   text,
+       entry_id int references entries(id),
+
+       i_type  text not null,
+       date timestamp not null default now(),
+       primary key(id)
+);
+
+-- HyperEstraier support table
+create table items_est (
+       path    text,                   -- unique index
+       uri     text not null,          -- unique index
+       size    int
+) inherits (items) ;
+
+-- Tags for Entries
+create table tags (
+       id      serial,
+       title   text,                   -- index
+       date timestamp not null default now(),
+       primary key(id)
+);
+
+create table entry_tag (
+       entry_id int references entries(id),
+       tag_id int references tags(id),
+       value text not null,
+       t_type text not null,           -- index
+       date timestamp not null default now(),
+       primary key (entry_id, tag_id)
+);
+