1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-12 05:01:15 +03:00

Another batch of fmgr updates. I think I have gotten all old-style

functions that take pass-by-value datatypes.  Should be ready for
port testing ...
This commit is contained in:
Tom Lane
2000-06-13 07:35:40 +00:00
parent 8f057d971d
commit f2d1205322
51 changed files with 1640 additions and 1592 deletions

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.56 2000/06/09 01:11:08 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.57 2000/06/13 07:35:03 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -68,7 +68,7 @@ static void _LOArrayRange(int *st, int *endp, int bsize, int srcfd,
int destfd, ArrayType *array, int isSrcLO, bool *isNull);
static void _ReadArray(int *st, int *endp, int bsize, int srcfd, int destfd,
ArrayType *array, int isDestLO, bool *isNull);
static int ArrayCastAndSet(char *src, bool typbyval, int typlen, char *dest);
static int ArrayCastAndSet(Datum src, bool typbyval, int typlen, char *dest);
static int SanityCheckInput(int ndim, int n, int *dim, int *lb, int *indx);
static int array_read(char *destptr, int eltsize, int nitems, char *srcptr);
static char *array_seek(char *ptr, int eltsize, int nitems);
@@ -76,16 +76,17 @@ static char *array_seek(char *ptr, int eltsize, int nitems);
/*---------------------------------------------------------------------
* array_in :
* converts an array from the external format in "string" to
* it internal format.
* its internal format.
* return value :
* the internal representation of the input array
*--------------------------------------------------------------------
*/
char *
array_in(char *string, /* input array in external form */
Oid element_type, /* type OID of an array element */
int32 typmod)
Datum
array_in(PG_FUNCTION_ARGS)
{
char *string = PG_GETARG_CSTRING(0); /* external form */
Oid element_type = PG_GETARG_OID(1); /* type of an array element */
int32 typmod = PG_GETARG_INT32(2); /* typmod for array elements */
int typlen;
bool typbyval,
done;
@@ -101,7 +102,7 @@ array_in(char *string, /* input array in external form */
nitems;
int32 nbytes;
char *dataPtr;
ArrayType *retval = NULL;
ArrayType *retval;
int ndim,
dim[MAXDIM],
lBound[MAXDIM];
@@ -183,11 +184,10 @@ array_in(char *string, /* input array in external form */
nitems = getNitems(ndim, dim);
if (nitems == 0)
{
char *emptyArray = palloc(sizeof(ArrayType));
MemSet(emptyArray, 0, sizeof(ArrayType));
*(int32 *) emptyArray = sizeof(ArrayType);
return emptyArray;
retval = (ArrayType *) palloc(sizeof(ArrayType));
MemSet(retval, 0, sizeof(ArrayType));
*(int32 *) retval = sizeof(ArrayType);
return PointerGetDatum(retval);
}
if (*p == '{')
@@ -235,9 +235,10 @@ array_in(char *string, /* input array in external form */
memmove(ARR_DATA_PTR(retval), dataPtr, bytes);
#endif
elog(ERROR, "large object arrays not supported");
PG_RETURN_NULL();
}
pfree(string_save);
return (char *) retval;
return PointerGetDatum(retval);
}
/*-----------------------------------------------------------------------------
@@ -578,7 +579,7 @@ _CopyArrayEls(char **values,
{
int inc;
inc = ArrayCastAndSet(values[i], typbyval, typlen, p);
inc = ArrayCastAndSet((Datum) values[i], typbyval, typlen, p);
p += inc;
if (!typbyval)
pfree(values[i]);
@@ -592,9 +593,11 @@ _CopyArrayEls(char **values,
* containing the array in its external format.
*-------------------------------------------------------------------------
*/
char *
array_out(ArrayType *v, Oid element_type)
Datum
array_out(PG_FUNCTION_ARGS)
{
ArrayType *v = (ArrayType *) PG_GETARG_VARLENA_P(0);
Oid element_type = PG_GETARG_OID(1);
int typlen;
bool typbyval;
char typdelim;
@@ -602,7 +605,6 @@ array_out(ArrayType *v, Oid element_type)
typelem;
FmgrInfo outputproc;
char typalign;
char *p,
*tmp,
*retval,
@@ -617,30 +619,31 @@ array_out(ArrayType *v, Oid element_type)
l,
#endif
indx[MAXDIM];
bool dummy_bool;
int ndim,
*dim;
if (v == (ArrayType *) NULL)
return (char *) NULL;
PG_RETURN_CSTRING((char *) NULL);
if (ARR_IS_LO(v) == true)
{
char *p,
*save_p;
int nbytes;
text *p;
int plen,
nbytes;
p = (text *) DatumGetPointer(DirectFunctionCall1(array_dims,
PointerGetDatum(v)));
plen = VARSIZE(p) - VARHDRSZ;
/* get a wide string to print to */
p = array_dims(v, &dummy_bool);
nbytes = strlen(ARR_DATA_PTR(v)) + VARHDRSZ + *(int *) p;
nbytes = strlen(ARR_DATA_PTR(v)) + strlen(ASSGN) + plen + 1;
retval = (char *) palloc(nbytes);
save_p = (char *) palloc(nbytes);
strcpy(save_p, p + sizeof(int));
strcat(save_p, ASSGN);
strcat(save_p, ARR_DATA_PTR(v));
memcpy(retval, VARDATA(p), plen);
strcpy(retval + plen, ASSGN);
strcat(retval, ARR_DATA_PTR(v));
pfree(p);
return save_p;
PG_RETURN_CSTRING(retval);
}
system_cache_lookup(element_type, false, &typlen, &typbyval,
@@ -653,12 +656,11 @@ array_out(ArrayType *v, Oid element_type)
if (nitems == 0)
{
char *emptyArray = palloc(3);
emptyArray[0] = '{';
emptyArray[1] = '}';
emptyArray[2] = '\0';
return emptyArray;
retval = (char *) palloc(3);
retval[0] = '{';
retval[1] = '}';
retval[2] = '\0';
PG_RETURN_CSTRING(retval);
}
p = ARR_DATA_PTR(v);
@@ -776,58 +778,61 @@ array_out(ArrayType *v, Oid element_type)
} while (j != -1);
pfree(values);
return retval;
PG_RETURN_CSTRING(retval);
}
/*-----------------------------------------------------------------------------
* array_dims :
* returns the dimension of the array pointed to by "v"
* returns the dimensions of the array pointed to by "v", as a "text"
*----------------------------------------------------------------------------
*/
char *
array_dims(ArrayType *v, bool *isNull)
Datum
array_dims(PG_FUNCTION_ARGS)
{
char *p,
*save_p;
ArrayType *v = (ArrayType *) PG_GETARG_VARLENA_P(0);
text *result;
char *p;
int nbytes,
i;
int *dimv,
*lb;
if (v == (ArrayType *) NULL)
RETURN_NULL;
nbytes = ARR_NDIM(v) * 33;
nbytes = ARR_NDIM(v) * 33 + 1;
/*
* 33 since we assume 15 digits per number + ':' +'[]'
*
* +1 allows for temp trailing null
*/
save_p = p = (char *) palloc(nbytes + VARHDRSZ);
MemSet(save_p, 0, nbytes + VARHDRSZ);
result = (text *) palloc(nbytes + VARHDRSZ);
MemSet(result, 0, nbytes + VARHDRSZ);
p = VARDATA(result);
dimv = ARR_DIMS(v);
lb = ARR_LBOUND(v);
p += VARHDRSZ;
for (i = 0; i < ARR_NDIM(v); i++)
{
sprintf(p, "[%d:%d]", lb[i], dimv[i] + lb[i] - 1);
p += strlen(p);
}
nbytes = strlen(save_p + VARHDRSZ) + VARHDRSZ;
memmove(save_p, &nbytes, VARHDRSZ);
return save_p;
VARSIZE(result) = strlen(VARDATA(result)) + VARHDRSZ;
PG_RETURN_TEXT_P(result);
}
/*---------------------------------------------------------------------------
* array_ref :
* This routing takes an array pointer and an index array and returns
* This routine takes an array pointer and an index array and returns
* a pointer to the referred element if element is passed by
* reference otherwise returns the value of the referred element.
*---------------------------------------------------------------------------
*/
Datum
array_ref(ArrayType *array,
int n,
int nSubscripts,
int *indx,
int reftype,
bool elmbyval,
int elmlen,
int arraylen,
bool *isNull)
@@ -839,10 +844,11 @@ array_ref(ArrayType *array,
offset,
nbytes;
struct varlena *v = NULL;
char *retval = NULL;
Datum result;
char *retval;
if (array == (ArrayType *) NULL)
RETURN_NULL;
RETURN_NULL(Datum);
if (arraylen > 0)
{
@@ -852,17 +858,17 @@ array_ref(ArrayType *array,
if (indx[0] * elmlen > arraylen)
elog(ERROR, "array_ref: array bound exceeded");
retval = (char *) array + indx[0] * elmlen;
return _ArrayCast(retval, reftype, elmlen);
return _ArrayCast(retval, elmbyval, elmlen);
}
dim = ARR_DIMS(array);
lb = ARR_LBOUND(array);
ndim = ARR_NDIM(array);
nbytes = (*(int32 *) array) - ARR_OVERHEAD(ndim);
if (!SanityCheckInput(ndim, n, dim, lb, indx))
RETURN_NULL;
if (!SanityCheckInput(ndim, nSubscripts, dim, lb, indx))
RETURN_NULL(Datum);
offset = GetOffset(n, dim, lb, indx);
offset = GetOffset(nSubscripts, dim, lb, indx);
if (ARR_IS_LO(array))
{
@@ -874,7 +880,7 @@ array_ref(ArrayType *array,
lo_name = (char *) ARR_DATA_PTR(array);
#ifdef LOARRAY
if ((fd = LOopen(lo_name, ARR_IS_INV(array) ? INV_READ : O_RDONLY)) < 0)
RETURN_NULL;
RETURN_NULL(Datum);
#endif
if (ARR_IS_CHUNKED(array))
v = _ReadChunkArray1El(indx, elmlen, fd, array, isNull);
@@ -884,7 +890,7 @@ array_ref(ArrayType *array,
Int32GetDatum(fd),
Int32GetDatum(offset),
Int32GetDatum(SEEK_SET))) < 0)
RETURN_NULL;
RETURN_NULL(Datum);
#ifdef LOARRAY
v = (struct varlena *)
DatumGetPointer(DirectFunctionCall2(loread,
@@ -893,20 +899,20 @@ array_ref(ArrayType *array,
#endif
}
if (*isNull)
RETURN_NULL;
RETURN_NULL(Datum);
if (VARSIZE(v) - VARHDRSZ < elmlen)
RETURN_NULL;
RETURN_NULL(Datum);
DirectFunctionCall1(lo_close, Int32GetDatum(fd));
retval = (char *) _ArrayCast((char *) VARDATA(v), reftype, elmlen);
if (reftype == 0)
result = _ArrayCast((char *) VARDATA(v), elmbyval, elmlen);
if (! elmbyval)
{ /* not by value */
char *tempdata = palloc(elmlen);
memmove(tempdata, retval, elmlen);
retval = tempdata;
memmove(tempdata, DatumGetPointer(result), elmlen);
result = PointerGetDatum(tempdata);
}
pfree(v);
return (Datum) retval;
return result;
}
if (elmlen > 0)
@@ -914,32 +920,25 @@ array_ref(ArrayType *array,
offset = offset * elmlen;
/* off the end of the array */
if (nbytes - offset < 1)
RETURN_NULL;
RETURN_NULL(Datum);
retval = ARR_DATA_PTR(array) + offset;
return _ArrayCast(retval, reftype, elmlen);
return _ArrayCast(retval, elmbyval, elmlen);
}
else
{
bool done = false;
char *temp;
int bytes = nbytes;
temp = ARR_DATA_PTR(array);
retval = ARR_DATA_PTR(array);
i = 0;
while (bytes > 0 && !done)
while (bytes > 0)
{
if (i == offset)
{
retval = temp;
done = true;
}
bytes -= INTALIGN(*(int32 *) temp);
temp += INTALIGN(*(int32 *) temp);
return PointerGetDatum(retval);
bytes -= INTALIGN(*(int32 *) retval);
retval += INTALIGN(*(int32 *) retval);
i++;
}
if (!done)
RETURN_NULL;
return (Datum) retval;
RETURN_NULL(Datum);
}
}
@@ -950,13 +949,13 @@ array_ref(ArrayType *array,
* and returns a pointer to it.
*-----------------------------------------------------------------------------
*/
Datum
ArrayType *
array_clip(ArrayType *array,
int n,
int nSubscripts,
int *upperIndx,
int *lowerIndx,
int reftype,
int len,
bool elmbyval,
int elmlen,
bool *isNull)
{
int i,
@@ -970,22 +969,20 @@ array_clip(ArrayType *array,
/* timer_start(); */
if (array == (ArrayType *) NULL)
RETURN_NULL;
RETURN_NULL(ArrayType *);
dim = ARR_DIMS(array);
lb = ARR_LBOUND(array);
ndim = ARR_NDIM(array);
nbytes = (*(int32 *) array) - ARR_OVERHEAD(ndim);
if (!SanityCheckInput(ndim, n, dim, lb, upperIndx))
RETURN_NULL;
if (!SanityCheckInput(ndim, nSubscripts, dim, lb, upperIndx) ||
!SanityCheckInput(ndim, nSubscripts, dim, lb, lowerIndx))
RETURN_NULL(ArrayType *);
if (!SanityCheckInput(ndim, n, dim, lb, lowerIndx))
RETURN_NULL;
for (i = 0; i < n; i++)
for (i = 0; i < nSubscripts; i++)
if (lowerIndx[i] > upperIndx[i])
elog(ERROR, "lowerIndex cannot be larger than upperIndx");
mda_get_range(n, span, lowerIndx, upperIndx);
mda_get_range(nSubscripts, span, lowerIndx, upperIndx);
if (ARR_IS_LO(array))
{
@@ -999,23 +996,23 @@ array_clip(ArrayType *array,
isDestLO = true,
rsize;
if (len < 0)
elog(ERROR, "array_clip: array of variable length objects not supported");
if (elmlen < 0)
elog(ERROR, "array_clip: array of variable length objects not implemented");
#ifdef LOARRAY
lo_name = (char *) ARR_DATA_PTR(array);
if ((fd = LOopen(lo_name, ARR_IS_INV(array) ? INV_READ : O_RDONLY)) < 0)
RETURN_NULL;
RETURN_NULL(ArrayType *);
newname = _array_newLO(&newfd, Unix);
#endif
bytes = strlen(newname) + 1 + ARR_OVERHEAD(n);
bytes = strlen(newname) + 1 + ARR_OVERHEAD(nSubscripts);
newArr = (ArrayType *) palloc(bytes);
memmove(newArr, array, sizeof(ArrayType));
memmove(newArr, &bytes, sizeof(int));
memmove(ARR_DIMS(newArr), span, n * sizeof(int));
memmove(ARR_LBOUND(newArr), lowerIndx, n * sizeof(int));
memmove(ARR_DIMS(newArr), span, nSubscripts * sizeof(int));
memmove(ARR_LBOUND(newArr), lowerIndx, nSubscripts * sizeof(int));
strcpy(ARR_DATA_PTR(newArr), newname);
rsize = compute_size(lowerIndx, upperIndx, n, len);
rsize = compute_size(lowerIndx, upperIndx, nSubscripts, elmlen);
if (rsize < MAX_BUFF_SIZE)
{
char *buff;
@@ -1026,12 +1023,12 @@ array_clip(ArrayType *array,
isDestLO = false;
if (ARR_IS_CHUNKED(array))
{
_ReadChunkArray(lowerIndx, upperIndx, len, fd, &(buff[VARHDRSZ]),
_ReadChunkArray(lowerIndx, upperIndx, elmlen, fd, &(buff[VARHDRSZ]),
array, 0, isNull);
}
else
{
_ReadArray(lowerIndx, upperIndx, len, fd, (int) &(buff[VARHDRSZ]),
_ReadArray(lowerIndx, upperIndx, elmlen, fd, (int) &(buff[VARHDRSZ]),
array,
0, isNull);
}
@@ -1048,11 +1045,11 @@ array_clip(ArrayType *array,
{
if (ARR_IS_CHUNKED(array))
{
_ReadChunkArray(lowerIndx, upperIndx, len, fd, (char *) newfd, array,
_ReadChunkArray(lowerIndx, upperIndx, elmlen, fd, (char *) newfd, array,
1, isNull);
}
else
_ReadArray(lowerIndx, upperIndx, len, fd, newfd, array, 1, isNull);
_ReadArray(lowerIndx, upperIndx, elmlen, fd, newfd, array, 1, isNull);
}
#ifdef LOARRAY
LOclose(fd);
@@ -1064,42 +1061,42 @@ array_clip(ArrayType *array,
newArr = NULL;
}
/* timer_end(); */
return (Datum) newArr;
return newArr;
}
if (len > 0)
if (elmlen > 0)
{
bytes = getNitems(n, span);
bytes = bytes * len + ARR_OVERHEAD(n);
bytes = getNitems(nSubscripts, span);
bytes = bytes * elmlen + ARR_OVERHEAD(nSubscripts);
}
else
{
bytes = _ArrayClipCount(lowerIndx, upperIndx, array);
bytes += ARR_OVERHEAD(n);
bytes += ARR_OVERHEAD(nSubscripts);
}
newArr = (ArrayType *) palloc(bytes);
memmove(newArr, array, sizeof(ArrayType));
memmove(newArr, &bytes, sizeof(int));
memmove(ARR_DIMS(newArr), span, n * sizeof(int));
memmove(ARR_LBOUND(newArr), lowerIndx, n * sizeof(int));
_ArrayRange(lowerIndx, upperIndx, len, ARR_DATA_PTR(newArr), array, 1);
return (Datum) newArr;
memmove(ARR_DIMS(newArr), span, nSubscripts * sizeof(int));
memmove(ARR_LBOUND(newArr), lowerIndx, nSubscripts * sizeof(int));
_ArrayRange(lowerIndx, upperIndx, elmlen, ARR_DATA_PTR(newArr), array, 1);
return newArr;
}
/*-----------------------------------------------------------------------------
* array_set :
* This routine sets the value of an array location (specified by an index array)
* to a new value specified by "dataPtr".
* This routine sets the value of an array location (specified by
* an index array) to a new value specified by "dataValue".
* result :
* returns a pointer to the modified array.
*-----------------------------------------------------------------------------
*/
char *
ArrayType *
array_set(ArrayType *array,
int n,
int nSubscripts,
int *indx,
char *dataPtr,
int reftype,
Datum dataValue,
bool elmbyval,
int elmlen,
int arraylen,
bool *isNull)
@@ -1112,7 +1109,7 @@ array_set(ArrayType *array,
char *pos;
if (array == (ArrayType *) NULL)
RETURN_NULL;
RETURN_NULL(ArrayType *);
if (arraylen > 0)
{
@@ -1122,20 +1119,20 @@ array_set(ArrayType *array,
if (indx[0] * elmlen > arraylen)
elog(ERROR, "array_ref: array bound exceeded");
pos = (char *) array + indx[0] * elmlen;
ArrayCastAndSet(dataPtr, (bool) reftype, elmlen, pos);
return (char *) array;
ArrayCastAndSet(dataValue, elmbyval, elmlen, pos);
return array;
}
dim = ARR_DIMS(array);
lb = ARR_LBOUND(array);
ndim = ARR_NDIM(array);
nbytes = (*(int32 *) array) - ARR_OVERHEAD(ndim);
if (!SanityCheckInput(ndim, n, dim, lb, indx))
if (!SanityCheckInput(ndim, nSubscripts, dim, lb, indx))
{
elog(ERROR, "array_set: array bound exceeded");
return (char *) array;
return array;
}
offset = GetOffset(n, dim, lb, indx);
offset = GetOffset(nSubscripts, dim, lb, indx);
if (ARR_IS_LO(array))
{
@@ -1149,35 +1146,33 @@ array_set(ArrayType *array,
lo_name = ARR_DATA_PTR(array);
if ((fd = LOopen(lo_name, ARR_IS_INV(array) ? INV_WRITE : O_WRONLY)) < 0)
return (char *) array;
return array;
#endif
if (DatumGetInt32(DirectFunctionCall3(lo_lseek,
Int32GetDatum(fd),
Int32GetDatum(offset),
Int32GetDatum(SEEK_SET))) < 0)
return (char *) array;
return array;
v = (struct varlena *) palloc(elmlen + VARHDRSZ);
VARSIZE(v) = elmlen + VARHDRSZ;
ArrayCastAndSet(dataPtr, (bool) reftype, elmlen, VARDATA(v));
ArrayCastAndSet(dataValue, elmbyval, elmlen, VARDATA(v));
#ifdef LOARRAY
n = DatumGetInt32(DirectFunctionCall2(lowrite,
if (DatumGetInt32(DirectFunctionCall2(lowrite,
Int32GetDatum(fd),
PointerGetDatum(v)));
PointerGetDatum(v)))
!= elmlen)
RETURN_NULL(ArrayType *);
#endif
/*
* if (n < VARSIZE(v) - VARHDRSZ) RETURN_NULL;
*/
pfree(v);
DirectFunctionCall1(lo_close, Int32GetDatum(fd));
return (char *) array;
return array;
}
if (elmlen > 0)
{
offset = offset * elmlen;
/* off the end of the array */
if (nbytes - offset < 1)
return (char *) array;
return array;
pos = ARR_DATA_PTR(array) + offset;
}
else
@@ -1194,18 +1189,18 @@ array_set(ArrayType *array,
elt_ptr = array_seek(ARR_DATA_PTR(array), -1, offset);
oldlen = INTALIGN(*(int32 *) elt_ptr);
newlen = INTALIGN(*(int32 *) dataPtr);
newlen = INTALIGN(*(int32 *) DatumGetPointer(dataValue));
if (oldlen == newlen)
{
/* new element with same size, overwrite old data */
ArrayCastAndSet(dataPtr, (bool) reftype, elmlen, elt_ptr);
return (char *) array;
ArrayCastAndSet(dataValue, elmbyval, elmlen, elt_ptr);
return array;
}
/* new element with different size, reallocate the array */
oldsize = array->size;
lth0 = ARR_OVERHEAD(n);
lth0 = ARR_OVERHEAD(nSubscripts);
lth1 = (int) (elt_ptr - ARR_DATA_PTR(array));
lth2 = (int) (oldsize - lth0 - lth1 - oldlen);
newsize = lth0 + lth1 + newlen + lth2;
@@ -1213,16 +1208,16 @@ array_set(ArrayType *array,
newarray = (ArrayType *) palloc(newsize);
memmove((char *) newarray, (char *) array, lth0 + lth1);
newarray->size = newsize;
newlen = ArrayCastAndSet(dataPtr, (bool) reftype, elmlen,
newlen = ArrayCastAndSet(dataValue, elmbyval, elmlen,
(char *) newarray + lth0 + lth1);
memmove((char *) newarray + lth0 + lth1 + newlen,
(char *) array + lth0 + lth1 + oldlen, lth2);
/* ??? who should free this storage ??? */
return (char *) newarray;
return newarray;
}
ArrayCastAndSet(dataPtr, (bool) reftype, elmlen, pos);
return (char *) array;
ArrayCastAndSet(dataValue, elmbyval, elmlen, pos);
return array;
}
/*----------------------------------------------------------------------------
@@ -1234,14 +1229,14 @@ array_set(ArrayType *array,
* returns a pointer to the modified array.
*----------------------------------------------------------------------------
*/
char *
ArrayType *
array_assgn(ArrayType *array,
int n,
int nSubscripts,
int *upperIndx,
int *lowerIndx,
ArrayType *newArr,
int reftype,
int len,
bool elmbyval,
int elmlen,
bool *isNull)
{
int i,
@@ -1250,19 +1245,19 @@ array_assgn(ArrayType *array,
*lb;
if (array == (ArrayType *) NULL)
RETURN_NULL;
if (len < 0)
elog(ERROR, "array_assgn:updates on arrays of variable length elements not allowed");
RETURN_NULL(ArrayType *);
if (elmlen < 0)
elog(ERROR, "array_assgn: updates on arrays of variable length elements not implemented");
dim = ARR_DIMS(array);
lb = ARR_LBOUND(array);
ndim = ARR_NDIM(array);
if (!SanityCheckInput(ndim, n, dim, lb, upperIndx) ||
!SanityCheckInput(ndim, n, dim, lb, lowerIndx))
return (char *) array;
if (!SanityCheckInput(ndim, nSubscripts, dim, lb, upperIndx) ||
!SanityCheckInput(ndim, nSubscripts, dim, lb, lowerIndx))
RETURN_NULL(ArrayType *);
for (i = 0; i < n; i++)
for (i = 0; i < nSubscripts; i++)
if (lowerIndx[i] > upperIndx[i])
elog(ERROR, "lowerIndex larger than upperIndx");
@@ -1276,28 +1271,28 @@ array_assgn(ArrayType *array,
lo_name = (char *) ARR_DATA_PTR(array);
if ((fd = LOopen(lo_name, ARR_IS_INV(array) ? INV_WRITE : O_WRONLY)) < 0)
return (char *) array;
return array;
#endif
if (ARR_IS_LO(newArr))
{
#ifdef LOARRAY
lo_name = (char *) ARR_DATA_PTR(newArr);
if ((newfd = LOopen(lo_name, ARR_IS_INV(newArr) ? INV_READ : O_RDONLY)) < 0)
return (char *) array;
return array;
#endif
_LOArrayRange(lowerIndx, upperIndx, len, fd, newfd, array, 1, isNull);
_LOArrayRange(lowerIndx, upperIndx, elmlen, fd, newfd, array, 1, isNull);
DirectFunctionCall1(lo_close, Int32GetDatum(newfd));
}
else
{
_LOArrayRange(lowerIndx, upperIndx, len, fd, (int) ARR_DATA_PTR(newArr),
_LOArrayRange(lowerIndx, upperIndx, elmlen, fd, (int) ARR_DATA_PTR(newArr),
array, 0, isNull);
}
DirectFunctionCall1(lo_close, Int32GetDatum(fd));
return (char *) array;
return array;
}
_ArrayRange(lowerIndx, upperIndx, len, ARR_DATA_PTR(newArr), array, 0);
return (char *) array;
_ArrayRange(lowerIndx, upperIndx, elmlen, ARR_DATA_PTR(newArr), array, 0);
return array;
}
/*
@@ -1353,7 +1348,7 @@ array_map(FunctionCallInfo fcinfo, Oid inpType, Oid retType)
elog(ERROR, "array_map: invalid nargs: %d", fcinfo->nargs);
if (PG_ARGISNULL(0))
elog(ERROR, "array_map: null input array");
v = (ArrayType *) PG_GETARG_POINTER(0);
v = (ArrayType *) PG_GETARG_VARLENA_P(0);
/* Large objects not yet supported */
if (ARR_IS_LO(v) == true)
@@ -1466,19 +1461,20 @@ array_map(FunctionCallInfo fcinfo, Oid inpType, Oid retType)
* array_eq :
* compares two arrays for equality
* result :
* returns 1 if the arrays are equal, 0 otherwise.
* returns true if the arrays are equal, false otherwise.
*-----------------------------------------------------------------------------
*/
int
array_eq(ArrayType *array1, ArrayType *array2)
Datum
array_eq(PG_FUNCTION_ARGS)
{
if ((array1 == NULL) || (array2 == NULL))
return 0;
if (*(int *) array1 != *(int *) array2)
return 0;
if (memcmp(array1, array2, *(int *) array1))
return 0;
return 1;
ArrayType *array1 = (ArrayType *) PG_GETARG_VARLENA_P(0);
ArrayType *array2 = (ArrayType *) PG_GETARG_VARLENA_P(1);
if (*(int32 *) array1 != *(int32 *) array2)
PG_RETURN_BOOL(false);
if (memcmp(array1, array2, *(int32 *) array1) != 0)
PG_RETURN_BOOL(false);
PG_RETURN_BOOL(true);
}
/***************************************************************************/
@@ -1545,7 +1541,7 @@ _ArrayCast(char *value, bool byval, int len)
static int
ArrayCastAndSet(char *src,
ArrayCastAndSet(Datum src,
bool typbyval,
int typlen,
char *dest)
@@ -1565,18 +1561,18 @@ ArrayCastAndSet(char *src,
*(int16 *) dest = DatumGetInt16(src);
break;
case 4:
*(int32 *) dest = (int32) src;
*(int32 *) dest = DatumGetInt32(src);
break;
}
}
else
memmove(dest, src, typlen);
memmove(dest, DatumGetPointer(src), typlen);
inc = typlen;
}
else
{
memmove(dest, src, *(int32 *) src);
inc = (INTALIGN(*(int32 *) src));
memmove(dest, DatumGetPointer(src), *(int32 *) DatumGetPointer(src));
inc = (INTALIGN(*(int32 *) DatumGetPointer(src)));
}
return inc;
}

View File

@@ -9,7 +9,7 @@
* workings can be found in the book "Software Solutions in C" by
* Dale Schumacher, Academic Press, ISBN: 0-12-632360-7.
*
* $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.37 2000/06/05 07:28:51 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.38 2000/06/13 07:35:03 tgl Exp $
*/
#include <limits.h>
@@ -535,31 +535,31 @@ cash_div_flt4(Cash *c, float4 *f)
/* cash_mul_int4()
* Multiply cash by int4.
*/
Cash *
cash_mul_int4(Cash *c, int4 i)
Datum
cash_mul_int4(PG_FUNCTION_ARGS)
{
Cash *result;
Cash c = PG_GETARG_CASH(0);
int32 i = PG_GETARG_INT32(1);
Cash result;
if (!PointerIsValid(c))
return NULL;
if (!PointerIsValid(result = palloc(sizeof(Cash))))
elog(ERROR, "Memory allocation failed, can't multiply cash");
*result = ((i) * (*c));
return result;
} /* cash_mul_int4() */
result = c * i;
PG_RETURN_CASH(result);
}
/* int4_mul_cash()
* Multiply int4 by cash.
*/
Cash *
int4_mul_cash(int4 i, Cash *c)
Datum
int4_mul_cash(PG_FUNCTION_ARGS)
{
return cash_mul_int4(c, i);
} /* int4_mul_cash() */
int32 i = PG_GETARG_INT32(0);
Cash c = PG_GETARG_CASH(1);
Cash result;
result = i * c;
PG_RETURN_CASH(result);
}
/* cash_div_int4()
@@ -568,24 +568,20 @@ int4_mul_cash(int4 i, Cash *c)
* XXX Don't know if rounding or truncating is correct behavior.
* Round for now. - tgl 97/04/15
*/
Cash *
cash_div_int4(Cash *c, int4 i)
Datum
cash_div_int4(PG_FUNCTION_ARGS)
{
Cash *result;
if (!PointerIsValid(c))
return NULL;
if (!PointerIsValid(result = palloc(sizeof(Cash))))
elog(ERROR, "Memory allocation failed, can't divide cash");
Cash c = PG_GETARG_CASH(0);
int32 i = PG_GETARG_INT32(1);
Cash result;
if (i == 0)
elog(ERROR, "cash_idiv: divide by 0 error");
elog(ERROR, "cash_div_int4: divide by 0 error");
*result = rint(*c / i);
result = rint(c / i);
return result;
} /* cash_div_int4() */
PG_RETURN_CASH(result);
}
/* cash_mul_int2()

View File

@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/chunk.c,v 1.26 2000/06/09 01:11:08 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/chunk.c,v 1.27 2000/06/13 07:35:03 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -517,7 +517,7 @@ _ReadChunkArray(int *st,
Int32GetDatum(fp),
Int32GetDatum(srcOff),
Int32GetDatum(SEEK_SET))) < 0)
RETURN_NULL;
RETURN_NULL(int);
jj = n - 1;
for (i = 0; i < n; chunk_off[i++] = 0)
@@ -541,7 +541,7 @@ _ReadChunkArray(int *st,
Int32GetDatum((int32) destfp),
Int32GetDatum(bptr),
Int32GetDatum(SEEK_SET))) < 0)
RETURN_NULL;
RETURN_NULL(int);
}
else
destfp = baseDestFp + bptr;
@@ -556,7 +556,7 @@ _ReadChunkArray(int *st,
Int32GetDatum(fp),
Int32GetDatum(srcOff),
Int32GetDatum(SEEK_SET))) < 0)
RETURN_NULL;
RETURN_NULL(int);
}
for (i = n - 1, to_read = bsize; i >= 0;
to_read *= min(C[i], array_span[i]), i--)
@@ -571,7 +571,7 @@ _ReadChunkArray(int *st,
Int32GetDatum(fp),
Int32GetDatum(srcOff),
Int32GetDatum(SEEK_SET))) < 0)
RETURN_NULL;
RETURN_NULL(int);
}
block_seek += cdist[j];
bptr += adist[j] * bsize;
@@ -581,13 +581,13 @@ _ReadChunkArray(int *st,
Int32GetDatum((int32) destfp),
Int32GetDatum(bptr),
Int32GetDatum(SEEK_SET))) < 0)
RETURN_NULL;
RETURN_NULL(int);
}
else
destfp = baseDestFp + bptr;
temp = _LOtransfer((char **) &destfp, to_read, 1, (char **) &fp, 1, isDestLO);
if (temp < to_read)
RETURN_NULL;
RETURN_NULL(int);
srcOff += to_read;
words_read += to_read;
bptr += to_read;
@@ -702,7 +702,7 @@ _ReadChunkArray1El(int *st,
Int32GetDatum(fp),
Int32GetDatum(srcOff),
Int32GetDatum(SEEK_SET))) < 0)
RETURN_NULL;
RETURN_NULL(struct varlena *);
#ifdef LOARRAY
return (struct varlena *)
DatumGetPointer(DirectFunctionCall2(loread,

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.59 2000/06/08 22:37:28 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.60 2000/06/13 07:35:04 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -48,12 +48,11 @@
*/
#include <ctype.h>
#include <errno.h>
#include <float.h> /* faked on sunos4 */
#include <math.h>
#include "postgres.h"
#ifdef HAVE_LIMITS_H
#include <limits.h>
#ifndef MAXINT
@@ -64,6 +63,7 @@
#include <values.h>
#endif
#endif
#include "fmgr.h"
#include "utils/builtins.h"
@@ -844,15 +844,14 @@ dtoi2(PG_FUNCTION_ARGS)
/*
* i4tod - converts an int4 number to a float8 number
*/
float64
i4tod(int32 num)
Datum
i4tod(PG_FUNCTION_ARGS)
{
float64 result;
int32 num = PG_GETARG_INT32(0);
float8 result;
result = (float64) palloc(sizeof(float64data));
*result = num;
return result;
result = num;
PG_RETURN_FLOAT8(result);
}
@@ -909,15 +908,14 @@ ftoi2(PG_FUNCTION_ARGS)
/*
* i4tof - converts an int4 number to a float8 number
*/
float32
i4tof(int32 num)
Datum
i4tof(PG_FUNCTION_ARGS)
{
float32 result;
int32 num = PG_GETARG_INT32(0);
float4 result;
result = (float32) palloc(sizeof(float32data));
*result = num;
return result;
result = num;
PG_RETURN_FLOAT4(result);
}

View File

@@ -1,7 +1,7 @@
/* -----------------------------------------------------------------------
* formatting.c
*
* $Header: /cvsroot/pgsql/src/backend/utils/adt/formatting.c,v 1.11 2000/06/09 03:18:34 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/formatting.c,v 1.12 2000/06/13 07:35:04 tgl Exp $
*
*
* Portions Copyright (c) 1999-2000, PostgreSQL, Inc
@@ -3848,13 +3848,11 @@ NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, char *number,
* ----------
*/
#define NUM_TOCHAR_prepare { \
if (!PointerIsValid(fmt)) \
return NULL; \
\
len = VARSIZE(fmt) - VARHDRSZ; \
\
if (!len) \
return textin(""); \
if (len <= 0) \
return PointerGetDatum(textin("")); \
\
result = (text *) palloc( (len * NUM_MAX_ITEM_SIZ) + 1 + VARHDRSZ); \
format = NUM_cache(len, &Num, VARDATA(fmt), &flag); \
@@ -3891,26 +3889,24 @@ NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, char *number,
* NUMERIC to_number() (convert string to numeric)
* -------------------
*/
Numeric
numeric_to_number(text *value, text *fmt)
Datum
numeric_to_number(PG_FUNCTION_ARGS)
{
text *value = PG_GETARG_TEXT_P(0);
text *fmt = PG_GETARG_TEXT_P(1);
NUMDesc Num;
Numeric result;
Datum result;
FormatNode *format;
char *numstr;
int flag = 0;
int len = 0;
int scale,
precision;
if ((!PointerIsValid(value)) || (!PointerIsValid(fmt)))
return NULL;
len = VARSIZE(fmt) - VARHDRSZ;
if (!len)
return numeric_in(NULL, 0, 0);
if (len <= 0)
PG_RETURN_NULL();
format = NUM_cache(len, &Num, VARDATA(fmt), &flag);
@@ -3925,7 +3921,10 @@ numeric_to_number(text *value, text *fmt)
if (flag)
pfree(format);
result = numeric_in(numstr, 0, ((precision << 16) | scale) + VARHDRSZ);
result = DirectFunctionCall3(numeric_in,
CStringGetDatum(numstr),
ObjectIdGetDatum(InvalidOid),
Int32GetDatum(((precision << 16) | scale) + VARHDRSZ));
pfree(numstr);
return result;
}
@@ -3934,9 +3933,11 @@ numeric_to_number(text *value, text *fmt)
* NUMERIC to_char()
* ------------------
*/
text *
numeric_to_char(Numeric value, text *fmt)
Datum
numeric_to_char(PG_FUNCTION_ARGS)
{
Numeric value = PG_GETARG_NUMERIC(0);
text *fmt = PG_GETARG_TEXT_P(1);
NUMDesc Num;
FormatNode *format;
text *result,
@@ -3948,7 +3949,7 @@ numeric_to_char(Numeric value, text *fmt)
char *numstr,
*orgnum,
*p;
Numeric x = NULL;
Numeric x;
NUM_TOCHAR_prepare;
@@ -3958,10 +3959,11 @@ numeric_to_char(Numeric value, text *fmt)
*/
if (IS_ROMAN(&Num))
{
x = numeric_round(value, 0);
x = DatumGetNumeric(DirectFunctionCall2(numeric_round,
NumericGetDatum(value),
Int32GetDatum(0)));
numstr = orgnum = int_to_roman(numeric_int4(x));
pfree(x);
}
else
{
@@ -3969,8 +3971,10 @@ numeric_to_char(Numeric value, text *fmt)
if (IS_MULTI(&Num))
{
Numeric a = int4_numeric(10);
Numeric b = int4_numeric(Num.multi);
Numeric a = DatumGetNumeric(DirectFunctionCall1(int4_numeric,
Int32GetDatum(10)));
Numeric b = DatumGetNumeric(DirectFunctionCall1(int4_numeric,
Int32GetDatum(Num.multi)));
x = numeric_power(a, b);
val = numeric_mul(value, x);
@@ -3980,8 +3984,11 @@ numeric_to_char(Numeric value, text *fmt)
Num.pre += Num.multi;
}
x = numeric_round(val, Num.post);
orgnum = numeric_out(x);
x = DatumGetNumeric(DirectFunctionCall2(numeric_round,
NumericGetDatum(val),
Int32GetDatum(Num.post)));
orgnum = DatumGetCString(DirectFunctionCall1(numeric_out,
NumericGetDatum(x)));
pfree(x);
if (*orgnum == '-')
@@ -4014,16 +4021,18 @@ numeric_to_char(Numeric value, text *fmt)
}
NUM_TOCHAR_finish;
return result;
PG_RETURN_TEXT_P(result);
}
/* ---------------
* INT4 to_char()
* ---------------
*/
text *
int4_to_char(int32 value, text *fmt)
Datum
int4_to_char(PG_FUNCTION_ARGS)
{
int32 value = PG_GETARG_INT32(0);
text *fmt = PG_GETARG_TEXT_P(1);
NUMDesc Num;
FormatNode *format;
text *result,
@@ -4044,7 +4053,6 @@ int4_to_char(int32 value, text *fmt)
if (IS_ROMAN(&Num))
{
numstr = orgnum = int_to_roman(value);
}
else
{
@@ -4097,16 +4105,18 @@ int4_to_char(int32 value, text *fmt)
}
NUM_TOCHAR_finish;
return result;
PG_RETURN_TEXT_P(result);
}
/* ---------------
* INT8 to_char()
* ---------------
*/
text *
int8_to_char(int64 *value, text *fmt)
Datum
int8_to_char(PG_FUNCTION_ARGS)
{
int64 value = PG_GETARG_INT64(0);
text *fmt = PG_GETARG_TEXT_P(1);
NUMDesc Num;
FormatNode *format;
text *result,
@@ -4126,8 +4136,9 @@ int8_to_char(int64 *value, text *fmt)
*/
if (IS_ROMAN(&Num))
{
numstr = orgnum = int_to_roman(int84(value));
/* Currently don't support int8 conversion to roman... */
numstr = orgnum = int_to_roman(DatumGetInt32(
DirectFunctionCall1(int84, Int64GetDatum(value))));
}
else
{
@@ -4135,11 +4146,15 @@ int8_to_char(int64 *value, text *fmt)
{
double multi = pow((double) 10, (double) Num.multi);
orgnum = int8out(int8mul(value, dtoi8((float64) &multi)));
value = DatumGetInt64(DirectFunctionCall2(int8mul,
Int64GetDatum(value),
DirectFunctionCall1(dtoi8,
Float8GetDatum(multi))));
Num.pre += Num.multi;
}
else
orgnum = int8out(value);
orgnum = DatumGetCString(DirectFunctionCall1(int8out,
Int64GetDatum(value)));
len = strlen(orgnum);
if (*orgnum == '-')
@@ -4178,16 +4193,18 @@ int8_to_char(int64 *value, text *fmt)
}
NUM_TOCHAR_finish;
return result;
PG_RETURN_TEXT_P(result);
}
/* -----------------
* FLOAT4 to_char()
* -----------------
*/
text *
float4_to_char(float32 value, text *fmt)
Datum
float4_to_char(PG_FUNCTION_ARGS)
{
float4 value = PG_GETARG_FLOAT4(0);
text *fmt = PG_GETARG_TEXT_P(1);
NUMDesc Num;
FormatNode *format;
text *result,
@@ -4204,30 +4221,30 @@ float4_to_char(float32 value, text *fmt)
if (IS_ROMAN(&Num))
{
numstr = orgnum = int_to_roman((int) rint(*value));
numstr = orgnum = int_to_roman((int) rint(value));
}
else
{
float32 val = value;
float4 val = value;
if (IS_MULTI(&Num))
{
float multi = pow((double) 10, (double) Num.multi);
val = float4mul(value, (float32) &multi);
val = value * multi;
Num.pre += Num.multi;
}
orgnum = (char *) palloc(MAXFLOATWIDTH + 1);
len = sprintf(orgnum, "%.0f", fabs(*val));
len = sprintf(orgnum, "%.0f", fabs(val));
if (Num.pre > len)
plen = Num.pre - len;
if (len >= FLT_DIG)
Num.post = 0;
else if (Num.post + len > FLT_DIG)
Num.post = FLT_DIG - len;
sprintf(orgnum, "%.*f", Num.post, *val);
sprintf(orgnum, "%.*f", Num.post, val);
if (*orgnum == '-')
{ /* < 0 */
@@ -4256,16 +4273,18 @@ float4_to_char(float32 value, text *fmt)
}
NUM_TOCHAR_finish;
return result;
PG_RETURN_TEXT_P(result);
}
/* -----------------
* FLOAT8 to_char()
* -----------------
*/
text *
float8_to_char(float64 value, text *fmt)
Datum
float8_to_char(PG_FUNCTION_ARGS)
{
float8 value = PG_GETARG_FLOAT8(0);
text *fmt = PG_GETARG_TEXT_P(1);
NUMDesc Num;
FormatNode *format;
text *result,
@@ -4282,29 +4301,29 @@ float8_to_char(float64 value, text *fmt)
if (IS_ROMAN(&Num))
{
numstr = orgnum = int_to_roman((int) rint(*value));
numstr = orgnum = int_to_roman((int) rint(value));
}
else
{
float64 val = value;
float8 val = value;
if (IS_MULTI(&Num))
{
double multi = pow((double) 10, (double) Num.multi);
val = float8mul(value, (float64) &multi);
val = value * multi;
Num.pre += Num.multi;
}
orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
len = sprintf(orgnum, "%.0f", fabs(*val));
len = sprintf(orgnum, "%.0f", fabs(val));
if (Num.pre > len)
plen = Num.pre - len;
if (len >= DBL_DIG)
Num.post = 0;
else if (Num.post + len > DBL_DIG)
Num.post = DBL_DIG - len;
sprintf(orgnum, "%.*f", Num.post, *val);
sprintf(orgnum, "%.*f", Num.post, val);
if (*orgnum == '-')
{ /* < 0 */
@@ -4333,5 +4352,5 @@ float8_to_char(float64 value, text *fmt)
}
NUM_TOCHAR_finish;
return result;
PG_RETURN_TEXT_P(result);
}

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_ops.c,v 1.50 2000/04/12 17:15:50 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_ops.c,v 1.51 2000/06/13 07:35:07 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -4361,22 +4361,21 @@ box_circle(BOX *box)
} /* box_circle() */
POLYGON *
circle_poly(int npts, CIRCLE *circle)
Datum
circle_poly(PG_FUNCTION_ARGS)
{
int32 npts = PG_GETARG_INT32(0);
CIRCLE *circle = PG_GETARG_CIRCLE_P(1);
POLYGON *poly;
int size;
int i;
double angle;
if (!PointerIsValid(circle))
return NULL;
if (FPzero(circle->radius) || (npts < 2))
elog(ERROR, "Unable to convert circle to polygon");
size = offsetof(POLYGON, p[0]) +(sizeof(poly->p[0]) * npts);
poly = palloc(size);
poly = (POLYGON *) palloc(size);
MemSet((char *) poly, 0, size); /* zero any holes */
poly->size = size;
@@ -4391,7 +4390,7 @@ circle_poly(int npts, CIRCLE *circle)
make_bound_box(poly);
return poly;
PG_RETURN_POLYGON_P(poly);
}
/* poly_circle - convert polygon to circle

View File

@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/int8.c,v 1.19 2000/05/28 17:56:05 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/int8.c,v 1.20 2000/06/13 07:35:07 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -57,17 +57,15 @@
/* int8in()
*/
int64 *
int8in(char *str)
Datum
int8in(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
char *str = PG_GETARG_CSTRING(0);
int64 result;
char *ptr = str;
int64 tmp = 0;
int sign = 1;
if (!PointerIsValid(str))
elog(ERROR, "Bad (null) int8 external representation");
/*
* Do our own scan, rather than relying on sscanf which might be
* broken for long long.
@@ -91,34 +89,28 @@ int8in(char *str)
if (*ptr) /* trailing junk? */
elog(ERROR, "Bad int8 external representation \"%s\"", str);
*result = (sign < 0) ? -tmp : tmp;
result = (sign < 0) ? -tmp : tmp;
return result;
} /* int8in() */
PG_RETURN_INT64(result);
}
/* int8out()
*/
char *
int8out(int64 *val)
Datum
int8out(PG_FUNCTION_ARGS)
{
int64 val = PG_GETARG_INT64(0);
char *result;
int len;
char buf[MAXINT8LEN + 1];
if (!PointerIsValid(val))
return NULL;
if ((len = snprintf(buf, MAXINT8LEN, INT64_FORMAT, *val)) < 0)
if ((len = snprintf(buf, MAXINT8LEN, INT64_FORMAT, val)) < 0)
elog(ERROR, "Unable to format int8");
result = palloc(len + 1);
strcpy(result, buf);
return result;
} /* int8out() */
result = pstrdup(buf);
PG_RETURN_CSTRING(result);
}
/*----------------------------------------------------------
@@ -128,511 +120,403 @@ int8out(int64 *val)
/* int8relop()
* Is val1 relop val2?
*/
bool
int8eq(int64 *val1, int64 *val2)
Datum
int8eq(PG_FUNCTION_ARGS)
{
if (!val1 || !val2)
return 0;
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
return *val1 == *val2;
} /* int8eq() */
PG_RETURN_BOOL(val1 == val2);
}
bool
int8ne(int64 *val1, int64 *val2)
Datum
int8ne(PG_FUNCTION_ARGS)
{
if (!val1 || !val2)
return 0;
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
return *val1 != *val2;
} /* int8ne() */
PG_RETURN_BOOL(val1 != val2);
}
bool
int8lt(int64 *val1, int64 *val2)
Datum
int8lt(PG_FUNCTION_ARGS)
{
if (!val1 || !val2)
return 0;
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
return *val1 < *val2;
} /* int8lt() */
PG_RETURN_BOOL(val1 < val2);
}
bool
int8gt(int64 *val1, int64 *val2)
Datum
int8gt(PG_FUNCTION_ARGS)
{
if (!val1 || !val2)
return 0;
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
return *val1 > *val2;
} /* int8gt() */
PG_RETURN_BOOL(val1 > val2);
}
bool
int8le(int64 *val1, int64 *val2)
Datum
int8le(PG_FUNCTION_ARGS)
{
if (!val1 || !val2)
return 0;
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
return *val1 <= *val2;
} /* int8le() */
PG_RETURN_BOOL(val1 <= val2);
}
bool
int8ge(int64 *val1, int64 *val2)
Datum
int8ge(PG_FUNCTION_ARGS)
{
if (!val1 || !val2)
return 0;
return *val1 >= *val2;
} /* int8ge() */
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 >= val2);
}
/* int84relop()
* Is 64-bit val1 relop 32-bit val2?
*/
bool
int84eq(int64 *val1, int32 val2)
Datum
int84eq(PG_FUNCTION_ARGS)
{
if (!val1)
return 0;
int64 val1 = PG_GETARG_INT64(0);
int32 val2 = PG_GETARG_INT32(1);
return *val1 == val2;
} /* int84eq() */
PG_RETURN_BOOL(val1 == val2);
}
bool
int84ne(int64 *val1, int32 val2)
Datum
int84ne(PG_FUNCTION_ARGS)
{
if (!val1)
return 0;
int64 val1 = PG_GETARG_INT64(0);
int32 val2 = PG_GETARG_INT32(1);
return *val1 != val2;
} /* int84ne() */
PG_RETURN_BOOL(val1 != val2);
}
bool
int84lt(int64 *val1, int32 val2)
Datum
int84lt(PG_FUNCTION_ARGS)
{
if (!val1)
return 0;
int64 val1 = PG_GETARG_INT64(0);
int32 val2 = PG_GETARG_INT32(1);
return *val1 < val2;
} /* int84lt() */
PG_RETURN_BOOL(val1 < val2);
}
bool
int84gt(int64 *val1, int32 val2)
Datum
int84gt(PG_FUNCTION_ARGS)
{
if (!val1)
return 0;
int64 val1 = PG_GETARG_INT64(0);
int32 val2 = PG_GETARG_INT32(1);
return *val1 > val2;
} /* int84gt() */
PG_RETURN_BOOL(val1 > val2);
}
bool
int84le(int64 *val1, int32 val2)
Datum
int84le(PG_FUNCTION_ARGS)
{
if (!val1)
return 0;
int64 val1 = PG_GETARG_INT64(0);
int32 val2 = PG_GETARG_INT32(1);
return *val1 <= val2;
} /* int84le() */
PG_RETURN_BOOL(val1 <= val2);
}
bool
int84ge(int64 *val1, int32 val2)
Datum
int84ge(PG_FUNCTION_ARGS)
{
if (!val1)
return 0;
return *val1 >= val2;
} /* int84ge() */
int64 val1 = PG_GETARG_INT64(0);
int32 val2 = PG_GETARG_INT32(1);
PG_RETURN_BOOL(val1 >= val2);
}
/* int48relop()
* Is 32-bit val1 relop 64-bit val2?
*/
bool
int48eq(int32 val1, int64 *val2)
Datum
int48eq(PG_FUNCTION_ARGS)
{
if (!val2)
return 0;
int32 val1 = PG_GETARG_INT32(0);
int64 val2 = PG_GETARG_INT64(1);
return val1 == *val2;
} /* int48eq() */
PG_RETURN_BOOL(val1 == val2);
}
bool
int48ne(int32 val1, int64 *val2)
Datum
int48ne(PG_FUNCTION_ARGS)
{
if (!val2)
return 0;
int32 val1 = PG_GETARG_INT32(0);
int64 val2 = PG_GETARG_INT64(1);
return val1 != *val2;
} /* int48ne() */
PG_RETURN_BOOL(val1 != val2);
}
bool
int48lt(int32 val1, int64 *val2)
Datum
int48lt(PG_FUNCTION_ARGS)
{
if (!val2)
return 0;
int32 val1 = PG_GETARG_INT32(0);
int64 val2 = PG_GETARG_INT64(1);
return val1 < *val2;
} /* int48lt() */
PG_RETURN_BOOL(val1 < val2);
}
bool
int48gt(int32 val1, int64 *val2)
Datum
int48gt(PG_FUNCTION_ARGS)
{
if (!val2)
return 0;
int32 val1 = PG_GETARG_INT32(0);
int64 val2 = PG_GETARG_INT64(1);
return val1 > *val2;
} /* int48gt() */
PG_RETURN_BOOL(val1 > val2);
}
bool
int48le(int32 val1, int64 *val2)
Datum
int48le(PG_FUNCTION_ARGS)
{
if (!val2)
return 0;
int32 val1 = PG_GETARG_INT32(0);
int64 val2 = PG_GETARG_INT64(1);
return val1 <= *val2;
} /* int48le() */
PG_RETURN_BOOL(val1 <= val2);
}
bool
int48ge(int32 val1, int64 *val2)
Datum
int48ge(PG_FUNCTION_ARGS)
{
if (!val2)
return 0;
int32 val1 = PG_GETARG_INT32(0);
int64 val2 = PG_GETARG_INT64(1);
return val1 >= *val2;
} /* int48ge() */
PG_RETURN_BOOL(val1 >= val2);
}
/*----------------------------------------------------------
* Arithmetic operators on 64-bit integers.
*---------------------------------------------------------*/
int64 *
int8um(int64 *val)
Datum
int8um(PG_FUNCTION_ARGS)
{
int64 temp = 0;
int64 *result = palloc(sizeof(int64));
int64 val = PG_GETARG_INT64(0);
if (!PointerIsValid(val))
return NULL;
PG_RETURN_INT64(- val);
}
result = int8mi(&temp, val);
return result;
} /* int8um() */
int64 *
int8pl(int64 *val1, int64 *val2)
Datum
int8pl(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
return NULL;
PG_RETURN_INT64(val1 + val2);
}
*result = *val1 + *val2;
return result;
} /* int8pl() */
int64 *
int8mi(int64 *val1, int64 *val2)
Datum
int8mi(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
return NULL;
PG_RETURN_INT64(val1 - val2);
}
*result = *val1 - *val2;
return result;
} /* int8mi() */
int64 *
int8mul(int64 *val1, int64 *val2)
Datum
int8mul(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
return NULL;
PG_RETURN_INT64(val1 * val2);
}
*result = *val1 * *val2;
return result;
} /* int8mul() */
int64 *
int8div(int64 *val1, int64 *val2)
Datum
int8div(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
return NULL;
*result = *val1 / *val2;
return result;
} /* int8div() */
PG_RETURN_INT64(val1 / val2);
}
/* int8abs()
* Absolute value
*/
int64 *
int8abs(int64 *arg1)
Datum
int8abs(PG_FUNCTION_ARGS)
{
int64 *result;
int64 arg1 = PG_GETARG_INT64(0);
if (!PointerIsValid(arg1))
return NULL;
result = palloc(sizeof(*result));
*result = ((*arg1 < 0) ? -*arg1 : *arg1);
return result;
PG_RETURN_INT64((arg1 < 0) ? -arg1 : arg1);
}
/* int8mod()
* Modulo operation.
*/
int64 *
int8mod(int64 *val1, int64 *val2)
Datum
int8mod(PG_FUNCTION_ARGS)
{
int64 *result;
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
int64 result;
/* use the divide operation to check params and allocate storage */
result = int8div(val1, val2);
*result *= *val2;
*result = *val1 - *result;
result = val1 / val2;
result *= val2;
result = val1 - result;
return result;
} /* int8mod() */
PG_RETURN_INT64(result);
}
/* int8fac()
* Factorial
*/
int64 *
int8fac(int64 *arg1)
Datum
int8fac(PG_FUNCTION_ARGS)
{
int64 *result;
int64 arg1 = PG_GETARG_INT64(0);
int64 result;
int64 i;
if (!PointerIsValid(arg1))
return NULL;
result = palloc(sizeof(*result));
if (*arg1 < 1)
*result = 0;
if (arg1 < 1)
result = 0;
else
for (i = *arg1, *result = 1; i > 0; --i)
*result *= i;
for (i = arg1, result = 1; i > 0; --i)
result *= i;
return result;
PG_RETURN_INT64(result);
}
int64 *
int8larger(int64 *val1, int64 *val2)
Datum
int8larger(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
int64 result;
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
return NULL;
result = ((val1 > val2) ? val1 : val2);
*result = ((*val1 > *val2) ? *val1 : *val2);
PG_RETURN_INT64(result);
}
return result;
} /* int8larger() */
int64 *
int8smaller(int64 *val1, int64 *val2)
Datum
int8smaller(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
int64 result;
if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))
return NULL;
result = ((val1 < val2) ? val1 : val2);
*result = ((*val1 < *val2) ? *val1 : *val2);
PG_RETURN_INT64(result);
}
return result;
} /* int8smaller() */
int64 *
int84pl(int64 *val1, int32 val2)
Datum
int84pl(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
int64 val1 = PG_GETARG_INT64(0);
int32 val2 = PG_GETARG_INT32(1);
if (!PointerIsValid(val1))
return NULL;
PG_RETURN_INT64(val1 + val2);
}
*result = *val1 + (int64) val2;
return result;
} /* int84pl() */
int64 *
int84mi(int64 *val1, int32 val2)
Datum
int84mi(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
int64 val1 = PG_GETARG_INT64(0);
int32 val2 = PG_GETARG_INT32(1);
if (!PointerIsValid(val1))
return NULL;
PG_RETURN_INT64(val1 - val2);
}
*result = *val1 - (int64) val2;
return result;
} /* int84mi() */
int64 *
int84mul(int64 *val1, int32 val2)
Datum
int84mul(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
int64 val1 = PG_GETARG_INT64(0);
int32 val2 = PG_GETARG_INT32(1);
if (!PointerIsValid(val1))
return NULL;
PG_RETURN_INT64(val1 * val2);
}
*result = *val1 * (int64) val2;
return result;
} /* int84mul() */
int64 *
int84div(int64 *val1, int32 val2)
Datum
int84div(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
int64 val1 = PG_GETARG_INT64(0);
int32 val2 = PG_GETARG_INT32(1);
if (!PointerIsValid(val1))
return NULL;
PG_RETURN_INT64(val1 / val2);
}
*result = *val1 / (int64) val2;
return result;
} /* int84div() */
int64 *
int48pl(int32 val1, int64 *val2)
Datum
int48pl(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
int32 val1 = PG_GETARG_INT32(0);
int64 val2 = PG_GETARG_INT64(1);
if (!PointerIsValid(val2))
return NULL;
PG_RETURN_INT64(val1 + val2);
}
*result = (int64) val1 + *val2;
return result;
} /* int48pl() */
int64 *
int48mi(int32 val1, int64 *val2)
Datum
int48mi(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
int32 val1 = PG_GETARG_INT32(0);
int64 val2 = PG_GETARG_INT64(1);
if (!PointerIsValid(val2))
return NULL;
PG_RETURN_INT64(val1 - val2);
}
*result = (int64) val1 - *val2;
return result;
} /* int48mi() */
int64 *
int48mul(int32 val1, int64 *val2)
Datum
int48mul(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
int32 val1 = PG_GETARG_INT32(0);
int64 val2 = PG_GETARG_INT64(1);
if (!PointerIsValid(val2))
return NULL;
PG_RETURN_INT64(val1 * val2);
}
*result = (int64) val1 **val2;
return result;
} /* int48mul() */
int64 *
int48div(int32 val1, int64 *val2)
Datum
int48div(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
int32 val1 = PG_GETARG_INT32(0);
int64 val2 = PG_GETARG_INT64(1);
if (!PointerIsValid(val2))
return NULL;
*result = (int64) val1 / *val2;
return result;
} /* int48div() */
PG_RETURN_INT64(val1 / val2);
}
/*----------------------------------------------------------
* Conversion operators.
*---------------------------------------------------------*/
int64 *
int48(int32 val)
Datum
int48(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
int32 val = PG_GETARG_INT32(0);
*result = val;
PG_RETURN_INT64((int64) val);
}
return result;
} /* int48() */
int32
int84(int64 *val)
Datum
int84(PG_FUNCTION_ARGS)
{
int64 val = PG_GETARG_INT64(0);
int32 result;
if (!PointerIsValid(val))
elog(ERROR, "Invalid (null) int64, can't convert int8 to int4");
if ((*val < INT_MIN) || (*val > INT_MAX))
if ((val < INT_MIN) || (val > INT_MAX))
elog(ERROR, "int8 conversion to int4 is out of range");
result = *val;
result = (int32) val;
return result;
} /* int84() */
PG_RETURN_INT32(result);
}
#if NOT_USED
int64 *
int2vector (int16 val)
Datum
i8tod(PG_FUNCTION_ARGS)
{
int64 *result;
int64 val = PG_GETARG_INT64(0);
float8 result;
result = palloc(sizeof(int64));
result = val;
*result = val;
return result;
} /* int2vector() */
int16
int82(int64 *val)
{
int16 result;
if (!PointerIsValid(val))
elog(ERROR, "Invalid (null) int8, can't convert to int2");
if ((*val < SHRT_MIN) || (*val > SHRT_MAX))
elog(ERROR, "int8 conversion to int2 is out of range");
result = *val;
return result;
} /* int82() */
#endif
float64
i8tod(int64 *val)
{
float64 result = palloc(sizeof(float64data));
if (!PointerIsValid(val))
elog(ERROR, "Invalid (null) int8, can't convert to float8");
*result = *val;
return result;
} /* i8tod() */
PG_RETURN_FLOAT8(result);
}
/* dtoi8()
* Convert double float to 8-byte integer.
@@ -644,62 +528,63 @@ i8tod(int64 *val)
* does the right thing on my i686/linux-rh4.2 box.
* - thomas 1998-06-16
*/
int64 *
dtoi8(float64 val)
Datum
dtoi8(PG_FUNCTION_ARGS)
{
int64 *result = palloc(sizeof(int64));
float8 val = PG_GETARG_FLOAT8(0);
int64 result;
if (!PointerIsValid(val))
elog(ERROR, "Invalid (null) float8, can't convert to int8");
if ((*val < (-pow(2, 63) + 1)) || (*val > (pow(2, 63) - 1)))
if ((val < (-pow(2.0, 63.0) + 1)) || (val > (pow(2.0, 63.0) - 1)))
elog(ERROR, "Floating point conversion to int64 is out of range");
*result = *val;
result = (int64) val;
return result;
} /* dtoi8() */
PG_RETURN_INT64(result);
}
/* text_int8()
*/
int64 *
text_int8(text *str)
Datum
text_int8(PG_FUNCTION_ARGS)
{
text *str = PG_GETARG_TEXT_P(0);
int len;
char *s;
if (!PointerIsValid(str))
elog(ERROR, "Bad (null) int8 external representation");
Datum result;
len = (VARSIZE(str) - VARHDRSZ);
s = palloc(len + 1);
memmove(s, VARDATA(str), len);
memcpy(s, VARDATA(str), len);
*(s + len) = '\0';
return int8in(s);
} /* text_int8() */
result = DirectFunctionCall1(int8in, CStringGetDatum(s));
pfree(s);
return result;
}
/* int8_text()
*/
text *
int8_text(int64 *val)
Datum
int8_text(PG_FUNCTION_ARGS)
{
/* val is int64, but easier to leave it as Datum */
Datum val = PG_GETARG_DATUM(0);
char *s;
int len;
text *result;
int len;
char *s;
if (!PointerIsValid(val))
return NULL;
s = int8out(val);
s = DatumGetCString(DirectFunctionCall1(int8out, val));
len = strlen(s);
result = palloc(VARHDRSZ + len);
result = (text *) palloc(VARHDRSZ + len);
VARSIZE(result) = len + VARHDRSZ;
memmove(VARDATA(result), s, len);
memcpy(VARDATA(result), s, len);
return result;
} /* int8_text() */
pfree(s);
PG_RETURN_TEXT_P(result);
}

