R/delete_graph.r

Defines functions delete_graph delete_graph_impl

Documented in delete_graph

delete_graph_impl <- function(tag, con) {
  DBI::dbExecute(con, sql_by_key("DELETE_GRAFI", tag = tag, .con = con))
  DBI::dbExecute(con, sql_by_key("DELETE_CONFLITTI", tag = tag, .con = con))
  orig_tables <- c("archi", "dati", "metadati", "formule")
  tables <- paste(orig_tables, tag, sep = "_")
  for (table in tables) {
    # nocov start
    if (DBI::dbExistsTable(con, table)) {
      DBI::dbExecute(con, sql_by_key("DROP_TABLE", tab = table, .con = con))
    }
    # nocov end
  }
  for (table in orig_tables) {
    # TODO: please fix this shit.
    DBI::dbExecute(con, paste0("delete from ", table, " where tag='", tag, "'"))
  }
}

#' Deletes a Graph from the DB
#'
#' Deletes a graph by its tag
#'
#' @name delete_graph
#' @param tag `tag` as the primary key of the Graph
#' @export
#' @include functions.r
#' @include db.r
#' @include sqlhelper.r

delete_graph <- function(tag) {
  tag <- if (is.grafodb(tag)) {
    tag <- tag@tag
  } else {
    tag <- tolower(tag)
  }

  con <- build_connection()
  on.exit(disconnect(con))

  tryCatch({
    DBI::dbBegin(con)
    delete_graph_impl(tag, con)
    DBI::dbCommit(con)
  }, error = function(err) {
    DBI::dbRollback(con)
    stop(err)
  })
}
giupo/GrafoDB documentation built on Oct. 12, 2022, 9:43 a.m.