Sql - How To Create An Abstract Data Type (Adt) In Postgresql
Very fast howto
V 0.1 of 2nd December 2003, by Laurent FALLET
Goal : Our objective here is to create an ADT called “complex” which consistsin the description of
a complex number, with a form z = a + i * b , a and b having type double.
First, let's have a look to the SQL request to define the “complex” type :
CREATE TYPE complex (internallength = 16,
input = complex_in,
output = complex_out,
alignment = double
);
We are giving the type's name, its length (in bytes), its alignment (storage alignment), and input and
outputfunctions. These functions will realize the conversion from the external textual
representation to the internal representation for input_function (and vice-versa for output_function).
We'll entercomplex in the table by putting values like (2,-5) for z = 2 – 5i and this representation
will be used by the operators and functions defined for the type.
So we need to define complex_in andcomplex_out before creating the type. We'll do this in C
language. Here is an abstract of the file “complex.c” :
#include "postgres.h"
typedef struct Complex
{
double
x;
double
y;
}
Complex;
/*Prototypes */
Complex *complex_in(char *str);
char
*complex_out(Complex * complex);
Complex *complex_add(Complex * a, Complex * b);
/* Input and Output functions */
Complex *
complex_in(char *str){
double
x;
double
y;
Complex *result;
}
if (sscanf(str, " ( %lf , %lf )", &x, &y) != 2)
{
elog(ERROR, "complex_in: error in parsing \"%s\"", str);
return NULL;
}
result = (Complex *)palloc(sizeof(Complex));
result->x = x;
result->y = y;
return result;
char *
complex_out(Complex * complex)
{
char
*result;
if (complex == NULL)
return NULL;
}
result =(char *) palloc(100);
snprintf(result, 100, "(%g,%g)", complex->x, complex->y);
return result;
/* New Operators */
Complex *
complex_add(Complex * a, Complex * b)
{
Complex *result;
}...
Regístrate para leer el documento completo.