|
PostgreSQL: Advanced SQL: User defined types
- Many types supported in base install
- Customise PostgreSQL to support complex types or application level types
- Create a type where you define the semantics (in C)
01 CREATE TYPE name (
02 INPUT = input_function,
03 OUTPUT = output_function
04 [ , RECEIVE = receive_function ]
05 [ , SEND = send_function ]
06 [ , INTERNALLENGTH = { internallength | VARIABLE } ]
07 [ , PASSEDBYVALUE ]
08 [ , ALIGNMENT = alignment ]
09 [ , STORAGE = storage ]
10 [ , DEFAULT = default ]
11 [ , ELEMENT = element ]
12 [ , DELIMITER = delimiter ]
13 )
- INPUT = function to receive input
- OUTPUT = function to output data from internal type
- Example: ISBN (taken from contrib/isbn_issn/)
01 typedef struct isbn
02 {
03 char num[13];
04 char pad[3];
05 } isbn;
06
07 isbn *
08 isbn_in(char *str)
09 {
10 isbn *result;
11
12 if (strlen(str) != 13)
13 {
14 ereport(ERROR,
15 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
16 errmsg("invalid ISBN: \"%s\"", str),
17 errdetail("incorrect length")));
18
19 return (NULL);
20 }
21 if (isbn_sum(str) != 0)
22 {
23 ereport(ERROR,
24 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
25 errmsg("invalid ISBN: \"%s\"", str),
26 errdetail("failed checksum")));
27 return (NULL);
28 }
29
30 result = (isbn *) palloc(sizeof(isbn));
31
32 strncpy(result->num, str, 13);
33 memset(result->pad, ' ', 3);
34 return (result);
35 }
36
37 char *
38 isbn_out(isbn * num)
39 {
40 char *result;
41
42 if (num == NULL)
43 return (NULL);
44
45 result = (char *) palloc(14);
46
47 result[0] = '\0';
48 strncat(result, num->num, 13);
49 return (result);
50 }
01 CREATE FUNCTION isbn_in(cstring)
02 RETURNS isbn
03 AS '/path/to/libisbn.so'
04 LANGUAGE 'C';
05
06 CREATE FUNCTION isbn_out(isbn)
07 RETURNS cstring
08 AS '/path/to/libisbn.so'
09 LANGUAGE 'C';
10
11 CREATE TYPE isbn (
12 INTERNALLENGTH = 16,
13 EXTERNALLENGTH = 13,
14 INPUT = isbn_in,
15 OUTPUT = isbn_out
16 );
- Also see: CREATE OPERATOR, CREATE OPERATOR CLASS (for indexes)
Page 27
|