Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 19 additions & 37 deletions packages/graphile-build-pg/src/postgresInterval.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]}}",
Expand Down Expand Up @@ -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]}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]}}",
Expand Down Expand Up @@ -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]}}",
Expand Down Expand Up @@ -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]}}",
Expand Down Expand Up @@ -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]}}",
Expand Down Expand Up @@ -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]}}",
Expand Down Expand Up @@ -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]}}",
Expand Down Expand Up @@ -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]}}",
Expand Down Expand Up @@ -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]}}",
Expand Down Expand Up @@ -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]}}",
Expand Down
2 changes: 1 addition & 1 deletion packages/postgraphile-core/__tests__/kitchen-sink-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down