“Support of nanotime by RSQLite” 可以翻译为 “RSQLite 对 nanotime 的支持”。

huangapple go评论63阅读模式
英文:

Support of nanotime by RSQLite

问题

I am looking into what is required to support nanotime objects in RSQLite queries. They are just integer64 wrappers.

Here is an example:

con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:", bigint = "integer64")
ts <- nanotime::as.nanotime(Sys.time())

str(ts) # integer64 2023-06-04 17:30:21.669581000 

DBI::dbExecute(con, 'CREATE TABLE test (ts INTEGER);')
DBI::dbExecute(con, 'INSERT INTO test (ts) VALUES (:ts);', list('ts' = ts))

DBI::dbGetQuery(con, 'SELECT ts FROM test;') # returns 5.757609e-196
# This is not correct: the returned value should have been 1685899821669581000
# of type integer64.

What does it take to make RSQLite understand that nanotime is an integer64 and should be saved as such? Hence I expect the last query to return an integer64 object instead of a double.

I looked into DBI::dbDataType(), but I am not sure how to use it. My current approach is sending the parameters through a function that converts nanotime to integer64 before sending to RSQLite, but it would be nicer if this conversion was seamless (i.e. if it was done within RSQLite package or some RSQLite configuration where one could state that nanotime objects should be mapped to integer64).

Note: I am using RSQLite_2.3.1 and nanotime_0.3.7.

英文:

I am looking into what is required to support nanotime objects in RSQLite queries. They are just integer64 wrappers.

Here is an example:

con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:", bigint = "integer64")
ts <- nanotime::as.nanotime(Sys.time())

str(ts) # integer64 2023-06-04 17:30:21.669581000 

DBI::dbExecute(con, 'CREATE TABLE test (ts INTEGER);')
DBI::dbExecute(con, 'INSERT INTO test (ts) VALUES (:ts);', list('ts' = ts))

DBI::dbGetQuery(con, 'SELECT ts FROM test;') # returns 5.757609e-196
# This is not correct: the returned value should have been 1685899821669581000
# of type integer64.

What does it take to make RSQLite understand that nanotime is an integer64 and should be saved as such? Hence I expect the last query to return an integer64 object instead of a double.

I looked into DBI::dbDataType(), but I am not sure how to use it. My current approach is sending the parameters through a function that converts nanotime to integer64 before sending to RSQLite, but it would be nicer if this conversion was seamless (i.e. if it was done within RSQLite package or some RSQLite configuration where one could state that nanotime objects should be mapped to integer64).

Note: I am using RSQLite_2.3.1 and nanotime_0.3.7.

答案1

得分: 1

以下是翻译好的部分:

一种选择是使用 bigint = "integer64" 并手动进行强制类型转换:

con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:", bigint = "integer64")
ts <- nanotime::as.nanotime(Sys.time())

str(ts)
#> integer64 2023-06-05T04:40:53.451239936+00:00

tsi <- bit64::as.integer64(ts)
out <- DBI::dbGetQuery(con, "SELECT :ts AS x", list("ts" = tsi))
out$x
#> integer64
#> [1] 1685940053451239936
nanotime::as.nanotime(out$x)
#> [1] 2023-06-05T04:40:53.451239936+00:00

创建于 2023-06-05,使用 reprex v2.0.2

英文:

One option is to use bigint = &quot;integer64&quot; and to cast manually:

con &lt;- DBI::dbConnect(RSQLite::SQLite(), &quot;:memory:&quot;, bigint = &quot;integer64&quot;)
ts &lt;- nanotime::as.nanotime(Sys.time())

str(ts)
#&gt; integer64 2023-06-05T04:40:53.451239936+00:00

tsi &lt;- bit64::as.integer64(ts)
out &lt;- DBI::dbGetQuery(con, &quot;SELECT :ts AS x&quot;, list(&quot;ts&quot; = tsi))
out$x
#&gt; integer64
#&gt; [1] 1685940053451239936
nanotime::as.nanotime(out$x)
#&gt; [1] 2023-06-05T04:40:53.451239936+00:00

<sup>Created on 2023-06-05 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月5日 01:42:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定