1
2R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
3Copyright (C) 2016 The R Foundation for Statistical Computing
4Platform: x86_64-pc-linux-gnu (64-bit)
5
6R is free software and comes with ABSOLUTELY NO WARRANTY.
7You are welcome to redistribute it under certain conditions.
8Type 'license()' or 'licence()' for distribution details.
9
10R is a collaborative project with many contributors.
11Type 'contributors()' for more information and
12'citation()' on how to cite R or R packages in publications.
13
14Type 'demo()' for some demos, 'help()' for on-line help, or
15'help.start()' for an HTML browser interface to help.
16Type 'q()' to quit R.
17
18> ## Test of data types, based on earlier version in inst/devTests
19> ##
20> ## Dirk Eddelbuettel, 21 Oct 2008
21>
22> if ((Sys.getenv("POSTGRES_USER") != "") &
23+     (Sys.getenv("POSTGRES_HOST") != "") &
24+     (Sys.getenv("POSTGRES_DATABASE") != "")) {
25+
26+     ## try to load our module and abort if this fails
27+     stopifnot(require(RPostgreSQL))
28+
29+     ## load the PostgresSQL driver
30+     drv <- dbDriver("PostgreSQL")
31+     ## can't print result as it contains process id which changes  print(summary(drv))
32+
33+     ## connect to the default db
34+     con <- dbConnect(drv,
35+                      user=Sys.getenv("POSTGRES_USER"),
36+                      password=Sys.getenv("POSTGRES_PASSWD"),
37+                      host=Sys.getenv("POSTGRES_HOST"),
38+                      dbname=Sys.getenv("POSTGRES_DATABASE"),
39+                      port=ifelse((p<-Sys.getenv("POSTGRES_PORT"))!="", p, 5432))
40+
41+     if (dbExistsTable(con, "tempostgrestable"))
42+         dbRemoveTable(con, "tempostgrestable")
43+
44+     ## Test the numeric mapping
45+     dbGetQuery(con, "create table tempostgrestable (intcolumn date, floatcolumn timestamp with time zone);")
46+
47+     sql <- paste("insert into tempostgrestable ",
48+                  "values ('2011-03-07', '2011-03-07 16:30:39') ")
49+     res <- dbGetQuery(con, sql)
50+
51+     dat <- dbReadTable(con, "tempostgrestable")
52+     dbRemoveTable(con, "tempostgrestable")
53+     cat("Read Date and TIMESTAMP values\n")
54+
55+     ## now test the types of the colums we got
56+     if( class(dat[1,1]) == "Date" ){
57+         cat("PASS -- Date type is as expected\n")
58+     }else{
59+         cat("FAIL -- Date type is other than Date: ")
60+         cat(class(dat[1,1]))
61+         cat("\n")
62+     }
63+     if( class(dat[1,2])[1] == "POSIXct" ){
64+         cat("PASS -- TIMESTAMP is received as POSIXct\n")
65+     }else{
66+         cat("FAIL -- TIMESTAMP is other than POSIXct: ")
67+         cat(class(dat[1,2]))
68+         cat("\n")
69+     }
70+
71+     dbWriteTable(con, "tempostgrestable2", dat)
72+     dat2 <- dbReadTable(con, "tempostgrestable2")
73+     dbRemoveTable(con, "tempostgrestable2")
74+     cat("Check that read after write gets the same data types\n")
75+
76+     ## now test the types of the colums we got
77+     if( class(dat2[1,1]) == "Date" ){
78+         cat("PASS -- Date type is as expected\n")
79+     }else{
80+         cat("FAIL -- Date type is other than Date: ")
81+         cat(class(dat2[1,1]))
82+         cat("\n")
83+     }
84+     if( class(dat2[1,2])[1] == "POSIXct" ){
85+         cat("PASS -- TIMESTAMP is received as POSIXct\n")
86+     }else{
87+         cat("FAIL -- TIMESTAMP is other than POSIXct: ")
88+         cat(class(dat2[1,2]))
89+         cat("\n")
90+     }
91+
92+
93+     dbDisconnect(con)
94+     dbUnloadDriver(drv)
95+
96+     cat("DONE\n")
97+ }
98Loading required package: RPostgreSQL
99Loading required package: DBI
100Read Date and TIMESTAMP values
101PASS -- Date type is as expected
102PASS -- TIMESTAMP is received as POSIXct
103Check that read after write gets the same data types
104PASS -- Date type is as expected
105PASS -- TIMESTAMP is received as POSIXct
106DONE
107>
108> proc.time()
109   user  system elapsed
110  0.513   0.031   0.618
111