Skip to content

Commit fef731d

Browse files
committed
The "Allow easy display of usernames in a group (pg_hba.conf uses groups
now)" item on the open items, and subsequent plpgsql function I sent in, made me realize it was too hard to get the upper and lower bound of an array. The attached creates two functions that I think will be very useful when combined with the ability of plpgsql to return sets. array_lower(array, dim_num) - and - array_upper(array, dim_num) They return the value (as an int) of the upper and lower bound of the requested dim in the provided array. Joe Conway
1 parent 7eb2b4b commit fef731d

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

src/backend/utils/adt/arrayfuncs.c

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.81 2002/09/18 21:35:22 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.82 2002/11/08 17:27:02 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -866,6 +866,65 @@ array_dims(PG_FUNCTION_ARGS)
866866
PG_RETURN_TEXT_P(result);
867867
}
868868

869+
/*-----------------------------------------------------------------------------
870+
* array_lower :
871+
* returns the lower dimension, of the DIM requested, for
872+
* the array pointed to by "v", as an int4
873+
*----------------------------------------------------------------------------
874+
*/
875+
Datum
876+
array_lower(PG_FUNCTION_ARGS)
877+
{
878+
ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
879+
int reqdim = PG_GETARG_INT32(1);
880+
int *lb;
881+
int result;
882+
883+
/* Sanity check: does it look like an array at all? */
884+
if (ARR_NDIM(v) <= 0 || ARR_NDIM(v) > MAXDIM)
885+
PG_RETURN_NULL();
886+
887+
/* Sanity check: was the requested dim valid */
888+
if (reqdim <= 0 || reqdim > ARR_NDIM(v))
889+
PG_RETURN_NULL();
890+
891+
lb = ARR_LBOUND(v);
892+
result = lb[reqdim - 1];
893+
894+
PG_RETURN_INT32(result);
895+
}
896+
897+
/*-----------------------------------------------------------------------------
898+
* array_upper :
899+
* returns the upper dimension, of the DIM requested, for
900+
* the array pointed to by "v", as an int4
901+
*----------------------------------------------------------------------------
902+
*/
903+
Datum
904+
array_upper(PG_FUNCTION_ARGS)
905+
{
906+
ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
907+
int reqdim = PG_GETARG_INT32(1);
908+
int *dimv,
909+
*lb;
910+
int result;
911+
912+
/* Sanity check: does it look like an array at all? */
913+
if (ARR_NDIM(v) <= 0 || ARR_NDIM(v) > MAXDIM)
914+
PG_RETURN_NULL();
915+
916+
/* Sanity check: was the requested dim valid */
917+
if (reqdim <= 0 || reqdim > ARR_NDIM(v))
918+
PG_RETURN_NULL();
919+
920+
lb = ARR_LBOUND(v);
921+
dimv = ARR_DIMS(v);
922+
923+
result = dimv[reqdim - 1] + lb[reqdim - 1] - 1;
924+
925+
PG_RETURN_INT32(result);
926+
}
927+
869928
/*---------------------------------------------------------------------------
870929
* array_ref :
871930
* This routine takes an array pointer and an index array and returns

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $Id: catversion.h,v 1.163 2002/11/02 18:41:22 tgl Exp $
40+
* $Id: catversion.h,v 1.164 2002/11/08 17:27:03 momjian Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200211021
56+
#define CATALOG_VERSION_NO 200211081
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: pg_proc.h,v 1.275 2002/11/02 18:41:22 tgl Exp $
10+
* $Id: pg_proc.h,v 1.276 2002/11/08 17:27:03 momjian Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -1000,6 +1000,10 @@ DATA(insert OID = 750 ( array_in PGNSP PGUID 12 f f t f s 3 2277 "2275 26 2
10001000
DESCR("array");
10011001
DATA(insert OID = 751 ( array_out PGNSP PGUID 12 f f t f s 1 2275 "2277" array_out - _null_ ));
10021002
DESCR("array");
1003+
DATA(insert OID = 2091 ( array_lower PGNSP PGUID 12 f f t f i 2 23 "2277 23" array_lower - _null_ ));
1004+
DESCR("array lower dimension");
1005+
DATA(insert OID = 2092 ( array_upper PGNSP PGUID 12 f f t f i 2 23 "2277 23" array_upper - _null_ ));
1006+
DESCR("array upper dimension");
10031007

10041008
DATA(insert OID = 760 ( smgrin PGNSP PGUID 12 f f t f s 1 210 "2275" smgrin - _null_ ));
10051009
DESCR("storage manager(internal)");

src/include/utils/array.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
1111
* Portions Copyright (c) 1994, Regents of the University of California
1212
*
13-
* $Id: array.h,v 1.35 2002/09/18 21:35:24 tgl Exp $
13+
* $Id: array.h,v 1.36 2002/11/08 17:27:03 momjian Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -85,6 +85,8 @@ extern Datum array_out(PG_FUNCTION_ARGS);
8585
extern Datum array_length_coerce(PG_FUNCTION_ARGS);
8686
extern Datum array_eq(PG_FUNCTION_ARGS);
8787
extern Datum array_dims(PG_FUNCTION_ARGS);
88+
extern Datum array_lower(PG_FUNCTION_ARGS);
89+
extern Datum array_upper(PG_FUNCTION_ARGS);
8890

8991
extern Datum array_ref(ArrayType *array, int nSubscripts, int *indx,
9092
int arraylen, int elmlen, bool elmbyval, char elmalign,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy