-
-
Notifications
You must be signed in to change notification settings - Fork 439
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
Please add support for vector parameters #133
Comments
The syntax in your second example should work. |
I tried supplying the param as a vector slice, but it seemed not coerced to into a &[&ToSql] type. let sql = "INSERT INTO person (name, data) VALUES ($1, $2)".to_string();
let me = Person {
id: 0,
name: "Steven".to_string(),
data: None
};
let mut param = vec![&me.name];
param.push(&me.data);
conn.execute(&sql, ¶m).unwrap();
let stmt = conn.prepare("SELECT id, name, data FROM person").unwrap();
for row in stmt.query(&[]).unwrap() {
let person = Person {
id: row.get(0),
name: row.get(1),
data: row.get(2)
};
println!("Found person {}", person.name);
} I got these error examples/test_person.rs:42:32: 42:38 error: mismatched types:
expected `&[&postgres::types::ToSql]`,
found `&collections::vec::Vec<&collections::string::String>`
(expected slice,
found struct `collections::vec::Vec`) [E0308]
examples/test_person.rs:42 conn.execute(&sql, ¶m).unwrap();
^~~~~~ I've tested in both rust 1.2.0 and rust 1.0.0 with the same error result rustc -V
rustc 1.2.0-nightly (0cc99f9cc 2015-05-17) (built 2015-05-18)
|
I found a way to work around this by explicitly casting the member elements of the vector to let sql = "INSERT INTO person (name, data) VALUES ($1, $2)".to_string();
let me = Person {
id: 0,
name: "Steven".to_string(),
data: None
};
let name:&ToSql = &me.name;
let data:&ToSql = &me.data;
let mut param = vec![name, data];
conn.execute(&sql, ¶m).unwrap();
let stmt = conn.prepare("SELECT id, name, data FROM person").unwrap();
for row in stmt.query(&[]).unwrap() {
let person = Person {
id: row.get(0),
name: row.get(1),
data: row.get(2)
};
println!("Found person {}", person.name);
} There seemed to be unnecessary code in explicit conversion, but it works. I'm gonna be using this technique, until something more elegant comes up. |
You should also be able to specify the type of just the vector, like in this example: http://is.gd/3r9PF6 |
Yeah, I just realized that it would also work if explicitly the type is specified upon declaration. I feel soo stupid. let conn = pg.conn;
let sql = "INSERT INTO person (name, data) VALUES ($1, $2)".to_string();
let me = Person {
id: 0,
name: "Steven".to_string(),
data: None
};
let mut param:Vec<&ToSql> = vec![&me.name];
param.push(&me.data);
conn.execute(&sql, ¶m).unwrap();
let stmt = conn.prepare("SELECT id, name, data FROM person").unwrap();
for row in stmt.query(&[]).unwrap() {
let person = Person {
id: row.get(0),
name: row.get(1),
data: row.get(2)
};
println!("Found person {}", person.name);
} |
Not a problem :) |
Just wanted to say that I struggled with this for more than a few days before I found this. Can I make a PR with this as a usage example in the docs? |
@sfackler Hoping you can help here. I am trying to create a temp table with multiple values for an insert. I am struggling to understand how to dynamically pass in the values. Here is the error I am receiving:
|
If you have a |
In case any other rust newbies come here looking for clues, I had the above problem re: the Sync trait, and here's what worked for me (as @sfackler suggested):
As a newbie, this seems like some kind of witchcraft that I don't quite understand. For example, the need to provide params as a slice of all elements was a head-scratcher. The friendly compiler got me there in the end! |
If you are using the fn my_query() {
let mut params: Vec<&(dyn ToSql + Sync)> = Vec::new();
for column in set_of_things{
params.push(&column);
}
params.push(&something_different);
client
.query_raw(&statement, slice_iter(¶ms[..]))
}
fn slice_iter<'a>(
s: &'a [&'a (dyn ToSql + Sync)],
) -> impl ExactSizeIterator<Item = &'a dyn ToSql> + 'a {
s.iter().map(|s| *s as _)
} |
I'm building an ORM on top of your library, but I haven't figured out, how to convert vector to a fixed sized array in rust dynamically.
The query parameters
would be much easier to manipulate this way
The text was updated successfully, but these errors were encountered: