1996-08-19 06:14:33 +08:00
|
|
|
/*
|
|
|
|
* array_iterator.c --
|
|
|
|
*
|
1997-11-06 05:38:25 +08:00
|
|
|
* This file defines a new class of operators which take an
|
1996-08-19 06:14:33 +08:00
|
|
|
* array and a scalar value, iterate a scalar operator over the
|
|
|
|
* elements of the array and the value and compute a result as
|
1997-11-06 05:38:25 +08:00
|
|
|
* the logical OR or AND of the iteration results.
|
1996-08-19 06:14:33 +08:00
|
|
|
*
|
1999-09-28 04:04:14 +08:00
|
|
|
* Copyright (C) 1999, Massimo Dal Zotto <dz@cs.unitn.it>
|
1999-01-22 06:40:16 +08:00
|
|
|
* ported to postgreSQL 6.3.2,added oid_functions, 18.1.1999,
|
|
|
|
* Tobias Gabele <gabele@wiz.uni-kassel.de>
|
1999-09-28 04:04:14 +08:00
|
|
|
*
|
|
|
|
* This software is distributed under the GNU General Public License
|
|
|
|
* either version 2, or (at your option) any later version.
|
1996-08-19 06:14:33 +08:00
|
|
|
*/
|
|
|
|
|
2000-11-17 06:30:52 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1996-08-19 06:14:33 +08:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2000-12-28 07:59:14 +08:00
|
|
|
#include "access/tupmacs.h"
|
1996-08-19 06:14:33 +08:00
|
|
|
#include "access/xact.h"
|
1999-01-22 06:40:16 +08:00
|
|
|
#include "fmgr.h"
|
2000-11-17 06:30:52 +08:00
|
|
|
#include "miscadmin.h"
|
1997-11-06 05:38:25 +08:00
|
|
|
#include "utils/array.h"
|
1996-08-19 06:14:33 +08:00
|
|
|
#include "utils/builtins.h"
|
1997-11-06 05:38:25 +08:00
|
|
|
#include "utils/memutils.h"
|
2000-11-17 06:30:52 +08:00
|
|
|
#include "utils/lsyscache.h"
|
1997-11-06 05:38:25 +08:00
|
|
|
|
|
|
|
#include "array_iterator.h"
|
1996-08-19 06:14:33 +08:00
|
|
|
|
2000-11-17 06:30:52 +08:00
|
|
|
|
1999-03-16 11:09:51 +08:00
|
|
|
static int32
|
1997-09-09 04:59:27 +08:00
|
|
|
array_iterator(Oid elemtype, Oid proc, int and, ArrayType *array, Datum value)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
2000-11-17 06:30:52 +08:00
|
|
|
int16 typlen;
|
1998-02-26 12:46:47 +08:00
|
|
|
bool typbyval;
|
|
|
|
int nitems,
|
2000-05-30 04:30:11 +08:00
|
|
|
i;
|
|
|
|
Datum result;
|
1998-02-26 12:46:47 +08:00
|
|
|
int ndim,
|
|
|
|
*dim;
|
|
|
|
char *p;
|
2000-05-30 04:30:11 +08:00
|
|
|
FmgrInfo finfo;
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
/* Sanity checks */
|
2000-07-22 11:34:43 +08:00
|
|
|
if (array == (ArrayType *) NULL)
|
1998-02-26 12:46:47 +08:00
|
|
|
{
|
|
|
|
/* elog(NOTICE, "array_iterator: array is null"); */
|
|
|
|
return (0);
|
|
|
|
}
|
2000-07-22 11:34:43 +08:00
|
|
|
|
|
|
|
/* detoast input if necessary */
|
|
|
|
array = DatumGetArrayTypeP(PointerGetDatum(array));
|
|
|
|
|
1998-02-26 12:46:47 +08:00
|
|
|
ndim = ARR_NDIM(array);
|
|
|
|
dim = ARR_DIMS(array);
|
2000-07-22 11:34:43 +08:00
|
|
|
nitems = ArrayGetNItems(ndim, dim);
|
1998-02-26 12:46:47 +08:00
|
|
|
if (nitems == 0)
|
|
|
|
{
|
|
|
|
/* elog(NOTICE, "array_iterator: nitems = 0"); */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup element type information */
|
2000-11-17 06:30:52 +08:00
|
|
|
get_typlenbyval(elemtype, &typlen, &typbyval);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
/* Lookup the function entry point */
|
2000-05-30 04:30:11 +08:00
|
|
|
fmgr_info(proc, &finfo);
|
|
|
|
if (finfo.fn_nargs != 2)
|
1998-02-26 12:46:47 +08:00
|
|
|
{
|
2000-05-30 04:30:11 +08:00
|
|
|
elog(ERROR, "array_iterator: proc %u does not take 2 args", proc);
|
1997-09-07 13:04:48 +08:00
|
|
|
return (0);
|
|
|
|
}
|
1997-11-06 05:38:25 +08:00
|
|
|
|
1998-02-26 12:46:47 +08:00
|
|
|
/* Scan the array and apply the operator to each element */
|
2000-05-30 04:30:11 +08:00
|
|
|
result = BoolGetDatum(false);
|
1998-02-26 12:46:47 +08:00
|
|
|
p = ARR_DATA_PTR(array);
|
|
|
|
for (i = 0; i < nitems; i++)
|
|
|
|
{
|
2000-12-28 07:59:14 +08:00
|
|
|
Datum itemvalue;
|
|
|
|
|
|
|
|
itemvalue = fetch_att(p, typbyval, typlen);
|
|
|
|
|
|
|
|
if (typlen > 0)
|
1998-02-26 12:46:47 +08:00
|
|
|
p += typlen;
|
|
|
|
else
|
2000-12-28 07:59:14 +08:00
|
|
|
p += INTALIGN(*(int32 *) p);
|
|
|
|
|
|
|
|
result = FunctionCall2(&finfo, itemvalue, value);
|
|
|
|
|
2000-05-30 04:30:11 +08:00
|
|
|
if (DatumGetBool(result))
|
1998-02-26 12:46:47 +08:00
|
|
|
{
|
|
|
|
if (!and)
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (and)
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-30 04:30:11 +08:00
|
|
|
if (and && DatumGetBool(result))
|
1998-02-26 12:46:47 +08:00
|
|
|
return (1);
|
|
|
|
else
|
|
|
|
return (0);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1997-11-06 05:38:25 +08:00
|
|
|
* Iterator functions for type _text
|
1996-08-19 06:14:33 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
int32
|
1998-02-26 12:46:47 +08:00
|
|
|
array_texteq(ArrayType *array, char *value)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return array_iterator((Oid) 25, /* text */
|
|
|
|
(Oid) 67, /* texteq */
|
2000-04-13 01:17:23 +08:00
|
|
|
0, /* logical or */
|
1998-02-26 12:46:47 +08:00
|
|
|
array, (Datum) value);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
1998-02-26 12:46:47 +08:00
|
|
|
array_all_texteq(ArrayType *array, char *value)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return array_iterator((Oid) 25, /* text */
|
|
|
|
(Oid) 67, /* texteq */
|
2000-04-13 01:17:23 +08:00
|
|
|
1, /* logical and */
|
1998-02-26 12:46:47 +08:00
|
|
|
array, (Datum) value);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
1998-02-26 12:46:47 +08:00
|
|
|
array_textregexeq(ArrayType *array, char *value)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return array_iterator((Oid) 25, /* text */
|
|
|
|
(Oid) 1254, /* textregexeq */
|
2000-04-13 01:17:23 +08:00
|
|
|
0, /* logical or */
|
1998-02-26 12:46:47 +08:00
|
|
|
array, (Datum) value);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
1998-02-26 12:46:47 +08:00
|
|
|
array_all_textregexeq(ArrayType *array, char *value)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return array_iterator((Oid) 25, /* text */
|
|
|
|
(Oid) 1254, /* textregexeq */
|
2000-04-13 01:17:23 +08:00
|
|
|
1, /* logical and */
|
1998-02-26 12:46:47 +08:00
|
|
|
array, (Datum) value);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
1999-01-22 06:40:16 +08:00
|
|
|
/*
|
1999-06-06 03:09:48 +08:00
|
|
|
* Iterator functions for type _varchar. Note that the regexp
|
1999-01-22 06:40:16 +08:00
|
|
|
* operators take the second argument of type text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int32
|
1999-06-06 03:09:48 +08:00
|
|
|
array_varchareq(ArrayType *array, char *value)
|
1999-01-22 06:40:16 +08:00
|
|
|
{
|
2000-02-14 02:59:53 +08:00
|
|
|
return array_iterator((Oid) 1043, /* varchar */
|
1999-06-06 03:09:48 +08:00
|
|
|
(Oid) 1070, /* varchareq */
|
2000-04-13 01:17:23 +08:00
|
|
|
0, /* logical or */
|
1999-01-22 06:40:16 +08:00
|
|
|
array, (Datum) value);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
1999-06-06 03:09:48 +08:00
|
|
|
array_all_varchareq(ArrayType *array, char *value)
|
1999-01-22 06:40:16 +08:00
|
|
|
{
|
2000-02-14 02:59:53 +08:00
|
|
|
return array_iterator((Oid) 1043, /* varchar */
|
1999-06-06 03:09:48 +08:00
|
|
|
(Oid) 1070, /* varchareq */
|
2000-04-13 01:17:23 +08:00
|
|
|
1, /* logical and */
|
1999-01-22 06:40:16 +08:00
|
|
|
array, (Datum) value);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
1999-06-06 03:09:48 +08:00
|
|
|
array_varcharregexeq(ArrayType *array, char *value)
|
1999-01-22 06:40:16 +08:00
|
|
|
{
|
2000-02-14 02:59:53 +08:00
|
|
|
return array_iterator((Oid) 1043, /* varchar */
|
1999-06-06 03:09:48 +08:00
|
|
|
(Oid) 1254, /* textregexeq */
|
2000-04-13 01:17:23 +08:00
|
|
|
0, /* logical or */
|
1999-06-06 03:09:48 +08:00
|
|
|
array, (Datum) value);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
|
|
|
array_all_varcharregexeq(ArrayType *array, char *value)
|
|
|
|
{
|
2000-02-14 02:59:53 +08:00
|
|
|
return array_iterator((Oid) 1043, /* varchar */
|
1999-06-06 03:09:48 +08:00
|
|
|
(Oid) 1254, /* textregexeq */
|
2000-04-13 01:17:23 +08:00
|
|
|
1, /* logical and */
|
1999-06-06 03:09:48 +08:00
|
|
|
array, (Datum) value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterator functions for type _bpchar. Note that the regexp
|
|
|
|
* operators take the second argument of type text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int32
|
|
|
|
array_bpchareq(ArrayType *array, char *value)
|
|
|
|
{
|
2000-02-14 02:59:53 +08:00
|
|
|
return array_iterator((Oid) 1042, /* bpchar */
|
1999-06-06 03:09:48 +08:00
|
|
|
(Oid) 1048, /* bpchareq */
|
2000-04-13 01:17:23 +08:00
|
|
|
0, /* logical or */
|
1999-06-06 03:09:48 +08:00
|
|
|
array, (Datum) value);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
|
|
|
array_all_bpchareq(ArrayType *array, char *value)
|
|
|
|
{
|
2000-02-14 02:59:53 +08:00
|
|
|
return array_iterator((Oid) 1042, /* bpchar */
|
1999-06-06 03:09:48 +08:00
|
|
|
(Oid) 1048, /* bpchareq */
|
2000-04-13 01:17:23 +08:00
|
|
|
1, /* logical and */
|
1999-06-06 03:09:48 +08:00
|
|
|
array, (Datum) value);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
|
|
|
array_bpcharregexeq(ArrayType *array, char *value)
|
|
|
|
{
|
2000-02-14 02:59:53 +08:00
|
|
|
return array_iterator((Oid) 1042, /* bpchar */
|
1999-06-06 03:09:48 +08:00
|
|
|
(Oid) 1254, /* textregexeq */
|
2000-04-13 01:17:23 +08:00
|
|
|
0, /* logical or */
|
1999-01-22 06:40:16 +08:00
|
|
|
array, (Datum) value);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
1999-06-06 03:09:48 +08:00
|
|
|
array_all_bpcharregexeq(ArrayType *array, char *value)
|
1999-01-22 06:40:16 +08:00
|
|
|
{
|
2000-02-14 02:59:53 +08:00
|
|
|
return array_iterator((Oid) 1042, /* bpchar */
|
1999-06-06 03:09:48 +08:00
|
|
|
(Oid) 1254, /* textregexeq */
|
2000-04-13 01:17:23 +08:00
|
|
|
1, /* logical and */
|
1999-01-22 06:40:16 +08:00
|
|
|
array, (Datum) value);
|
|
|
|
}
|
|
|
|
|
1996-08-19 06:14:33 +08:00
|
|
|
/*
|
1997-11-06 05:38:25 +08:00
|
|
|
* Iterator functions for type _int4
|
1996-08-19 06:14:33 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
int32
|
1997-09-09 04:59:27 +08:00
|
|
|
array_int4eq(ArrayType *array, int4 value)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return array_iterator((Oid) 23, /* int4 */
|
|
|
|
(Oid) 65, /* int4eq */
|
2000-04-13 01:17:23 +08:00
|
|
|
0, /* logical or */
|
1998-02-26 12:46:47 +08:00
|
|
|
array, (Datum) value);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
1997-09-09 04:59:27 +08:00
|
|
|
array_all_int4eq(ArrayType *array, int4 value)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return array_iterator((Oid) 23, /* int4 */
|
|
|
|
(Oid) 65, /* int4eq */
|
2000-04-13 01:17:23 +08:00
|
|
|
1, /* logical and */
|
1998-02-26 12:46:47 +08:00
|
|
|
array, (Datum) value);
|
1997-11-06 05:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
|
|
|
array_int4ne(ArrayType *array, int4 value)
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return array_iterator((Oid) 23, /* int4 */
|
|
|
|
(Oid) 144, /* int4ne */
|
2000-04-13 01:17:23 +08:00
|
|
|
0, /* logical or */
|
1998-02-26 12:46:47 +08:00
|
|
|
array, (Datum) value);
|
1997-11-06 05:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
|
|
|
array_all_int4ne(ArrayType *array, int4 value)
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return array_iterator((Oid) 23, /* int4 */
|
|
|
|
(Oid) 144, /* int4ne */
|
2000-04-13 01:17:23 +08:00
|
|
|
1, /* logical and */
|
1998-02-26 12:46:47 +08:00
|
|
|
array, (Datum) value);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
1997-09-09 04:59:27 +08:00
|
|
|
array_int4gt(ArrayType *array, int4 value)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return array_iterator((Oid) 23, /* int4 */
|
|
|
|
(Oid) 147, /* int4gt */
|
2000-04-13 01:17:23 +08:00
|
|
|
0, /* logical or */
|
1998-02-26 12:46:47 +08:00
|
|
|
array, (Datum) value);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
1997-09-09 04:59:27 +08:00
|
|
|
array_all_int4gt(ArrayType *array, int4 value)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return array_iterator((Oid) 23, /* int4 */
|
|
|
|
(Oid) 147, /* int4gt */
|
2000-04-13 01:17:23 +08:00
|
|
|
1, /* logical and */
|
1998-02-26 12:46:47 +08:00
|
|
|
array, (Datum) value);
|
1997-11-06 05:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
|
|
|
array_int4ge(ArrayType *array, int4 value)
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return array_iterator((Oid) 23, /* int4 */
|
|
|
|
(Oid) 150, /* int4ge */
|
2000-04-13 01:17:23 +08:00
|
|
|
0, /* logical or */
|
1998-02-26 12:46:47 +08:00
|
|
|
array, (Datum) value);
|
1997-11-06 05:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
|
|
|
array_all_int4ge(ArrayType *array, int4 value)
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return array_iterator((Oid) 23, /* int4 */
|
|
|
|
(Oid) 150, /* int4ge */
|
2000-04-13 01:17:23 +08:00
|
|
|
1, /* logical and */
|
1998-02-26 12:46:47 +08:00
|
|
|
array, (Datum) value);
|
1997-11-06 05:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
|
|
|
array_int4lt(ArrayType *array, int4 value)
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return array_iterator((Oid) 23, /* int4 */
|
|
|
|
(Oid) 66, /* int4lt */
|
2000-04-13 01:17:23 +08:00
|
|
|
0, /* logical or */
|
1998-02-26 12:46:47 +08:00
|
|
|
array, (Datum) value);
|
1997-11-06 05:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
|
|
|
array_all_int4lt(ArrayType *array, int4 value)
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return array_iterator((Oid) 23, /* int4 */
|
|
|
|
(Oid) 66, /* int4lt */
|
2000-04-13 01:17:23 +08:00
|
|
|
1, /* logical and */
|
1998-02-26 12:46:47 +08:00
|
|
|
array, (Datum) value);
|
1997-11-06 05:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
|
|
|
array_int4le(ArrayType *array, int4 value)
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return array_iterator((Oid) 23, /* int4 */
|
|
|
|
(Oid) 149, /* int4le */
|
2000-04-13 01:17:23 +08:00
|
|
|
0, /* logical or */
|
1998-02-26 12:46:47 +08:00
|
|
|
array, (Datum) value);
|
1997-11-06 05:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
|
|
|
array_all_int4le(ArrayType *array, int4 value)
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return array_iterator((Oid) 23, /* int4 */
|
|
|
|
(Oid) 149, /* int4le */
|
2000-04-13 01:17:23 +08:00
|
|
|
1, /* logical and */
|
1998-02-26 12:46:47 +08:00
|
|
|
array, (Datum) value);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
1997-11-06 05:38:25 +08:00
|
|
|
|
1999-01-22 06:40:16 +08:00
|
|
|
/* new tobias gabele 1999 */
|
1998-08-31 03:37:51 +08:00
|
|
|
|
1999-01-22 06:40:16 +08:00
|
|
|
int32
|
|
|
|
array_oideq(ArrayType *array, Oid value)
|
|
|
|
{
|
|
|
|
return array_iterator((Oid) 26, /* oid */
|
1999-05-26 00:15:34 +08:00
|
|
|
(Oid) 184, /* oideq */
|
2000-04-13 01:17:23 +08:00
|
|
|
0, /* logical or */
|
1999-01-22 06:40:16 +08:00
|
|
|
array, (Datum) value);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
|
|
|
array_all_oidne(ArrayType *array, Oid value)
|
|
|
|
{
|
|
|
|
return array_iterator((Oid) 26, /* int4 */
|
|
|
|
(Oid) 185, /* oidne */
|
2000-04-13 01:17:23 +08:00
|
|
|
1, /* logical and */
|
1999-01-22 06:40:16 +08:00
|
|
|
array, (Datum) value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* end of file */
|
1999-06-06 03:09:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local Variables:
|
2000-04-13 01:17:23 +08:00
|
|
|
* tab-width: 4
|
|
|
|
* c-indent-level: 4
|
|
|
|
* c-basic-offset: 4
|
1999-06-06 03:09:48 +08:00
|
|
|
* End:
|
|
|
|
*/
|