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")

View File

@ -2,8 +2,10 @@ impl_database_ext! {
sqlx::postgres::Postgres {
bool,
String | &str,
i8,
i16,
i32,
u32,
i64,
f32,
f64,
@ -46,8 +48,10 @@ impl_database_ext! {
// Arrays
Vec<bool> | &[bool],
Vec<String> | &[String],
Vec<i8> | &[i8],
Vec<i16> | &[i16],
Vec<i32> | &[i32],
Vec<u32> | &[u32],
Vec<i64> | &[i64],
Vec<f32> | &[f32],
Vec<f64> | &[f64],

View File

@ -22,8 +22,17 @@ test_type!(bool(
"true::boolean" == true
));
test_type!(i8(Postgres, i8, "120::\"char\"" == 120_i8));
test_type!(i16(Postgres, i16, "821::smallint" == 821_i16));
test_type!(i32(Postgres, i32, "94101::int" == 94101_i32));
test_type!(i32(
Postgres,
i32,
"94101::int" == 94101_i32,
"-5101::int" == -5101_i32
));
test_type!(u32(Postgres, u32, "94101::oid" == 94101_u32));
test_type!(i64(Postgres, i64, "9358295312::bigint" == 9358295312_i64));
test_type!(f32(Postgres, f32, "9419.122::real" == 9419.122_f32));