Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Rogni opened this issue Feb 12, 2020 · 2 comments
Open

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

Rogni opened this issue Feb 12, 2020 · 2 comments
Milestone

Comments

@Rogni
Copy link

Rogni commented Feb 12, 2020

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

o.id = insertId.get!long;

@SingingBush
Copy link
Collaborator

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 added a commit that referenced this issue Aug 31, 2022
SingingBush added a commit that referenced this issue Aug 31, 2022
SingingBush added a commit that referenced this issue Aug 31, 2022
SingingBush added a commit that referenced this issue Aug 31, 2022
@SingingBush SingingBush added this to the 0.6.0 milestone Aug 31, 2022
SingingBush added a commit that referenced this issue Aug 31, 2022
* #89 add more tests for pods
* drop support for D < 2.097
@SingingBush
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants