0

I am trying to define a custom data type in postgreSQL that takes two arguments: a long long int (int8 in postgres) and a dynamic string (varchar or TEXT in postgres). I am able to get the long long int working but I am having troubles to implement the dynamic string. This is what I have in my c code for the in and out functions:

In function:

 Datum object3d_in(PG_FUNCTION_ARGS) {
    char* str = PG_GETARG_CSTRING(0);
    long long int timeStamp;
    char *temp;
    Object3d *result;

    if (sscanf(str, "(%lli, %s)", &timeStamp, temp) != 2)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                        errmsg("Invalid input syntax for object3d: \"%s\"",
                                str)));

    result = (Object3d *) palloc(sizeof(Object3d));
    result->timeStamp = timeStamp;

    result->object = (char*) palloc(sizeof(char)*(strlen(temp) + 1));
    sscanf(str, "(%lli, %s)", &timeStamp, result->object); //Reload
    PG_RETURN_POINTER(result);
}

Out function:

Datum object3d_out(PG_FUNCTION_ARGS) {
    Object3d *object3d = (Object3d *) PG_GETARG_POINTER(0);
    char *result;
    result = (char *) palloc(128);
    snprintf(result, 128, "[%lli, %s]", object3d->timeStamp, object3d->object);
    PG_RETURN_CSTRING(result);
}

postgres type commands:

CREATE TYPE object3d;

CREATE FUNCTION object3d_in(cstring)
RETURNS object3d
AS 'mdobject.dll'
LANGUAGE C IMMUTABLE STRICT;

CREATE FUNCTION object3d_out(object3d)
RETURNS cstring
AS 'mdobject.dll'
LANGUAGE C IMMUTABLE STRICT; 

CREATE TYPE object3d(
    INTERNALLENGTH = 128, 
    input = object3d_in,
    output = object3d_out
);

CREATE TABLE listobject3d (id integer, theobject object3d);
INSERT INTO listobject3d VALUES (random()*100, '(203,12 )'); 
INSERT INTO listobject3d VALUES (random()*100, '(20120202,r )');
INSERT INTO listobject3d VALUES (random()*100, '(20120203,c )');
INSERT INTO listobject3d VALUES (random()*100, '(20120203,triangle )');

Output:

SELECT * FROM listobject3d;

 id |     theobject
----+-------------------
 21 | [203, 12]
 42 | [20120202, /\x01]
 19 | [20120203, /\x01]
 33 | [20120203,     ]
(4 rows)

1 Answer 1

1

result data should be in varlena format - etc first four bytes should to carry complete length.

http://www.iphelp.ru/faq/15/ch06lev1sec1.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.