From 23347231a53bc373710db71559a194d87f60a7cb Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 15 Jun 2007 20:56:52 +0000 Subject: Tweak the API for per-datatype typmodin functions so that they are passed an array of strings rather than an array of integers, and allow any simple constant or identifier to be used in typmods; for example create table foo (f1 widget(42,'23skidoo',point)); Of course the typmodin function has still got to pack this info into a non-negative int32 for storage, but it's still a useful improvement in flexibility, especially considering that you can do nearly anything if you are willing to keep the info in a side table. We can get away with this change since we have not yet released a version providing user-definable typmods. Per discussion. --- src/backend/utils/adt/arrayutils.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'src/backend/utils/adt/arrayutils.c') diff --git a/src/backend/utils/adt/arrayutils.c b/src/backend/utils/adt/arrayutils.c index 695f4516329..cd59ffb62d6 100644 --- a/src/backend/utils/adt/arrayutils.c +++ b/src/backend/utils/adt/arrayutils.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/arrayutils.c,v 1.23 2007/01/05 22:19:40 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/arrayutils.c,v 1.24 2007/06/15 20:56:50 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -17,6 +17,7 @@ #include "catalog/pg_type.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/memutils.h" @@ -191,16 +192,21 @@ mda_next_tuple(int n, int *curr, const int *span) } /* - * ArrayGetTypmods: verify that argument is a 1-D integer array, - * return its length and a pointer to the first contained integer. + * ArrayGetIntegerTypmods: verify that argument is a 1-D cstring array, + * and get the contents converted to integers. Returns a palloc'd array + * and places the length at *n. */ int32 * -ArrayGetTypmods(ArrayType *arr, int *n) +ArrayGetIntegerTypmods(ArrayType *arr, int *n) { - if (ARR_ELEMTYPE(arr) != INT4OID) + int32 *result; + Datum *elem_values; + int i; + + if (ARR_ELEMTYPE(arr) != CSTRINGOID) ereport(ERROR, (errcode(ERRCODE_ARRAY_ELEMENT_ERROR), - errmsg("typmod array must be type integer[]"))); + errmsg("typmod array must be type cstring[]"))); if (ARR_NDIM(arr) != 1) ereport(ERROR, @@ -212,7 +218,18 @@ ArrayGetTypmods(ArrayType *arr, int *n) (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("typmod array must not contain nulls"))); - *n = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr)); + /* hardwired knowledge about cstring's representation details here */ + deconstruct_array(arr, CSTRINGOID, + -2, false, 'c', + &elem_values, NULL, n); + + result = (int32 *) palloc(*n * sizeof(int32)); + + for (i = 0; i < *n; i++) + result[i] = pg_atoi(DatumGetCString(elem_values[i]), + sizeof(int32), '\0'); + + pfree(elem_values); - return (int32 *) ARR_DATA_PTR(arr); + return result; } -- cgit v1.2.3