Skip to content

Commit 54db960

Browse files
committed
Use SQL IN instead of ANY
1 parent b1cb2a7 commit 54db960

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/krate.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,14 @@ impl Crate {
127127

128128
pub fn encode_many(conn: &Connection, crates: Vec<Crate>)
129129
-> CargoResult<Vec<EncodableCrate>> {
130+
if crates.len() == 0 { return Ok(Vec::new()) }
131+
130132
// TODO: can rust-postgres do this escaping?
131133
let crateids: Vec<i32> = crates.iter().map(|p| p.id).collect();
132134
let mut map = HashMap::new();
133-
let query = format!("'{{{:#}}}'::int[]", crateids.as_slice());
134135
let stmt = try!(conn.prepare(format!("SELECT id, crate_id FROM versions \
135-
WHERE crate_id = ANY({})",
136-
query).as_slice()));
136+
WHERE crate_id IN({:#})",
137+
crateids).as_slice()));
137138
for row in try!(stmt.query(&[])) {
138139
match map.entry(row.get("crate_id")) {
139140
Occupied(e) => e.into_mut(),

src/version.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,26 +178,29 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
178178
// TODO: can rust-postgres do this for us?
179179
let mut versions = Vec::new();
180180
let mut set = HashSet::new();
181-
let query = format!("'{{{:#}}}'::int[]", ids.as_slice());
182-
let stmt = try!(conn.prepare(format!("SELECT * FROM versions \
183-
WHERE id = ANY({})",
184-
query).as_slice()));
185-
for row in try!(stmt.query(&[])) {
186-
let v: Version = Model::from_row(&row);
187-
set.insert(v.crate_id);
188-
versions.push(v);
181+
if ids.len() > 0 {
182+
let stmt = try!(conn.prepare(format!("SELECT * FROM versions \
183+
WHERE id IN({:#})",
184+
ids).as_slice()));
185+
for row in try!(stmt.query(&[])) {
186+
let v: Version = Model::from_row(&row);
187+
set.insert(v.crate_id);
188+
versions.push(v);
189+
}
189190
}
190191

191192
// Load all crates
192-
let ids = set.into_iter().collect::<Vec<i32>>();
193-
let query = format!("'{{{:#}}}'::int[]", ids.as_slice());
194-
let stmt = try!(conn.prepare(format!("SELECT * FROM crates \
195-
WHERE id = ANY({})",
196-
query).as_slice()));
197193
let mut map = HashMap::new();
198-
for row in try!(stmt.query(&[])) {
199-
let p: Crate = Model::from_row(&row);
200-
map.insert(p.id, p);
194+
if set.len() > 0 {
195+
let ids = set.into_iter().collect::<Vec<i32>>();
196+
let stmt = try!(conn.prepare(format!("SELECT id, name FROM crates \
197+
WHERE id IN({:#})",
198+
ids).as_slice()));
199+
for row in try!(stmt.query(&[])) {
200+
let id: i32 = row.get("id");
201+
let name: String = row.get("name");
202+
map.insert(id, name);
203+
}
201204
}
202205

203206
// And respond!

0 commit comments

Comments
 (0)