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"
51 Datum *vals,
bool *nulls,
int *valcount,
58 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);
91 text *id_column_text = PG_GETARG_TEXT_P(4);
93 char *geom_column = text_to_cstring(geom_column_text);
94 char *id_column = text_to_cstring(id_column_text);
95 Oid geom_oid = InvalidOid;
96 Oid geog_oid = InvalidOid;
99 postgis_initialize_cache();
100 geom_oid = postgis_oid(GEOMETRYOID);
101 geog_oid = postgis_oid(GEOGRAPHYOID);
103 if (strlen(geom_column) == 0)
105 if (strlen(id_column) == 0)
108 result = makeStringInfo();
112 PG_RETURN_TEXT_P(cstring_to_text_with_len(
result->data,
result->len));
121 char *geom_column_name,
122 char *id_column_name,
123 int32 maxdecimaldigits,
133 HeapTupleData tmptup,
136 bool needsep =
false;
138 StringInfo props = makeStringInfo();
139 StringInfo
id = makeStringInfo();
140 bool geom_column_found =
false;
141 bool id_column_found =
false;
143 sep = use_line_feeds ?
",\n " :
", ";
145 td = DatumGetHeapTupleHeader(composite);
148 tupType = HeapTupleHeaderGetTypeId(td);
149 tupTypmod = HeapTupleHeaderGetTypMod(td);
150 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
153 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
157 appendStringInfoString(
result,
"{\"type\": \"Feature\", \"geometry\": ");
159 for (i = 0; i < tupdesc->natts; i++)
166 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
167 bool is_geom_column =
false;
168 bool is_id_column =
false;
170 if (att->attisdropped)
173 attname = NameStr(att->attname);
175 if (geom_column_name)
176 is_geom_column = (strcmp(attname, geom_column_name) == 0);
178 is_geom_column = (att->atttypid == geom_oid || att->atttypid == geog_oid);
181 is_id_column = (strcmp(attname, id_column_name) == 0);
183 if ((!geom_column_found) && is_geom_column)
186 geom_column_found =
true;
188 val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
198 Int32GetDatum(maxdecimaldigits))));
202 appendStringInfoString(
result,
"null");
205 else if (is_id_column)
207 id_column_found =
true;
209 val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
214 outfuncoid = InvalidOid;
219 datum_to_json(val, isnull,
id, tcategory, outfuncoid,
false);
224 appendStringInfoString(props, sep);
227 escape_json(props, attname);
228 appendStringInfoString(props,
": ");
230 val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
235 outfuncoid = InvalidOid;
240 datum_to_json(val, isnull, props, tcategory, outfuncoid,
false);
244 if (!geom_column_found)
246 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
247 errmsg(
"geometry column is missing")));
251 if (!id_column_found)
253 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
254 errmsg(
"Specified id column \"%s\" is missing", id_column_name)));
256 appendStringInfoString(
result,
", \"id\": ");
257 appendStringInfo(
result,
"%s", id->data);
260 appendStringInfoString(
result,
", \"properties\": {");
261 appendStringInfo(
result,
"%s", props->data);
263 appendStringInfoString(
result,
"}}");
264 ReleaseTupleDesc(tupdesc);
290 typoid = getBaseType(typoid);
292 *outfuncoid = InvalidOid;
312 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
330 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
336 if (OidIsValid(get_element_type(typoid)) || typoid == ANYARRAYOID
337 || typoid == RECORDARRAYOID)
339 else if (type_is_rowtype(typoid))
346 if (typoid >= FirstNormalObjectId)
349 CoercionPathType ctype;
351 ctype = find_coercion_pathway(JSONOID, typoid,
354 if (ctype == COERCION_PATH_FUNC && OidIsValid(castfunc))
357 *outfuncoid = castfunc;
362 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
368 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
395 Assert(!(key_scalar && is_null));
399 appendStringInfoString(
result,
"null");
409 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
410 errmsg(
"key value must be scalar, not array, composite, or json")));
421 outputstr = DatumGetBool(val) ?
"true" :
"false";
423 escape_json(
result, outputstr);
425 appendStringInfoString(
result, outputstr);
428 outputstr = OidOutputFunctionCall(outfuncoid, val);
434 if (!key_scalar && IsValidJsonNumber(outputstr, strlen(outputstr)))
435 appendStringInfoString(
result, outputstr);
437 escape_json(
result, outputstr);
442 char buf[MAXDATELEN + 1];
445 appendStringInfo(
result,
"\"%s\"", buf);
450 char buf[MAXDATELEN + 1];
453 appendStringInfo(
result,
"\"%s\"", buf);
458 char buf[MAXDATELEN + 1];
461 appendStringInfo(
result,
"\"%s\"", buf);
466 outputstr = OidOutputFunctionCall(outfuncoid, val);
467 appendStringInfoString(
result, outputstr);
472 jsontext = DatumGetTextPP(OidFunctionCall1(outfuncoid, val));
473 outputstr = text_to_cstring(jsontext);
474 appendStringInfoString(
result, outputstr);
479 outputstr = OidOutputFunctionCall(outfuncoid, val);
480 escape_json(
result, outputstr);
492 ArrayType *v = DatumGetArrayTypeP(array);
493 Oid element_type = ARR_ELEMTYPE(v);
508 nitems = ArrayGetNItems(ndim, dim);
512 appendStringInfoString(
result,
"[]");
516 get_typlenbyvalalign(element_type,
517 &typlen, &typbyval, &typalign);
520 &tcategory, &outfuncoid);
522 deconstruct_array(v, element_type, typlen, typbyval,
523 typalign, &elements, &nulls,
527 outfuncoid, use_line_feeds);
543 HeapTupleData tmptup,
546 bool needsep =
false;
549 sep = use_line_feeds ?
",\n " :
",";
551 td = DatumGetHeapTupleHeader(composite);
554 tupType = HeapTupleHeaderGetTypeId(td);
555 tupTypmod = HeapTupleHeaderGetTypMod(td);
556 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
559 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
563 appendStringInfoChar(
result,
'{');
565 for (i = 0; i < tupdesc->natts; i++)
572 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
574 if (att->attisdropped)
578 appendStringInfoString(
result, sep);
581 attname = NameStr(att->attname);
582 escape_json(
result, attname);
583 appendStringInfoChar(
result,
':');
585 val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
590 outfuncoid = InvalidOid;
598 appendStringInfoChar(
result,
'}');
599 ReleaseTupleDesc(tupdesc);
610 Oid outfuncoid,
bool use_line_feeds)
617 sep = use_line_feeds ?
",\n " :
",";
619 appendStringInfoChar(
result,
'[');
621 for (i = 1; i <= dims[dim]; i++)
624 appendStringInfoString(
result, sep);
626 if (dim + 1 == ndims)
639 valcount, tcategory, outfuncoid,
false);
643 appendStringInfoChar(
result,
']');
649 tm->tm_hour = time / USECS_PER_HOUR;
650 time -= tm->tm_hour * USECS_PER_HOUR;
651 tm->tm_min = time / USECS_PER_MINUTE;
652 time -= tm->tm_min * USECS_PER_MINUTE;
653 tm->tm_sec = time / USECS_PER_SEC;
654 time -= tm->tm_sec * USECS_PER_SEC;
662 TimeOffset trem = time->time;
664 tm->tm_hour = trem / USECS_PER_HOUR;
665 trem -= tm->tm_hour * USECS_PER_HOUR;
666 tm->tm_min = trem / USECS_PER_MINUTE;
667 trem -= tm->tm_min * USECS_PER_MINUTE;
668 tm->tm_sec = trem / USECS_PER_SEC;
669 *fsec = trem - tm->tm_sec * USECS_PER_SEC;
681 buf = palloc(MAXDATELEN + 1);
690 date = DatumGetDateADT(
value);
693 if (DATE_NOT_FINITE(date))
694 EncodeSpecialDate(date, buf);
697 j2date(date + POSTGRES_EPOCH_JDATE,
698 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
699 EncodeDateOnly(&tm, USE_XSD_DATES, buf);
705 TimeADT time = DatumGetTimeADT(
value);
712 EncodeTimeOnly(tm, fsec,
false, 0, USE_XSD_DATES, buf);
717 TimeTzADT *time = DatumGetTimeTzADTP(
value);
725 EncodeTimeOnly(tm, fsec,
true, tz, USE_XSD_DATES, buf);
734 timestamp = DatumGetTimestamp(
value);
736 if (TIMESTAMP_NOT_FINITE(timestamp))
737 EncodeSpecialTimestamp(timestamp, buf);
738 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
739 EncodeDateTime(&tm, fsec,
false, 0, NULL, USE_XSD_DATES, buf);
742 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
743 errmsg(
"timestamp out of range")));
748 TimestampTz timestamp;
752 const char *tzn = NULL;
754 timestamp = DatumGetTimestampTz(
value);
756 if (TIMESTAMP_NOT_FINITE(timestamp))
757 EncodeSpecialTimestamp(timestamp, buf);
758 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
759 EncodeDateTime(&tm, fsec,
true, tz, tzn, USE_XSD_DATES, buf);
762 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
763 errmsg(
"timestamp out of range")));
767 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 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)
static void composite_to_geojson(FunctionCallInfo fcinfo, Datum composite, char *geom_column_name, char *id_column_name, int32 maxdecimaldigits, StringInfo result, bool use_line_feeds, Oid geom_oid, Oid geog_oid)
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)