Skip to content

Commit a0ab249

Browse files
committed
Support FLOAT4[]
1 parent 9a6116f commit a0ab249

3 files changed

Lines changed: 46 additions & 30 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ types. The driver currently supports the following conversions:
266266
<td>types::array::ArrayBase&lt;Option&lt;i64&gt;&gt;</td>
267267
<td>INT8[], INT8[][], ...</td>
268268
</tr>
269+
<tr>
270+
<td>types::array::ArrayBase&lt;Option&lt;f32&gt;&gt;</td>
271+
<td>FLOAT4[], FLOAT4[][], ...</td>
272+
</tr>
269273
<tr>
270274
<td>std::hashmap::HashMap&lt;~str, Option&lt;~str&gt;&gt;</td>
271275
<td>HSTORE</td>

test.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ fn test_result_descriptions() {
223223
ResultDescription { name: ~"b", ty: PgVarchar}]);
224224
}
225225
226-
fn test_type<T: Eq+FromSql+ToSql>(sql_type: &str, checks: &[(T, &str)]) {
226+
fn test_type<T: Eq+FromSql+ToSql, S: Str>(sql_type: &str, checks: &[(T, S)]) {
227227
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
228228
for &(ref val, ref repr) in checks.iter() {
229-
let stmt = conn.prepare("SELECT " + *repr + "::" + sql_type);
229+
let stmt = conn.prepare(format!("SELECT {:s}::{}", *repr, sql_type));
230230
let result = stmt.query([]).next().unwrap()[1];
231231
assert_eq!(val, &result);
232232

@@ -381,10 +381,7 @@ macro_rules! test_range(
381381
"'(" + $low_str + "," + $high_str + ")'"),
382382
(Some(Range::empty()), ~"'empty'"),
383383
(None, ~"NULL")];
384-
let test_refs: ~[(Option<Range<$t>>, &str)] = tests.iter()
385-
.map(|&(ref val, ref s)| { (val.clone(), s.as_slice()) })
386-
.collect();
387-
test_type($name, test_refs);
384+
test_type($name, tests);
388385
})
389386
)
390387
@@ -421,30 +418,33 @@ fn test_tstzrange_params() {
421418
test_timespec_range_params("TSTZRANGE");
422419
}
423420
421+
macro_rules! test_array_params(
422+
($name:expr, $v1:expr, $s1:expr, $v2:expr, $s2:expr, $v3:expr, $s3:expr) => ({
423+
let tests = [(Some(ArrayBase::from_vec(~[Some($v1), Some($v2), None], 1)),
424+
"'{" + $s1 + "," + $s2 + ",NULL}'"),
425+
(None, ~"NULL")];
426+
test_type($name + "[]", tests);
427+
let mut a = ArrayBase::from_vec(~[Some($v1), Some($v2)], 0);
428+
a.wrap(-1);
429+
a.push_move(ArrayBase::from_vec(~[None, Some($v3)], 0));
430+
let tests = [(Some(a), "'[-1:0][0:1]={{" + $s1 + "," + $s2 + "},{NULL," + $s3 + "}}'")];
431+
test_type($name + "[][]", tests);
432+
})
433+
)
434+
424435
#[test]
425436
fn test_int4array_params() {
426-
test_type("INT4[]",
427-
[(Some(ArrayBase::from_vec(~[Some(0i32), Some(1), None], 1)),
428-
"'{0,1,NULL}'"),
429-
(None, "NULL")]);
430-
let mut a = ArrayBase::from_vec(~[Some(0i32), Some(1)], 0);
431-
a.wrap(-1);
432-
a.push_move(ArrayBase::from_vec(~[None, Some(3)], 0));
433-
test_type("INT4[][]",
434-
[(Some(a), "'[-1:0][0:1]={{0,1},{NULL,3}}'")]);
437+
test_array_params!("INT4", 0i32, "0", 1i32, "1", 2i32, "2");
435438
}
436439
437440
#[test]
438441
fn test_int8array_params() {
439-
test_type("INT8[]",
440-
[(Some(ArrayBase::from_vec(~[Some(0i64), Some(1), None], 1)),
441-
"'{0,1,NULL}'"),
442-
(None, "NULL")]);
443-
let mut a = ArrayBase::from_vec(~[Some(0i64), Some(1)], 0);
444-
a.wrap(-1);
445-
a.push_move(ArrayBase::from_vec(~[None, Some(3)], 0));
446-
test_type("INT8[][]",
447-
[(Some(a), "'[-1:0][0:1]={{0,1},{NULL,3}}'")]);
442+
test_array_params!("INT8", 0i64, "0", 1i64, "1", 2i64, "2");
443+
}
444+
445+
#[test]
446+
fn test_float4array_params() {
447+
test_array_params!("FLOAT4", 0f32, "0", 1.5f32, "1.5", 0.009f32, ".009");
448448
}
449449
450450
#[test]
@@ -518,7 +518,7 @@ fn test_get_named() {
518518
let stmt = conn.prepare("SELECT 10::INT as val");
519519
let result = stmt.query([]);
520520

521-
assert_eq!(~[10i32], result.map(|row| { row["val"] }).collect());
521+
assert_eq!(~[10i32], result.map(|row| row["val"]).collect());
522522
}
523523

