File: dbDataType.Rd

package info (click to toggle)
dbi 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,004 kB
  • sloc: makefile: 2
file content (172 lines) | stat: -rw-r--r-- 5,122 bytes parent folder | download
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/dbDataType.R
\name{dbDataType}
\alias{dbDataType}
\title{Determine the SQL data type of an object}
\usage{
dbDataType(dbObj, obj, ...)
}
\arguments{
\item{dbObj}{A object inheriting from \linkS4class{DBIDriver}
or \linkS4class{DBIConnection}}

\item{obj}{An R object whose SQL type we want to determine.}

\item{...}{Other arguments passed on to methods.}
}
\value{
\code{dbDataType()} returns the SQL type that corresponds to the \code{obj} argument
as a non-empty
character string.
For data frames, a character vector with one element per column
is returned.
}
\description{
Returns an SQL string that describes the SQL data type to be used for an
object.
The default implementation of this generic determines the SQL type of an
R object according to the SQL 92 specification, which may serve as a starting
point for driver implementations. DBI also provides an implementation
for data.frame which will return a character vector giving the type for each
column in the dataframe.

\Sexpr[results=rd,stage=render]{DBI:::methods_as_rd("dbDataType")}
}
\details{
The data types supported by databases are different than the data types in R,
but the mapping between the primitive types is straightforward:
\itemize{
\item Any of the many fixed and varying length character types are mapped to
character vectors
\item Fixed-precision (non-IEEE) numbers are mapped into either numeric or
integer vectors.
}

Notice that many DBMS do not follow IEEE arithmetic, so there are potential
problems with under/overflows and loss of precision.
}
\section{Failure modes}{


An error is raised for invalid values for the \code{obj} argument such as a
\code{NULL} value.

}

\section{Specification}{


The backend can override the \code{\link[=dbDataType]{dbDataType()}} generic
for its driver class.

This generic expects an arbitrary object as second argument.
To query the values returned by the default implementation,
run \code{example(dbDataType, package = "DBI")}.
If the backend needs to override this generic,
it must accept all basic R data types as its second argument, namely
\link{logical},
\link{integer},
\link{numeric},
\link{character},
dates (see \link{Dates}),
date-time (see \link{DateTimeClasses}),
and \link{difftime}.
If the database supports blobs,
this method also must accept lists of \link{raw} vectors,
and \link[blob:blob]{blob::blob} objects.
As-is objects (i.e., wrapped by \code{\link[=I]{I()}}) must be
supported and return the same results as their unwrapped counterparts.
The SQL data type for \link{factor} and
\link{ordered} is the same as for character.
The behavior for other object types is not specified.




All data types returned by \code{dbDataType()} are usable in an SQL statement
of the form
\code{"CREATE TABLE test (a ...)"}.

}

\examples{
dbDataType(ANSI(), 1:5)
dbDataType(ANSI(), 1)
dbDataType(ANSI(), TRUE)
dbDataType(ANSI(), Sys.Date())
dbDataType(ANSI(), Sys.time())
dbDataType(ANSI(), Sys.time() - as.POSIXct(Sys.Date()))
dbDataType(ANSI(), c("x", "abc"))
dbDataType(ANSI(), list(raw(10), raw(20)))
dbDataType(ANSI(), I(3))

dbDataType(ANSI(), iris)
\dontshow{if (requireNamespace("RSQLite", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf}

con <- dbConnect(RSQLite::SQLite(), ":memory:")

dbDataType(con, 1:5)
dbDataType(con, 1)
dbDataType(con, TRUE)
dbDataType(con, Sys.Date())
dbDataType(con, Sys.time())
dbDataType(con, Sys.time() - as.POSIXct(Sys.Date()))
dbDataType(con, c("x", "abc"))
dbDataType(con, list(raw(10), raw(20)))
dbDataType(con, I(3))

dbDataType(con, iris)

dbDisconnect(con)
\dontshow{\}) # examplesIf}
}
\seealso{
Other DBIDriver generics: 
\code{\link{DBIDriver-class}},
\code{\link{dbCanConnect}()},
\code{\link{dbConnect}()},
\code{\link{dbDriver}()},
\code{\link{dbGetInfo}()},
\code{\link{dbIsReadOnly}()},
\code{\link{dbIsValid}()},
\code{\link{dbListConnections}()}

Other DBIConnection generics: 
\code{\link{DBIConnection-class}},
\code{\link{dbAppendTable}()},
\code{\link{dbAppendTableArrow}()},
\code{\link{dbCreateTable}()},
\code{\link{dbCreateTableArrow}()},
\code{\link{dbDisconnect}()},
\code{\link{dbExecute}()},
\code{\link{dbExistsTable}()},
\code{\link{dbGetException}()},
\code{\link{dbGetInfo}()},
\code{\link{dbGetQuery}()},
\code{\link{dbGetQueryArrow}()},
\code{\link{dbIsReadOnly}()},
\code{\link{dbIsValid}()},
\code{\link{dbListFields}()},
\code{\link{dbListObjects}()},
\code{\link{dbListResults}()},
\code{\link{dbListTables}()},
\code{\link{dbQuoteIdentifier}()},
\code{\link{dbReadTable}()},
\code{\link{dbReadTableArrow}()},
\code{\link{dbRemoveTable}()},
\code{\link{dbSendQuery}()},
\code{\link{dbSendQueryArrow}()},
\code{\link{dbSendStatement}()},
\code{\link{dbUnquoteIdentifier}()},
\code{\link{dbWriteTable}()},
\code{\link{dbWriteTableArrow}()}

Other DBIConnector generics: 
\code{\link{DBIConnector-class}},
\code{\link{dbConnect}()},
\code{\link{dbGetConnectArgs}()},
\code{\link{dbIsReadOnly}()}
}
\concept{DBIConnection generics}
\concept{DBIConnector generics}
\concept{DBIDriver generics}