Skip to content

Ability to insert multiple rows by specifying multiple rows in VALUES? #218

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

Closed
carols10cents opened this issue Nov 15, 2016 · 3 comments
Closed

Comments

@carols10cents
Copy link

What I'd like to be able to do is something like this:

let categories = vec!["shoes", "socks"];

try!(tx.execute("
    INSERT INTO categories (category)
    VALUES $1",
    &[&&categories[..]]
));

And I expected the SQL generated to be:

INSERT INTO categories (category)
VALUES ('shoes'), ('socks');

The code above compiles, but I get a panic on unwrapping:

Db(DbError { 
    severity: "ERROR", 
    code: SyntaxError, 
    message: "syntax error at or near \"$1\"", 
    detail: None, hint: None, position: Some(Normal(59)), where_: None, schema: None, 
    table: None, column: None, datatype: None, constraint: None, file: Some("scan.l"), 
    line: Some(1087), routine: Some("scanner_yyerror")
})

Which looks to me like the $1 wasn't substituted at all.

Is there a way to do this currently that I'm missing? If not, is this something that you'd want rust-postgres to support, or would it be better as a separate crate, something like activerecord-import?

Thanks!! ❤️

@sfackler
Copy link
Owner

rust-postgres passes the query string along directly to the postgres backend, which only allows parameters in the place of values, not here where it's looking for a comma-separated list of tuples. You could maybe do something with an array of composite types, but it'd probably be way too complicated to be worth it.

I think an external crate would make sense here, yeah. It'd probably involve generating query strings that look like INSERT INTO categories (category) VALUES ($1), ($2), ($3), ($4), ... for relatively small sets of values, and then switching over to COPY categories (category) FROM STDIN (FORMAT BINARY) using something like https://crates.io/crates/postgres-binary-copy when the value set grows larger.

@carols10cents
Copy link
Author

Ok, thank you! I might look into that in my copious free time... thank you for your time! ❤️

@isosphere
Copy link

@carols10cents did you find a solution for your needs?

I am doing a row-by-row insert of spreadsheet data into PostgreSQL and I think the overhead of each individual query is slowing me down (~200 million rows). I "think" it would be much faster if I could build larger queries with many rows of data in them but don't currently see a solution for that in rust.

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

3 participants