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

Commit b8e9dfb

Browse files
author
Nikita Glukhov
committed
Add jbvDatetime JsonbValue type
1 parent 5d5298d commit b8e9dfb

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
}
@@ -1698,6 +1700,44 @@ convertJsonbScalar(StringInfo buffer, JEntry *jentry, JsonbValue *scalarVal)
16981700
JENTRY_ISBOOL_TRUE : JENTRY_ISBOOL_FALSE;
16991701
break;
17001702

1703+
case jbvDatetime:
1704+
{
1705+
char *str;
1706+
PGFunction typoutput;
1707+
int len;
1708+
1709+
switch (scalarVal->val.datetime.typid)
1710+
{
1711+
case DATEOID:
1712+
typoutput = date_out;
1713+
break;
1714+
case TIMEOID:
1715+
typoutput = time_out;
1716+
break;
1717+
case TIMETZOID:
1718+
typoutput = timetz_out;
1719+
break;
1720+
case TIMESTAMPOID:
1721+
typoutput = timestamp_out;
1722+
break;
1723+
case TIMESTAMPTZOID:
1724+
typoutput = timestamptz_out;
1725+
break;
1726+
default:
1727+
elog(ERROR, "unknown jsonb value datetime type oid %d",
1728+
scalarVal->val.datetime.typid);
1729+
}
1730+
1731+
str = DatumGetCString(DirectFunctionCall1(typoutput,
1732+
scalarVal->val.datetime.value));
1733+
1734+
len = strlen(str);
1735+
1736+
appendToBuffer(buffer, str, len);
1737+
*jentry = JENTRY_ISSTRING | len;
1738+
}
1739+
break;
1740+
17011741
default:
17021742
elog(ERROR, "invalid jsonb scalar type");
17031743
}

src/include/utils/jsonb.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ enum jbvType
248248
jbvArray = 0x10,
249249
jbvObject,
250250
/* Binary (i.e. struct Jsonb) jbvArray/jbvObject */
251-
jbvBinary
251+
jbvBinary,
252+
/* Virtual types */
253+
jbvDatetime = 0x20,
252254
};
253255

254256
/*
@@ -289,11 +291,19 @@ struct JsonbValue
289291
int len;
290292
JsonbContainer *data;
291293
} binary; /* Array or object, in on-disk format */
294+
295+
struct
296+
{
297+
Datum value;
298+
Oid typid;
299+
int32 typmod;
300+
} datetime;
292301
} val;
293302
};
294303

295-
#define IsAJsonbScalar(jsonbval) ((jsonbval)->type >= jbvNull && \
296-
(jsonbval)->type <= jbvBool)
304+
#define IsAJsonbScalar(jsonbval) (((jsonbval)->type >= jbvNull && \
305+
(jsonbval)->type <= jbvBool) || \
306+
(jsonbval)->type == jbvDatetime)
297307

298308
/*
299309
* Key/value pair within an Object.

0 commit comments

Comments
 (0)