aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2000-08-03 16:35:08 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2000-08-03 16:35:08 +0000
commitc298d74d4957845bb03a67092c30b53e5e0d01c2 (patch)
tree4dca901e87c14a249de21374da867db1059b8462 /src/backend/utils/adt
parent1bd3a8f58bb8147628a85643b0246051a6e4e5ee (diff)
downloadpostgresql-c298d74d4957845bb03a67092c30b53e5e0d01c2.tar.gz
postgresql-c298d74d4957845bb03a67092c30b53e5e0d01c2.zip
More functions updated to new fmgr style --- money, name, tid datatypes.
We're reaching the mopup stage here (good thing too, this is getting tedious).
Diffstat (limited to 'src/backend/utils/adt')
-rw-r--r--src/backend/utils/adt/cash.c184
-rw-r--r--src/backend/utils/adt/name.c98
-rw-r--r--src/backend/utils/adt/selfuncs.c7
-rw-r--r--src/backend/utils/adt/tid.c86
4 files changed, 179 insertions, 196 deletions
diff --git a/src/backend/utils/adt/cash.c b/src/backend/utils/adt/cash.c
index e1a68940a84..d924bf3fe09 100644
--- a/src/backend/utils/adt/cash.c
+++ b/src/backend/utils/adt/cash.c
@@ -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.44 2000/08/01 18:29:35 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.45 2000/08/03 16:34:22 tgl Exp $
*/
#include <limits.h>
@@ -66,11 +66,11 @@ CashGetDatum(Cash value)
* XXX UNHACK Allow the currency symbol to be multi-byte.
* - thomas 1998-03-01
*/
-Cash *
-cash_in(const char *str)
+Datum
+cash_in(PG_FUNCTION_ARGS)
{
- Cash *result;
-
+ char *str = PG_GETARG_CSTRING(0);
+ Cash result;
Cash value = 0;
Cash dec = 0;
Cash sgn = 1;
@@ -200,17 +200,14 @@ cash_in(const char *str)
if (*s != '\0')
elog(ERROR, "Bad money external representation %s", str);
- if (!PointerIsValid(result = palloc(sizeof(Cash))))
- elog(ERROR, "Memory allocation failed, can't input cash '%s'", str);
-
- *result = (value * sgn);
+ result = (value * sgn);
#ifdef CASHDEBUG
- printf("cashin- result is %d\n", *result);
+ printf("cashin- result is %d\n", result);
#endif
- return result;
-} /* cash_in() */
+ PG_RETURN_CASH(result);
+}
/* cash_out()
@@ -218,10 +215,10 @@ cash_in(const char *str)
* XXX HACK This code appears to assume US conventions for
* positive-valued amounts. - tgl 97/04/14
*/
-const char *
-cash_out(Cash *in_value)
+Datum
+cash_out(PG_FUNCTION_ARGS)
{
- Cash value = *in_value;
+ Cash value = PG_GETARG_CASH(0);
char *result;
char buf[CASH_BUFSZ];
int minus = 0;
@@ -323,103 +320,95 @@ cash_out(Cash *in_value)
strcpy(result, buf + count);
}
- return result;
-} /* cash_out() */
+ PG_RETURN_CSTRING(result);
+}
-bool
-cash_eq(Cash *c1, Cash *c2)
+Datum
+cash_eq(PG_FUNCTION_ARGS)
{
- if (!PointerIsValid(c1) || !PointerIsValid(c2))
- return FALSE;
+ Cash c1 = PG_GETARG_CASH(0);
+ Cash c2 = PG_GETARG_CASH(1);
- return *c1 == *c2;
-} /* cash_eq() */
+ PG_RETURN_BOOL(c1 == c2);
+}
-bool
-cash_ne(Cash *c1, Cash *c2)
+Datum
+cash_ne(PG_FUNCTION_ARGS)
{
- if (!PointerIsValid(c1) || !PointerIsValid(c2))
- return FALSE;
+ Cash c1 = PG_GETARG_CASH(0);
+ Cash c2 = PG_GETARG_CASH(1);
- return *c1 != *c2;
-} /* cash_ne() */
+ PG_RETURN_BOOL(c1 != c2);
+}
-bool
-cash_lt(Cash *c1, Cash *c2)
+Datum
+cash_lt(PG_FUNCTION_ARGS)
{
- if (!PointerIsValid(c1) || !PointerIsValid(c2))
- return FALSE;
+ Cash c1 = PG_GETARG_CASH(0);
+ Cash c2 = PG_GETARG_CASH(1);
- return *c1 < *c2;
-} /* cash_lt() */
+ PG_RETURN_BOOL(c1 < c2);
+}
-bool
-cash_le(Cash *c1, Cash *c2)
+Datum
+cash_le(PG_FUNCTION_ARGS)
{
- if (!PointerIsValid(c1) || !PointerIsValid(c2))
- return FALSE;
+ Cash c1 = PG_GETARG_CASH(0);
+ Cash c2 = PG_GETARG_CASH(1);
- return *c1 <= *c2;
-} /* cash_le() */
+ PG_RETURN_BOOL(c1 <= c2);
+}
-bool
-cash_gt(Cash *c1, Cash *c2)
+Datum
+cash_gt(PG_FUNCTION_ARGS)
{
- if (!PointerIsValid(c1) || !PointerIsValid(c2))
- return FALSE;
+ Cash c1 = PG_GETARG_CASH(0);
+ Cash c2 = PG_GETARG_CASH(1);
- return *c1 > *c2;
-} /* cash_gt() */
+ PG_RETURN_BOOL(c1 > c2);
+}
-bool
-cash_ge(Cash *c1, Cash *c2)
+Datum
+cash_ge(PG_FUNCTION_ARGS)
{
- if (!PointerIsValid(c1) || !PointerIsValid(c2))
- return FALSE;
+ Cash c1 = PG_GETARG_CASH(0);
+ Cash c2 = PG_GETARG_CASH(1);
- return *c1 >= *c2;
-} /* cash_ge() */
+ PG_RETURN_BOOL(c1 >= c2);
+}
/* cash_pl()
* Add two cash values.
*/
-Cash *
-cash_pl(Cash *c1, Cash *c2)
+Datum
+cash_pl(PG_FUNCTION_ARGS)
{
- Cash *result;
-
- if (!PointerIsValid(c1) || !PointerIsValid(c2))
- return NULL;
-
- if (!PointerIsValid(result = palloc(sizeof(Cash))))
- elog(ERROR, "Memory allocation failed, can't add cash");
+ Cash c1 = PG_GETARG_CASH(0);
+ Cash c2 = PG_GETARG_CASH(1);
+ Cash result;
- *result = (*c1 + *c2);
+ result = c1 + c2;
- return result;
-} /* cash_pl() */
+ PG_RETURN_CASH(result);
+}
/* cash_mi()
* Subtract two cash values.
*/
-Cash *
-cash_mi(Cash *c1, Cash *c2)
+Datum
+cash_mi(PG_FUNCTION_ARGS)
{
- Cash *result;
-
- if (!PointerIsValid(c1) || !PointerIsValid(c2))
- return NULL;
-
- if (!PointerIsValid(result = palloc(sizeof(Cash))))
- elog(ERROR, "Memory allocation failed, can't subtract cash");
+ Cash c1 = PG_GETARG_CASH(0);
+ Cash c2 = PG_GETARG_CASH(1);
+ Cash result;
- *result = (*c1 - *c2);
+ result = c1 - c2;
- return result;
-} /* cash_mi() */
+ PG_RETURN_CASH(result);
+}
/* cash_mul_flt8()
@@ -626,41 +615,32 @@ cash_div_int2(PG_FUNCTION_ARGS)
/* cashlarger()
* Return larger of two cash values.
*/
-Cash *
-cashlarger(Cash *c1, Cash *c2)
+Datum
+cashlarger(PG_FUNCTION_ARGS)
{
- Cash *result;
-
- if (!PointerIsValid(c1) || !PointerIsValid(c2))
- return NULL;
-
- if (!PointerIsValid(result = palloc(sizeof(Cash))))
- elog(ERROR, "Memory allocation failed, can't return larger cash");
-
- *result = ((*c1 > *c2) ? *c1 : *c2);
+ Cash c1 = PG_GETARG_CASH(0);
+ Cash c2 = PG_GETARG_CASH(1);
+ Cash result;
- return result;
-} /* cashlarger() */
+ result = (c1 > c2) ? c1 : c2;
+ PG_RETURN_CASH(result);
+}
/* cashsmaller()
* Return smaller of two cash values.
*/
-Cash *
-cashsmaller(Cash *c1, Cash *c2)
+Datum
+cashsmaller(PG_FUNCTION_ARGS)
{
- Cash *result;
-
- if (!PointerIsValid(c1) || !PointerIsValid(c2))
- return NULL;
-
- if (!PointerIsValid(result = palloc(sizeof(Cash))))
- elog(ERROR, "Memory allocation failed, can't return smaller cash");
+ Cash c1 = PG_GETARG_CASH(0);
+ Cash c2 = PG_GETARG_CASH(1);
+ Cash result;
- *result = ((*c1 < *c2) ? *c1 : *c2);
+ result = (c1 < c2) ? c1 : c2;
- return result;
-} /* cashsmaller() */
+ PG_RETURN_CASH(result);
+}
/* cash_words()
diff --git a/src/backend/utils/adt/name.c b/src/backend/utils/adt/name.c
index 630070ce3b3..3acf40e0d43 100644
--- a/src/backend/utils/adt/name.c
+++ b/src/backend/utils/adt/name.c
@@ -12,11 +12,13 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/name.c,v 1.28 2000/04/12 17:15:50 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/name.c,v 1.29 2000/08/03 16:34:22 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
+
+#include "miscadmin.h"
#include "utils/builtins.h"
/*****************************************************************************
@@ -31,14 +33,13 @@
* [Old] Currently if strlen(s) < NAMEDATALEN, the extra chars are nulls
* Now, always NULL terminated
*/
-NameData *
-namein(const char *s)
+Datum
+namein(PG_FUNCTION_ARGS)
{
+ char *s = PG_GETARG_CSTRING(0);
NameData *result;
int len;
- if (s == NULL)
- return NULL;
result = (NameData *) palloc(NAMEDATALEN);
/* always keep it null-padded */
StrNCpy(NameStr(*result), s, NAMEDATALEN);
@@ -48,19 +49,18 @@ namein(const char *s)
*(NameStr(*result) + len) = '\0';
len++;
}
- return result;
+ PG_RETURN_NAME(result);
}
/*
- * nameout - converts internal reprsentation to "..."
+ * nameout - converts internal representation to "..."
*/
-char *
-nameout(const NameData *s)
+Datum
+nameout(PG_FUNCTION_ARGS)
{
- if (s == NULL)
- return "-";
- else
- return pstrdup(NameStr(*s));
+ Name s = PG_GETARG_NAME(0);
+
+ PG_RETURN_CSTRING(pstrdup(NameStr(*s)));
}
@@ -82,57 +82,67 @@ nameout(const NameData *s)
* namege - returns 1 iff a <= b
*
*/
-bool
-nameeq(const NameData *arg1, const NameData *arg2)
+Datum
+nameeq(PG_FUNCTION_ARGS)
{
- if (!arg1 || !arg2)
- return 0;
- else
- return (bool) (strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) == 0);
+ Name arg1 = PG_GETARG_NAME(0);
+ Name arg2 = PG_GETARG_NAME(1);
+
+ PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) == 0);
}
-bool
-namene(const NameData *arg1, const NameData *arg2)
+Datum
+namene(PG_FUNCTION_ARGS)
{
- if (arg1 == NULL || arg2 == NULL)
- return (bool) 0;
- return (bool) (strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) != 0);
+ Name arg1 = PG_GETARG_NAME(0);
+ Name arg2 = PG_GETARG_NAME(1);
+
+ PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) != 0);
}
-bool
-namelt(const NameData *arg1, const NameData *arg2)
+Datum
+namelt(PG_FUNCTION_ARGS)
{
- if (arg1 == NULL || arg2 == NULL)
- return (bool) 0;
- return (bool) (strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) < 0);
+ Name arg1 = PG_GETARG_NAME(0);
+ Name arg2 = PG_GETARG_NAME(1);
+
+ PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) < 0);
}
-bool
-namele(const NameData *arg1, const NameData *arg2)
+Datum
+namele(PG_FUNCTION_ARGS)
{
- if (arg1 == NULL || arg2 == NULL)
- return (bool) 0;
- return (bool) (strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) <= 0);
+ Name arg1 = PG_GETARG_NAME(0);
+ Name arg2 = PG_GETARG_NAME(1);
+
+ PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) <= 0);
}
-bool
-namegt(const NameData *arg1, const NameData *arg2)
+Datum
+namegt(PG_FUNCTION_ARGS)
{
- if (arg1 == NULL || arg2 == NULL)
- return (bool) 0;
+ Name arg1 = PG_GETARG_NAME(0);
+ Name arg2 = PG_GETARG_NAME(1);
- return (bool) (strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) > 0);
+ PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) > 0);
}
-bool
-namege(const NameData *arg1, const NameData *arg2)
+Datum
+namege(PG_FUNCTION_ARGS)
{
- if (arg1 == NULL || arg2 == NULL)
- return (bool) 0;
+ Name arg1 = PG_GETARG_NAME(0);
+ Name arg2 = PG_GETARG_NAME(1);
- return (bool) (strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) >= 0);
+ PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) >= 0);
}
+/* SQL-function interface to GetPgUserName() */
+Datum
+getpgusername(PG_FUNCTION_ARGS)
+{
+ PG_RETURN_DATUM(DirectFunctionCall1(namein,
+ CStringGetDatum(GetPgUserName())));
+}
/* (see char.c for comparison/operation routines) */
diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index 16b7964392e..e31860c4d35 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -15,7 +15,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/selfuncs.c,v 1.77 2000/08/03 00:58:22 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/selfuncs.c,v 1.78 2000/08/03 16:34:22 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -1889,7 +1889,8 @@ string_lessthan(const char *str1, const char *str2, Oid datatype)
break;
case NAMEOID:
- result = namelt((NameData *) datum1, (NameData *) datum2);
+ result = DatumGetBool(DirectFunctionCall2(namelt,
+ datum1, datum2));
break;
default:
@@ -1933,7 +1934,7 @@ string_to_datum(const char *str, Oid datatype)
* varchar constants too...
*/
if (datatype == NAMEOID)
- return PointerGetDatum(namein((char *) str));
+ return DirectFunctionCall1(namein, CStringGetDatum(str));
else
return DirectFunctionCall1(textin, CStringGetDatum(str));
}
diff --git a/src/backend/utils/adt/tid.c b/src/backend/utils/adt/tid.c
index 7d0f4447c2d..0f7bd5f9159 100644
--- a/src/backend/utils/adt/tid.c
+++ b/src/backend/utils/adt/tid.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/tid.c,v 1.21 2000/07/05 23:11:35 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/tid.c,v 1.22 2000/08/03 16:34:23 tgl Exp $
*
* NOTES
* input routine largely stolen from boxin().
@@ -21,6 +21,11 @@
#include "access/heapam.h"
#include "utils/builtins.h"
+#define DatumGetItemPointer(X) ((ItemPointer) DatumGetPointer(X))
+#define ItemPointerGetDatum(X) PointerGetDatum(X)
+#define PG_GETARG_ITEMPOINTER(n) DatumGetItemPointer(PG_GETARG_DATUM(n))
+#define PG_RETURN_ITEMPOINTER(x) return ItemPointerGetDatum(x)
+
#define LDELIM '('
#define RDELIM ')'
#define DELIM ','
@@ -30,30 +35,23 @@
* tidin
* ----------------------------------------------------------------
*/
-ItemPointer
-tidin(const char *str)
+Datum
+tidin(PG_FUNCTION_ARGS)
{
- const char *p,
+ char *str = PG_GETARG_CSTRING(0);
+ char *p,
*coord[NTIDARGS];
int i;
ItemPointer result;
-
BlockNumber blockNumber;
OffsetNumber offsetNumber;
- if (str == NULL)
- return NULL;
-
for (i = 0, p = str; *p && i < NTIDARGS && *p != RDELIM; p++)
if (*p == DELIM || (*p == LDELIM && !i))
coord[i++] = p + 1;
- /* if (i < NTIDARGS - 1) */
if (i < NTIDARGS)
- {
- elog(ERROR, "%s invalid tid format", str);
- return NULL;
- }
+ elog(ERROR, "invalid tid format: '%s'", str);
blockNumber = (BlockNumber) atoi(coord[0]);
offsetNumber = (OffsetNumber) atoi(coord[1]);
@@ -62,67 +60,61 @@ tidin(const char *str)
ItemPointerSet(result, blockNumber, offsetNumber);
- return result;
+ PG_RETURN_ITEMPOINTER(result);
}
/* ----------------------------------------------------------------
* tidout
* ----------------------------------------------------------------
*/
-char *
-tidout(ItemPointer itemPtr)
+Datum
+tidout(PG_FUNCTION_ARGS)
{
+ ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
+ BlockId blockId;
BlockNumber blockNumber;
OffsetNumber offsetNumber;
- BlockId blockId;
char buf[32];
- char *str;
static char *invalidTid = "()";
- if (!itemPtr || !ItemPointerIsValid(itemPtr))
- {
- str = palloc(strlen(invalidTid));
- strcpy(str, invalidTid);
- return str;
- }
+ if (!ItemPointerIsValid(itemPtr))
+ PG_RETURN_CSTRING(pstrdup(invalidTid));
blockId = &(itemPtr->ip_blkid);
blockNumber = BlockIdGetBlockNumber(blockId);
offsetNumber = itemPtr->ip_posid;
- sprintf(buf, "(%d,%d)", blockNumber, offsetNumber);
-
- str = (char *) palloc(strlen(buf) + 1);
- strcpy(str, buf);
+ sprintf(buf, "(%d,%d)", (int) blockNumber, (int) offsetNumber);
- return str;
+ PG_RETURN_CSTRING(pstrdup(buf));
}
/*****************************************************************************
* PUBLIC ROUTINES *
*****************************************************************************/
-bool
-tideq(ItemPointer arg1, ItemPointer arg2)
+Datum
+tideq(PG_FUNCTION_ARGS)
{
- if ((!arg1) || (!arg2))
- return false;
+ ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
+ ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
- return (BlockIdGetBlockNumber(&(arg1->ip_blkid)) ==
- BlockIdGetBlockNumber(&(arg2->ip_blkid)) &&
- arg1->ip_posid == arg2->ip_posid);
+ PG_RETURN_BOOL(BlockIdGetBlockNumber(&(arg1->ip_blkid)) ==
+ BlockIdGetBlockNumber(&(arg2->ip_blkid)) &&
+ arg1->ip_posid == arg2->ip_posid);
}
#ifdef NOT_USED
-bool
-tidne(ItemPointer arg1, ItemPointer arg2)
+Datum
+tidne(PG_FUNCTION_ARGS)
{
- if ((!arg1) || (!arg2))
- return false;
- return (BlockIdGetBlockNumber(&(arg1->ip_blkid)) !=
- BlockIdGetBlockNumber(&(arg2->ip_blkid)) ||
- arg1->ip_posid != arg2->ip_posid);
+ ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
+ ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
+
+ PG_RETURN_BOOL(BlockIdGetBlockNumber(&(arg1->ip_blkid)) !=
+ BlockIdGetBlockNumber(&(arg2->ip_blkid)) ||
+ arg1->ip_posid != arg2->ip_posid);
}
#endif
@@ -135,7 +127,7 @@ Datum
currtid_byreloid(PG_FUNCTION_ARGS)
{
Oid reloid = PG_GETARG_OID(0);
- ItemPointer tid = (ItemPointer) PG_GETARG_POINTER(1);
+ ItemPointer tid = PG_GETARG_ITEMPOINTER(1);
ItemPointer result,
ret;
Relation rel;
@@ -152,14 +144,14 @@ currtid_byreloid(PG_FUNCTION_ARGS)
else
elog(ERROR, "Relation %u not found", reloid);
- PG_RETURN_POINTER(result);
+ PG_RETURN_ITEMPOINTER(result);
}
Datum
currtid_byrelname(PG_FUNCTION_ARGS)
{
text *relname = PG_GETARG_TEXT_P(0);
- ItemPointer tid = (ItemPointer) PG_GETARG_POINTER(1);
+ ItemPointer tid = PG_GETARG_ITEMPOINTER(1);
ItemPointer result,
ret;
char *str;
@@ -182,5 +174,5 @@ currtid_byrelname(PG_FUNCTION_ARGS)
pfree(str);
- PG_RETURN_POINTER(result);
+ PG_RETURN_ITEMPOINTER(result);
}