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

Commit c83a3ea

Browse files
author
Nikita Glukhov
committed
Add jbvDatetime JsonbValue type
1 parent 1393b87 commit c83a3ea

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/backend/utils/adt/jsonb_util.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
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"
21+
#include "utils/datetime.h"
22+
#include "utils/jsonapi.h"
2023
#include "utils/jsonb.h"
2124
#include "utils/memutils.h"
2225
#include "utils/varlena.h"
@@ -241,6 +244,7 @@ compareJsonbContainers(JsonbContainer *a, JsonbContainer *b)
241244
res = (va.val.object.nPairs > vb.val.object.nPairs) ? 1 : -1;
242245
break;
243246
case jbvBinary:
247+
case jbvDatetime:
244248
elog(ERROR, "unexpected jbvBinary value");
245249
}
246250
}
@@ -1741,11 +1745,27 @@ convertJsonbScalar(StringInfo buffer, JEntry *jentry, JsonbValue *scalarVal)
17411745
JENTRY_ISBOOL_TRUE : JENTRY_ISBOOL_FALSE;
17421746
break;
17431747

1748+
case jbvDatetime:
1749+
{
1750+
char buf[MAXDATELEN + 1];
1751+
size_t len;
1752+
1753+
JsonEncodeDateTime(buf,
1754+
scalarVal->val.datetime.value,
1755+
scalarVal->val.datetime.typid);
1756+
len = strlen(buf);
1757+
appendToBuffer(buffer, buf, len);
1758+
1759+
*jentry = JENTRY_ISSTRING | len;
1760+
}
1761+
break;
1762+
17441763
default:
17451764
elog(ERROR, "invalid jsonb scalar type");
17461765
}
17471766
}
17481767

1768+
17491769
/*
17501770
* Compare two jbvString JsonbValue values, a and b.
17511771
*

src/include/utils/jsonb.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,14 @@ enum jbvType
238238
jbvArray = 0x10,
239239
jbvObject,
240240
/* Binary (i.e. struct Jsonb) jbvArray/jbvObject */
241-
jbvBinary
241+
jbvBinary,
242+
/*
243+
* Virtual types.
244+
*
245+
* These types are used only for in-memory JSON processing and serialized
246+
* into JSON strings when outputted to json/jsonb.
247+
*/
248+
jbvDatetime = 0x20,
242249
};
243250

244251
/*
@@ -279,11 +286,19 @@ struct JsonbValue
279286
int len;
280287
JsonbContainer *data;
281288
} binary; /* Array or object, in on-disk format */
289+
290+
struct
291+
{
292+
Datum value;
293+
Oid typid;
294+
int32 typmod;
295+
} datetime;
282296
} val;
283297
};
284298

285-
#define IsAJsonbScalar(jsonbval) ((jsonbval)->type >= jbvNull && \
286-
(jsonbval)->type <= jbvBool)
299+
#define IsAJsonbScalar(jsonbval) (((jsonbval)->type >= jbvNull && \
300+
(jsonbval)->type <= jbvBool) || \
301+
(jsonbval)->type == jbvDatetime)
287302

288303
/*
289304
* Key/value pair within an Object.

0 commit comments

Comments
 (0)