##-*- R -*-
library(RODBC)
library(MASS)
USArrests[1,2] <- NA
hills <- hills[1:15,]
row.names(hills)[12] <- "Dollar ('$')"
set.seed(1)

channel <- odbcConnect("Oracle", pwd = Sys.getenv("DB_PWD"))
if(!inherits(channel, "RODBC")) q("no")
odbcGetInfo(channel)
sqlTables(channel, schema = "RIPLEY")
sqlDrop(channel, "USArrests", errors = FALSE)
sqlSave(channel, USArrests, rownames = "State", addPK = TRUE)
sqlTables(channel, schema = "RIPLEY")
sqlColumns(channel, "USArrests")
sqlColumns(channel, "USArrests", special = TRUE)
sqlPrimaryKeys(channel, "USArrests")
sqlFetch(channel, "USArrests", rownames = "State")
sqlQuery(channel, 'select "State", "Murder" from "USArrests" where "Rape" > 30 order by "Murder"')
foo <- cbind(State=row.names(USArrests), USArrests)[1:3, c(1,3)]
foo[1,2] <- 236
sqlUpdate(channel, foo, "USArrests", index = "State")
sqlFetch(channel, "USArrests", rownames = "State", max = 5)
sqlFetchMore(channel, rownames = "State", max = 8)
sqlDrop(channel, "USArrests")

Btest <- Atest <-
    data.frame(x = c(paste(1:100, collapse="+"), letters[2:4]), rn=1:4)
Btest[,1] <- Atest[c(4,1:3),1]
sqlDrop(channel, "Atest", errors = FALSE)
## max for varchar is 4000, for char 2000, half that for nchar, nvarchar
colspec <- list(character="varchar(1000)", double="binary_double",
                integer="decimal", logical="varchar(5)")
sqlSave(channel, Atest, typeInfo = colspec)
sqlColumns(channel, "Atest")
sqlFetch(channel, "Atest")
sqlUpdate(channel, Btest, "Atest", index = "rn")
sqlFetch(channel, "Atest")
sqlDrop(channel, "Atest")
varspec <- "varchar(1000)"; names(varspec) <- "x"
sqlSave(channel, Atest, varTypes = varspec)
sqlColumns(channel, "Atest")
sqlFetch(channel, "Atest")
sqlDrop(channel, "Atest")

dates <- as.character(seq(as.Date("2004-01-01"), by="week", length=10))
times <- paste(1:10, "05", "00", sep=":")
dt <- as.POSIXct(paste(dates, times))
Dtest <- data.frame(dates, dt, logi=c(TRUE, NA, FALSE, FALSE, FALSE))
varspec <- c("date", "timestamp", "varchar(5)")
names(varspec) <- names(Dtest)
sqlDrop(channel, "Dtest", errors = FALSE)
sqlSave(channel, Dtest, varTypes = varspec)
sqlColumns(channel, "Dtest")
sqlFetch(channel, "Dtest")
sqlDrop(channel, "Dtest")

sqlQuery(channel, "alter session set NLS_DATE_FORMAT='yyyy-mm-dd hh:mi:ss'")
sqlDrop(channel, "Dtest", errors = FALSE)
sqlSave(channel, Dtest[-1], "Dtest", fast = FALSE,
        varTypes = c(dt = "date", logi = "varchar(5)"))
sqlColumns(channel, "Dtest")
sqlFetch(channel, "Dtest")
sqlDrop(channel, "Dtest")

sqlDrop(channel, "hills test", errors = FALSE)
sqlSave(channel, hills, "hills test", verbose=TRUE)
sqlUpdate(channel, hills[11:15,], "hills test", verbose=TRUE, fast=TRUE)
sqlFetch(channel, "hills test")
sqlDrop(channel, "hills test")
sqlSave(channel, hills, "hills test", verbose=TRUE, fast=FALSE)
sqlUpdate(channel, hills[11:15,], "hills test", verbose=TRUE, fast=FALSE)
sqlDrop(channel, "hills test")

## dropping tables in Oracle does not delete them.
sqlQuery(channel, "PURGE recyclebin")

sqlTables(channel, catalog="", schema="", tableName="", tableType="%")
sqlTables(channel, catalog="", schema="%", tableName="")
sqlSave(channel, women)
sqlFetch(channel, "SYSTEM.TAB")
sqlFetch(channel, "SYSTEM.COL")
sqlColumns(channel, schema="SYSTEM", "TAB")
sqlDrop(channel, "women")

## dropping tables in Oracle does not delete them.
sqlQuery(channel, "PURGE recyclebin")
close(channel)
