Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 27562c9

Browse files
author
Nikita Glukhov
committed
Add jbvDatetime JsonbValue type
1 parent 7f28c63 commit 27562c9

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/backend/utils/adt/jsonb_util.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "access/hash.h"
1717
#include "catalog/pg_collation.h"
18+
#include "catalog/pg_type.h"
1819
#include "miscadmin.h"
1920
#include "utils/builtins.h"
2021
#include "utils/jsonb.h"
@@ -241,6 +242,7 @@ compareJsonbContainers(JsonbContainer *a, JsonbContainer *b)
241242
res = (va.val.object.nPairs > vb.val.object.nPairs) ? 1 : -1;
242243
break;
243244
case jbvBinary:
245+
case jbvDatetime:
244246
elog(ERROR, "unexpected jbvBinary value");
245247
}
246248
}
@@ -1741,6 +1743,44 @@ convertJsonbScalar(StringInfo buffer, JEntry *jentry, JsonbValue *scalarVal)
17411743
JENTRY_ISBOOL_TRUE : JENTRY_ISBOOL_FALSE;
17421744
break;
17431745

1746+
case jbvDatetime:
1747+
{
1748+
char *str;
1749+
PGFunction typoutput;
1750+
int len;
1751+
1752+
switch (scalarVal->val.datetime.typid)
1753+
{
1754+
case DATEOID:
1755+
typoutput = date_out;
1756+
break;
1757+
case TIMEOID:
1758+
typoutput = time_out;
1759+
break;
1760+
case TIMETZOID:
1761+
typoutput = timetz_out;
1762+
break;
1763+
case TIMESTAMPOID:
1764+
typoutput = timestamp_out;
1765+
break;
1766+
case TIMESTAMPTZOID:
1767+
typoutput = timestamptz_out;
1768+
break;
1769+
default:
1770+
elog(ERROR, "unknown jsonb value datetime type oid %d",
1771+
scalarVal->val.datetime.typid);
1772+
}
1773+
1774+
str = DatumGetCString(DirectFunctionCall1(typoutput,
1775+
scalarVal->val.datetime.value));
1776+
1777+
len = strlen(str);
1778+
1779+
appendToBuffer(buffer, str, len);
1780+
*jentry = JENTRY_ISSTRING | len;
1781+
}
1782+
break;
1783+
17441784
default:
17451785
elog(ERROR, "invalid jsonb scalar type");
17461786
}

src/include/utils/jsonb.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ enum jbvType
238238
jbvArray = 0x10,
239239
jbvObject,
240240
/* Binary (i.e. struct Jsonb) jbvArray/jbvObject */
241-
jbvBinary
241+
jbvBinary,
242+
/* Virtual types */
243+
jbvDatetime = 0x20,
242244
};
243245

244246
/*
@@ -279,11 +281,19 @@ struct JsonbValue
279281
int len;
280282
JsonbContainer *data;
281283
} binary; /* Array or object, in on-disk format */
284+
285+
struct
286+
{
287+
Datum value;
288+
Oid typid;
289+
int32 typmod;
290+
} datetime;
282291
} val;
283292
};
284293

285-
#define IsAJsonbScalar(jsonbval) ((jsonbval)->type >= jbvNull && \
286-
(jsonbval)->type <= jbvBool)
294+
#define IsAJsonbScalar(jsonbval) (((jsonbval)->type >= jbvNull && \
295+
(jsonbval)->type <= jbvBool) || \
296+
(jsonbval)->type == jbvDatetime)
287297

288298
/*
289299
* Key/value pair within an Object.

0 commit comments

Comments
 (0)