buggins / ddbc

DDBC is DB Connector for D language (similar to JDBC)
78 stars 45 forks source link

postgres. insert POD. Attempt to use an uninitialized VariantN #89

Open Rogni opened 4 years ago

Rogni commented 4 years ago

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

SingingBush commented 2 years ago

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 commented 2 years ago

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.