create schema rr;

create table rr.resource (
   ivoid varchar(*),
   res_type varchar(*) not null,
   created timestamp not null,
   updated timestamp not null,
   status varchar(*) not null,
   short_name varchar(*) not null,
   res_title varchar(*) not null,
   content_level varchar(*) default '',  -- hash list
   res_description varchar(*) not null,
   reference_url varchar(*) not null,
   content_type varchar(*) default '',  -- hash list
   source_format varchar(*) default null,
   source_value varchar(*) default null,
   version varchar(*) default null,
   region_of_regard real default null,
   waveband varchar(*) default null,        -- hash list
   footprint_ivoid varchar(*) default null,
   footprint_url varchar(*) default null,
   rights varchar(*) default null,     -- hash list
   facility varchar(*) default null,    -- hash list
   instrument varchar(*) default null,  -- hash list
   data_url varchar(*) default null,

   primary key (ivoid)
);

create table rr.res_role (
   ivoid varchar(*) not null,
   role_name varchar(*) not null,
   role_ivoid varchar(*) default null,
   base_utype varchar(*) not null,
   address varchar(*) default null,
   email varchar(*) default null,
   telephone varchar(*) default null,
   logo varchar(*) default null,

   foreign key (ivoid) references rr.resource (ivoid)
);

create table rr.subject (
   ivoid varchar(*) not null,
   subject varchar(*) not null,

   foreign key (ivoid) references rr.resource (ivoid)
);

create table rr.capability (
   ivoid varchar(*) not null,
   cap_index smallint not null,
   cap_name varchar(*) default null,    -- non-standard
   cap_type varchar(*) default null,
   cap_description varchar(*) default null,
   standard_id varchar(*) default null,

   foreign key (ivoid) references rr.resource (ivoid)
);

create table rr.res_schema (
   ivoid varchar(*) not null,
   schema_index smallint not null,
   schema_name varchar(*) default null,
   schema_title varchar(*) default null,
   schema_description varchar(*) default null,
   schema_utype varchar(*) default null,

   foreign key (ivoid) references rr.resource (ivoid)
);

create table rr.res_table (
   ivoid varchar(*) not null,
   schema_index smallint not null,
   table_index smallint not null,
   table_name varchar(*) not null,
   table_title varchar(*) default null,
   table_type varchar(*) not null,
   table_utype varchar(*) default null,
   table_description varchar(*) default null,

   primary key (ivoid,schema_index,table_index),
   foreign key (ivoid) references rr.resource (ivoid)
);

create table rr.table_column (
   ivoid varchar(*) not null,
   schema_index smallint not null,
   table_index smallint not null,
   name varchar(*) not null,
   datatype varchar(*) not null,
   description varchar(*) default null,
   ucd varchar(*) default null,
   unit varchar(*) default null,
   utype varchar(*) default null,
   flag varchar(*) default null,
   std smallint not null,
   extended_schema varchar(*) default null,
   extended_type varchar(*) default null,
   arraysize varchar(*) default null,
   delim varchar(*) default null,
   type_system varchar(*) default null,

   primary key (ivoid,schema_index,table_index),
   foreign key (ivoid) references rr.resource (ivoid)
);

create table rr.interface (
   ivoid varchar(*) not null,
   cap_index smallint not null,
   intf_index smallint not null,
   intf_type varchar(*) default null,
   intf_role varchar(*) default null,
   std_version varchar(*) default null,
   query_type varchar(*) default null,
   result_type varchar(*) default null,
   wsdl_url varchar(*) default null,
   url_use varchar(*) not null,
   access_url varchar(*) not null,

   primary key (ivoid,cap_index,intf_index),
   foreign key (ivoid) references rr.resource (ivoid)
);

create table rr.intf_param (
   ivoid varchar(*) not null,
   cap_index smallint not null,
   intf_index smallint not null,
   name varchar(*) not null,
   datatype varchar(*) not null,
   description varchar(*) default null,
   ucd varchar(*) default null,
   unit varchar(*) default null,
   utype varchar(*) default null,
   flag varchar(*) default null,
   std smallint not null,
   extended_schema varchar(*) default null,
   extended_type varchar(*) default null,
   form varchar(*) default null,

   primary key (ivoid,cap_index,intf_index,name),
   foreign key (ivoid) references rr.resource (ivoid),
);

create table rr.relationship (
   ivoid varchar(*) not null,
   relationship_type varchar(*) not null,
   related_id varchar(*) not null,
   related_name varchar(*) not null,

   foreign key (ivoid) references rr.resource (ivoid)
);

create table rr.validation (
   ivoid varchar(*) not null,
   validated_by varchar(*) not null,
   level smallint not null,
   cap_index smallint default null,

   foreign key (ivoid) references rr.resource (ivoid)
);

create table rr.date (
   ivoid varchar(*) not null,
   date_value date not null,
   value_role varchar(*) default null,

   foreign key (ivoid) references rr.resource (ivoid)
);

create table rr.res_detail (
   ivoid varchar(*) not null,
   cap_index smallint default null,
   detail_utype varchar(*) not null,
   detail_value varchar(*) not null,

   foreign key (ivoid) references rr.resource (ivoid)   
);

