# internal functions ####
#' get_tblName
#'
#' @param dbMatrix A database-backed object inheriting from `dbData`.
#' @return A character scalar giving the DuckDB table name associated with the
#'   object.
#'
#' @keywords internal
get_tblName <- function(dbMatrix) {
  # check if dbData is a dbData object
  if (!inherits(dbMatrix, "dbData")) {
    stopf("dbMatrix must be a dbData object")
  }

  tblName <- dbMatrix@name

  return(tblName)
}

#' get_dbdir
#'
#' @param dbMatrix A database-backed object inheriting from `dbData`.
#' @return A character scalar giving the DuckDB database directory used by the
#'   object.
#'
#' @keywords internal
#' string of the path to the database directory
get_dbdir <- function(dbMatrix) {
  # check if dbData is a dbData object
  if (!inherits(dbMatrix, "dbData")) {
    stopf("dbMatrix must be a dbData object")
  }

  dbdir <- dbMatrix@value[[1]]$con@driver@dbdir

  return(dbdir)
}


#' get_con
#'
#' @param dbMatrix A database-backed object inheriting from `dbData`.
#' @return A live DBI connection associated with the database-backed object.
#'
#' @keywords internal
#'
get_con <- function(dbMatrix) {
  # check if dbData is a dbData object
  if (!inherits(dbMatrix, "dbData")) {
    stopf("dbMatrix must be a dbData object")
  }

  # Use dbProject's connection accessor for consistency and auto-reconnection
  con <- dbProject::conn(dbMatrix)

  # Fallback to direct access if dbProject method doesn't work
  if (is.null(con)) {
    con <- dbMatrix@value[[1]]$con
  }

  return(con)
}
