rs <- relation_schema(
list(
a = list(c("a", "b"), list("a")),
b = list(c("b", "c"), list("b", "c"))
),
attrs_order = c("a", "b", "c", "d")
)
ds <- database_schema(
rs,
list(list("a", "b", "b", "b"))
)
print(ds)
attrs(ds)
keys(ds)
attrs_order(ds)
names(ds)
references(ds)
# relations can't reference themselves
if (FALSE) {
database_schema(
relation_schema(
list(a = list("a", list("a"))),
c("a", "b")
),
list(list("a", "a", "a", "a"))
)
database_schema(
relation_schema(
list(a = list(c("a", "b"), list("a"))),
c("a", "b")
),
list(list("a", "b", "a", "a"))
)
}
# an example with references between differently-named attributes
print(database_schema(
relation_schema(
list(
citation = list(c("citer", "citee"), list(c("citer", "citee"))),
article = list("article", list("article"))
),
c("citer", "citee", "article")
),
list(
list("citation", "citer", "article", "article"),
list("citation", "citee", "article", "article")
)
))
# vector operations
ds2 <- database_schema(
relation_schema(
list(
e = list(c("a", "e"), list("e"))
),
attrs_order = c("a", "e")
),
list()
)
c(ds, ds2) # attrs_order attributes are merged
unique(c(ds, ds))
# subsetting
ds[1]
stopifnot(identical(ds[[1]], ds[1]))
ds[c(1, 2, 1, 2)] # replicates the foreign key references
c(ds[c(1, 2)], ds[c(1, 2)]) # doesn't reference between separate copies of ds
unique(ds[c(1, 2, 1, 2)]) # unique() also merges references
# another example of unique() merging references
ds_merge <- database_schema(
relation_schema(
list(
a = list(c("a", "b"), list("a")),
b = list(c("b", "c", "d"), list("b")),
c_d = list(c("c", "d", "e"), list(c("c", "d"))),
a.1 = list(c("a", "b"), list("a")),
b.1 = list(c("b", "c", "d"), list("b"))
),
c("a", "b", "c", "d", "e")
),
list(
list("a", "b", "b", "b"),
list("b.1", c("c", "d"), "c_d", c("c", "d"))
)
)
print(ds_merge)
unique(ds_merge)
# reassignment
# can't change keys included in references
if (FALSE) keys(ds)[[2]] <- list("c")
# can't remove attributes included in keys
if (FALSE) attrs(ds)[[2]] <- list("c", "d")
# can't remove attributes included in references
if (FALSE) attrs(ds)[[1]] <- c("a", "d")
ds3 <- ds
# can change subset of schema, but loses references between altered and
# non-altered subsets
ds3[2] <- database_schema(
relation_schema(
list(d = list(c("d", "c"), list("d"))),
attrs_order(ds3)
),
list()
)
print(ds3) # note the schema's name doesn't change
# names(ds3)[2] <- "d" # this would change the name
keys(ds3)[[2]] <- list(character()) # removing keys first...
attrs(ds3)[[2]] <- c("b", "c") # so we can change the attrs legally
keys(ds3)[[2]] <- list("b", "c") # add the new keys
# add the reference lost during subset replacement
references(ds3) <- c(references(ds3), list(list("a", "b", "b", "b")))
stopifnot(identical(ds3, ds))
# changing appearance priority for attributes
attrs_order(ds3) <- c("d", "c", "b", "a")
print(ds3)
# changing relation schema names changes them in references
names(ds3) <- paste0(names(ds3), "_long")
print(ds3)
# reconstructing from components
ds_recon <- database_schema(
relation_schema(
Map(list, attrs(ds), keys(ds)),
attrs_order(ds)
),
references(ds)
)
stopifnot(identical(ds_recon, ds))
ds_recon2 <- database_schema(
subschemas(ds),
references(ds)
)
stopifnot(identical(ds_recon2, ds))
# can be a data frame column
data.frame(id = 1:2, schema = ds)
Run the code above in your browser using DataLab