use DB;

GRANT SELECT ON DB.DBA.partsupp  TO "SPARQL";
GRANT SELECT ON DB.DBA.supplier  TO "SPARQL";
GRANT SELECT ON DB.DBA.customer  TO "SPARQL";
GRANT SELECT ON DB.DBA.part      TO "SPARQL";
GRANT SELECT ON DB.DBA.lineitem  TO "SPARQL";
GRANT SELECT ON DB.DBA.orders    TO "SPARQL";
GRANT SELECT ON DB.DBA.nation    TO "SPARQL";
GRANT SELECT ON DB.DBA.region    TO "SPARQL";

sparql drop quad map virtrdf:RDFH11
;

sparql
prefix tpcd: <http://lod2.eu/schemas/rdfh#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
create iri class tpcd:customer "http://lod2.eu/schemas/rdfh-inst#customer_%d" (in c_custkey integer not null) option (bijection, deref) .
;

sparql
prefix tpcd: <http://lod2.eu/schemas/rdfh#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
create iri class tpcd:lineitem "http://lod2.eu/schemas/rdfh-inst#lineitem_%d_%d" (in l_orderkey integer not null, in l_linenumber integer not null) option (bijection, deref) .
;

sparql
prefix tpcd: <http://lod2.eu/schemas/rdfh#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
create iri class tpcd:nation "http://lod2.eu/schemas/rdfh-inst#nation_%d" (in l_nationkey integer not null) option (bijection, deref) .
;

sparql
prefix tpcd: <http://lod2.eu/schemas/rdfh#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
create iri class tpcd:order "http://lod2.eu/schemas/rdfh-inst#order_%d" (in o_orderkey integer not null) option (bijection, deref) .
;

sparql
prefix tpcd: <http://lod2.eu/schemas/rdfh#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
create iri class tpcd:part "http://lod2.eu/schemas/rdfh-inst#part_%d" (in p_partkey integer not null) option (bijection, deref) .
;

sparql
prefix tpcd: <http://lod2.eu/schemas/rdfh#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
create iri class tpcd:partsupp "http://lod2.eu/schemas/rdfh-inst#partsupp_%d_%d" (in ps_partkey integer not null, in ps_suppkey integer not null) option (bijection, deref) .
;

sparql
prefix tpcd: <http://lod2.eu/schemas/rdfh#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
create iri class tpcd:region "http://lod2.eu/schemas/rdfh-inst#region_%d" (in r_regionkey integer not null) option (bijection, deref) .
;

sparql
prefix tpcd: <http://lod2.eu/schemas/rdfh#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
create iri class tpcd:supplier "http://lod2.eu/schemas/rdfh-inst#supplier_%d" (in s_supplierkey integer not null) option (bijection, deref) .
;

sparql
prefix tpcd: <http://lod2.eu/schemas/rdfh#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
alter quad storage virtrdf:DefaultQuadStorage
from DB.DBA.LINEITEM as lineitems
from DB.DBA.CUSTOMER as customers
from DB.DBA.NATION as nations
from DB.DBA.ORDERS as orders
from DB.DBA.PART as parts
from DB.DBA.PARTSUPP as partsupps
from DB.DBA.REGION as regions
from DB.DBA.SUPPLIER as suppliers
where (^{suppliers.}^.S_NATIONKEY = ^{nations.}^.N_NATIONKEY)
where (^{customers.}^.C_NATIONKEY = ^{nations.}^.N_NATIONKEY)
where (^{orders.}^.O_CUSTKEY = ^{customers.}^.C_CUSTKEY)
where (^{lineitems.}^.L_ORDERKEY = ^{orders.}^.O_ORDERKEY)
{
  create virtrdf:RDFH11 as
    {
      create virtrdf:RDFH11-cust-1 as graph tpcd:customer (customers.C_CUSTKEY)
        {
          tpcd:customer (customers.C_CUSTKEY)
              a tpcd:customer
                  as virtrdf:RDFH11-customer-tpcd-type ;
              a foaf:Organization
                  as virtrdf:RDFH11-customer-foaf-type ;
              tpcd:c_custkey customers.C_CUSTKEY
                  as virtrdf:RDFH11-customer-c_custkey ;
              tpcd:c_name customers.C_NAME
                  as virtrdf:RDFH11-customer-c_name ;
              tpcd:c_has_nation tpcd:nation (customers.C_NATIONKEY)
                  as virtrdf:RDFH11-customer-c_nationkey ;
              tpcd:c_address customers.C_ADDRESS
                  as virtrdf:RDFH11-customer-c_address ;
              foaf:phone customers.C_PHONE
                  as virtrdf:RDFH11-customer-foaf_phone ;
              tpcd:c_phone customers.C_PHONE
                  as virtrdf:RDFH11-customer-phone ;
              tpcd:c_acctbal customers.C_ACCTBAL
                  as virtrdf:RDFH11-customer-acctbal ;
              tpcd:c_mktsegment customers.C_MKTSEGMENT
                  as virtrdf:RDFH11-customer-c_mktsegment ;
              tpcd:c_comment customers.C_COMMENT
                  as virtrdf:RDFH11-customer-c_comment .
        } .
      create virtrdf:RDFH11-cust-2 as graph tpcd:customer (orders.O_CUSTKEY)
        {
          tpcd:lineitem (orders.O_ORDERKEY, lineitems.L_LINENUMBER)
              a tpcd:lineitem
                  as virtrdf:RDFH11-lineitem-lineitems ;
              tpcd:l_has_order tpcd:order (lineitems.L_ORDERKEY)
                  as virtrdf:RDFH11-lineitem-l_orderkey ;
              tpcd:l_has_part tpcd:part (lineitems.L_PARTKEY)
                  as virtrdf:RDFH11-lineitem-l_partkey ;
              tpcd:l_has_supplier tpcd:supplier (lineitems.L_SUPPKEY)
                  as virtrdf:RDFH11-lineitem-l_suppkey ;
              tpcd:l_number lineitems.L_LINENUMBER
                  as virtrdf:RDFH11-lineitem-l_number ;
              tpcd:l_quantity lineitems.L_QUANTITY
                  as virtrdf:RDFH11-lineitem-l_quantity ;
              tpcd:l_extendedprice lineitems.L_EXTENDEDPRICE
                  as virtrdf:RDFH11-lineitem-l_lineextendedprice ;
              tpcd:l_discount lineitems.L_DISCOUNT
                  as virtrdf:RDFH11-lineitem-l_discount ;
              tpcd:l_tax lineitems.L_TAX
                  as virtrdf:RDFH11-lineitem-l_tax ;
              tpcd:l_returnflag lineitems.L_RETURNFLAG
                  as virtrdf:RDFH11-lineitem-l_returnflag ;
              tpcd:l_linestatus lineitems.L_LINESTATUS
                  as virtrdf:RDFH11-lineitem-l_linestatus ;
              tpcd:l_shipdate lineitems.L_SHIPDATE
                  as virtrdf:RDFH11-lineitem-l_shipdate ;
              tpcd:l_commitdate lineitems.L_COMMITDATE
                  as virtrdf:RDFH11-lineitem-l_commitdate ;
              tpcd:l_receiptdate lineitems.L_RECEIPTDATE
                  as virtrdf:RDFH11-lineitem-l_receiptdate ;
              tpcd:l_shipinstruct lineitems.L_SHIPINSTRUCT
                  as virtrdf:RDFH11-lineitem-l_shipinstruct ;
              tpcd:l_shipmode lineitems.L_SHIPMODE
                  as virtrdf:RDFH11-lineitem-l_shipmode .
          tpcd:order (orders.O_ORDERKEY)
              a tpcd:order
                  as virtrdf:RDFH11-order-orders ;
              tpcd:o_orderkey orders.O_ORDERKEY
                  as virtrdf:RDFH11-order-o_orderkey ;
              tpcd:o_orderstatus orders.O_ORDERSTATUS
                  as virtrdf:RDFH11-order-o_orderstatus ;
              tpcd:o_totalprice orders.O_TOTALPRICE
                  as virtrdf:RDFH11-order-o_totalprice ;
              tpcd:o_orderdate orders.O_ORDERDATE
                  as virtrdf:RDFH11-order-o_orderdate ;
              tpcd:o_orderpriority orders.O_ORDERPRIORITY
                  as virtrdf:RDFH11-order-o_orderpriority ;
              tpcd:o_clerk orders.O_CLERK
                  as virtrdf:RDFH11-order-o_clerk ;
              tpcd:o_shippriority orders.O_SHIPPRIORITY
                  as virtrdf:RDFH11-order-o_shippriority ;
              tpcd:o_comment orders.O_COMMENT
                  as virtrdf:RDFH11-order-o_comment .
          tpcd:customer (orders.O_CUSTKEY)
              tpcd:c_customer_of tpcd:order (orders.O_ORDERKEY) as virtrdf:RDFH11-order-customer_of .
        } .
      create virtrdf:RDFH11-common as graph iri ("http://example.com/tpcd-g") option (exclusive)
        {
          tpcd:nation (customers.C_NATIONKEY)
              tpcd:n_nation_of tpcd:customer (customers.C_CUSTKEY) as virtrdf:RDFH11-customer-nation_of .
          tpcd:nation (nations.N_NATIONKEY)
              a tpcd:nation
                  as virtrdf:RDFH11-nation-nations ;
              tpcd:n_name nations.N_NAME
                  as virtrdf:RDFH11-nation-n_name ;
              tpcd:n_has_region tpcd:region (nations.N_REGIONKEY)
                  as virtrdf:RDFH11-nation-n_regionkey ;
              tpcd:n_comment nations.N_COMMENT
                  as virtrdf:RDFH11-nation-n_comment .
          tpcd:region (nations.N_REGIONKEY)
              tpcd:r_region_of tpcd:nation (nations.N_NATIONKEY) as virtrdf:RDFH11-nation-region_of .
          tpcd:part (parts.P_PARTKEY)
              a tpcd:part
                  as virtrdf:RDFH11-part-parts ;
              tpcd:p_partkey parts.P_PARTKEY
                  as virtrdf:RDFH11-part-p_partkey ;
              tpcd:p_name parts.P_NAME
                  as virtrdf:RDFH11-part-p_name ;
              tpcd:p_mfgr parts.P_MFGR
                  as virtrdf:RDFH11-part-p_mfgr ;
              tpcd:p_brand parts.P_BRAND
                  as virtrdf:RDFH11-part-p_brand ;
              tpcd:p_type parts.P_TYPE
                  as virtrdf:RDFH11-part-p_type ;
              tpcd:p_size parts.P_SIZE
                  as virtrdf:RDFH11-part-p_size ;
              tpcd:p_container parts.P_CONTAINER
                  as virtrdf:RDFH11-part-p_container ;
              tpcd:p_comment parts.P_COMMENT
                  as virtrdf:RDFH11-part-p_comment .
          tpcd:partsupp (partsupps.PS_PARTKEY, partsupps.PS_SUPPKEY)
              a tpcd:partsupp
                  as virtrdf:RDFH11-partsupp-partsupps ;
              tpcd:ps_has_part tpcd:part (partsupps.PS_PARTKEY)
                  as virtrdf:RDFH11-partsupp-ps_partkey ;
              tpcd:ps_has_supplier tpcd:supplier (partsupps.PS_SUPPKEY)
                  as virtrdf:RDFH11-partsupp-ps_suppkey ;
              tpcd:ps_availqty partsupps.PS_AVAILQTY
                  as virtrdf:RDFH11-partsupp-ps_availqty ;
              tpcd:ps_supplycost partsupps.PS_SUPPLYCOST
                  as virtrdf:RDFH11-partsupp-ps_supplycost .
          tpcd:region (regions.R_REGIONKEY)
              a tpcd:region
                  as virtrdf:RDFH11-region-regions ;
              tpcd:r_name regions.R_NAME
                  as virtrdf:RDFH11-region-r_name ;
              tpcd:r_comment regions.R_COMMENT
                  as virtrdf:RDFH11-region-r_comment .
          tpcd:supplier (suppliers.S_SUPPKEY)
              a tpcd:supplier
                  as virtrdf:RDFH11-supplier-suppliers ;
              tpcd:s_suppkey suppliers.S_SUPPKEY
                  as virtrdf:RDFH11-supplier-s_suppkey ;
              tpcd:s_name suppliers.S_NAME
                  as virtrdf:RDFH11-supplier-s_name ;
              tpcd:s_address suppliers.S_ADDRESS
                  as virtrdf:RDFH11-supplier-s_address ;
              tpcd:s_has_nation tpcd:nation (suppliers.S_NATIONKEY)
                  as virtrdf:RDFH11-supplier-s_nationkey ;
              foaf:phone suppliers.S_PHONE
                  as virtrdf:RDFH11-supplier-foaf_phone ;
              tpcd:s_phone suppliers.S_PHONE
                  as virtrdf:RDFH11-supplier-s_phone ;
              tpcd:s_acctbal suppliers.S_ACCTBAL
                  as virtrdf:RDFH11-supplier-s_acctbal ;
              tpcd:s_comment suppliers.S_COMMENT
                  as virtrdf:RDFH11-supplier-s_comment .
        }
    }
}
;

