ddbc icon indicating copy to clipboard operation
ddbc copied to clipboard

postgres. insert POD. Attempt to use an uninitialized VariantN

Open Rogni opened this issue 4 years ago • 2 comments

receive exception

2020-02-12T11:52:50.484:pgsqlddbc.d:executeUpdate:670 INSERT INTO custom_table(firstname,lastname) VALUES ('TestFirstname','TestLastname')
std.variant.VariantException@std/variant.d(1545): Attempt to use an uninitialized VariantN
----------------
??:? long std.variant.VariantN!(32uL).VariantN.handler!(void).handler(std.variant.VariantN!(32uL).VariantN.OpID, ubyte[32]*, void*) [0x18f46946]
??:? [0xa646ad0]
??:? [0xa6436d6]
??:? [0xa642e54]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll() [0x189d3c2f]
??:? _d_run_main [0x189d3afb]
??:? [0xa65e4a4]
??:? __libc_start_main [0x17f2eb96]
??:? [0xa642b79]

before sql script:

create table custom_table ( 
    id SERIAL PRIMARY KEY, 
    firstname TEXT,
    lastname TEXT
);

code:

static import config;
import std.conv: to;


private const string CONNECTION_URL = "postgresql://"~ config.HOST 
                            ~ ":" ~ to!string(config.PORT) ~"/"
                            ~ config.DATABASE ~ "?user="
                            ~ config.USER ~",password="
                            ~ config.PASSWORD ~ ",ssl=true";

struct custom_table
{
    size_t id;
    string firstname;
    string lastname;
}

int main(string args[]) {
    custom_table item;
    item.id = 0;
    item.firstname = "TestFirstname";
    item.lastname = "TestLastname";
    auto connection = createConnection(CONNECTION_URL);
    scope (exit) connection.close();
    auto statement = connection.createStatement();
    scope (exit) statement.close();
    insert!custom_table(statement, item); // receive exception here
    return 0;
}

maybe need check variant id in https://github.com/buggins/ddbc/blob/cb72071bbe0ce089204e40d88ef71b6f605852c3/source/ddbc/pods.d#L1071

Rogni avatar Feb 12 '20 09:02 Rogni

I think it could be fixed by changing that line to:

    static if(is(typeof(o.id) == int)) {
        o.id = insertId.coerce!int;
    } else if(is(typeof(o.id) == uint)) {
        o.id = insertId.coerce!uint;
    } else if(is(typeof(o.id) == long)) {
        o.id = insertId.coerce!long;
    } else {
        o.id = insertId.coerce!ulong;
    }

I did some testing locally but want to add some more tests before I push anything. Would you mind trying that out locally and seeing if it solves the problem you've been facing.

SingingBush avatar Mar 16 '22 02:03 SingingBush

this should be sorted in the pods code but I suspect there may be some postgres specific changes that are needed for your scenario. If you have time please checkout the latest changes in master and see if it fixes the problem.

SingingBush avatar Aug 31 '22 16:08 SingingBush