Skip to content

Commit

Permalink
Functional fileMDB
Browse files Browse the repository at this point in the history
  • Loading branch information
patzaw committed Jul 31, 2020
1 parent cc079c5 commit 94a4d09
Show file tree
Hide file tree
Showing 21 changed files with 654 additions and 288 deletions.
11 changes: 11 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
# Generated by roxygen2: do not edit by hand

S3method("collection_members<-",fileMDB)
S3method("db_info<-",fileMDB)
S3method("names<-",fileMDB)
S3method(collection_members,chTKCat)
S3method(collection_members,fileMDB)
S3method(count_records,fileMDB)
S3method(data_model,fileMDB)
S3method(data_tables,fileMDB)
S3method(db_info,fileMDB)
S3method(format,MDB)
S3method(format,chTKCat)
S3method(length,MDB)
S3method(names,MDB)
S3method(print,MDB)
S3method(print,chTKCat)
export("collection_members<-")
export("db_info<-")
export(add_chMDB_user)
export(add_chTKCat_collection)
export(chTKCat)
Expand All @@ -16,6 +25,7 @@ export(ch_list_tables)
export(ch_reconnect)
export(check_chTKCat)
export(collection_members)
export(count_records)
export(create_chMDB)
export(create_chTKCat_user)
export(data_model)
Expand All @@ -40,6 +50,7 @@ export(list_local_collections)
export(mergeTree_from_RelTableModel)
export(mergeTrees_from_RelDataModel)
export(read_collection_members)
export(read_fileMDB)
export(reconnect_chTKCat)
export(remove_chMDB_user)
export(remove_chTKCat_collection)
Expand Down
98 changes: 98 additions & 0 deletions R/MDB.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,101 @@
is.MDB <- function(x){
inherits(x, "MDB")
}


###############################################################################@
#' @export
#'
names.MDB <- function(x){
names(data_model(x))
}


###############################################################################@
#' Get the number of tables in an MDB object
#'
#' @param x an MDB object
#'
#' @return A single integer value
#'
#' @export
#'
length.MDB <- function(x){
length(data_model(x))
}


###############################################################################@
#' @export
#'
format.MDB <- function(x, ...){
cm <- collection_members(x)
maintainer <- db_info(x)$maintainer
return(sprintf(
paste(
"%s %s (version %s%s): %s",
" - %s tables",
"",
"Collections: ",
"%s",
"",
"%s (%s)",
sep="\n"
),
class(x)[1],
db_info(x)$name,
db_info(x)$version,
ifelse(is.na(maintainer) || maintainer=="", "", paste(",", maintainer)),
db_info(x)$title,
length(x),
paste(
unlist(lapply(
unique(cm$collection),
function(y){
return(sprintf(
" - %s %s member%s",
length(unique(cm$table[which(cm$collection==y)])),
y,
ifelse(
length(unique(cm$table[which(cm$collection==y)]))>1,
"s", ""
)
))
}
)),
collapse="\n"
),
db_info(x)$description,
db_info(x)$url
))
}


###############################################################################@
#' @export
#'
print.MDB <- function(x, ...){
cat(format(x, ...), "\n")
}


###############################################################################@
## Helpers ----
.check_dbInfo <- function(dbInfo){
mandFields <- c(
"name", "title", "description", "url",
"version", "maintainer"
)
for(f in mandFields){
if(
!is.character(dbInfo[[f]]) || length(dbInfo[[f]])!=1 ||
is.na(dbInfo[[f]]) || dbInfo[[f]]==""
){
stop(sprintf(
"%s in dbInfo should be a non-empty character"
))
}
}
dbInfo <- as.list(dbInfo[mandFields])
return(dbInfo)
}
19 changes: 10 additions & 9 deletions R/chTKCat.R
Original file line number Diff line number Diff line change
Expand Up @@ -1048,37 +1048,38 @@ remove_chTKCat_collection <- function(x, title){


###############################################################################@
#' Get collection members of a [chTKCat] object
#'
#' @param x a [chTKCat] object
#' @param collections a character vector indicating the name of the collections
#' to focus on (default: NULL ==> all of them)
#' @param ... not used
#' @param ... names of the collections
#' to focus on. By default, all of them are taken.
#'
#' @rdname collection_members
#' @method collection_members chTKCat
#'
#' @export
#'
collection_members.chTKCat <- function(
x,
collections=NULL,
...
){
#### TODO ####
stop("TODO")

toTake <- unlist(list(...))

stopifnot(
is.null(collections) || is.character(collections)
length(toTake)==0 || is.character(toTake)
)
toRet <- DBI::dbGetQuery(
conn=x$chcon,
statement=
sprintf(
"SELECT * FROM default.CollectionMembers %s",
if(is.null(collections)){
if(length(toTake)==0){
""
}else{
sprintf(
"WHERE collection IN ('%s')",
paste(collections, collapse="', '")
paste(toTake, collapse="', '")
)
}
)
Expand Down
Loading

0 comments on commit 94a4d09

Please sign in to comment.