Skip to content

Commit 5a58966

Browse files
authored
Merge pull request #190 from programble/fix/empty-array
Fix empty array conversion
2 parents 5938db2 + 5b6cb16 commit 5a58966

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/types/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,11 @@ impl<T: FromSql> FromSql for Vec<T> {
358358
_ => panic!("expected array type"),
359359
};
360360

361-
if try!(raw.read_i32::<BigEndian>()) != 1 {
362-
return Err(Error::Conversion("array contains too many dimensions".into()));
363-
}
361+
match try!(raw.read_i32::<BigEndian>()) {
362+
0 => return Ok(Vec::new()),
363+
1 => (),
364+
_ => return Err(Error::Conversion("array contains too many dimensions".into())),
365+
};
364366

365367
let _has_nulls = try!(raw.read_i32::<BigEndian>());
366368
let _member_oid = try!(raw.read_u32::<BigEndian>());

tests/types/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ fn test_hstore_params() {
171171
(None, "NULL")]);
172172
}
173173

174+
#[test]
175+
fn test_array_params() {
176+
test_type("integer[]", &[(Some(vec!(1i32, 2i32)), "ARRAY[1,2]"),
177+
(Some(vec!(1i32)), "ARRAY[1]"),
178+
(Some(vec!()), "ARRAY[]"),
179+
(None, "NULL")]);
180+
}
181+
174182
fn test_nan_param<T: PartialEq+ToSql+FromSql>(sql_type: &str) {
175183
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
176184
let stmt = or_panic!(conn.prepare(&*format!("SELECT 'NaN'::{}", sql_type)));

0 commit comments

Comments
 (0)