524524
#[test]
@@ -610,7 +610,7 @@ fn test_cancel_query() {
610610
do spawn {
611611
timer::sleep(500);
612612
assert!(lib::cancel_query("postgres://postgres@localhost", &NoSsl,
613-
cancel_data).is_none());
613+
cancel_data).is_none());
614614
}
615615

616616
match conn.try_update("SELECT pg_sleep(10)", []) {

types/mod.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static FLOAT4OID: Oid = 700;
3535
static FLOAT8OID: Oid = 701;
3636
static INT4ARRAYOID: Oid = 1007;
3737
static INT8ARRAYOID: Oid = 1016;
38+
static FLOAT4ARRAYOID: Oid = 1021;
3839
static BPCHAROID: Oid = 1042;
3940
static VARCHAROID: Oid = 1043;
4041
static TIMESTAMPOID: Oid = 1114;
@@ -84,6 +85,8 @@ pub enum PostgresType {
8485
PgInt4Array,
8586
/// INT8[]
8687
PgInt8Array,
88+
/// FLOAT4[]
89+
PgFloat4Array,
8790
/// TIMESTAMP
8891
PgTimestamp,
8992
/// TIMESTAMP WITH TIME ZONE
@@ -127,6 +130,7 @@ impl PostgresType {
127130
FLOAT8OID => PgFloat8,
128131
INT4ARRAYOID => PgInt4Array,
129132
INT8ARRAYOID => PgInt8Array,
133+
FLOAT4ARRAYOID => PgFloat4Array,
130134
TIMESTAMPOID => PgTimestamp,
131135
TIMESTAMPZOID => PgTimestampZ,
132136
BPCHAROID => PgCharN,
@@ -196,6 +200,7 @@ macro_rules! raw_from_impl(
196200

197201
raw_from_impl!(i32, read_be_i32)
198202
raw_from_impl!(i64, read_be_i64)
203+
raw_from_impl!(f32, read_be_f32)
199204

200205
impl RawFromSql for Timespec {
201206
fn raw_from_sql<R: Reader>(raw: &mut R) -> Timespec {
@@ -260,13 +265,13 @@ from_raw_from_impl!(PgInt4, i32)
260265
from_option_impl!(i32)
261266
from_raw_from_impl!(PgInt8, i64)
262267
from_option_impl!(i64)
268+
from_raw_from_impl!(PgFloat4, f32)
269+
from_option_impl!(f32)
263270

264271
from_conversions_impl!(PgChar, i8, read_i8)
265272
from_option_impl!(i8)
266273
from_conversions_impl!(PgInt2, i16, read_be_i16)
267274
from_option_impl!(i16)
268-
from_conversions_impl!(PgFloat4, f32, read_be_f32)
269-
from_option_impl!(f32)
270275
from_conversions_impl!(PgFloat8, f64, read_be_f64)
271276
from_option_impl!(f64)
272277

@@ -381,6 +386,9 @@ from_option_impl!(ArrayBase<Option<i32>>)
381386
from_array_impl!(PgInt8Array, i64)
382387
from_option_impl!(ArrayBase<Option<i64>>)
383388

389+
from_array_impl!(PgFloat4Array, f32)
390+
from_option_impl!(ArrayBase<Option<f32>>)
391+
384392
from_map_impl!(PgUnknownType { name: ~"hstore", .. },
385393
HashMap<~str, Option<~str>>, |buf| {
386394
let mut rdr = BufReader::new(buf.as_slice());
@@ -440,6 +448,7 @@ macro_rules! raw_to_impl(
440448
441449
raw_to_impl!(i32, write_be_i32)
442450
raw_to_impl!(i64, write_be_i64)
451+
raw_to_impl!(f32, write_be_f32)
443452
444453
impl RawToSql for Timespec {
445454
fn raw_to_sql<W: Writer>(&self, w: &mut W) {
@@ -523,13 +532,13 @@ to_raw_to_impl!(PgInt4, i32)
523532
to_option_impl!(PgInt4, i32)
524533
to_raw_to_impl!(PgInt8, i64)
525534
to_option_impl!(PgInt8, i64)
535+
to_raw_to_impl!(PgFloat4, f32)
536+
to_option_impl!(PgFloat4, f32)
526537
527538
to_conversions_impl!(PgChar, i8, write_i8)
528539
to_option_impl!(PgChar, i8)
529540
to_conversions_impl!(PgInt2, i16, write_be_i16)
530541
to_option_impl!(PgInt2, i16)
531-
to_conversions_impl!(PgFloat4, f32, write_be_f32)
532-
to_option_impl!(PgFloat4, f32)
533542
to_conversions_impl!(PgFloat8, f64, write_be_f64)
534543
to_option_impl!(PgFloat8, f64)
535544
@@ -683,6 +692,9 @@ to_option_impl!(PgInt4Array, ArrayBase<Option<i32>>)
683692
to_array_impl!(PgInt8Array, INT8OID, i64)
684693
to_option_impl!(PgInt8Array, ArrayBase<Option<i64>>)
685694
695+
to_array_impl!(PgFloat4Array, FLOAT4OID, f32)
696+
to_option_impl!(PgFloat4Array, ArrayBase<Option<f32>>)
697+
686698
impl<'self> ToSql for HashMap<~str, Option<~str>> {
687699
fn to_sql(&self, ty: &PostgresType) -> (Format, Option<~[u8]>) {
688700
check_types!(PgUnknownType { name: ~"hstore", .. }, ty)

0 commit comments

Comments
 (0)