From c78c456b0bd430789ad6ace02418a223ff055a9b Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Wed, 21 Oct 2020 11:07:34 +0100 Subject: [PATCH 1/2] Simplify postgresInterval file --- .../graphile-build-pg/src/postgresInterval.js | 56 +++++++------------ 1 file changed, 19 insertions(+), 37 deletions(-) diff --git a/packages/graphile-build-pg/src/postgresInterval.js b/packages/graphile-build-pg/src/postgresInterval.js index 06c82d11b..778fce430 100644 --- a/packages/graphile-build-pg/src/postgresInterval.js +++ b/packages/graphile-build-pg/src/postgresInterval.js @@ -30,48 +30,30 @@ export type Interval = { seconds: number, }; -// All intervals will have exactly these properties: -const BASE: Interval = Object.freeze({ - years: 0, - months: 0, - days: 0, - hours: 0, - minutes: 0, - seconds: 0.0, -}); - export function parseInterval(interval: string): Interval { - const result = { ...BASE }; - if (!interval) { - return result; + return { + years: 0, + months: 0, + days: 0, + hours: 0, + minutes: 0, + seconds: 0.0, + }; } - const matches = INTERVAL.exec(interval); - if (!matches) { - throw new Error(`Failed to parse interval '${interval}' from PostgreSQL`); - } - - const [ - , - years, - months, - days, - plusMinusTime, - hours, - minutes, - seconds, - ] = matches; + const [, years, months, days, plusMinusTime, hours, minutes, seconds] = + INTERVAL.exec(interval) || []; const timeMultiplier = plusMinusTime === "-" ? -1 : 1; - if (years) result.years = parseInt(years, 10); - if (months) result.months = parseInt(months, 10); - if (days) result.days = parseInt(days, 10); - if (hours) result.hours = timeMultiplier * parseInt(hours, 10); - if (minutes) result.minutes = timeMultiplier * parseInt(minutes, 10); - // Seconds can be decimal; all other values are integer - if (seconds) result.seconds = timeMultiplier * parseFloat(seconds); - - return result; + return { + years: years ? parseInt(years, 10) : 0, + months: months ? parseInt(months, 10) : 0, + days: days ? parseInt(days, 10) : 0, + hours: hours ? timeMultiplier * parseInt(hours, 10) : 0, + minutes: minutes ? timeMultiplier * parseInt(minutes, 10) : 0, + // Seconds can be decimal; all other values are integer + seconds: seconds ? timeMultiplier * parseFloat(seconds) : 0, + }; } From 43ec1215e2ffc4dd1e75c6a8abbfbfc3e23df48a Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Wed, 21 Oct 2020 11:09:07 +0100 Subject: [PATCH 2/2] Track another couple intervals --- .../__snapshots__/mutations.test.js.snap | 32 ++++ .../__snapshots__/queries.test.js.snap | 144 ++++++++++++++++++ .../__tests__/kitchen-sink-data.sql | 2 +- 3 files changed, 177 insertions(+), 1 deletion(-) diff --git a/packages/postgraphile-core/__tests__/integration/__snapshots__/mutations.test.js.snap b/packages/postgraphile-core/__tests__/integration/__snapshots__/mutations.test.js.snap index cf665beb3..c380e8e93 100644 --- a/packages/postgraphile-core/__tests__/integration/__snapshots__/mutations.test.js.snap +++ b/packages/postgraphile-core/__tests__/integration/__snapshots__/mutations.test.js.snap @@ -2234,6 +2234,22 @@ Object { "seconds": 1, "years": 1, }, + Object { + "days": 0, + "hours": 0, + "minutes": 0, + "months": 0, + "seconds": 0, + "years": 200, + }, + Object { + "days": 0, + "hours": 0, + "minutes": 3, + "months": 0, + "seconds": 20.2, + "years": 0, + }, ], "json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}", "jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}", @@ -2532,6 +2548,22 @@ Object { "seconds": 1, "years": 1, }, + Object { + "days": 0, + "hours": 0, + "minutes": 0, + "months": 0, + "seconds": 0, + "years": 200, + }, + Object { + "days": 0, + "hours": 0, + "minutes": 3, + "months": 0, + "seconds": 20.2, + "years": 0, + }, ], "json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}", "jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}", diff --git a/packages/postgraphile-core/__tests__/integration/__snapshots__/queries.test.js.snap b/packages/postgraphile-core/__tests__/integration/__snapshots__/queries.test.js.snap index 760d0128f..2787e03bc 100644 --- a/packages/postgraphile-core/__tests__/integration/__snapshots__/queries.test.js.snap +++ b/packages/postgraphile-core/__tests__/integration/__snapshots__/queries.test.js.snap @@ -8940,6 +8940,22 @@ Object { "seconds": 1, "years": 1, }, + Object { + "days": 0, + "hours": 0, + "minutes": 0, + "months": 0, + "seconds": 0, + "years": 200, + }, + Object { + "days": 0, + "hours": 0, + "minutes": 3, + "months": 0, + "seconds": 20.2, + "years": 0, + }, ], "json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}", "jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}", @@ -9237,6 +9253,22 @@ Object { "seconds": 1, "years": 1, }, + Object { + "days": 0, + "hours": 0, + "minutes": 0, + "months": 0, + "seconds": 0, + "years": 200, + }, + Object { + "days": 0, + "hours": 0, + "minutes": 3, + "months": 0, + "seconds": 20.2, + "years": 0, + }, ], "json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}", "jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}", @@ -9541,6 +9573,22 @@ Object { "seconds": 1, "years": 1, }, + Object { + "days": 0, + "hours": 0, + "minutes": 0, + "months": 0, + "seconds": 0, + "years": 200, + }, + Object { + "days": 0, + "hours": 0, + "minutes": 3, + "months": 0, + "seconds": 20.2, + "years": 0, + }, ], "json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}", "jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}", @@ -10134,6 +10182,22 @@ Object { "seconds": 1, "years": 1, }, + Object { + "days": 0, + "hours": 0, + "minutes": 0, + "months": 0, + "seconds": 0, + "years": 200, + }, + Object { + "days": 0, + "hours": 0, + "minutes": 3, + "months": 0, + "seconds": 20.2, + "years": 0, + }, ], "json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}", "jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}", @@ -10431,6 +10495,22 @@ Object { "seconds": 1, "years": 1, }, + Object { + "days": 0, + "hours": 0, + "minutes": 0, + "months": 0, + "seconds": 0, + "years": 200, + }, + Object { + "days": 0, + "hours": 0, + "minutes": 3, + "months": 0, + "seconds": 20.2, + "years": 0, + }, ], "json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}", "jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}", @@ -10735,6 +10815,22 @@ Object { "seconds": 1, "years": 1, }, + Object { + "days": 0, + "hours": 0, + "minutes": 0, + "months": 0, + "seconds": 0, + "years": 200, + }, + Object { + "days": 0, + "hours": 0, + "minutes": 3, + "months": 0, + "seconds": 20.2, + "years": 0, + }, ], "json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}", "jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}", @@ -11635,6 +11731,22 @@ Object { "seconds": 1, "years": 1, }, + Object { + "days": 0, + "hours": 0, + "minutes": 0, + "months": 0, + "seconds": 0, + "years": 200, + }, + Object { + "days": 0, + "hours": 0, + "minutes": 3, + "months": 0, + "seconds": 20.2, + "years": 0, + }, ], "json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}", "jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}", @@ -11932,6 +12044,22 @@ Object { "seconds": 1, "years": 1, }, + Object { + "days": 0, + "hours": 0, + "minutes": 0, + "months": 0, + "seconds": 0, + "years": 200, + }, + Object { + "days": 0, + "hours": 0, + "minutes": 3, + "months": 0, + "seconds": 20.2, + "years": 0, + }, ], "json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}", "jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}", @@ -12236,6 +12364,22 @@ Object { "seconds": 1, "years": 1, }, + Object { + "days": 0, + "hours": 0, + "minutes": 0, + "months": 0, + "seconds": 0, + "years": 200, + }, + Object { + "days": 0, + "hours": 0, + "minutes": 3, + "months": 0, + "seconds": 20.2, + "years": 0, + }, ], "json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}", "jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}", diff --git a/packages/postgraphile-core/__tests__/kitchen-sink-data.sql b/packages/postgraphile-core/__tests__/kitchen-sink-data.sql index fcafa2bfb..5f8a37d32 100644 --- a/packages/postgraphile-core/__tests__/kitchen-sink-data.sql +++ b/packages/postgraphile-core/__tests__/kitchen-sink-data.sql @@ -133,7 +133,7 @@ insert into b.types values ( '04:05:06', '04:05:06 -8:00', '1 year 2 months 3 days 4 hours 5 minutes 6.789123456 seconds', - ARRAY['1 year 2 months 3 days 4 hours 5 minutes 6.789123456 seconds', '1 year 1 months 1 days 1 hours 1 minutes 1 seconds']::interval[], + ARRAY['1 year 2 months 3 days 4 hours 5 minutes 6.789123456 seconds', '1 year 1 months 1 days 1 hours 1 minutes 1 seconds', '200 years', '200.200 seconds']::interval[], '9876543.21', (null, null, null, null, null, null, null, null), ((3, '456', 'red', 'aed18400-2a92-46df-8204-b70c728b3520', 'BAR_FOO', 'one', interval '6 hours', 93), (null, null, null, null, null, null, null, null), 7),