r-dbi / odbc

Connect to ODBC databases (using the DBI interface)
https://odbc.r-dbi.org/
Other
392 stars 107 forks source link

Provide helper for SQLite #659

Open hadley opened 11 months ago

hadley commented 11 months ago

Supported arguments:

  Database (string)     name of SQLite2/3 database file; default empty
  PWD (string)          password when built with SEE support; default empty
  Timeout (integer)     lock time out in milliseconds; default 100000
  StepAPI (boolean)     if true, use sqlite[3]_step et.al.; default false
  NoTXN (boolean)       if true, only pretend transactions; default false
  NoWCHAR (boolean)     if true, don't support WCHAR types for character
                        data; default false
  NoCreat (boolean)     if true and database file doesn't exist, don't
                        create it automatically; default false, unsupported
                        for SQLite2
  LongNames (boolean)   if true, don't shorten column names; default false
  ShortNames (boolean)  if true, enforce short column names; default false
  SyncPragma (string)   value for PRAGMA SYNCHRONOUS; default empty
  FKSupport (boolean)   if true, support SQLite3 foreign key constraints;
                        default false
  JournalMode (string)  value for PRAGMA JOURNAL_MODE; default empty
  OEMCP (boolean)       Win32 only: if true, translate strings from/to
                        UTF8 to current code page; default false
  BigInt (boolean)      if true, force integer columns to SQL_BIGINT;
                        default false
  JDConv (boolean)      if true, use SQLite3 julian day representation for
                        SQL_TIME, SQL_TIMESTAMP, SQL_DATE types; default false
  TraceFile (string)    name of file to write SQLite traces to; default empty
  AttachAs (string)     SQLite3 only: comma separated names to be the attach
                        names to the current database file; default empty
  ILike (boolean)       SQLite3 only: if true, treat ILIKE as LIKE in SQL
                        to mimic PostgreSQL syntax; default false 
hadley commented 9 months ago
sqlite_driver_default <- function() {
  # Assume that if SQLite driver is setup up it's correct
  drivers <- unique(odbcListDrivers()$name)
  if ("SQLite" %in% drivers) {
    return("SQLite")
  }

  if (Sys.info()["sysname"] == "Darwin") {
    if (Sys.which("brew") == "") {
      abort("Homebrew is not installed")
    }

    paths <- system("brew list sqliteodbc", intern = TRUE)
    if (attr(paths, "status") != 0) {
      abort("Failed to list sqliteodbc brew file")
    }
    paths[basename(paths) == "libsqlite3odbc.dylib"]
  } else {
    abort("Autodiscovery currently only supported on macOS")
  }
}