Re: user defined type - Mailing list pgsql-novice

From Kjetil Haaland
Subject Re: user defined type
Date
Msg-id 200411101437.49058.kjetil.haaland@student.uib.no
Whole thread Raw
In response to Re: user defined type  (Michael Fuhr <mike@fuhr.org>)
Responses Re: user defined type  (Michael Fuhr <mike@fuhr.org>)
List pgsql-novice
hello again

Thanks to you i have no managed to store a value and a string of variable
length in the structure. I thought it would be easy to expand it to two
strings and a value, by doing it the same way, but that didn't work out. When
i insert something into the type this is what happends:

1) the first string in the structure has one character( or the size of the
table that holds the first string) from the first string i gave, a blank
space and the second string i gave.
2)the second string is correct - has the second string i gave.

Is there anyway to have two or more strings in one type or do i have to save
them both in one string? This is my type and the in function so far:

typedef struct alignres {
  int32 length;
  int value;
  char fstring[1];
  char sstring[1];
}alignres;

Datum alignres_in(PG_FUNCTION_ARGS) {
  char *in = PG_GETARG_CSTRING(0);
  char *workstr = pstrdup(in);
  char *svalue = NULL;
  char *first = NULL;
  char *second = NULL;

  svalue = strtok(workstr, ", ");
  first = strtok(NULL, ", ");
  second = strtok(NULL, ")");

  int value = atoi(++svalue);
  alignres *result;
  result = (alignres *) palloc(sizeof(*result)+strlen(in));

  result->value = value;
  strcpy(result->fstring, first);
  strcpy(result->sstring, second);
  result->length=sizeof(*result)+strlen(result->fstring)+strlen(result->sstring);

  PG_RETURN_POINTER(result);
}

-Kjetil

pgsql-novice by date:

Previous
From: "Vishal Kashyap @ [Sai Hertz And Control Systems]"
Date:
Subject: Re: VACUUM ANALYZE : Is this a time consuming procedure?
Next
From: Michael Fuhr
Date:
Subject: Re: user defined type