2 #include "../postgis_config.h"
8 #include "access/htup_details.h"
9 #include "access/transam.h"
10 #include "catalog/pg_type.h"
11 #include "executor/spi.h"
12 #include "lib/stringinfo.h"
13 #include "libpq/pqformat.h"
14 #include "mb/pg_wchar.h"
15 #include "parser/parse_coerce.h"
16 #include "utils/array.h"
17 #include "utils/builtins.h"
18 #include "utils/date.h"
19 #include "utils/datetime.h"
20 #include "utils/lsyscache.h"
21 #include "utils/json.h"
22 #if POSTGIS_PGSQL_VERSION < 130
23 #include "utils/jsonapi.h"
25 #include "common/jsonapi.h"
27 #include "utils/typcache.h"
28 #include "utils/syscache.h"
31 #include "lwgeom_pg.h"
52 Datum *vals,
bool *nulls,
int *valcount,
59 char *geom_column_name,
60 int32 maxdecimaldigits,
74 static int postgis_time2tm(TimeADT time,
struct pg_tm *tm, fsec_t *fsec);
75 static int postgis_timetz2tm(TimeTzADT *time,
struct pg_tm *tm, fsec_t *fsec,
int *tzp);
87 Datum array = PG_GETARG_DATUM(0);
88 text *geom_column_text = PG_GETARG_TEXT_P(1);
89 int32 maxdecimaldigits = PG_GETARG_INT32(2);
90 bool do_pretty = PG_GETARG_BOOL(3);
92 char *geom_column = text_to_cstring(geom_column_text);
93 Oid geom_oid = InvalidOid;
94 Oid geog_oid = InvalidOid;
97 postgis_initialize_cache();
98 geom_oid = postgis_oid(GEOMETRYOID);
99 geog_oid = postgis_oid(GEOGRAPHYOID);
101 if (strlen(geom_column) == 0)
104 result = makeStringInfo();
108 PG_RETURN_TEXT_P(cstring_to_text_with_len(
result->data,
result->len));
117 char *geom_column_name,
118 int32 maxdecimaldigits,
128 HeapTupleData tmptup,
131 bool needsep =
false;
133 StringInfo props = makeStringInfo();
134 bool geom_column_found =
false;
136 sep = use_line_feeds ?
",\n " :
", ";
138 td = DatumGetHeapTupleHeader(composite);
141 tupType = HeapTupleHeaderGetTypeId(td);
142 tupTypmod = HeapTupleHeaderGetTypMod(td);
143 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
146 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
150 appendStringInfoString(
result,
"{\"type\": \"Feature\", \"geometry\": ");
152 for (i = 0; i < tupdesc->natts; i++)
159 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
160 bool is_geom_column =
false;
162 if (att->attisdropped)
165 attname = NameStr(att->attname);
167 if (geom_column_name)
168 is_geom_column = (strcmp(attname, geom_column_name) == 0);
170 is_geom_column = (att->atttypid == geom_oid || att->atttypid == geog_oid);
172 if ((!geom_column_found) && is_geom_column)
175 geom_column_found =
true;
177 val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
187 Int32GetDatum(maxdecimaldigits))));
191 appendStringInfoString(
result,
"{\"type\": null}");
197 appendStringInfoString(props, sep);
200 escape_json(props, attname);
201 appendStringInfoString(props,
": ");
203 val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
208 outfuncoid = InvalidOid;
213 datum_to_json(val, isnull, props, tcategory, outfuncoid,
false);
217 if (!geom_column_found)
219 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
220 errmsg(
"geometry column is missing")));
222 appendStringInfoString(
result,
", \"properties\": {");
223 appendStringInfo(
result,
"%s", props->data);
225 appendStringInfoString(
result,
"}}");
226 ReleaseTupleDesc(tupdesc);
251 typoid = getBaseType(typoid);
253 *outfuncoid = InvalidOid;
273 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
291 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
297 if (OidIsValid(get_element_type(typoid)) || typoid == ANYARRAYOID
298 || typoid == RECORDARRAYOID)
300 else if (type_is_rowtype(typoid))
307 if (typoid >= FirstNormalObjectId)
310 CoercionPathType ctype;
312 ctype = find_coercion_pathway(JSONOID, typoid,
315 if (ctype == COERCION_PATH_FUNC && OidIsValid(castfunc))
318 *outfuncoid = castfunc;
323 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
329 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
356 Assert(!(key_scalar && is_null));
360 appendStringInfoString(
result,
"null");
370 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
371 errmsg(
"key value must be scalar, not array, composite, or json")));
382 outputstr = DatumGetBool(val) ?
"true" :
"false";
384 escape_json(
result, outputstr);
386 appendStringInfoString(
result, outputstr);
389 outputstr = OidOutputFunctionCall(outfuncoid, val);
395 if (!key_scalar && IsValidJsonNumber(outputstr, strlen(outputstr)))
396 appendStringInfoString(
result, outputstr);
398 escape_json(
result, outputstr);
403 char buf[MAXDATELEN + 1];
406 appendStringInfo(
result,
"\"%s\"", buf);
411 char buf[MAXDATELEN + 1];
414 appendStringInfo(
result,
"\"%s\"", buf);
419 char buf[MAXDATELEN + 1];
422 appendStringInfo(
result,
"\"%s\"", buf);
427 outputstr = OidOutputFunctionCall(outfuncoid, val);
428 appendStringInfoString(
result, outputstr);
433 jsontext = DatumGetTextPP(OidFunctionCall1(outfuncoid, val));
434 outputstr = text_to_cstring(jsontext);
435 appendStringInfoString(
result, outputstr);
440 outputstr = OidOutputFunctionCall(outfuncoid, val);
441 escape_json(
result, outputstr);
453 ArrayType *v = DatumGetArrayTypeP(array);
454 Oid element_type = ARR_ELEMTYPE(v);
469 nitems = ArrayGetNItems(ndim, dim);
473 appendStringInfoString(
result,
"[]");
477 get_typlenbyvalalign(element_type,
478 &typlen, &typbyval, &typalign);
481 &tcategory, &outfuncoid);
483 deconstruct_array(v, element_type, typlen, typbyval,
484 typalign, &elements, &nulls,
488 outfuncoid, use_line_feeds);
504 HeapTupleData tmptup,
507 bool needsep =
false;
510 sep = use_line_feeds ?
",\n " :
",";
512 td = DatumGetHeapTupleHeader(composite);
515 tupType = HeapTupleHeaderGetTypeId(td);
516 tupTypmod = HeapTupleHeaderGetTypMod(td);
517 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
520 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
524 appendStringInfoChar(
result,
'{');
526 for (i = 0; i < tupdesc->natts; i++)
533 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
535 if (att->attisdropped)
539 appendStringInfoString(
result, sep);
542 attname = NameStr(att->attname);
543 escape_json(
result, attname);
544 appendStringInfoChar(
result,
':');
546 val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
551 outfuncoid = InvalidOid;
559 appendStringInfoChar(
result,
'}');
560 ReleaseTupleDesc(tupdesc);
571 Oid outfuncoid,
bool use_line_feeds)
578 sep = use_line_feeds ?
",\n " :
",";
580 appendStringInfoChar(
result,
'[');
582 for (i = 1; i <= dims[dim]; i++)
585 appendStringInfoString(
result, sep);
587 if (dim + 1 == ndims)
600 valcount, tcategory, outfuncoid,
false);
604 appendStringInfoChar(
result,
']');
610 tm->tm_hour = time / USECS_PER_HOUR;
611 time -= tm->tm_hour * USECS_PER_HOUR;
612 tm->tm_min = time / USECS_PER_MINUTE;
613 time -= tm->tm_min * USECS_PER_MINUTE;
614 tm->tm_sec = time / USECS_PER_SEC;
615 time -= tm->tm_sec * USECS_PER_SEC;
623 TimeOffset trem = time->time;
625 tm->tm_hour = trem / USECS_PER_HOUR;
626 trem -= tm->tm_hour * USECS_PER_HOUR;
627 tm->tm_min = trem / USECS_PER_MINUTE;
628 trem -= tm->tm_min * USECS_PER_MINUTE;
629 tm->tm_sec = trem / USECS_PER_SEC;
630 *fsec = trem - tm->tm_sec * USECS_PER_SEC;
642 buf = palloc(MAXDATELEN + 1);
651 date = DatumGetDateADT(
value);
654 if (DATE_NOT_FINITE(date))
655 EncodeSpecialDate(date, buf);
658 j2date(date + POSTGRES_EPOCH_JDATE,
659 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
660 EncodeDateOnly(&tm, USE_XSD_DATES, buf);
666 TimeADT time = DatumGetTimeADT(
value);
673 EncodeTimeOnly(tm, fsec,
false, 0, USE_XSD_DATES, buf);
678 TimeTzADT *time = DatumGetTimeTzADTP(
value);
686 EncodeTimeOnly(tm, fsec,
true, tz, USE_XSD_DATES, buf);
695 timestamp = DatumGetTimestamp(
value);
697 if (TIMESTAMP_NOT_FINITE(timestamp))
698 EncodeSpecialTimestamp(timestamp, buf);
699 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
700 EncodeDateTime(&tm, fsec,
false, 0, NULL, USE_XSD_DATES, buf);
703 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
704 errmsg(
"timestamp out of range")));
709 TimestampTz timestamp;
713 const char *tzn = NULL;
715 timestamp = DatumGetTimestampTz(
value);
717 if (TIMESTAMP_NOT_FINITE(timestamp))
718 EncodeSpecialTimestamp(timestamp, buf);
719 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
720 EncodeDateTime(&tm, fsec,
true, tz, tzn, USE_XSD_DATES, buf);
723 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
724 errmsg(
"timestamp out of range")));
728 elog(ERROR,
"unknown jsonb value datetime type oid %d", typid);
char result[OUT_DOUBLE_BUFFER_SIZE]
This library is the generic geometry handling section of PostGIS.
Datum row_to_geojson(PG_FUNCTION_ARGS)
static int postgis_timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
static int postgis_time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
static void json_categorize_type(Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid)
PG_FUNCTION_INFO_V1(ST_AsGeoJsonRow)
Datum LWGEOM_asGeoJson(PG_FUNCTION_ARGS)
static void composite_to_geojson(FunctionCallInfo fcinfo, Datum composite, char *geom_column_name, int32 maxdecimaldigits, StringInfo result, bool use_line_feeds, Oid geom_oid, Oid geog_oid)
static void array_dim_to_json(StringInfo result, int dim, int ndims, int *dims, Datum *vals, bool *nulls, int *valcount, JsonTypeCategory tcategory, Oid outfuncoid, bool use_line_feeds)
Datum ST_AsGeoJsonRow(PG_FUNCTION_ARGS)
static char * postgis_JsonEncodeDateTime(char *buf, Datum value, Oid typid)
static void composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
static void array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
static void datum_to_json(Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)