Skip to content

Commit 2cc7f0f

Browse files
committed
Support for TEXT type
1 parent 46b4228 commit 2cc7f0f

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

src/test.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ fn test_varchar_params() {
151151
Some(~"イロハニホヘト チリヌルヲ"), None]);
152152
}
153153
154+
#[test]
155+
fn test_text_params() {
156+
test_param_type("TEXT", [Some(~"hello world"),
157+
Some(~"イロハニホヘト チリヌルヲ"), None]);
158+
159+
}
160+
154161
#[test]
155162
fn test_bytea_params() {
156163
test_param_type("BYTEA", [Some(~[0u8, 1, 2, 3, 254, 255]), None]);

src/types.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ static BYTEAOID: Oid = 17;
1111
static INT8OID: Oid = 20;
1212
static INT2OID: Oid = 21;
1313
static INT4OID: Oid = 23;
14+
static TEXTOID: Oid = 25;
1415
static FLOAT4OID: Oid = 700;
1516
static FLOAT8OID: Oid = 701;
1617
static VARCHAROID: Oid = 1043;
@@ -34,9 +35,10 @@ pub fn result_format(ty: Oid) -> Format {
3435
}
3536

3637
macro_rules! check_oid(
37-
($expected:ident, $actual:ident) => (
38-
if $expected != $actual {
39-
fail!("Expected Oid %? but got Oid %?", $expected, $actual);
38+
($($expected:ident)|+, $actual:ident) => (
39+
match $actual {
40+
$($expected)|+ => (),
41+
actual => fail!("Invalid Oid %?", actual)
4042
}
4143
)
4244
)
@@ -95,7 +97,7 @@ from_option_impl!(f64)
9597

9698
impl FromSql for Option<~str> {
9799
fn from_sql(ty:Oid, raw: &Option<~[u8]>) -> Option<~str> {
98-
check_oid!(VARCHAROID, ty)
100+
check_oid!(VARCHAROID | TEXTOID, ty)
99101
do raw.chain_ref |buf| {
100102
Some(str::from_bytes(buf.as_slice()))
101103
}
@@ -116,10 +118,10 @@ pub trait ToSql {
116118
}
117119

118120
macro_rules! to_option_impl(
119-
($oid:ident, $t:ty) => (
121+
($($oid:ident)|+, $t:ty) => (
120122
impl ToSql for Option<$t> {
121123
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>) {
122-
check_oid!($oid, ty)
124+
check_oid!($($oid)|+, ty)
123125

124126
match *self {
125127
None => (Text, None),
@@ -131,10 +133,10 @@ macro_rules! to_option_impl(
131133
)
132134

133135
macro_rules! to_conversions_impl(
134-
($oid:ident, $t:ty, $f:ident) => (
136+
($($oid:ident)|+, $t:ty, $f:ident) => (
135137
impl ToSql for $t {
136138
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>) {
137-
check_oid!($oid, ty)
139+
check_oid!($($oid)|+, ty)
138140

139141
let mut writer = MemWriter::new();
140142
writer.$f(*self);
@@ -165,16 +167,16 @@ to_option_impl!(FLOAT8OID, f64)
165167

166168
impl<'self> ToSql for &'self str {
167169
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>) {
168-
check_oid!(VARCHAROID, ty)
170+
check_oid!(VARCHAROID | TEXTOID, ty)
169171
(Text, Some(self.as_bytes().to_owned()))
170172
}
171173
}
172174

173-
to_option_impl!(VARCHAROID, ~str)
175+
to_option_impl!(VARCHAROID | TEXTOID, ~str)
174176

175177
impl<'self> ToSql for Option<&'self str> {
176178
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>) {
177-
check_oid!(VARCHAROID, ty)
179+
check_oid!(VARCHAROID | TEXTOID, ty)
178180
match *self {
179181
None => (Text, None),
180182
Some(val) => val.to_sql(ty)

0 commit comments

Comments
 (0)