View File

@@ -5,7 +5,7 @@
*
* 1998 Jan Wieck
*
* $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.28 2000/06/05 07:28:52 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.29 2000/06/13 07:35:07 tgl Exp $
*
* ----------
*/
@@ -33,9 +33,6 @@
* Local definitions
* ----------
*/
#define PG_GETARG_NUMERIC(n) ((Numeric) DatumGetPointer(fcinfo->arg[n]))
#define PG_RETURN_NUMERIC(x) return PointerGetDatum(x)
#ifndef MIN
#define MIN(a,b) (((a)<(b)) ? (a) : (b))
#endif
@@ -189,28 +186,23 @@ static void sub_abs(NumericVar *var1, NumericVar *var2, NumericVar *result);
* Input function for numeric data type
* ----------
*/
Numeric
numeric_in(char *str, int dummy, int32 typmod)
Datum
numeric_in(PG_FUNCTION_ARGS)
{
char *str = PG_GETARG_CSTRING(0);
#ifdef NOT_USED
Oid typelem = PG_GETARG_OID(1);
#endif
int32 typmod = PG_GETARG_INT32(2);
NumericVar value;
Numeric res;
/* ----------
* Check for NULL
* ----------
*/
if (str == NULL)
return NULL;
if (strcmp(str, "NULL") == 0)
return NULL;
/* ----------
* Check for NaN
* ----------
*/
if (strcmp(str, "NaN") == 0)
return make_result(&const_nan);
PG_RETURN_NUMERIC(make_result(&const_nan));
/* ----------
* Use set_var_from_str() to parse the input string
@@ -225,7 +217,7 @@ numeric_in(char *str, int dummy, int32 typmod)
res = make_result(&value);
free_var(&value);
return res;
PG_RETURN_NUMERIC(res);
}
@@ -235,25 +227,19 @@ numeric_in(char *str, int dummy, int32 typmod)
* Output function for numeric data type
* ----------
*/
char *
numeric_out(Numeric num)
Datum
numeric_out(PG_FUNCTION_ARGS)
{
Numeric num = PG_GETARG_NUMERIC(0);
NumericVar x;
char *str;
/* ----------
* Handle NULL
* ----------
*/
if (num == NULL)
return pstrdup("NULL");
/* ----------
* Handle NaN
* ----------
*/
if (NUMERIC_IS_NAN(num))
return pstrdup("NaN");
PG_RETURN_CSTRING(pstrdup("NaN"));
/* ----------
* Get the number in the variable format.
@@ -271,7 +257,7 @@ numeric_out(Numeric num)
free_var(&x);
return str;
PG_RETURN_CSTRING(str);
}
@@ -283,9 +269,11 @@ numeric_out(Numeric num)
* scale of the attribute have to be applied on the value.
* ----------
*/
Numeric
numeric(Numeric num, int32 typmod)
Datum
numeric(PG_FUNCTION_ARGS)
{
Numeric num = PG_GETARG_NUMERIC(0);
int32 typmod = PG_GETARG_INT32(1);
Numeric new;
int32 tmp_typmod;
int precision;
@@ -293,19 +281,12 @@ numeric(Numeric num, int32 typmod)
int maxweight;
NumericVar var;
/* ----------
* Handle NULL
* ----------
*/
if (num == NULL)
return NULL;
/* ----------
* Handle NaN
* ----------
*/
if (NUMERIC_IS_NAN(num))
return make_result(&const_nan);
PG_RETURN_NUMERIC(make_result(&const_nan));
/* ----------
* If the value isn't a valid type modifier, simply return a
@@ -316,7 +297,7 @@ numeric(Numeric num, int32 typmod)
{
new = (Numeric) palloc(num->varlen);
memcpy(new, num, num->varlen);
return new;
PG_RETURN_NUMERIC(new);
}
/* ----------
@@ -341,7 +322,7 @@ numeric(Numeric num, int32 typmod)
new->n_rscale = scale;
new->n_sign_dscale = NUMERIC_SIGN(new) |
((uint16) scale & NUMERIC_DSCALE_MASK);
return new;
PG_RETURN_NUMERIC(new);
}
/* ----------
@@ -357,7 +338,7 @@ numeric(Numeric num, int32 typmod)
free_var(&var);
return new;
PG_RETURN_NUMERIC(new);
}
@@ -502,26 +483,21 @@ numeric_sign(Numeric num)
* point --- Oracle interprets rounding that way.
* ----------
*/
Numeric
numeric_round(Numeric num, int32 scale)
Datum
numeric_round(PG_FUNCTION_ARGS)
{
Numeric num = PG_GETARG_NUMERIC(0);
int32 scale = PG_GETARG_INT32(1);
Numeric res;
NumericVar arg;
int i;
/* ----------
* Handle NULL
* ----------
*/
if (num == NULL)
return NULL;
/* ----------
* Handle NaN
* ----------
*/
if (NUMERIC_IS_NAN(num))
return make_result(&const_nan);
PG_RETURN_NUMERIC(make_result(&const_nan));
/* ----------
* Limit the scale value to avoid possible overflow in calculations below.
@@ -587,7 +563,7 @@ numeric_round(Numeric num, int32 scale)
res = make_result(&arg);
free_var(&arg);
return res;
PG_RETURN_NUMERIC(res);
}
@@ -599,25 +575,20 @@ numeric_round(Numeric num, int32 scale)
* point --- Oracle interprets truncation that way.
* ----------
*/
Numeric
numeric_trunc(Numeric num, int32 scale)
Datum
numeric_trunc(PG_FUNCTION_ARGS)
{
Numeric num = PG_GETARG_NUMERIC(0);
int32 scale = PG_GETARG_INT32(1);
Numeric res;
NumericVar arg;
/* ----------
* Handle NULL
* ----------
*/
if (num == NULL)
return NULL;
/* ----------
* Handle NaN
* ----------
*/
if (NUMERIC_IS_NAN(num))
return make_result(&const_nan);
PG_RETURN_NUMERIC(make_result(&const_nan));
/* ----------
* Limit the scale value to avoid possible overflow in calculations below.
@@ -650,7 +621,7 @@ numeric_trunc(Numeric num, int32 scale)
res = make_result(&arg);
free_var(&arg);
return res;
PG_RETURN_NUMERIC(res);
}
@@ -1708,9 +1679,10 @@ numeric_power(Numeric num1, Numeric num2)
*/
Numeric
int4_numeric(int32 val)
Datum
int4_numeric(PG_FUNCTION_ARGS)
{
int32 val = PG_GETARG_INT32(0);
Numeric res;
NumericVar result;
char *tmp;
@@ -1725,7 +1697,7 @@ int4_numeric(int32 val)
free_var(&result);
pfree(tmp);
return res;
PG_RETURN_NUMERIC(res);
}
@@ -1769,7 +1741,8 @@ int8_numeric(int64 *val)
init_var(&result);
tmp = int8out(val);
tmp = DatumGetCString(DirectFunctionCall1(int8out,
PointerGetDatum(val)));
set_var_from_str(tmp, &result);
res = make_result(&result);
@@ -1785,7 +1758,7 @@ numeric_int8(Numeric num)
{
NumericVar x;
char *str;
int64 *result;
Datum result;
if (num == NULL)
return NULL;
@@ -1804,10 +1777,11 @@ numeric_int8(Numeric num)
free_var(&x);
result = int8in(str);
result = DirectFunctionCall1(int8in, CStringGetDatum(str));
pfree(str);
return result;
return (int64 *) (result);
}
@@ -1904,7 +1878,8 @@ numeric_float8(Numeric num)
return result;
}
tmp = numeric_out(num);
tmp = DatumGetCString(DirectFunctionCall1(numeric_out,
NumericGetDatum(num)));
result = float8in(tmp);
pfree(tmp);
@@ -1954,7 +1929,8 @@ numeric_float4(Numeric num)
return result;
}
tmp = numeric_out(num);
tmp = DatumGetCString(DirectFunctionCall1(numeric_out,
NumericGetDatum(num)));
result = float4in(tmp);
pfree(tmp);

View File

@@ -1,7 +1,7 @@
/*
* Edmund Mergl <E.Mergl@bawue.de>
*
* $Id: oracle_compat.c,v 1.24 2000/04/12 17:15:51 momjian Exp $
* $Id: oracle_compat.c,v 1.25 2000/06/13 07:35:07 tgl Exp $
*
*/
@@ -150,9 +150,12 @@ initcap(text *string)
*
********************************************************************/
text *
lpad(text *string1, int4 len, text *string2)
Datum
lpad(PG_FUNCTION_ARGS)
{
text *string1 = PG_GETARG_TEXT_P(0);
int32 len = PG_GETARG_INT32(1);
text *string2 = PG_GETARG_TEXT_P(2);
text *ret;
char *ptr1,
*ptr2,
@@ -160,12 +163,10 @@ lpad(text *string1, int4 len, text *string2)
int m,
n;
if ((string1 == (text *) NULL) ||
(len <= (VARSIZE(string1) - VARHDRSZ)) ||
((m = len - VARSIZE(string1) + VARHDRSZ) <= 0) ||
(string2 == (text *) NULL) ||
if (((VARSIZE(string1) - VARHDRSZ) < 0) ||
((m = len - (VARSIZE(string1) - VARHDRSZ)) <= 0) ||
((VARSIZE(string2) - VARHDRSZ) <= 0))
return string1;
PG_RETURN_TEXT_P(string1);
ret = (text *) palloc(VARHDRSZ + len);
VARSIZE(ret) = VARHDRSZ + len;
@@ -176,7 +177,7 @@ lpad(text *string1, int4 len, text *string2)
while (m--)
{
*ptr_ret++ = *ptr2;
ptr2 = ptr2 == VARDATA(string2) + VARSIZE(string2) - VARHDRSZ - 1 ? VARDATA(string2) : ++ptr2;
ptr2 = (ptr2 == VARDATA(string2) + VARSIZE(string2) - VARHDRSZ - 1) ? VARDATA(string2) : ++ptr2;
}
n = VARSIZE(string1) - VARHDRSZ;
@@ -185,7 +186,7 @@ lpad(text *string1, int4 len, text *string2)
while (n--)
*ptr_ret++ = *ptr1++;
return ret;
PG_RETURN_TEXT_P(ret);
}
@@ -204,9 +205,12 @@ lpad(text *string1, int4 len, text *string2)
*
********************************************************************/
text *
rpad(text *string1, int4 len, text *string2)
Datum
rpad(PG_FUNCTION_ARGS)
{
text *string1 = PG_GETARG_TEXT_P(0);
int32 len = PG_GETARG_INT32(1);
text *string2 = PG_GETARG_TEXT_P(2);
text *ret;
char *ptr1,
*ptr2,
@@ -214,12 +218,10 @@ rpad(text *string1, int4 len, text *string2)
int m,
n;
if ((string1 == (text *) NULL) ||
(len <= (VARSIZE(string1) - VARHDRSZ)) ||
((m = len - VARSIZE(string1) + VARHDRSZ) <= 0) ||
(string2 == (text *) NULL) ||
if (((VARSIZE(string1) - VARHDRSZ) < 0) ||
((m = len - (VARSIZE(string1) - VARHDRSZ)) <= 0) ||
((VARSIZE(string2) - VARHDRSZ) <= 0))
return string1;
PG_RETURN_TEXT_P(string1);
ret = (text *) palloc(VARHDRSZ + len);
VARSIZE(ret) = VARHDRSZ + len;
@@ -236,10 +238,10 @@ rpad(text *string1, int4 len, text *string2)
while (m--)
{
*ptr_ret++ = *ptr2;
ptr2 = ptr2 == VARDATA(string2) + VARSIZE(string2) - VARHDRSZ - 1 ? VARDATA(string2) : ++ptr2;
ptr2 = (ptr2 == VARDATA(string2) + VARSIZE(string2) - VARHDRSZ - 1) ? VARDATA(string2) : ++ptr2;
}
return ret;
PG_RETURN_TEXT_P(ret);
}
@@ -551,22 +553,25 @@ ascii(text *string)
} /* ascii() */
text *
ichar(int4 cvalue)
Datum
ichar(PG_FUNCTION_ARGS)
{
int32 cvalue = PG_GETARG_INT32(0);
text *result;
result = (text *) palloc(VARHDRSZ + 1);
VARSIZE(result) = VARHDRSZ + 1;
*VARDATA(result) = (char) cvalue;
return result;
} /* ichar() */
PG_RETURN_TEXT_P(result);
}
text *
repeat(text *string, int4 count)
Datum
repeat(PG_FUNCTION_ARGS)
{
text *string = PG_GETARG_TEXT_P(0);
int32 count = PG_GETARG_INT32(1);
text *result;
int slen,
tlen;
@@ -589,5 +594,5 @@ repeat(text *string, int4 count)
cp += slen;
}
return result;
} /* repeat() */
PG_RETURN_TEXT_P(result);
}

View File

@@ -3,7 +3,7 @@
* out of its tuple
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.53 2000/06/12 19:40:43 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.54 2000/06/13 07:35:08 tgl Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
@@ -551,18 +551,19 @@ pg_get_indexdef(PG_FUNCTION_ARGS)
* fallback to 'unknown (UID=n)'
* ----------
*/
NameData *
pg_get_userbyid(int32 uid)
Datum
pg_get_userbyid(PG_FUNCTION_ARGS)
{
int32 uid = PG_GETARG_INT32(0);
Name result;
HeapTuple usertup;
Form_pg_shadow user_rec;
NameData *result;
/* ----------
* Allocate space for the result
* ----------
*/
result = (NameData *) palloc(NAMEDATALEN);
result = (Name) palloc(NAMEDATALEN);
memset(NameStr(*result), 0, NAMEDATALEN);
/* ----------
@@ -570,16 +571,17 @@ pg_get_userbyid(int32 uid)
* ----------
*/
usertup = SearchSysCacheTuple(SHADOWSYSID,
ObjectIdGetDatum(uid), 0, 0, 0);
ObjectIdGetDatum(uid),
0, 0, 0);
if (HeapTupleIsValid(usertup))
{
user_rec = (Form_pg_shadow) GETSTRUCT(usertup);
StrNCpy(NameStr(*result), NameStr(user_rec->usename), NAMEDATALEN);
}
else
sprintf((char *) result, "unknown (UID=%d)", uid);
sprintf(NameStr(*result), "unknown (UID=%d)", uid);
return result;
PG_RETURN_NAME(result);
}
/* ----------

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varchar.c,v 1.63 2000/06/05 07:28:52 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varchar.c,v 1.64 2000/06/13 07:35:08 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -61,20 +61,21 @@ extern char *convertstr(char *, int, int);
/*
* bpcharin -
* converts a string of char() type to the internal representation.
* len is the length specified in () plus VARHDRSZ bytes. (XXX dummy is here
* because we pass typelem as the second argument for array_in.)
* len is the length specified in () plus VARHDRSZ bytes.
*/
char *
bpcharin(char *s, int dummy, int32 atttypmod)
Datum
bpcharin(PG_FUNCTION_ARGS)
{
char *result,
*r;
char *s = PG_GETARG_CSTRING(0);
#ifdef NOT_USED
Oid typelem = PG_GETARG_OID(1);
#endif
int32 atttypmod = PG_GETARG_INT32(2);
BpChar *result;
char *r;
int len;
int i;
if (s == NULL)
return (char *) NULL;
if (atttypmod < (int32) VARHDRSZ)
{
/* If typmod is -1 (or invalid), use the actual string length */
@@ -84,7 +85,7 @@ bpcharin(char *s, int dummy, int32 atttypmod)
else
len = atttypmod - VARHDRSZ;
result = (char *) palloc(atttypmod);
result = (BpChar *) palloc(atttypmod);
VARSIZE(result) = atttypmod;
r = VARDATA(result);
for (i = 0; i < len; i++, r++, s++)
@@ -95,85 +96,78 @@ bpcharin(char *s, int dummy, int32 atttypmod)
}
#ifdef CYR_RECODE
convertstr(result + VARHDRSZ, len, 0);
convertstr(VARDATA(result), len, 0);
#endif
/* blank pad the string if necessary */
for (; i < len; i++)
*r++ = ' ';
return result;
PG_RETURN_BPCHAR_P(result);
}
char *
bpcharout(char *s)
Datum
bpcharout(PG_FUNCTION_ARGS)
{
BpChar *s = PG_GETARG_BPCHAR_P(0);
char *result;
int len;
if (s == NULL)
{
result = (char *) palloc(2);
result[0] = '-';
result[1] = '\0';
}
else
{
len = VARSIZE(s) - VARHDRSZ;
result = (char *) palloc(len + 1);
StrNCpy(result, VARDATA(s), len + 1); /* these are blank-padded */
}
len = VARSIZE(s) - VARHDRSZ;
result = (char *) palloc(len + 1);
StrNCpy(result, VARDATA(s), len + 1); /* copy and add null term */
#ifdef CYR_RECODE
convertstr(result, len, 1);
#endif
return result;
PG_RETURN_CSTRING(result);
}
/* bpchar()
* Converts a char() type to a specific internal length.
* len is the length specified in () plus VARHDRSZ bytes.
*/
char *
bpchar(char *s, int32 len)
Datum
bpchar(PG_FUNCTION_ARGS)
{
char *result,
*r;
BpChar *str = PG_GETARG_BPCHAR_P(0);
int32 len = PG_GETARG_INT32(1);
BpChar *result;
char *r,
*s;
int rlen,
slen;
int i;
if (s == NULL)
return (char *) NULL;
/* No work if typmod is invalid or supplied data matches it already */
if (len < (int32) VARHDRSZ || len == VARSIZE(s))
return s;
if (len < (int32) VARHDRSZ || len == VARSIZE(str))
PG_RETURN_BPCHAR_P(str);
rlen = len - VARHDRSZ;
#ifdef STRINGDEBUG
printf("bpchar- convert string length %d (%d) ->%d (%d)\n",
VARSIZE(s) - VARHDRSZ, VARSIZE(s), rlen, len);
VARSIZE(str) - VARHDRSZ, VARSIZE(str), rlen, len);
#endif
result = (char *) palloc(len);
result = (BpChar *) palloc(len);
VARSIZE(result) = len;
r = VARDATA(result);
#ifdef MULTIBYTE
#ifdef MULTIBYTE
/*
* truncate multi-byte string in a way not to break multi-byte
* boundary
*/
if (VARSIZE(s) > len)
slen = pg_mbcliplen(VARDATA(s), VARSIZE(s) - VARHDRSZ, rlen);
if (VARSIZE(str) > len)
slen = pg_mbcliplen(VARDATA(str), VARSIZE(str) - VARHDRSZ, rlen);
else
slen = VARSIZE(s) - VARHDRSZ;
slen = VARSIZE(str) - VARHDRSZ;
#else
slen = VARSIZE(s) - VARHDRSZ;
slen = VARSIZE(str) - VARHDRSZ;
#endif
s = VARDATA(s);
s = VARDATA(str);
#ifdef STRINGDEBUG
printf("bpchar- string is '");
@@ -181,13 +175,9 @@ bpchar(char *s, int32 len)
for (i = 0; (i < rlen) && (i < slen); i++)
{
if (*s == '\0')
break;
#ifdef STRINGDEBUG
printf("%c", *s);
#endif
*r++ = *s++;
}
@@ -199,19 +189,21 @@ bpchar(char *s, int32 len)
for (; i < rlen; i++)
*r++ = ' ';
return result;
} /* bpchar() */
PG_RETURN_BPCHAR_P(result);
}
/* _bpchar()
* Converts an array of char() type to a specific internal length.
* Converts an array of char() elements to a specific internal length.
* len is the length specified in () plus VARHDRSZ bytes.
*/
ArrayType *
_bpchar(ArrayType *v, int32 len)
Datum
_bpchar(PG_FUNCTION_ARGS)
{
ArrayType *v = (ArrayType *) PG_GETARG_VARLENA_P(0);
int32 len = PG_GETARG_INT32(1);
FunctionCallInfoData locfcinfo;
Datum result;
/* Since bpchar() is a built-in function, we should only need to
/*
* Since bpchar() is a built-in function, we should only need to
* look it up once per run.
*/
static FmgrInfo bpchar_finfo;
@@ -226,9 +218,7 @@ _bpchar(ArrayType *v, int32 len)
locfcinfo.arg[0] = PointerGetDatum(v);
locfcinfo.arg[1] = Int32GetDatum(len);
result = array_map(&locfcinfo, BPCHAROID, BPCHAROID);
return (ArrayType *) DatumGetPointer(result);
return array_map(&locfcinfo, BPCHAROID, BPCHAROID);
}
@@ -240,7 +230,7 @@ _bpchar(ArrayType *v, int32 len)
Datum
bpchar_char(PG_FUNCTION_ARGS)
{
struct varlena *s = PG_GETARG_BPCHAR_P(0);
BpChar *s = PG_GETARG_BPCHAR_P(0);
PG_RETURN_CHAR(*VARDATA(s));
}
@@ -252,9 +242,9 @@ Datum
char_bpchar(PG_FUNCTION_ARGS)
{
char c = PG_GETARG_CHAR(0);
struct varlena *result;
BpChar *result;
result = (struct varlena *) palloc(VARHDRSZ + 1);
result = (BpChar *) palloc(VARHDRSZ + 1);
VARSIZE(result) = VARHDRSZ + 1;
*(VARDATA(result)) = c;
@@ -338,75 +328,67 @@ name_bpchar(NameData *s)
/*
* varcharin -
* converts a string of varchar() type to the internal representation.
* len is the length specified in () plus VARHDRSZ bytes. (XXX dummy is here
* because we pass typelem as the second argument for array_in.)
* len is the length specified in () plus VARHDRSZ bytes.
*/
char *
varcharin(char *s, int dummy, int32 atttypmod)
Datum
varcharin(PG_FUNCTION_ARGS)
{
char *result;
char *s = PG_GETARG_CSTRING(0);
#ifdef NOT_USED
Oid typelem = PG_GETARG_OID(1);
#endif
int32 atttypmod = PG_GETARG_INT32(2);
VarChar *result;
int len;
if (s == NULL)
return (char *) NULL;
len = strlen(s) + VARHDRSZ;
if (atttypmod >= (int32) VARHDRSZ && len > atttypmod)
len = atttypmod; /* clip the string at max length */
result = (char *) palloc(len);
result = (VarChar *) palloc(len);
VARSIZE(result) = len;
strncpy(VARDATA(result), s, len - VARHDRSZ);
memcpy(VARDATA(result), s, len - VARHDRSZ);
#ifdef CYR_RECODE
convertstr(result + VARHDRSZ, len, 0);
convertstr(VARDATA(result), len, 0);
#endif
return result;
PG_RETURN_VARCHAR_P(result);
}
char *
varcharout(char *s)
Datum
varcharout(PG_FUNCTION_ARGS)
{
VarChar *s = PG_GETARG_VARCHAR_P(0);
char *result;
int len;
if (s == NULL)
{
result = (char *) palloc(2);
result[0] = '-';
result[1] = '\0';
}
else
{
len = VARSIZE(s) - VARHDRSZ;
result = (char *) palloc(len + 1);
StrNCpy(result, VARDATA(s), len + 1);
}
len = VARSIZE(s) - VARHDRSZ;
result = (char *) palloc(len + 1);
StrNCpy(result, VARDATA(s), len + 1); /* copy and add null term */
#ifdef CYR_RECODE
convertstr(result, len, 1);
#endif
return result;
PG_RETURN_CSTRING(result);
}
/* varchar()
* Converts a varchar() type to the specified size.
* slen is the length specified in () plus VARHDRSZ bytes.
*/
char *
varchar(char *s, int32 slen)
Datum
varchar(PG_FUNCTION_ARGS)
{
char *result;
VarChar *s = PG_GETARG_VARCHAR_P(0);
int32 slen = PG_GETARG_INT32(1);
VarChar *result;
int len;
if (s == NULL)
return (char *) NULL;
len = VARSIZE(s);
if (slen < (int32) VARHDRSZ || len <= slen)
return (char *) s;
PG_RETURN_VARCHAR_P(s);
/* only reach here if we need to truncate string... */
@@ -422,23 +404,25 @@ varchar(char *s, int32 slen)
len = slen - VARHDRSZ;
#endif
result = (char *) palloc(slen);
result = (VarChar *) palloc(slen);
VARSIZE(result) = slen;
strncpy(VARDATA(result), VARDATA(s), len);
memcpy(VARDATA(result), VARDATA(s), len);
return result;
} /* varchar() */
PG_RETURN_VARCHAR_P(result);
}
/* _varchar()
* Converts an array of varchar() type to the specified size.
* Converts an array of varchar() elements to the specified size.
* len is the length specified in () plus VARHDRSZ bytes.
*/
ArrayType *
_varchar(ArrayType *v, int32 len)
Datum
_varchar(PG_FUNCTION_ARGS)
{
ArrayType *v = (ArrayType *) PG_GETARG_VARLENA_P(0);
int32 len = PG_GETARG_INT32(1);
FunctionCallInfoData locfcinfo;
Datum result;
/* Since varchar() is a built-in function, we should only need to
/*
* Since varchar() is a built-in function, we should only need to
* look it up once per run.
*/
static FmgrInfo varchar_finfo;
@@ -453,9 +437,7 @@ _varchar(ArrayType *v, int32 len)
locfcinfo.arg[0] = PointerGetDatum(v);
locfcinfo.arg[1] = Int32GetDatum(len);
result = array_map(&locfcinfo, VARCHAROID, VARCHAROID);
return (ArrayType *) DatumGetPointer(result);
return array_map(&locfcinfo, VARCHAROID, VARCHAROID);
}

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.58 2000/04/12 17:15:52 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.59 2000/06/13 07:35:08 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -323,21 +323,19 @@ textcat(text *t1, text *t2)
* Formerly returned the entire string; now returns a portion.
* - Thomas Lockhart 1998-12-10
*/
text *
text_substr(text *string, int32 m, int32 n)
Datum
text_substr(PG_FUNCTION_ARGS)
{
text *string = PG_GETARG_TEXT_P(0);
int32 m = PG_GETARG_INT32(1);
int32 n = PG_GETARG_INT32(2);
text *ret;
int len;
#ifdef MULTIBYTE
int i;
char *p;
#endif
if (string == (text *) NULL)
return string;
len = VARSIZE(string) - VARHDRSZ;
#ifdef MULTIBYTE
len = pg_mbstrlen_with_len(VARDATA(string), len);
@@ -374,13 +372,14 @@ text_substr(text *string, int32 m, int32 n)
p += pg_mblen(p);
n = p - (VARDATA(string) + m);
#endif
ret = (text *) palloc(VARHDRSZ + n);
VARSIZE(ret) = VARHDRSZ + n;
memcpy(VARDATA(ret), VARDATA(string) + m, n);
return ret;
} /* text_substr() */
PG_RETURN_TEXT_P(ret);
}
/*
* textpos -
@@ -636,19 +635,17 @@ byteaoctetlen(bytea *v)
* byteaGetByte
*
* this routine treats "bytea" as an array of bytes.
* It returns the Nth byte (a number between 0 and 255) or
* it dies if the length of this array is less than n.
* It returns the Nth byte (a number between 0 and 255).
*-------------------------------------------------------------
*/
int32
byteaGetByte(bytea *v, int32 n)
Datum
byteaGetByte(PG_FUNCTION_ARGS)
{
bytea *v = PG_GETARG_BYTEA_P(0);
int32 n = PG_GETARG_INT32(1);
int len;
int byte;
if (!PointerIsValid(v))
return 0;
len = VARSIZE(v) - VARHDRSZ;
if (n < 0 || n >= len)
@@ -657,7 +654,7 @@ byteaGetByte(bytea *v, int32 n)
byte = ((unsigned char *) VARDATA(v))[n];
return (int32) byte;
PG_RETURN_INT32(byte);
}
/*-------------------------------------------------------------
@@ -665,21 +662,19 @@ byteaGetByte(bytea *v, int32 n)
*
* This routine treats a "bytea" type like an array of bits.
* It returns the value of the Nth bit (0 or 1).
* If 'n' is out of range, it dies!
*
*-------------------------------------------------------------
*/
int32
byteaGetBit(bytea *v, int32 n)
Datum
byteaGetBit(PG_FUNCTION_ARGS)
{
bytea *v = PG_GETARG_BYTEA_P(0);
int32 n = PG_GETARG_INT32(1);
int byteNo,
bitNo;
int len;
int byte;
if (!PointerIsValid(v))
return 0;
len = VARSIZE(v) - VARHDRSZ;
if (n < 0 || n >= len * 8)
@@ -692,9 +687,9 @@ byteaGetBit(bytea *v, int32 n)
byte = ((unsigned char *) VARDATA(v))[byteNo];
if (byte & (1 << bitNo))
return (int32) 1;
PG_RETURN_INT32(1);
else
return (int32) 0;
PG_RETURN_INT32(0);
}
/*-------------------------------------------------------------
@@ -705,15 +700,15 @@ byteaGetBit(bytea *v, int32 n)
*
*-------------------------------------------------------------
*/
bytea *
byteaSetByte(bytea *v, int32 n, int32 newByte)
Datum
byteaSetByte(PG_FUNCTION_ARGS)
{
bytea *v = PG_GETARG_BYTEA_P(0);
int32 n = PG_GETARG_INT32(1);
int32 newByte = PG_GETARG_INT32(2);
int len;
bytea *res;
if (!PointerIsValid(v))
return 0;
len = VARSIZE(v) - VARHDRSZ;
if (n < 0 || n >= len)
@@ -731,7 +726,7 @@ byteaSetByte(bytea *v, int32 n, int32 newByte)
*/
((unsigned char *) VARDATA(res))[n] = newByte;
return res;
PG_RETURN_BYTEA_P(res);
}
/*-------------------------------------------------------------
@@ -742,9 +737,12 @@ byteaSetByte(bytea *v, int32 n, int32 newByte)
*
*-------------------------------------------------------------
*/
bytea *
byteaSetBit(bytea *v, int32 n, int32 newBit)
Datum
byteaSetBit(PG_FUNCTION_ARGS)
{
bytea *v = PG_GETARG_BYTEA_P(0);
int32 n = PG_GETARG_INT32(1);
int32 newBit = PG_GETARG_INT32(2);
bytea *res;
int len;
int oldByte,
@@ -752,9 +750,6 @@ byteaSetBit(bytea *v, int32 n, int32 newBit)
int byteNo,
bitNo;
if (!PointerIsValid(v))
return NULL;
len = VARSIZE(v) - VARHDRSZ;
if (n < 0 || n >= len * 8)
@@ -771,24 +766,24 @@ byteaSetBit(bytea *v, int32 n, int32 newBit)
elog(ERROR, "byteaSetBit: new bit must be 0 or 1");
/*
* get the byte where the bit we want is stored.
* Make a copy of the original varlena.
*/
oldByte = byteaGetByte(v, byteNo);
res = (bytea *) palloc(VARSIZE(v));
memcpy((char *) res, (char *) v, VARSIZE(v));
/*
* calculate the new value for that byte
* Update the byte.
*/
oldByte = ((unsigned char *) VARDATA(res))[byteNo];
if (newBit == 0)
newByte = oldByte & (~(1 << bitNo));
else
newByte = oldByte | (1 << bitNo);
/*
* NOTE: 'byteaSetByte' creates a copy of 'v' & sets the byte.
*/
res = byteaSetByte(v, byteNo, newByte);
((unsigned char *) VARDATA(res))[byteNo] = newByte;
return res;
PG_RETURN_BYTEA_P(res);
}