File: typeTest.r

package info (click to toggle)
r-cran-rpostgresql 0.6-2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 940 kB
  • sloc: ansic: 3,091; sh: 2,863; makefile: 7
file content (66 lines) | stat: -rwxr-xr-x 1,534 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env r

usePG <- TRUE

if (usePG) {
    cat("Using Pg\n")
    tempdb <- "pgdatetime"
    system(paste("createdb", tempdb))   # create a temp database

    suppressMessages(library(RPostgreSQL))
    drv <- dbDriver("PostgreSQL")
    con <- dbConnect(drv, dbname=tempdb)

} else {
    cat("Using SQLite\n")
    tempdb <- "/tmp/tempdb.sqlite"          # assumed to not exist
    suppressMessages(library(RSQLite))
    drv <- dbDriver("SQLite")
    if (file.exists(tempdb))
        unlink(tempdb)
    con <- dbConnect(drv, tempdb)
}


sql <- "create table foo (i integer, r real, t text)"
res <- dbSendQuery(con, sql)
cat("Created table\n")

i <- as.integer(11)
r <- as.numeric(22.22)
txt <- as.character("blim blom")

sql <- paste("insert into foo ",
             "values (",
             i, ",",
             r, ", '",
             txt, "') ", sep="")
res <- dbSendQuery(con, sql)
cat("Wrote values\n")

df <- dbReadTable(con, "foo")
cat("Read values\n")

## now test the types of the colums we got
stopifnot( class(df[,1]) == "integer" )
stopifnot( class(df[,2]) == "numeric" )
stopifnot( class(df[,3]) == "character" )
cat("GOOD -- all types are as expected\n")

## and test the values
stopifnot( identical( df[1,1], i))
stopifnot( identical( df[1,2], r))
stopifnot( identical( df[1,3], txt))
cat("GOOD -- all values are as expected\n")

if (usePG) {
    dbDisconnect(con)
    dbUnloadDriver(drv)
    system(paste("dropdb", tempdb))
} else {
    dbDisconnect(con)
    dbUnloadDriver(drv)
    unlink(tempdb)
}

cat("DONE\n")