Skip to content

Commit 58aa200

Browse files
committed
Merge pull request rust-postgres#37 from jsanders/add-name-type
Add "name" type
2 parents 9a9b3c1 + de99157 commit 58aa200

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

src/test.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,13 @@ fn test_i8_params() {
406406
test_type("\"char\"", [(Some('a' as i8), "'a'"), (None, "NULL")]);
407407
}
408408

409+
#[test]
410+
fn test_name_params() {
411+
test_type("NAME", [(Some("hello world".to_owned()), "'hello world'"),
412+
(Some("イロハニホヘト チリヌルヲ".to_owned()), "'イロハニホヘト チリヌルヲ'"),
413+
(None, "NULL")]);
414+
}
415+
409416
#[test]
410417
fn test_i16_params() {
411418
test_type("SMALLINT", [(Some(15001i16), "15001"),
@@ -591,6 +598,12 @@ fn test_chararray_params() {
591598
'0' as i8, "0");
592599
}
593600

601+
#[test]
602+
fn test_namearray_params() {
603+
test_array_params!("NAME", "hello".to_owned(), "hello", "world".to_owned(),
604+
"world", "!".to_owned(), "!");
605+
}
606+
594607
#[test]
595608
fn test_int2array_params() {
596609
test_array_params!("INT2", 0i16, "0", 1i16, "1", 2i16, "2");
@@ -978,3 +991,14 @@ fn test_jsonarray_params() {
978991
json::from_str(r#"{"a": [10], "b": true}"#).unwrap(),
979992
r#""{\"a\": [10], \"b\": true}""#);
980993
}
994+
995+
#[test]
996+
fn test_pg_database_datname() {
997+
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));
998+
let stmt = or_fail!(conn.prepare("SELECT datname FROM pg_database"));
999+
let mut result = or_fail!(stmt.query([]));
1000+
1001+
let next = result.next().unwrap();
1002+
or_fail!(next.get::<uint, ~str>(1));
1003+
or_fail!(next.get::<&str, ~str>("datname"));
1004+
}

src/types/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub type Oid = u32;
2424
static BOOLOID: Oid = 16;
2525
static BYTEAOID: Oid = 17;
2626
static CHAROID: Oid = 18;
27+
static NAMEOID: Oid = 19;
2728
static INT8OID: Oid = 20;
2829
static INT2OID: Oid = 21;
2930
static INT4OID: Oid = 23;
@@ -35,6 +36,7 @@ static FLOAT8OID: Oid = 701;
3536
static BOOLARRAYOID: Oid = 1000;
3637
static BYTEAARRAYOID: Oid = 1001;
3738
static CHARARRAYOID: Oid = 1002;
39+
static NAMEARRAYOID: Oid = 1003;
3840
static INT2ARRAYOID: Oid = 1005;
3941
static INT4ARRAYOID: Oid = 1007;
4042
static TEXTARRAYOID: Oid = 1009;
@@ -136,6 +138,8 @@ make_postgres_type!(
136138
BYTEAOID => PgByteA,
137139
#[doc="\"char\""]
138140
CHAROID => PgChar,
141+
#[doc="NAME"]
142+
NAMEOID => PgName,
139143
#[doc="INT8/BIGINT"]
140144
INT8OID => PgInt8,
141145
#[doc="INT2/SMALLINT"]
@@ -158,6 +162,8 @@ make_postgres_type!(
158162
BYTEAARRAYOID => PgByteAArray member PgByteA,
159163
#[doc="\"char\"[]"]
160164
CHARARRAYOID => PgCharArray member PgChar,
165+
#[doc="NAME[]"]
166+
NAMEARRAYOID => PgNameArray member PgName,
161167
#[doc="INT2[]"]
162168
INT2ARRAYOID => PgInt2Array member PgInt2,
163169
#[doc="INT4[]"]
@@ -392,7 +398,7 @@ macro_rules! from_raw_from_impl(
392398

393399
from_raw_from_impl!(PgBool, bool)
394400
from_raw_from_impl!(PgByteA, Vec<u8>)
395-
from_raw_from_impl!(PgVarchar | PgText | PgCharN, ~str)
401+
from_raw_from_impl!(PgVarchar | PgText | PgCharN | PgName, ~str)
396402
from_raw_from_impl!(PgChar, i8)
397403
from_raw_from_impl!(PgInt2, i16)
398404
from_raw_from_impl!(PgInt4, i32)
@@ -447,7 +453,7 @@ from_array_impl!(PgByteAArray, Vec<u8>)
447453
from_array_impl!(PgCharArray, i8)
448454
from_array_impl!(PgInt2Array, i16)
449455
from_array_impl!(PgInt4Array, i32)
450-
from_array_impl!(PgTextArray | PgCharNArray | PgVarcharArray, ~str)
456+
from_array_impl!(PgTextArray | PgCharNArray | PgVarcharArray | PgNameArray, ~str)
451457
from_array_impl!(PgInt8Array, i64)
452458
from_array_impl!(PgTimestampArray | PgTimestampTZArray, Timespec)
453459
from_array_impl!(PgJsonArray, Json)
@@ -684,7 +690,7 @@ macro_rules! to_raw_to_impl(
684690

685691
to_raw_to_impl!(PgBool, bool)
686692
to_raw_to_impl!(PgByteA, Vec<u8>)
687-
to_raw_to_impl!(PgVarchar | PgText | PgCharN, ~str)
693+
to_raw_to_impl!(PgVarchar | PgText | PgCharN | PgName, ~str)
688694
to_raw_to_impl!(PgJson, Json)
689695
to_raw_to_impl!(PgChar, i8)
690696
to_raw_to_impl!(PgInt2, i16)
@@ -699,12 +705,12 @@ to_raw_to_impl!(PgTsRange | PgTstzRange, Range<Timespec>)
699705
impl<'a> ToSql for &'a str {
700706
fn to_sql(&self, ty: &PostgresType)
701707
-> PostgresResult<(Format, Option<Vec<u8>>)> {
702-
check_types!(PgVarchar | PgText | PgCharN, ty)
708+
check_types!(PgVarchar | PgText | PgCharN | PgName, ty)
703709
Ok((Text, Some(Vec::from_slice(self.as_bytes()))))
704710
}
705711
}
706712

707-
to_option_impl_lifetime!(PgVarchar | PgText | PgCharN, &'a str)
713+
to_option_impl_lifetime!(PgVarchar | PgText | PgCharN | PgName, &'a str)
708714

709715
impl<'a> ToSql for &'a [u8] {
710716
fn to_sql(&self, ty: &PostgresType)
@@ -762,7 +768,7 @@ to_array_impl!(PgByteAArray, Vec<u8>)
762768
to_array_impl!(PgCharArray, i8)
763769
to_array_impl!(PgInt2Array, i16)
764770
to_array_impl!(PgInt4Array, i32)
765-
to_array_impl!(PgTextArray | PgCharNArray | PgVarcharArray, ~str)
771+
to_array_impl!(PgTextArray | PgCharNArray | PgVarcharArray | PgNameArray, ~str)
766772
to_array_impl!(PgInt8Array, i64)
767773
to_array_impl!(PgTimestampArray | PgTimestampTZArray, Timespec)
768774
to_array_impl!(PgFloat4Array, f32)

0 commit comments

Comments
 (0)