Skip to content

Commit 29372eb

Browse files
committed
Support TEXT[]
1 parent 97eea50 commit 29372eb

3 files changed

Lines changed: 33 additions & 12 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ types. The driver currently supports the following conversions:
278278
<td>types::array::ArrayBase&lt;Option&lt;i32&gt;&gt;</td>
279279
<td>INT4[], INT4[][], ...</td>
280280
</tr>
281+
<tr>
282+
<td>types::array::ArrayBase&lt;Option&lt;~str&gt;&gt;</td>
283+
<td>TEXT[], TEXT[][], ...</td>
284+
</tr>
281285
<tr>
282286
<td>types::array::ArrayBase&lt;Option&lt;i64&gt;&gt;</td>
283287
<td>INT8[], INT8[][], ...</td>

test.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,11 @@ fn test_int4array_params() {
459459
test_array_params!("INT4", 0i32, "0", 1i32, "1", 2i32, "2");
460460
}
461461

462+
#[test]
463+
fn test_textarray_params() {
464+
test_array_params!("TEXT", ~"hello", "hello", ~"world", "world", ~"!", "!");
465+
}
466+
462467
#[test]
463468
fn test_int8array_params() {
464469
test_array_params!("INT8", 0i64, "0", 1i64, "1", 2i64, "2");

types/mod.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static BYTEAARRAYOID: Oid = 1001;
3838
static CHARARRAYOID: Oid = 1002;
3939
static INT2ARRAYOID: Oid = 1005;
4040
static INT4ARRAYOID: Oid = 1007;
41+
static TEXTARRAYOID: Oid = 1009;
4142
static INT8ARRAYOID: Oid = 1016;
4243
static FLOAT4ARRAYOID: Oid = 1021;
4344
static FLAOT8ARRAYOID: Oid = 1022;
@@ -96,6 +97,8 @@ pub enum PostgresType {
9697
PgInt2Array,
9798
/// INT4[]
9899
PgInt4Array,
100+
/// TEXT[]
101+
PgTextArray,
99102
/// INT8[]
100103
PgInt8Array,
101104
/// FLOAT4[]
@@ -148,6 +151,7 @@ impl PostgresType {
148151
CHARARRAYOID => PgCharArray,
149152
INT2ARRAYOID => PgInt2Array,
150153
INT4ARRAYOID => PgInt4Array,
154+
TEXTARRAYOID => PgTextArray,
151155
INT8ARRAYOID => PgInt8Array,
152156
FLOAT4ARRAYOID => PgFloat4Array,
153157
FLAOT8ARRAYOID => PgFloat8Array,
@@ -230,6 +234,12 @@ impl RawFromSql for ~[u8] {
230234
}
231235
}
232236

237+
impl RawFromSql for ~str {
238+
fn raw_from_sql<R: Reader>(len: uint, raw: &mut R) -> ~str {
239+
str::from_utf8_owned(raw.read_bytes(len))
240+
}
241+
}
242+
233243
raw_from_impl!(i8, read_i8)
234244
raw_from_impl!(i16, read_be_i16)
235245
raw_from_impl!(i32, read_be_i32)
@@ -282,17 +292,14 @@ macro_rules! from_raw_from_impl(
282292

283293
from_raw_from_impl!(PgBool, bool)
284294
from_raw_from_impl!(PgByteA, ~[u8])
295+
from_raw_from_impl!(PgVarchar | PgText | PgCharN, ~str)
285296
from_raw_from_impl!(PgChar, i8)
286297
from_raw_from_impl!(PgInt2, i16)
287298
from_raw_from_impl!(PgInt4, i32)
288299
from_raw_from_impl!(PgInt8, i64)
289300
from_raw_from_impl!(PgFloat4, f32)
290301
from_raw_from_impl!(PgFloat8, f64)
291302

292-
from_map_impl!(PgVarchar | PgText | PgCharN, ~str, |buf| {
293-
str::from_utf8_owned(buf.clone())
294-
})
295-
296303
from_map_impl!(PgJson, Json, |buf| {
297304
json::from_str(str::from_utf8(buf.as_slice())).unwrap()
298305
})
@@ -386,6 +393,7 @@ from_array_impl!(PgByteAArray, ~[u8])
386393
from_array_impl!(PgCharArray, i8)
387394
from_array_impl!(PgInt2Array, i16)
388395
from_array_impl!(PgInt4Array, i32)
396+
from_array_impl!(PgTextArray, ~str)
389397
from_array_impl!(PgInt8Array, i64)
390398
from_array_impl!(PgFloat4Array, f32)
391399
from_array_impl!(PgFloat8Array, f64)
@@ -466,6 +474,16 @@ impl RawToSql for ~[u8] {
466474
}
467475
}
468476
477+
impl RawToSql for ~str {
478+
fn raw_to_sql<W: Writer>(&self, w: &mut W) {
479+
w.write(self.as_bytes())
480+
}
481+
482+
fn raw_size(&self) -> uint {
483+
self.len()
484+
}
485+
}
486+
469487
raw_to_impl!(i8, write_i8)
470488
raw_to_impl!(i16, write_be_i16)
471489
raw_to_impl!(i32, write_be_i32)
@@ -533,28 +551,21 @@ macro_rules! to_raw_to_impl(
533551
534552
to_raw_to_impl!(PgBool, bool)
535553
to_raw_to_impl!(PgByteA, ~[u8])
554+
to_raw_to_impl!(PgVarchar | PgText | PgCharN, ~str)
536555
to_raw_to_impl!(PgChar, i8)
537556
to_raw_to_impl!(PgInt2, i16)
538557
to_raw_to_impl!(PgInt4, i32)
539558
to_raw_to_impl!(PgInt8, i64)
540559
to_raw_to_impl!(PgFloat4, f32)
541560
to_raw_to_impl!(PgFloat8, f64)
542561
543-
impl ToSql for ~str {
544-
fn to_sql(&self, ty: &PostgresType) -> (Format, Option<~[u8]>) {
545-
check_types!(PgVarchar | PgText | PgCharN, ty)
546-
(Text, Some(self.as_bytes().to_owned()))
547-
}
548-
}
549-
550562
impl<'self> ToSql for &'self str {
551563
fn to_sql(&self, ty: &PostgresType) -> (Format, Option<~[u8]>) {
552564
check_types!(PgVarchar | PgText | PgCharN, ty)
553565
(Text, Some(self.as_bytes().to_owned()))
554566
}
555567
}
556568
557-
to_option_impl!(PgVarchar | PgText | PgCharN, ~str)
558569
to_option_impl_self!(PgVarchar | PgText | PgCharN, &'self str)
559570
560571
impl<'self> ToSql for &'self [u8] {
@@ -679,6 +690,7 @@ to_array_impl!(PgByteAArray, BYTEAOID, ~[u8])
679690
to_array_impl!(PgCharArray, CHAROID, i8)
680691
to_array_impl!(PgInt2Array, INT2OID, i16)
681692
to_array_impl!(PgInt4Array, INT4OID, i32)
693+
to_array_impl!(PgTextArray, TEXTOID, ~str)
682694
to_array_impl!(PgInt8Array, INT8OID, i64)
683695
to_array_impl!(PgFloat4Array, FLOAT4OID, f32)
684696
to_array_impl!(PgFloat8Array, FLOAT8OID, f64)

0 commit comments

Comments
 (0)