diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2007-06-15 20:56:52 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2007-06-15 20:56:52 +0000 |
commit | 23347231a53bc373710db71559a194d87f60a7cb (patch) | |
tree | 04c6e8af0afb299a0a112da5c92124a377d1abb3 /src/backend/utils/adt/arrayutils.c | |
parent | 839fcc9fd09452c406b67fb2e1af87d55d0ad4e2 (diff) | |
download | postgresql-23347231a53bc373710db71559a194d87f60a7cb.tar.gz postgresql-23347231a53bc373710db71559a194d87f60a7cb.zip |
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.
Diffstat (limited to 'src/backend/utils/adt/arrayutils.c')
-rw-r--r-- | src/backend/utils/adt/arrayutils.c | 33 |
1 files changed, 25 insertions, 8 deletions
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; } |