postgres: support "CHAR" and OID

This commit is contained in:
Ryan Leckey
2020-03-25 04:46:17 -07:00
parent 50a9120efb
commit 2274b69556
4 changed files with 98 additions and 1 deletions

View File

@@ -15,10 +15,14 @@ impl TypeId {
pub(crate) const BOOL: TypeId = TypeId(16);
pub(crate) const CHAR: TypeId = TypeId(18);
pub(crate) const INT2: TypeId = TypeId(21);
pub(crate) const INT4: TypeId = TypeId(23);
pub(crate) const INT8: TypeId = TypeId(20);
pub(crate) const OID: TypeId = TypeId(26);
pub(crate) const FLOAT4: TypeId = TypeId(700);
pub(crate) const FLOAT8: TypeId = TypeId(701);
@@ -46,10 +50,14 @@ impl TypeId {
pub(crate) const ARRAY_BOOL: TypeId = TypeId(1000);
pub(crate) const ARRAY_CHAR: TypeId = TypeId(1002);
pub(crate) const ARRAY_INT2: TypeId = TypeId(1005);
pub(crate) const ARRAY_INT4: TypeId = TypeId(1007);
pub(crate) const ARRAY_INT8: TypeId = TypeId(1016);
pub(crate) const ARRAY_OID: TypeId = TypeId(1028);
pub(crate) const ARRAY_FLOAT4: TypeId = TypeId(1021);
pub(crate) const ARRAY_FLOAT8: TypeId = TypeId(1022);
@@ -83,10 +91,14 @@ impl Display for TypeId {
match *self {
TypeId::BOOL => f.write_str("BOOL"),
TypeId::CHAR => f.write_str("\"CHAR\""),
TypeId::INT2 => f.write_str("INT2"),
TypeId::INT4 => f.write_str("INT4"),
TypeId::INT8 => f.write_str("INT8"),
TypeId::OID => f.write_str("OID"),
TypeId::FLOAT4 => f.write_str("FLOAT4"),
TypeId::FLOAT8 => f.write_str("FLOAT8"),
@@ -112,10 +124,14 @@ impl Display for TypeId {
TypeId::ARRAY_BOOL => f.write_str("BOOL[]"),
TypeId::ARRAY_CHAR => f.write_str("\"CHAR\"[]"),
TypeId::ARRAY_INT2 => f.write_str("INT2[]"),
TypeId::ARRAY_INT4 => f.write_str("INT4[]"),
TypeId::ARRAY_INT8 => f.write_str("INT8[]"),
TypeId::ARRAY_OID => f.write_str("OID[]"),
TypeId::ARRAY_FLOAT4 => f.write_str("FLOAT4[]"),
TypeId::ARRAY_FLOAT8 => f.write_str("FLOAT8[]"),

View File

@@ -10,6 +10,39 @@ use crate::postgres::{PgData, PgValue, Postgres};
use crate::types::Type;
use crate::Error;
impl Type<Postgres> for i8 {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::CHAR, "CHAR")
}
}
impl Type<Postgres> for [i8] {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::ARRAY_CHAR, "CHAR[]")
}
}
impl Type<Postgres> for Vec<i8> {
fn type_info() -> PgTypeInfo {
<[i8] as Type<Postgres>>::type_info()
}
}
impl Encode<Postgres> for i8 {
fn encode(&self, buf: &mut Vec<u8>) {
buf.extend_from_slice(&self.to_be_bytes());
}
}
impl<'de> Decode<'de, Postgres> for i8 {
fn decode(value: PgValue<'de>) -> crate::Result<Postgres, Self> {
match value.try_get()? {
PgData::Binary(mut buf) => buf.read_i8().map_err(Error::decode),
PgData::Text(s) => Ok(s.as_bytes()[0] as i8),
}
}
}
impl Type<Postgres> for i16 {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::INT2, "INT2")
@@ -21,6 +54,7 @@ impl Type<Postgres> for [i16] {
PgTypeInfo::new(TypeId::ARRAY_INT2, "INT2[]")
}
}
impl Type<Postgres> for Vec<i16> {
fn type_info() -> PgTypeInfo {
<[i16] as Type<Postgres>>::type_info()
@@ -53,6 +87,7 @@ impl Type<Postgres> for [i32] {
PgTypeInfo::new(TypeId::ARRAY_INT4, "INT4[]")
}
}
impl Type<Postgres> for Vec<i32> {
fn type_info() -> PgTypeInfo {
<[i32] as Type<Postgres>>::type_info()
@@ -74,6 +109,39 @@ impl<'de> Decode<'de, Postgres> for i32 {
}
}
impl Type<Postgres> for u32 {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::OID, "OID")
}
}
impl Type<Postgres> for [u32] {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::ARRAY_OID, "OID[]")
}
}
impl Type<Postgres> for Vec<u32> {
fn type_info() -> PgTypeInfo {
<[u32] as Type<Postgres>>::type_info()
}
}
impl Encode<Postgres> for u32 {
fn encode(&self, buf: &mut Vec<u8>) {
buf.extend_from_slice(&self.to_be_bytes());
}
}
impl<'de> Decode<'de, Postgres> for u32 {
fn decode(value: PgValue<'de>) -> crate::Result<Postgres, Self> {
match value.try_get()? {
PgData::Binary(mut buf) => buf.read_u32::<NetworkEndian>().map_err(Error::decode),
PgData::Text(s) => u32::from_str(s).map_err(Error::decode),
}
}
}
impl Type<Postgres> for i64 {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::INT8, "INT8")