Skip to content

Commit d5a125f

Browse files
committed
Support INT2[]
1 parent 7dde115 commit d5a125f

3 files changed

Lines changed: 41 additions & 45 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ types. The driver currently supports the following conversions:
270270
<td>types::array::ArrayBase&lt;Option&lt;i8&gt;&gt;</td>
271271
<td>"char"[], "char"[][], ...</td>
272272
</tr>
273+
<tr>
274+
<td>types::array::ArrayBase&lt;Option&lt;i16&gt;&gt;</td>
275+
<td>INT2[], INT2[][], ...</td>
276+
</tr>
273277
<tr>
274278
<td>types::array::ArrayBase&lt;Option&lt;i32&gt;&gt;</td>
275279
<td>INT4[], INT4[][], ...</td>

test.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -433,40 +433,45 @@ macro_rules! test_array_params(
433433
)
434434
435435
#[test]
436-
fn test_int4array_params() {
437-
test_array_params!("INT4", 0i32, "0", 1i32, "1", 2i32, "2");
436+
fn test_boolarray_params() {
437+
test_array_params!("BOOL", false, "f", true, "t", true, "t");
438438
}
439439
440440
#[test]
441-
fn test_int8array_params() {
442-
test_array_params!("INT8", 0i64, "0", 1i64, "1", 2i64, "2");
441+
fn test_byteaarray_params() {
442+
test_array_params!("BYTEA", ~[0u8, 1], r#""\\x0001""#, ~[254u8, 255u8],
443+
r#""\\xfeff""#, ~[10u8, 11u8], r#""\\x0a0b""#);
443444
}
444445
445446
#[test]
446-
fn test_float4array_params() {
447-
test_array_params!("FLOAT4", 0f32, "0", 1.5f32, "1.5", 0.009f32, ".009");
447+
fn test_chararray_params() {
448+
test_array_params!("\"char\"", 'a' as i8, "a", 'z' as i8, "z",
449+
'0' as i8, "0");
448450
}
449451

450452
#[test]
451-
fn test_float8array_params() {
452-
test_array_params!("FLOAT8", 0f64, "0", 1.5f64, "1.5", 0.009f64, ".009");
453+
fn test_int2array_params() {
454+
test_array_params!("INT2", 0i16, "0", 1i16, "1", 2i16, "2");
453455
}
454456

455457
#[test]
456-
fn test_boolarray_params() {
457-
test_array_params!("BOOL", false, "f", true, "t", true, "t");
458+
fn test_int4array_params() {
459+
test_array_params!("INT4", 0i32, "0", 1i32, "1", 2i32, "2");
458460
}
459461

460462
#[test]
461-
fn test_byteaarray_params() {
462-
test_array_params!("BYTEA", ~[0u8, 1], r#""\\x0001""#, ~[254u8, 255u8],
463-
r#""\\xfeff""#, ~[10u8, 11u8], r#""\\x0a0b""#);
463+
fn test_int8array_params() {
464+
test_array_params!("INT8", 0i64, "0", 1i64, "1", 2i64, "2");
464465
}
465466

466467
#[test]
467-
fn test_chararray_params() {
468-
test_array_params!("\"char\"", 'a' as i8, "a", 'z' as i8, "z",
469-
'0' as i8, "0");
468+
fn test_float4array_params() {
469+
test_array_params!("FLOAT4", 0f32, "0", 1.5f32, "1.5", 0.009f32, ".009");
470+
}
471+
472+
#[test]
473+
fn test_float8array_params() {
474+
test_array_params!("FLOAT8", 0f64, "0", 1.5f64, "1.5", 0.009f64, ".009");
470475
}
471476

472477
#[test]

types/mod.rs

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static FLOAT8OID: Oid = 701;
3636
static BOOLARRAYOID: Oid = 1000;
3737
static BYTEAARRAYOID: Oid = 1001;
3838
static CHARARRAYOID: Oid = 1002;
39+
static INT2ARRAYOID: Oid = 1005;
3940
static INT4ARRAYOID: Oid = 1007;
4041
static INT8ARRAYOID: Oid = 1016;
4142
static FLOAT4ARRAYOID: Oid = 1021;
@@ -91,6 +92,8 @@ pub enum PostgresType {
9192
PgByteAArray,
9293
/// "char"[]
9394
PgCharArray,
95+
/// INT2[]
96+
PgInt2Array,
9497
/// INT4[]
9598
PgInt4Array,
9699
/// INT8[]
@@ -143,6 +146,7 @@ impl PostgresType {
143146
BOOLARRAYOID => PgBoolArray,
144147
BYTEAARRAYOID => PgByteAArray,
145148
CHARARRAYOID => PgCharArray,
149+
INT2ARRAYOID => PgInt2Array,
146150
INT4ARRAYOID => PgInt4Array,
147151
INT8ARRAYOID => PgInt8Array,
148152
FLOAT4ARRAYOID => PgFloat4Array,
@@ -227,6 +231,7 @@ impl RawFromSql for ~[u8] {
227231
}
228232

229233
raw_from_impl!(i8, read_i8)
234+
raw_from_impl!(i16, read_be_i16)
230235
raw_from_impl!(i32, read_be_i32)
231236
raw_from_impl!(i64, read_be_i64)
232237
raw_from_impl!(f32, read_be_f32)
@@ -267,15 +272,6 @@ macro_rules! from_raw_from_impl(
267272
)
268273
)
269274

270-
macro_rules! from_conversions_impl(
271-
($expected:pat, $t:ty, $f:ident) => (
272-
from_map_impl!($expected, $t, |buf| {
273-
let mut reader = BufReader::new(buf.as_slice());
274-
reader.$f()
275-
})
276-
)
277-
)
278-
279275
macro_rules! from_option_impl(
280276
($t:ty) => (
281277
impl FromSql for $t {
@@ -294,6 +290,8 @@ from_raw_from_impl!(PgByteA, ~[u8])
294290
from_option_impl!(~[u8])
295291
from_raw_from_impl!(PgChar, i8)
296292
from_option_impl!(i8)
293+
from_raw_from_impl!(PgInt2, i16)
294+
from_option_impl!(i16)
297295
from_raw_from_impl!(PgInt4, i32)
298296
from_option_impl!(i32)
299297
from_raw_from_impl!(PgInt8, i64)
@@ -303,9 +301,6 @@ from_option_impl!(f32)
303301
from_raw_from_impl!(PgFloat8, f64)
304302
from_option_impl!(f64)
305303

306-
from_conversions_impl!(PgInt2, i16, read_be_i16)
307-
from_option_impl!(i16)
308-
309304
from_map_impl!(PgVarchar | PgText | PgCharN, ~str, |buf| {
310305
str::from_utf8_owned(buf.clone())
311306
})
@@ -416,6 +411,9 @@ from_option_impl!(ArrayBase<Option<~[u8]>>)
416411
from_array_impl!(PgCharArray, i8)
417412
from_option_impl!(ArrayBase<Option<i8>>)
418413

414+
from_array_impl!(PgInt2Array, i16)
415+
from_option_impl!(ArrayBase<Option<i16>>)
416+
419417
from_array_impl!(PgInt4Array, i32)
420418
from_option_impl!(ArrayBase<Option<i32>>)
421419

@@ -506,6 +504,7 @@ impl RawToSql for ~[u8] {
506504
}
507505
508506
raw_to_impl!(i8, write_i8)
507+
raw_to_impl!(i16, write_be_i16)
509508
raw_to_impl!(i32, write_be_i32)
510509
raw_to_impl!(i64, write_be_i64)
511510
raw_to_impl!(f32, write_be_f32)
@@ -567,26 +566,14 @@ macro_rules! to_raw_to_impl(
567566
)
568567
)
569568
570-
macro_rules! to_conversions_impl(
571-
($($oid:ident)|+, $t:ty, $f:ident) => (
572-
impl ToSql for $t {
573-
fn to_sql(&self, ty: &PostgresType) -> (Format, Option<~[u8]>) {
574-
check_types!($($oid)|+, ty)
575-
576-
let mut writer = MemWriter::new();
577-
writer.$f(*self);
578-
(Binary, Some(writer.inner()))
579-
}
580-
}
581-
)
582-
)
583-
584569
to_raw_to_impl!(PgBool, bool)
585570
to_option_impl!(PgBool, bool)
586571
to_raw_to_impl!(PgByteA, ~[u8])
587572
to_option_impl!(PgByteA, ~[u8])
588573
to_raw_to_impl!(PgChar, i8)
589574
to_option_impl!(PgChar, i8)
575+
to_raw_to_impl!(PgInt2, i16)
576+
to_option_impl!(PgInt2, i16)
590577
to_raw_to_impl!(PgInt4, i32)
591578
to_option_impl!(PgInt4, i32)
592579
to_raw_to_impl!(PgInt8, i64)
@@ -596,9 +583,6 @@ to_option_impl!(PgFloat4, f32)
596583
to_raw_to_impl!(PgFloat8, f64)
597584
to_option_impl!(PgFloat8, f64)
598585
599-
to_conversions_impl!(PgInt2, i16, write_be_i16)
600-
to_option_impl!(PgInt2, i16)
601-
602586
impl ToSql for ~str {
603587
fn to_sql(&self, ty: &PostgresType) -> (Format, Option<~[u8]>) {
604588
check_types!(PgVarchar | PgText | PgCharN, ty)
@@ -744,6 +728,9 @@ to_option_impl!(PgByteAArray, ArrayBase<Option<~[u8]>>)
744728
to_array_impl!(PgCharArray, CHAROID, i8)
745729
to_option_impl!(PgCharArray, ArrayBase<Option<i8>>)
746730
731+
to_array_impl!(PgInt2Array, INT2OID, i16)
732+
to_option_impl!(PgInt2Array, ArrayBase<Option<i16>>)
733+
747734
to_array_impl!(PgInt4Array, INT4OID, i32)
748735
to_option_impl!(PgInt4Array, ArrayBase<Option<i32>>)
749736

0 commit comments

Comments
 (0)