-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break out a new pgx_value_core module
This will let people using the Lwt or Unix backends still have access to the Core_kernel.{Date,Time} converters. See #73
- Loading branch information
1 parent
66596db
commit e87e43e
Showing
12 changed files
with
122 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
(tests | ||
(names test_pgx_async test_pgx_async_conversions) | ||
(names test_pgx_async) | ||
(libraries alcotest alcotest-async pgx_async pgx_test)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Pgx_value converters for Core types like Date and Time |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
opam-version: "2.0" | ||
authors: ["Arena Developers <silver-snakes@arena.io>"] | ||
maintainer: "Arena Developers <silver-snakes@arena.io>" | ||
homepage: "https://github.com/arenadotio/pgx" | ||
dev-repo: "git+https://github.com/arenadotio/pgx.git" | ||
bug-reports: "https://github.com/arenadotio/pgx/issues" | ||
doc: "https://arenadotio.github.io/pgx/doc" | ||
|
||
build: [ | ||
["dune" "build" "-p" name "-j" jobs] | ||
["dune" "runtest" "-p" name "-j" jobs] {with-test} | ||
] | ||
|
||
depends: [ | ||
"core_kernel" {>= "v0.13.0"} | ||
"pgx" {= version} | ||
|
||
"alcotest" {with-test & >= "1.0.0"} | ||
"base64" {with-test & >= "3.0.0"} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
(* -*- tuareg -*- *) | ||
|
||
let preprocess = | ||
match Sys.getenv "BISECT_ENABLE" with | ||
| "yes" -> "(preprocess (pps bisect_ppx))" | ||
| _ -> "" | ||
| exception Not_found -> "" | ||
|
||
let () = Jbuild_plugin.V1.send @@ {| | ||
|
||
(library | ||
(public_name pgx_value_core) | ||
(libraries core_kernel pgx) | ||
|} ^ preprocess ^ {|) | ||
|} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
open Core_kernel | ||
include Pgx.Value | ||
|
||
let of_time t = | ||
(* | ||
Postgres behaves differently depending on whether the timestamp data type | ||
includes the timezone or not: | ||
Without timezone all inserted timezones are ignored | ||
2016-06-07 15:37:46 (no timezone) | ||
2016-06-07 15:37:46Z (utc timezone) | ||
2016-06-07 15:37:46-04 (local timezone) | ||
Get inserted as | ||
2016-06-07 15:37:46 | ||
With timezones: | ||
2016-06-07 15:37:46 (no timezone) -> 2016-06-07 15:37:46-04 | ||
2016-06-07 15:37:46Z (utc timezone) -> 2016-06-07 11:37:46-04 | ||
2016-06-07 15:37:46-04 (local timezone) -> 2016-06-07 15:37:46-04 | ||
*) | ||
Some (Time.to_string_abs ~zone:Time.Zone.utc t) | ||
;; | ||
|
||
let to_time' = | ||
(* | ||
The time string can come in various forms depending on whether the | ||
Postgres timestamp used includes the time zone: | ||
Without timezone | ||
2016-06-07 15:37:46 | ||
2016-06-07 15:37:46.962425 | ||
With timezone | ||
2016-06-07 15:37:46-04 | ||
2016-06-07 15:37:46.962425-04 | ||
For the first one we need to indicate that it's a UTC time by appending | ||
a 'Z'. For the second one we need to append the minutes to the timezone. | ||
Without these formattings Time.of_string fails spectacularly | ||
*) | ||
let open Re in | ||
let tz = seq [ alt [ char '-'; char '+' ]; digit; digit ] in | ||
let utctz = seq [ char 'Z'; eol ] |> compile in | ||
let localtz_no_min = seq [ tz; eol ] |> compile in | ||
let localtz = seq [ tz; char ':'; digit; digit; eol ] |> compile in | ||
fun s -> | ||
Time.of_string | ||
@@ | ||
match matches utctz s, matches localtz s, matches localtz_no_min s with | ||
| [], [], [] -> s ^ "Z" | ||
| _, [], [] -> s | ||
| [], _, [] -> s | ||
| [], [], _ -> s ^ ":00" | ||
(* It either finishes in one of the patterns above or it doesn't *) | ||
| _ -> convert_failure "time" s | ||
;; | ||
|
||
let to_time_exn = required to_time' | ||
let to_time = Option.map ~f:to_time' | ||
let of_date d = Some (Date.to_string d) | ||
let to_date' = Date.of_string | ||
let to_date_exn = required to_date' | ||
let to_date = Option.map ~f:to_date' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
(** Pgx_value types using Core_kernel's Date and Time modules *) | ||
open Core_kernel | ||
|
||
include Pgx_value_intf.S | ||
|
||
val of_date : Date.t -> t | ||
val to_date_exn : t -> Date.t | ||
val to_date : t -> Date.t option | ||
val of_time : Time.t -> t | ||
val to_time_exn : t -> Time.t | ||
val to_time : t -> Time.t option |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(tests | ||
(names test_pgx_value_core) | ||
(libraries alcotest pgx_value_core)) |
4 changes: 2 additions & 2 deletions
4
pgx_async/test/test_pgx_async_conversions.ml → pgx_value_core/test/test_pgx_value_core.ml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters