aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2002-08-24 15:00:47 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2002-08-24 15:00:47 +0000
commit976246cc7e4d8b673fc62fe6daa61c76d94af1af (patch)
treeb61ea3f5bbd8fa025587754bc90c3fd43889337c /src/backend/utils/adt
parentcf4d885c67744637d82f6fec657e8205578c5905 (diff)
downloadpostgresql-976246cc7e4d8b673fc62fe6daa61c76d94af1af.tar.gz
postgresql-976246cc7e4d8b673fc62fe6daa61c76d94af1af.zip
The cstring datatype can now be copied, passed around, etc. The typlen
value '-2' is used to indicate a variable-width type whose width is computed as strlen(datum)+1. Everything that looks at typlen is updated except for array support, which Joe Conway is working on; at the moment it wouldn't work to try to create an array of cstring.
Diffstat (limited to 'src/backend/utils/adt')
-rw-r--r--src/backend/utils/adt/datum.c56
-rw-r--r--src/backend/utils/adt/format_type.c8
-rw-r--r--src/backend/utils/adt/pseudotypes.c15
3 files changed, 51 insertions, 28 deletions
diff --git a/src/backend/utils/adt/datum.c b/src/backend/utils/adt/datum.c
index 7c0dbeeb49c..0a751ff1dff 100644
--- a/src/backend/utils/adt/datum.c
+++ b/src/backend/utils/adt/datum.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/datum.c,v 1.23 2002/06/20 20:29:37 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/datum.c,v 1.24 2002/08/24 15:00:46 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -19,15 +19,19 @@
* Datum itself (i.e. no pointers involved!). In this case the
* length of the type is always greater than zero and not more than
* "sizeof(Datum)"
- * B) if a type is not "byVal" and it has a fixed length, then
- * the "Datum" always contain a pointer to a stream of bytes.
- * The number of significant bytes are always equal to the length of the
- * type.
- * C) if a type is not "byVal" and is of variable length (i.e. it has
- * length == -1) then "Datum" always points to a "struct varlena".
+ *
+ * B) if a type is not "byVal" and it has a fixed length (typlen > 0),
+ * then the "Datum" always contains a pointer to a stream of bytes.
+ * The number of significant bytes are always equal to the typlen.
+ *
+ * C) if a type is not "byVal" and has typlen == -1,
+ * then the "Datum" always points to a "struct varlena".
* This varlena structure has information about the actual length of this
* particular instance of the type and about its value.
*
+ * D) if a type is not "byVal" and has typlen == -2,
+ * then the "Datum" always points to a null-terminated C string.
+ *
* Note that we do not treat "toasted" datums specially; therefore what
* will be copied or compared is the compressed data or toast reference.
*/
@@ -36,17 +40,15 @@
#include "utils/datum.h"
+
/*-------------------------------------------------------------------------
* datumGetSize
*
* Find the "real" size of a datum, given the datum value,
- * whether it is a "by value", and its length.
+ * whether it is a "by value", and the declared type length.
*
- * To cut a long story short, usually the real size is equal to the
- * type length, with the exception of variable length types which have
- * a length equal to -1. In this case, we have to look at the value of
- * the datum itself (which is a pointer to a 'varlena' struct) to find
- * its size.
+ * This is essentially an out-of-line version of the att_addlength()
+ * macro in access/tupmacs.h. We do a tad more error checking though.
*-------------------------------------------------------------------------
*/
Size
@@ -62,19 +64,33 @@ datumGetSize(Datum value, bool typByVal, int typLen)
}
else
{
- if (typLen == -1)
+ if (typLen > 0)
+ {
+ /* Fixed-length pass-by-ref type */
+ size = (Size) typLen;
+ }
+ else if (typLen == -1)
{
- /* Assume it is a varlena datatype */
+ /* It is a varlena datatype */
struct varlena *s = (struct varlena *) DatumGetPointer(value);
if (!PointerIsValid(s))
elog(ERROR, "datumGetSize: Invalid Datum Pointer");
- size = (Size) VARSIZE(s);
+ size = (Size) VARATT_SIZE(s);
+ }
+ else if (typLen == -2)
+ {
+ /* It is a cstring datatype */
+ char *s = (char *) DatumGetPointer(value);
+
+ if (!PointerIsValid(s))
+ elog(ERROR, "datumGetSize: Invalid Datum Pointer");
+ size = (Size) (strlen(s) + 1);
}
else
{
- /* Fixed-length pass-by-ref type */
- size = (Size) typLen;
+ elog(ERROR, "datumGetSize: Invalid typLen %d", typLen);
+ size = 0; /* keep compiler quiet */
}
}
@@ -159,7 +175,9 @@ datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen)
/*
* just compare the two datums. NOTE: just comparing "len" bytes
* will not do the work, because we do not know how these bytes
- * are aligned inside the "Datum".
+ * are aligned inside the "Datum". We assume instead that any
+ * given datatype is consistent about how it fills extraneous
+ * bits in the Datum.
*/
res = (value1 == value2);
}
diff --git a/src/backend/utils/adt/format_type.c b/src/backend/utils/adt/format_type.c
index e3d78f1f908..2aca3d7fc5f 100644
--- a/src/backend/utils/adt/format_type.c
+++ b/src/backend/utils/adt/format_type.c
@@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/format_type.c,v 1.31 2002/08/04 06:44:47 thomas Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/format_type.c,v 1.32 2002/08/24 15:00:46 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -149,7 +149,7 @@ format_type_internal(Oid type_oid, int32 typemod,
array_base_type = typeform->typelem;
if (array_base_type != InvalidOid &&
- typeform->typlen < 0 &&
+ typeform->typlen == -1 &&
typeform->typtype != 'd')
{
/* Switch our attention to the array element type */
@@ -411,11 +411,11 @@ format_type_internal(Oid type_oid, int32 typemod,
/*
- * type_maximum_size --- determine maximum width of a varlena column
+ * type_maximum_size --- determine maximum width of a variable-width column
*
* If the max width is indeterminate, return -1. In particular, we return
* -1 for any type not known to this routine. We assume the caller has
- * already determined that the type is a varlena type, so it's not
+ * already determined that the type is a variable-width type, so it's not
* necessary to look up the type's pg_type tuple here.
*
* This may appear unrelated to format_type(), but in fact the two routines
diff --git a/src/backend/utils/adt/pseudotypes.c b/src/backend/utils/adt/pseudotypes.c
index 7e5cd2950d8..cb374e8f93c 100644
--- a/src/backend/utils/adt/pseudotypes.c
+++ b/src/backend/utils/adt/pseudotypes.c
@@ -16,7 +16,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/pseudotypes.c,v 1.1 2002/08/22 00:01:43 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/pseudotypes.c,v 1.2 2002/08/24 15:00:46 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -50,24 +50,29 @@ record_out(PG_FUNCTION_ARGS)
/*
* cstring_in - input routine for pseudo-type CSTRING.
+ *
+ * We might as well allow this to support constructs like "foo_in('blah')".
*/
Datum
cstring_in(PG_FUNCTION_ARGS)
{
- elog(ERROR, "Cannot accept a constant of type %s", "CSTRING");
+ char *str = PG_GETARG_CSTRING(0);
- PG_RETURN_VOID(); /* keep compiler quiet */
+ PG_RETURN_CSTRING(pstrdup(str));
}
/*
* cstring_out - output routine for pseudo-type CSTRING.
+ *
+ * We allow this mainly so that "SELECT some_output_function(...)" does
+ * what the user will expect.
*/
Datum
cstring_out(PG_FUNCTION_ARGS)
{
- elog(ERROR, "Cannot display a value of type %s", "CSTRING");
+ char *str = PG_GETARG_CSTRING(0);
- PG_RETURN_VOID(); /* keep compiler quiet */
+ PG_RETURN_CSTRING(pstrdup(str));
}